Created
February 21, 2024 11:31
-
-
Save kenpower/ac811740d947c7bea01babe2512bf407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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