Question

I need help refactoring this program so that it it doesn't matter what the exact menu...

I need help refactoring this program so that it it doesn't matter what the exact menu items are, how much they cost, and even how many there are.

Here is the assignment:

Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:

  1. Show the customer the different breakfast items offered by the restaurant.
  2. Allow the customer to select multiple items from this menu and specify the quantity of each item selected.
  3. Calculate and print the bill.

Here is my code:

#include<iostream>

#include<string>

#include <iomanip>

using namespace std;


struct menuItemType //defines a struct which stores menu item and menu price

{

string menuItem;

double menuPrice;

};

menuItemType menuList[9]; //this is an array of the menuItemType

//function prototypes

void getdata();

void makeSelection();

void printCheck();

void showMenu();

//paralell array that stores the order quanitities

int customerOrder[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

int main()

{

// sets output to two decimal places

cout << fixed << setprecision(2);

// calls the function to load the data into the array

getdata();

//calls the showMenu function and displays the data

showMenu();

//call the selection function to update the quantities and shows the user what they have selected

makeSelection();

//prints the bill

printCheck();

return 0;

}

// this function loads the data values into the menuList array

void getdata()

{

menuList[1].menuItem = "Plain Egg";

menuList[1].menuPrice = 1.45;

menuList[2].menuItem = "Bacon and Egg";

menuList[2].menuPrice = 2.45;

menuList[3].menuItem = "Muffin";

menuList[3].menuPrice = .99;

menuList[4].menuItem = "French Toast";

menuList[4].menuPrice = 1.99;

menuList[5].menuItem = "Fruit Basket";

menuList[5].menuPrice = 2.49;

menuList[6].menuItem = "Cereal";

menuList[6].menuPrice = .69;

menuList[7].menuItem = "Coffee";

menuList[7].menuPrice = .50;

menuList[8].menuItem = "Tea";

menuList[8].menuPrice = .75;

}

//definition of a function to display the menu by taking the values from the menuList array

void showMenu()

{

cout << "Breakfast items offered by the restaurant are" << endl;

cout

<< setw(6)<< left<< 1 << setw(18) << menuList[1].menuItem << " $" << menuList[1].menuPrice << endl;

cout

<< setw(6)<< left<< 2 << setw(18) << menuList[2].menuItem << " $" << menuList[2].menuPrice << endl;

cout

<< setw(6)<< left<< 3 << setw(18) << menuList[3].menuItem << " $" << menuList[3].menuPrice << endl;

cout

<< setw(6)<< left<< 4 << setw(18) << menuList[4].menuItem << " $" << menuList[4].menuPrice << endl;

cout

<< setw(6)<< left<< 5 << setw(18) << menuList[5].menuItem << " $" << menuList[5].menuPrice << endl;

cout

<< setw(6)<< left<< 6 << setw(18) << menuList[6].menuItem << " $" << menuList[6].menuPrice << endl;

cout

<< setw(6)<< left<< 7 << setw(18) << menuList[7].menuItem << " $" << menuList[7].menuPrice << endl;

cout

<< setw(6)<< left<< 8 << setw(18) << menuList[8].menuItem << " $" << menuList[8].menuPrice << endl;

}

//definition of a function to select items from the menu

void makeSelection()

{

int choice,quantity;

char yn;

do{

//gets input for the item and the quantity

cout << "Select an item number:";

cin >> choice;

cout << "Enter the quantity for this item:";

cin >> quantity;

//switch based on the choice input

switch (choice)

{

case 1:

//increments the quanity in customer order array

customerOrder[1] = customerOrder[1] + quantity;

//outputs what the yser has selected and how many they have selected

cout << "You have Selected: " << customerOrder[1] << " - " << menuList[1].menuItem << endl;

break;

case 2:

customerOrder[2] = customerOrder[2] + quantity;

cout << "You have Selected: " << customerOrder[2] << " - " << menuList[2].menuItem << endl;

break;

case 3:

customerOrder[3] = customerOrder[3] + quantity;

cout << "You have Selected: " << customerOrder[3] << " - " << menuList[3].menuItem << endl;

break;

case 4:

customerOrder[4] = customerOrder[4] + quantity;

cout << "You have Selected: " << customerOrder[4] << " - " << menuList[4].menuItem << endl;

break;

case 5:

customerOrder[5] = customerOrder[5] + quantity;

cout << "You have Selected: " << customerOrder[5] << " - " << menuList[5].menuItem << endl;

break;

case 6:

customerOrder[6] = customerOrder[6] + quantity;

cout << "You have Selected: " << customerOrder[6] << " - " << menuList[6].menuItem << endl;

break;

case 7:

customerOrder[7] = customerOrder[7] + quantity;

cout << "You have Selected: " << customerOrder[7] << " - " << menuList[7].menuItem << endl;

break;

case 8:

customerOrder[8] = customerOrder[8] + quantity;

cout << "You have Selected: " << customerOrder[8] << " - " << menuList[8].menuItem << endl;

break;

default:

//case for invalid input

cout << "invalid input" << endl;

}

//asks the user if they would like to select more items

cout << "to select more items (y/n)";

cin >> yn;


} while (yn != 'n');//this loop runs while input does not equal n

cout << endl;

}

//definition of a function that prints the checks

void printCheck()

{

string spacer =" ";//a variable to add space in the table where there is no quantity

double total = 0, tax, due;//declares local variables

cout <<" Welcome to Johnny's Restaurant! " << endl<< "-----------------------------------------------" <<endl;

//goes through the customer order loop and and prints out the results for any item where the quanitiy is greater than 0

for (int i = 1; i < 8; i++)

{

if (customerOrder[i] > 0)

{

cout

<< setw(6)<< left<< customerOrder[i] << setw(18) << menuList[i].menuItem << " $" << menuList[i].menuPrice*customerOrder[i] << endl;

total = total + (menuList[i].menuPrice*customerOrder[i]);

}

}

tax = total*0.05; //calculates tax

due = total + tax; //calculates grand total with tax

//prints the tax and total

cout << setw(6) << left << spacer << setw(18)<< "Tax" << " $"<< tax << endl;

cout << "-----------------------------------------------" << endl;

cout << setw(6) << left << spacer << setw(18)<< "Total" << " $"<< due << endl;

cout << "-----------------------------------------------" << endl;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>

#include <iomanip>

#include <vector>

#include <string>

using namespace std;

struct menuItemType

{

string menuItem;

double menuPrice;

};

void getData(menuItemType menuList[]);

void showMenu(menuItemType menuList[], int x);

void printCheck(vector <menuItemType> menuOrder);

int main()

{

//setting the precision to two decimal places

std::cout << std::setprecision(2) << std::fixed;

// functions & variables

const int menuItems = 8;

menuItemType menuList[menuItems];

vector< menuItemType > menuOrder;

int orderChoice = 0;

bool ordering = true;

int count = 0;

getData(menuList);

showMenu(menuList, menuItems);

while (ordering)

{

cout << "Please enter the number for the item you would "

<< "like to order, or enter [0] to complete your order."

<< endl;

cin >> orderChoice;

if (orderChoice > 0 && orderChoice <= menuItems)

{

menuOrder.push_back(menuList[orderChoice - 1]);

}

else

ordering = false;

}

printCheck(menuOrder);

return 0;

}

void getData(menuItemType menuList[])

{

// menu type

menuItemType plainEgg;

menuItemType baconEgg;

menuItemType muffin;

menuItemType frenchToast;

menuItemType fruitBasket;

menuItemType cereal;

menuItemType coffee;

menuItemType tea;

// menu variables

plainEgg.menuItem = "Plain Egg-------";

plainEgg.menuPrice = 1.45;

baconEgg.menuItem = "Bacon and Egg-----";

baconEgg.menuPrice = 2.45;

muffin.menuItem = "Muffin--------";

muffin.menuPrice = 0.99;

frenchToast.menuItem = "French Toast----------";

frenchToast.menuPrice = 1.99;

fruitBasket.menuItem = "Fruit Basket---------";

fruitBasket.menuPrice = 2.49;

cereal.menuItem = "Cereal---------";

cereal.menuPrice = 0.69;

coffee.menuItem = "Coffee---------";

coffee.menuPrice = 0.50;

tea.menuItem = "Tea-----------";

tea.menuPrice = 0.75;

menuList[0] = plainEgg;

menuList[1] = baconEgg;

menuList[2] = muffin;

menuList[3] = frenchToast;

menuList[4] = fruitBasket;

menuList[5] = cereal;

menuList[6] = coffee;

menuList[7] = tea;

}

void showMenu(menuItemType menuList[], int x)

{

// Function variables

int count;

cout << "Welcome to Johnny's Restaurant !" << endl;

cout << "What would you to order?" << endl;

for (count = 0; count < x; count++)

{

cout << setw(2) << left << "[" << count + 1 << "]"

<< menuList[count].menuItem << '$'

<< menuList[count].menuPrice << endl;

}

}

// print check

void printCheck(vector<menuItemType> menuOrder)

{

double checkTotal = 0;

double checkTax = 0;

const double TAX = .05;

cout << "Thanks for eating at Johnny's Restaurant!"

<< "Customer check: " << endl;

for (int i = 0; i < menuOrder.size(); i++)

{

cout << setprecision(2) << setw(10) << left << menuOrder[i].menuItem

<< '$' << menuOrder[i].menuPrice << endl;

checkTotal += menuOrder[i].menuPrice;

}

checkTax = checkTotal * TAX;

checkTotal += checkTax;

cout << setw(14) << left << "Tax" << checkTax << endl

<< setw(14) << left << "Total" << checkTotal << endl;

system("pause");

}

__________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
I need help refactoring this program so that it it doesn't matter what the exact menu...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT