In: Computer Science
In C++
Define enumeration data type menuItemName with enumerators of PlainEgg, BaconAndEgg, Muffin, FrenchToast, FruitBasket, Cereal, Coffee, and Tea.
Use an array, menuList, of the struct menuItemType, with tree components: menuItem of type menuItemName, menuPrice of type double, and Count of type int.
Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
a. Show the customer the different breakfast items offered by the restaurant.
b. Allow the customer to select more than one item from the menu.
c. Calculate and print the bill
Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):
Plain Egg$1.45
Bacon and Egg$2.45
Muffin$0.99
French Toast$1.99
Fruit Basket$2.49
Cereal$0.69
Coffee$0.50
Tea$0.75
Use an array, menuList, of the struct menuItemType, with three components: menuItem of enummenuItemName as described above, menuPrice of type double, and Count of type int. Your program must contain at least the following functions:
● Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items.
● Function printCheck: This function calculates and prints the check. (Note that the billing amount should include a 5% tax.)
A sample output is:
Welcome to Johnny’s Restaurant
Bacon and Egg1$2.45
Muffin1$0.99
Coffee2$1.00
Amount Total$4.44
Tax$0.22
Amount Due$4.66
Code in C++
#include <bits/stdc++.h>
using namespace std;
//enum declaration
enum menuItemName { PlainEgg, BaconAndEgg, Muffin, FrenchToast, FruitBasket, Cereal, Coffee, Tea};
//function to get item name from enum number
string enum_to_string(menuItemName item) {
switch(item){
case PlainEgg:
return "PlainEgg";
case BaconAndEgg:
return "BaconAndEgg";
case Muffin:
return "Muffin";
case FrenchToast:
return "FrenchToast";
case FruitBasket:
return "FruitBasket";
case Cereal:
return "Cereal";
case Coffee:
return "Coffee";
case Tea:
return "Tea";
default:
return "Invalid";
}
}
//struct declaration
struct menuItemType{
menuItemName menuItem;
double menuPrice;
int count;
};
void show_menu(struct menuItemType menuList[], int size){
cout<<"Select Items from Menu"<<endl;
for(int i=0;i<size;i++){
cout<<"Enter "<<(i+1)<<" for "<<enum_to_string(menuList[i].menuItem)<<" $"<<menuList[i].menuPrice<<endl;
}
cout<<"Enter -1 to finish ordering"<<endl;
}
void printCheck(struct menuItemType menuList[], int size, double tax){
double total_price=0;
for(int i=0;i<size;i++){
total_price+=(menuList[i].menuPrice*menuList[i].count);
}
setprecision(2);
cout<<"Amount Total = $"<<total_price<<endl;
cout<<"Tax = $"<<tax<<endl;
double final_amount = total_price+((total_price*tax)/100);
cout<<"Amount Due = $"<<final_amount<<endl;
}
int main(int argc, char const *argv[])
{
//print welcome message
cout<<"Welcome to Johnny's Restaurant"<<endl;
//set precision to 2 so that we only see amounts till two decimal places
setprecision(2);
// create menuList array
struct menuItemType menuList[8];
menuList[0] = {static_cast<menuItemName>(0), 1.45, 0};
menuList[1] = {static_cast<menuItemName>(1), 2.45, 0};
menuList[2] = {static_cast<menuItemName>(2), 0.99, 0};
menuList[3] = {static_cast<menuItemName>(3), 1.99, 0};
menuList[4] = {static_cast<menuItemName>(4), 2.49, 0};
menuList[5] = {static_cast<menuItemName>(5), 0.69, 0};
menuList[6] = {static_cast<menuItemName>(6), 0.50, 0};
menuList[7] = {static_cast<menuItemName>(7), 0.75, 0};
int val;
do{
show_menu(menuList,8);
cin>>val;
if(val>=1 && val<=8){
cout<<enum_to_string(menuList[val-1].menuItem)<<" added"<<endl;
menuList[val].count++;
}
}while(val!=-1);
//call printCheck
printCheck(menuList,8,5);
}
Sample Console Input/Output
Welcome to Johnny's Restaurant
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
4
FrenchToast added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
5
FruitBasket added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
2
BaconAndEgg added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
3
Muffin added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
3
Muffin added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
4
FrenchToast added
Select Items from Menu
Enter 1 for PlainEgg $1.45
Enter 2 for BaconAndEgg $2.45
Enter 3 for Muffin $0.99
Enter 4 for FrenchToast $1.99
Enter 5 for FruitBasket $2.49
Enter 6 for Cereal $0.69
Enter 7 for Coffee $0.5
Enter 8 for Tea $0.75
Enter -1 to finish ordering
-1
Amount Total = $10.64
Tax = $5
Amount Due = $11.172
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.