|
| 1 | +#ifndef TOMATO_APP_H |
| 2 | +#define TOMATO_APP_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include "models/User.h" |
| 7 | +#include "models/Restaurant.h" |
| 8 | +#include "models/Cart.h" |
| 9 | +#include "managers/RestaurantManager.h" |
| 10 | +#include "managers/OrderManager.h" |
| 11 | +#include "strategies/PaymentStrategy.h" |
| 12 | +#include "strategies/UpiPaymentStrategy.h" |
| 13 | +#include "factories/NowOrderFactory.h" |
| 14 | +#include "factories/ScheduledOrderFactory.h" |
| 15 | +#include "services/NotificationService.h" |
| 16 | +#include "utils/TimeUtils.h" |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +class TomatoApp { |
| 20 | +public: |
| 21 | + TomatoApp() { |
| 22 | + initializeRestaurants(); |
| 23 | + } |
| 24 | + |
| 25 | + void initializeRestaurants() { |
| 26 | + Restaurant* restaurant1 = new Restaurant("Bikaner", "Delhi"); |
| 27 | + restaurant1->addMenuItem(MenuItem("P1", "Chole Bhature", 120)); |
| 28 | + restaurant1->addMenuItem(MenuItem("P2", "Samosa", 15)); |
| 29 | + |
| 30 | + Restaurant* restaurant2 = new Restaurant("Haldiram", "Kolkata"); |
| 31 | + restaurant2->addMenuItem(MenuItem("P1", "Raj Kachori", 80)); |
| 32 | + restaurant2->addMenuItem(MenuItem("P2", "Pav Bhaji", 100)); |
| 33 | + restaurant2->addMenuItem(MenuItem("P3", "Dhokla", 50)); |
| 34 | + |
| 35 | + Restaurant* restaurant3 = new Restaurant("Saravana Bhavan", "Chennai"); |
| 36 | + restaurant3->addMenuItem(MenuItem("P1", "Masala Dosa", 90)); |
| 37 | + restaurant3->addMenuItem(MenuItem("P2", "Idli Vada", 60)); |
| 38 | + restaurant3->addMenuItem(MenuItem("P3", "Filter Coffee", 30)); |
| 39 | + |
| 40 | + RestaurantManager* restaurantManager = RestaurantManager::getInstance(); |
| 41 | + restaurantManager->addRestaurant(restaurant1); |
| 42 | + restaurantManager->addRestaurant(restaurant2); |
| 43 | + restaurantManager->addRestaurant(restaurant3); |
| 44 | + |
| 45 | + // Add other sample restaurants... |
| 46 | + } |
| 47 | + |
| 48 | + vector<Restaurant*> searchRestaurants(const string& location) { |
| 49 | + return RestaurantManager::getInstance()->searchByLocation(location); |
| 50 | + } |
| 51 | + |
| 52 | + void selectRestaurant(User* user, Restaurant* restaurant) { |
| 53 | + Cart* cart = user->getCart(); |
| 54 | + cart->setRestaurant(restaurant); |
| 55 | + } |
| 56 | + |
| 57 | + void addToCart(User* user, const string& itemCode) { |
| 58 | + Restaurant* restaurant = user->getCart()->getRestaurant(); |
| 59 | + if (!restaurant) { |
| 60 | + cout << "Please select a restaurant first." << endl; |
| 61 | + return; |
| 62 | + } |
| 63 | + for (const auto& item : restaurant->getMenu()) { |
| 64 | + if (item.getCode() == itemCode) { |
| 65 | + user->getCart()->addItem(item); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Order* checkoutNow(User* user, const string& orderType, PaymentStrategy* paymentStrategy) { |
| 72 | + return checkout(user, orderType, paymentStrategy, new NowOrderFactory()); |
| 73 | + } |
| 74 | + |
| 75 | + Order* checkoutScheduled(User* user, const string& orderType, PaymentStrategy* paymentStrategy, const string& scheduleTime) { |
| 76 | + return checkout(user, orderType, paymentStrategy, new ScheduledOrderFactory(scheduleTime)); |
| 77 | + } |
| 78 | + |
| 79 | + Order* checkout(User* user, const string& orderType, |
| 80 | + PaymentStrategy* paymentStrategy, OrderFactory* orderFactory) { |
| 81 | + if (user->getCart()->isEmpty()) |
| 82 | + return nullptr; |
| 83 | + |
| 84 | + Cart* userCart = user->getCart(); |
| 85 | + Restaurant* orderedRestaurant = userCart->getRestaurant(); |
| 86 | + vector<MenuItem> itemsOrdered = userCart->getItems(); |
| 87 | + double totalCost = userCart->getTotalCost(); |
| 88 | + |
| 89 | + Order* order = orderFactory->createOrder(user, userCart, orderedRestaurant, itemsOrdered, paymentStrategy, totalCost, orderType); |
| 90 | + OrderManager::getInstance()->addOrder(order); |
| 91 | + return order; |
| 92 | + } |
| 93 | + |
| 94 | + void payForOrder(User* user, Order* order) { |
| 95 | + bool isPaymentSuccess = order->processPayment(); |
| 96 | + |
| 97 | + // clear user cart if payment is successful. |
| 98 | + if(isPaymentSuccess) { |
| 99 | + NotificationService* notification = new NotificationService(); |
| 100 | + notification->notify(order); |
| 101 | + user->getCart()->clear(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void printUserCart(User* user) { |
| 106 | + cout << "Items in cart:" << endl; |
| 107 | + cout << "------------------------------------" << endl; |
| 108 | + for (const auto& item : user->getCart()->getItems()) { |
| 109 | + cout << item.getCode() << " : " << item.getName() << " : ₹" << item.getPrice() << endl; |
| 110 | + } |
| 111 | + cout << "------------------------------------" << endl; |
| 112 | + cout << "Grand total : ₹" << user->getCart()->getTotalCost() << endl; |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +#endif // TOMATO_APP_H |
0 commit comments