Created
October 31, 2018 11:44
-
-
Save mentix02/3fe18eab2ea963991bc4fe8f5ccd613a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <fstream> | |
| #include <iostream> | |
| #include <string.h> | |
| #include <sstream> | |
| #include <stdlib.h> | |
| #include <string> | |
| #include <unordered_map> | |
| using namespace std; | |
| int get_input(); | |
| void display_program_menu(); | |
| int POST_NUMBERS = 0; // global number for total posts read by a file in main() | |
| string food_menu[] = { | |
| "Pizza", | |
| "Burger", | |
| "Noodles", | |
| "Pasta", | |
| "Sprite", | |
| "I-Cream", | |
| }; | |
| unordered_map<string, float> food_menu_with_prices = { | |
| {"Pizza", 200.50}, | |
| {"Burger", 150.30}, | |
| {"Noodles", 100.40}, | |
| {"Pasta", 500.00}, | |
| {"Sprite", 120.20}, | |
| {"I-Cream", 180.90}, | |
| }; | |
| class Order | |
| { | |
| private: | |
| void display_food_menu() | |
| { | |
| for (int food_menu_index = 0; food_menu_index < 6; food_menu_index++) | |
| { | |
| cout << "|\t" << food_menu_index + 1 << ". " << food_menu[food_menu_index] << "\t\t\t\t|\n"; | |
| } | |
| cout << "|\t99. Exit Selection\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| } | |
| void calculate_total_cost(); | |
| public: | |
| int id; // all posts having a single unique ID | |
| string customer_name; | |
| int number_of_items; | |
| string address; | |
| string food_items[100]; | |
| float food_items_cost[100]; | |
| char delivery_or_take_away; | |
| float total_cost = 0; | |
| Order() | |
| { | |
| POST_NUMBERS++; | |
| id = POST_NUMBERS; | |
| number_of_items = 0; | |
| delivery_or_take_away = 'd'; | |
| address = ""; | |
| } | |
| ~Order() | |
| { | |
| ofstream write_number_of_orders_file("num_orders.txt", ios::out | ios::trunc); | |
| write_number_of_orders_file << POST_NUMBERS; | |
| write_number_of_orders_file.close(); | |
| } | |
| void display_details(); | |
| void enter_details(); | |
| }; | |
| void display_details(Order order); | |
| int main() | |
| { | |
| /* | |
| opens file num_orders.txt to | |
| read the number of orders | |
| that exists inside orders.txt | |
| */ | |
| ifstream number_of_orders_file("num_orders.txt", ios::in); | |
| // assigns the number to the global variable on line 11 | |
| number_of_orders_file >> POST_NUMBERS; | |
| // close the file | |
| number_of_orders_file.close(); | |
| // option for menu by user | |
| int option; | |
| // main loop for program starts | |
| display_program_menu(); | |
| do { | |
| option = get_input(); | |
| switch (option) | |
| { | |
| case 1: | |
| { | |
| Order new_order; | |
| new_order.enter_details(); | |
| new_order.display_details(); | |
| ofstream orders("orders", ios::out); | |
| orders.write((char*)&new_order, sizeof(new_order)); | |
| orders.close(); | |
| break; | |
| } | |
| case 2: | |
| { | |
| ifstream orders("orders", ios::in); | |
| Order old_order; | |
| orders.read((char*)&old_order, sizeof(old_order)); | |
| while (!orders.eof()) | |
| { | |
| display_details(old_order); | |
| orders.read((char*)&old_order, sizeof(old_order)); | |
| } | |
| orders.close(); | |
| break; | |
| } | |
| default: | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tERROR! Wrong Input!\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| display_program_menu(); | |
| continue; | |
| } | |
| display_program_menu(); | |
| } while(option != 9); | |
| // Order order; | |
| // order.enter_details(); | |
| // order.display_details(); | |
| return 0; | |
| } | |
| void Order::enter_details() | |
| { | |
| string p_customer_name, p_food_items[100], p_address; | |
| float p_food_items_cost[100]; | |
| char p_delivery_or_take_away; | |
| cin.ignore(); | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tEnter Customer Name : "; | |
| getline(cin, p_customer_name); | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tEnter Food Items : \t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| int menu_option; | |
| string food; | |
| float cost; | |
| int order_food_index = 0; | |
| display_food_menu(); | |
| cout << "|\t> "; | |
| cin >> menu_option; | |
| do | |
| { | |
| food = food_menu[menu_option-1]; | |
| cost = food_menu_with_prices[food]; | |
| // cout << "Selected : " << food << " with price " << cost << endl; | |
| p_food_items[order_food_index] = food; | |
| p_food_items_cost[order_food_index] = cost; | |
| order_food_index++; | |
| cout << "|\t> "; | |
| cin >> menu_option; | |
| } while (menu_option != 99); | |
| for (int i=0; i<order_food_index;i++) | |
| { | |
| food_items[i] = p_food_items[i]; | |
| food_items_cost[i] = p_food_items_cost[i]; | |
| } | |
| number_of_items = order_food_index; | |
| customer_name = p_customer_name; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tDelivery Or Takeaway (d/t) : "; | |
| cin >> p_delivery_or_take_away; | |
| delivery_or_take_away = (p_delivery_or_take_away=='d' ? 'D':'T'); | |
| if (delivery_or_take_away == 'D') | |
| { | |
| cin.ignore(); | |
| cout << "|\tAddress : "; | |
| getline(cin, p_address); | |
| address = p_address; | |
| } | |
| calculate_total_cost(); | |
| } | |
| void Order::display_details() | |
| { | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|~~~~~~~~~~~~~~~ORDER NUMBER 0" << id << "~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tCustomer Name : " << customer_name << "\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tFood Items : " << "\t\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| string food; | |
| for (int food_index=0; food_index < number_of_items; food_index++) | |
| { | |
| food = food_items[food_index]; | |
| cout << "|\t "; | |
| cout << food_index+1 << ". " << food; | |
| cout << "\t\t" << food_menu_with_prices[food]; | |
| cout << "\t\t|" << endl; | |
| } | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tTotal Cost\t:\t" << total_cost << "\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tDelivery Type\t:\t" << (delivery_or_take_away=='D' ? "Delivery":"Takeaway") << "\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| if (strlen(address.c_str()) > 0) | |
| { | |
| cout << "|\tAddress : \t\t\t\t|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\t " << address << endl; | |
| } | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| } | |
| void Order::calculate_total_cost() | |
| { | |
| for (int item=0; item<number_of_items; item++) | |
| { | |
| total_cost += food_items_cost[item]; | |
| } | |
| } | |
| int get_input() | |
| { | |
| int option; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\t==> "; | |
| cin >> option; | |
| if (cin.eof() || option == 99) | |
| { | |
| if (option == 99) | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| else | |
| cout << "99\n|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tBye\t\t\t\t\t|\n"; | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n" << endl; | |
| ofstream write_number_of_orders_file("num_orders.txt", ios::out | ios::trunc); | |
| write_number_of_orders_file << POST_NUMBERS; | |
| write_number_of_orders_file.close(); | |
| exit(EXIT_SUCCESS); | |
| } | |
| return option; | |
| } | |
| void display_program_menu() | |
| { | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|" << endl; | |
| cout << "|\t1. New Order\t\t\t\t|" << endl; | |
| cout << "|\t2. Get Orders\t\t\t\t|" << endl; | |
| cout << "|\t3. Search Order\t\t\t\t|" << endl; | |
| cout << "|\t99. Exit Program\t\t\t|" << endl; | |
| } | |
| void display_details(Order order) | |
| { | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|~~~~~~~~~~~~~~~ORDER NUMBER 0" << order.id << "~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tCustomer Name : " << order.customer_name << "\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tFood Items : " << "\t\t\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| string food; | |
| for (int food_index=0; food_index < order.number_of_items; food_index++) | |
| { | |
| food = order.food_items[food_index]; | |
| cout << "|\t "; | |
| cout << food_index+1 << ". " << food; | |
| cout << "\t\t" << food_menu_with_prices[food]; | |
| cout << "\t\t|" << endl; | |
| } | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tTotal Cost\t:\t" << order.total_cost << "\t\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\tDelivery Type\t:\t" << (order.delivery_or_take_away=='D' ? "Delivery":"Takeaway") << "\t|" << endl; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| if (strlen(order.address.c_str()) > 0) | |
| { | |
| cout << "|\tAddress : \t\t\t\t|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| cout << "|\t " << order.address << endl; | |
| } | |
| cout << "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n"; | |
| cout << "|\t\t\t\t\t\t|" << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment