Facebook
From sex, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 286
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class FoodItem {
  9.     string name;
  10.     double price;
  11. public:
  12.     FoodItem(string n, double p) : name(n), price(p) {}
  13.     string getName() const { return name; }
  14.     double getPrice() const { return price; }
  15. };
  16.  
  17. class Order {
  18.     vector<FoodItem> items;
  19. public:
  20.     void addItem(const FoodItem& item) {
  21.         items.push_back(item);
  22.     }
  23.    
  24.     double getTotal() const {
  25.         double total = 0;
  26.         for (const FoodItem& item : items) {
  27.             total += item.getPrice();
  28.         }
  29.         return total;
  30.     }
  31.    
  32.     void displayOrder() const {
  33.         cout << "Order:" << endl;
  34.         for (const FoodItem& item : items) {
  35.             cout << item.getName() << " - " << item.getPrice() << " BDT" << endl;
  36.         }
  37.         cout << "Total: " << getTotal() << " BDT" << endl;
  38.     }
  39.  
  40.     const vector<FoodItem>& getItems() const {
  41.         return items;
  42.     }
  43. };
  44.  
  45. class Menu {
  46.     vector<FoodItem> items;
  47. public:
  48.     Menu() {
  49.         // Add menu items
  50.         items.push_back(FoodItem("French Fry", 140));
  51.         items.push_back(FoodItem("Chicken Cheese Burger", 230));
  52.         items.push_back(FoodItem("Steamed Momo", 180));
  53.         items.push_back(FoodItem("Chicken Strips", 160));
  54.         items.push_back(FoodItem("Cold Drinks", 25));
  55.         items.push_back(FoodItem("Water", 20));
  56.     }
  57.    
  58.     void displayMenu() const {
  59.         cout << "Menu:" << endl;
  60.         for (size_t i = 0; i < items.size(); ++i) {
  61.             cout << i+1 << ". " << items[i].getName() << " - " << items[i].getPrice() << " BDT" << endl;
  62.         }
  63.     }
  64.    
  65.     FoodItem getItem(int index) const {
  66.         if (index >= 0 && index < items.size()) {
  67.             return items[index];
  68.         } else {
  69.             throw out_of_range("Invalid menu item index");
  70.         }
  71.     }
  72. };
  73.  
  74. class FileManager {
  75. public:
  76.     static void saveOrder(const Order& order, const string& filename) {
  77.         ofstream file&#40;filename&#41;;
  78.         if (file.is_open()) {
  79.             for (const FoodItem& item : order.getItems()) {
  80.                 file << item.getName() << "," << item.getPrice() << endl;
  81.             }
  82.             file.close();
  83.             cout << "Order saved to " << filename << endl;
  84.         } else {
  85.             cout << "Error: Unable to open file for saving order." << endl;
  86.         }
  87.     }
  88.  
  89.     static Order loadOrder(const string& filename) {
  90.         Order order;
  91.         ifstream file&#40;filename&#41;;
  92.         if (file.is_open()) {
  93.             string line;
  94.             while (getline(file, line)) {
  95.                 size_t pos = line.find(",");
  96.                 if (pos != string::npos) {
  97.                     string name = line.substr(0, pos);
  98.                     double price = stod(line.substr(pos + 1));
  99.                     order.addItem(FoodItem(name, price));
  100.                 }
  101.             }
  102.             file.close();
  103.             cout << "Order loaded from " << filename << endl;
  104.         } else {
  105.             cout << "Error: Unable to open file for loading order." << endl;
  106.         }
  107.         return order;
  108.     }
  109. };
  110.  
  111. int main() {
  112.     Menu menu;
  113.     Order order;
  114.    
  115.     // Display a welcome message
  116.     cout << "Welcome to the Food Ordering System!" << endl;
  117.    
  118.     // Main menu loop
  119.     while (true) {
  120.         cout << "\nMain Menu:" << endl;
  121.         cout << "1. View Menu" << endl;
  122.         cout << "2. Add Item to Order" << endl;
  123.         cout << "3. View Order" << endl;
  124.         cout << "4. Save Order" << endl;
  125.         cout << "5. Load Order" << endl;
  126.         cout << "6. Checkout and Exit" << endl;
  127.         cout << "Enter your choice: ";
  128.        
  129.         int choice;
  130.         cin >> choice;
  131.        
  132.         switch (choice) {
  133.             case 1: // View Menu
  134.                 menu.displayMenu();
  135.                 break;
  136.             case 2: // Add Item to Order
  137.                 int index;
  138.                 cout << "Enter the item number to add: ";
  139.                 cin >> index;
  140.                 try {
  141.                     order.addItem(menu.getItem(index - 1));
  142.                     cout << "Item added to order." << endl;
  143.                 } catch (const out_of_range& e) {
  144.                     cout << "Error: " << e.what() << endl;
  145.                 }
  146.                 break;
  147.             case 3: // View Order
  148.                 order.displayOrder();
  149.                 break;
  150.             case 4: // Save Order
  151.                 FileManager::saveOrder(order, "order.txt");
  152.                 break;
  153.             case 5: // Load Order
  154.                 order = FileManager::loadOrder("order.txt");
  155.                 break;
  156.             case 6: // Checkout and Exit
  157.                 cout << "Total Bill: " << order.getTotal() << " BDT" << endl;
  158.                 cout << "Thank you for using the Food Ordering System!" << endl;
  159.                 return 0;
  160.             default:
  161.                 cout << "Invalid choice. Please enter a number from 1 to 6." << endl;
  162.                 break;
  163.         }
  164.     }
  165.    
  166.     return 0;
  167. }
  168.