Untitled
unknown
plain_text
2 years ago
9.4 kB
5
Indexable
#include <iostream>
#include <iomanip>
#include <map>
#include <ctime>
#include <fstream>
using namespace std;
struct OrderedItem {
int quantity;
double totalPrice;
};
void menu();
double money(double rm_usd , double total_price , double rm_euro);
void order_item(const map<int, OrderedItem>& orderedItems);
int main() {
int choice;
int code;
int quantity;
char choose;
double total_price = 0 ;
double price[5] = {12, 8, 15, 21, 8.5};
const double rm_usd = 4.66; // Convert to Usd
const double rm_euro = 5.07; // Convert to Euro
time_t curr_time;
curr_time = time(nullptr);
char *tm = ctime(&curr_time); // Variable to store the order date
map<int, OrderedItem> orderedItems; // Map to store the ordered items in key-value pairs
/*
Example :
map<int, OrderedItem> orderedItems{
"quantity" : "quantity",
"price" : "totalPrice"
}
*/
ofstream MyMenu("filename.txt");
MyMenu << setw(23) << "Menu" << endl;
MyMenu<< "-----------------------------------------------" <<endl;
MyMenu << "| Code | Meal | Price | " << endl;
MyMenu << "-----------------------------------------------" << endl;
MyMenu<< "| 1 | Mushroom Burger | RM 12.00 | " << endl;
MyMenu << "-----------------------------------------------" << endl;
MyMenu << "| 2 | Crispy Fried Chicken | RM 8.00 |" << endl;
MyMenu << "-----------------------------------------------" << endl;
MyMenu<< "| 3 | Fish & Chips | RM 15.00 |" << endl;
MyMenu<< "-----------------------------------------------" << endl;
MyMenu << "| 4 | Spaghetti & Meatballs | RM 21.00 |" << endl;
MyMenu<< "-----------------------------------------------" << endl;
MyMenu<< "| 5 | Hotdog Sandwich | RM 8.50 |" << endl;
MyMenu<< "-----------------------------------------------" << endl;
MyMenu.close() ;
string myText;
ifstream MyMenuFile("filename.txt");
do {
cout << "Food Ordering System" << endl;
cout << "1 [Order Meals]" << endl;
cout << "2 [View Order]" << endl;
cout << "3 [Exit]" << endl << endl;
cout << "Enter Option:";
cin >> choice;
switch (choice) {
case 1:
while(getline(MyMenuFile, myText)){
cout << myText <<endl ;
}
MyMenuFile.close();
do {
cout << "Please select the meal code:";
cin >> code;
if (code < 1 || code > 5) {
cerr << "Invalid code. Please enter a valid code (1-5)." << endl;
} else {
cout << "Please enter quantity:";
cin >> quantity;
if (quantity < 1) {
cerr << "Unable to process quantity of order, please enter a valid quantity" << endl;
} else {
total_price += quantity * price[code - 1];
// Check if the item code is already in the map
if (orderedItems.find(code) != orderedItems.end()) {
// If the item is already in the map, update the quantity and total price
orderedItems[code].quantity += quantity;
orderedItems[code].totalPrice += quantity * price[code - 1];
} else {
// If the item is not in the map, add it with the current quantity and total price
orderedItems[code] = {quantity, quantity * price[code - 1]};
}
cout << "Do you want to continue your order? (Y/N)" << endl;
cout << "choice: ";
cin >> choose;
}
}
} while (choose == 'y' || choose == 'Y');
// Ask the user about the currency once they choose not to continue the order
money( rm_usd , total_price , rm_euro);
break;
case 2:
// Add logic for viewing orders here
cout << "Current Order:" << endl;
cout << "order no" << "\t\t date :"<< tm<< endl;
cout << "Code Meal Quantity Total" << endl;
cout << "------------------------------------------------------------" << endl;
order_item(orderedItems);
//if else statement write here the tax
//Declare variable tax
double tax;
if (total_price <= 15){
tax = 0;
}
else if(total_price > 15 && total_price < 50){
tax = 0.02;
}
else{
tax = 0.05;
}
// Output a horizontal line
std::cout << "============================================================\n";
// Output "tax(" followed by the tax percentage in whole numbers and the tax amount
std::cout << setw(40) << "Tax(" << static_cast<int>(tax * 100) << "%):"<<setw(7)<< "RM"
<<std::fixed << std::setprecision(2) <<setw(5) << tax * total_price << "\n";
// Update total_price by adding the product of tax and total_price
total_price += tax * total_price;
// Output the updated total_price
std::cout <<setw(45) << "Updated Total Price: "
<< std::fixed << std::setprecision(2) <<setw(7)<<"RM "<< total_price << std::endl;
break;
case 3:
// Option 3: Exit the loop
cout << "Exit Food Ordering System. Goodbye!" << endl;
break;
default:
cout << "Invalid option. Please enter a valid option (1-3)." << endl;
}
} while (choice != 3); // Exit the program when the user is done ordering or viewing
system("pause>0");
return 0;
}
void menu(){
cout << setw(23) << "Menu" << endl;
cout << "-----------------------------------------------" << endl;
cout << "| Code | Meal | Price | " << endl;
cout << "-----------------------------------------------" << endl;
cout << "| 1 | Mushroom Burger | RM 12.00 | " << endl;
cout << "-----------------------------------------------" << endl;
cout << "| 2 | Crispy Fried Chicken | RM 8.00 |" << endl;
cout << "-----------------------------------------------" << endl;
cout << "| 3 | Fish & Chips | RM 15.00 |" << endl;
cout << "-----------------------------------------------" << endl;
cout << "| 4 | Spaghetti & Meatballs | RM 21.00 |" << endl;
cout << "-----------------------------------------------" << endl;
cout << "| 5 | Hotdog Sandwich | RM 8.50 |" << endl;
cout << "-----------------------------------------------" << endl;
}
double money( double rm_usd , double total_price , double rm_euro){
cout << "Please select the currency in which payment would be made in:" << endl;
cout << "RM -------1" << endl;
cout << "USD -------2" << endl;
cout << "EURO -------3" << endl;
int currency ;
cin >> currency ;
// Display total price after completing the order
cout << "Total amount due: ";
double amount_due;
switch (currency) {
case 1:
amount_due = total_price;
cout << "RM " << fixed << setprecision(2) << total_price << endl;
break;
case 2:
amount_due = total_price / rm_usd;
cout << "USD " << fixed << setprecision(2) << total_price / rm_usd << endl;
break;
case 3:
amount_due = total_price / rm_euro;
cout << "Euro " << fixed << setprecision(2) << total_price / rm_euro << endl;
break;
default:
cerr << "Invalid currency selection." << endl;
amount_due = -1 ;
}
return amount_due ;
}
void order_item(const map<int, OrderedItem>& orderedItems){
// Display ordered items and their quantities using a map
for (const auto& entry : orderedItems) {
// Get the item code from the current entry
int itemCode = entry.first;
string foodname ;
// Determine the food name based on the item code
if(itemCode == 1){
foodname = "Mushroom Burger ";
}else if (itemCode == 2){
foodname = "Crispy Fried Chicken ";
}else if (itemCode == 3 ){
foodname = "Fish & Chips ";
}else if(itemCode == 4) {
foodname = "Spaghetti & Meatballs";
}else if (itemCode == 5){
foodname = "Hotdog Sandwich ";
}
// Get a constant reference to the OrderedItem object for the current entry
const OrderedItem& itemDetails = entry.second;
cout << itemCode << " " <<foodname<< " "<< itemDetails.quantity << " RM " << fixed << setprecision(2) << itemDetails.totalPrice << " " <<endl ;
}
}Editor is loading...
Leave a Comment