Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created February 21, 2024 11:31
Show Gist options
  • Save kenpower/ac811740d947c7bea01babe2512bf407 to your computer and use it in GitHub Desktop.
Save kenpower/ac811740d947c7bea01babe2512bf407 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
class Book {
public:
std::string title;
double price;
Book(std::string t, double p) : title(t), price(p) {}
};
class ShoppingCart {
private:
std::vector<Book> books;
public:
void addBook(const Book& book) {
books.push_back(book);
std::cout << book.title << " added to the cart.\n";
}
double getTotal() {
double total = 0.0;
for (auto& book : books) {
total += book.price;
}
return total;
}
};
class PaymentProcessor {
public:
bool processPayment(double amount) {
std::cout << "Processing payment: $" << amount << "\n";
// Simplification: Assume payment always succeeds
return true;
}
};
class Bookstore {
public:
Book searchBook(std::string title) {
// Simplification: Assume the book is always found
std::cout << "Book found: " << title << "\n";
return Book(title, 10.0); // Example book
}
};
class User {
private:
ShoppingCart cart;
public:
void searchAndAddBook(Bookstore& store, std::string title) {
Book book = store.searchBook(title);
cart.addBook(book);
}
void checkout(PaymentProcessor& processor) {
double total = cart.getTotal();
if (processor.processPayment(total)) {
std::cout << "Payment successful. Checkout complete.\n";
} else {
std::cout << "Payment failed.\n";
}
}
};
int main() {
Bookstore store;
PaymentProcessor processor;
User user;
user.searchAndAddBook(store, "C++ for Beginners");
user.checkout(processor);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment