In: Computer Science
In C++, develop a menu program which reads in data from 4 different input files - appetizers, entrees, desserts, and drinks. Then, record the item information ordered by the customer for each item selected. When finished, display all items ordered to the screen with the subtotal, tax (10% for easy math), and the total. Be sure to get tip as well. Then, ensure enough payment was received before generating an output file receipt which can be printed off. Use functions to prevent redudant code.
program developed with user friendly interface
(code to copy)
#include<bits/stdc++.h>
using namespace std;
class Order{
public:
vector<pair<string, float>> itemsOrdered;
vector<string> filenames;
Order(vector<string> fileNames){
filenames=fileNames;
}
int menuSelection(){
cout<<"-------------Menu-------------"<<endl;
cout<<"1. Entrees"<<endl;
cout<<"2. Drinks"<<endl;
cout<<"3. Appetizers"<<endl;
cout<<"4. Desserts"<<endl;
cout<<"0. Nothing, I am done!"<<endl;
cout<<"What do you want to have?"<<endl;
int option;
cin>>option;
return option;
}
void itemSelection(int category){
//read all items in the category
ifstream infile(filenames[category-1]);
vector<pair<string, float>> items;
string itemName;
float price;
while(infile>>itemName){
infile>>price;
items.push_back(make_pair(itemName, price));
}
infile.close();
//display items
cout<<"-------------Items-------------"<<endl;
for(int i=0;i<items.size();i++){
cout<<(i+1)<<". "<<items[i].first<<" - $"<<items[i].second<<endl;
}
cout<<"0. Go back"<<endl;
cout<<"What do you want to have?"<<endl;
int option;
cin>>option;
if(option!=0){
itemsOrdered.push_back(items[option-1]);
cout<<"Item added!"<<endl;
}
}
float printOrder(){
float total_bill=0;
if(itemsOrdered.size()==0){
cout<<"No items ordered yet."<<endl;
}else{
cout<<"Items Ordered"<<endl;
cout<<"Dish-----------------Price"<<endl;
for(int i=0;i<itemsOrdered.size();i++){
cout<<itemsOrdered[i].first<<" $"<<itemsOrdered[i].second<<endl;
total_bill+=itemsOrdered[i].second;
}
cout<<"-----------------"<<endl;
cout<<"Total $"<<total_bill<<endl;
cout<<"Tax @ 10% $"<<total_bill*0.1<<endl;
cout<<"Tax after Tax $"<<total_bill*1.1<<endl;
cout<<"-----------------"<<endl;
}
return total_bill*1.1;
}
float askForTip(){
float amount=0;
cout<<"Would you like to five a tip? y/n? ";
char inp;
cin>>inp;
if(inp=='y'||inp=='Y'){
cout<<"Enter tip amount: ";
cin>>amount;
}
return amount;
}
void takeOrder(){
while(true){
int op = menuSelection();
if(op==0)
break;
itemSelection(op);
}
float bill = printOrder();
float tip = askForTip();
float payable=bill+tip;
cout<<"please pay $"<<payable<<endl;
float amount;
cin>>amount;
cout<<"Thanks for visting us! Have a great day!"<<endl;
}
};
int main()
{
vector<string> filenames;
filenames.push_back("entrees.txt");
filenames.push_back("drinks.txt");
filenames.push_back("appetizers.txt");
filenames.push_back("desserts.txt");
Order newOrder(filenames);
newOrder.takeOrder();
}
code screenshot
Example data in entrees.txt
Example data in appetizers.txt
Example data in drinks.txt
Example data in desserts.txt
Sample Input/Output Screenshot