Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1. /*
  2.      Lab 1
  3.      CS2
  4.      Updated by: Tommy Weber (5 points) - fixed
  5.      Date: 1/18/2019 (5 points) - fixed
  6.  
  7.      Breakfast billing system for a restaurant.
  8.      
  9.      This program allows restaurant customers to select breakfast items and prints the bill.
  10.  
  11.  
  12. Problem Requirements
  13.   - Write a C++ program to help a local restaurant automate its breakfast billing system. The program should do the following at a minimum.
  14.  
  15.   - Program should read the menu data from file menu.txt, sample is provided to you.
  16.   - Show customer the different breakfast items offered by the restaurant.
  17.   - Allow customer to select more than one item from the menu.
  18.   - Do not allow customer to select the same item more than once.
  19.   - When done selecting items, prompt user to enter filename and save the receipt in the output file as shown in the given guest1.check sample file.
  20.   - Bonus 10 points.  Make the program run continuously, until the user wants to quit.
  21.  
  22.  
  23. Follow best programming practices:
  24.   - Write adequate comments. Comments can help you plan for the solution and think through the problem.
  25.   - Refrain from using global variables; constants are okay.
  26.   - Use adequate indentation and whitespaces to cleanly format your code.
  27.  
  28. Submission:
  29. If your program doesn’t compile, you’ll get a zero for this lab.
  30. Add all the source file(s) *.cpp, *.h, etc., sample input and output files into the repository
  31. $ git status
  32. $ git add … - add each file that was new or modified that is part of the lab
  33. $ git commit -m “Final Submission”
  34. $ git push
  35. NOTE: Do not modify this folder contents after the due date as it may be considered late submission!
  36.  
  37. ===================================================================================
  38. */
  39.  
  40. #include <iostream>
  41. #include <fstream>
  42. #include <iomanip>
  43. #include <string>
  44.  
  45. using namespace std;
  46.  
  47. // max no. of menu items that can be stored by the program
  48. const int NO_OF_ITEMS = 8;
  49.  
  50. // data structure to hold each menu item info
  51. struct menuItemType
  52. {
  53.     string itemName;
  54.     double itemPrice;
  55. };
  56.  
  57. // initialize menu list with some default values
  58. void initMenuList(menuItemType menuList[], int menuListSize);
  59. // function that reads menu data into menuList array
  60. void readMenuData(ifstream& fin, menuItemType menuList[], int menuListSize);
  61. // function that displays the menu
  62. void showMenu(menuItemType menuList[], int menuListSize);
  63. // function to print check after menu items are selected
  64. void printCheck(menuItemType menuList[], int menuListSize,
  65.     int selectedList[], int selectedListLength);
  66. // function to select items from the menu list
  67. void makeSelection(menuItemType menuList[], int menuListSize,
  68.     int selectedList[], int& selectedListLength);
  69. // function that returns true if the itemNo is in the selectedList otherwise false.
  70. // this function avoids user repeating the menu item
  71. bool isItemSelected(int selectedList[], int selectedListLength, int itemNo);
  72.  
  73. int main()
  74. {
  75.     menuItemType menuList[NO_OF_ITEMS];
  76.     int choiceList[NO_OF_ITEMS]; // array to keep track of choices based on item number
  77.     int choiceListLength; // variable to keep track of no. of choices
  78.     ifstream fin;
  79.     cout << fixed << showpoint << setprecision(2);
  80.     // open menu.txt file to read menu data from
  81.     string menuFileName = "menu.txt";
  82.     fin.open(menuFileName);
  83.     if (!fin)
  84.     {
  85.         cout << "Input file " << menuFileName << " does not exist. Program Terminates!"
  86.             << endl;
  87.         cout << "Hit enter to terminate the program!";
  88.         cin.get();
  89.         return 1;
  90.     }
  91.     initMenuList(menuList, NO_OF_ITEMS);
  92.     // FIXME3 – (5 points) - fixed
  93.     // Call readMenuData function passing proper arguments
  94.     readMenuData(fin, menuList, NO_OF_ITEMS);
  95.    
  96.     fin.close(); //close input file
  97.     showMenu(menuList, NO_OF_ITEMS);
  98.     makeSelection(menuList, NO_OF_ITEMS, choiceList, choiceListLength);
  99.     printCheck(menuList, NO_OF_ITEMS, choiceList, choiceListLength);
  100.     // pause the program until a key is entered
  101.     cin.get();
  102.     cin.get();
  103.     return 0;
  104. }
  105.  
  106. void initMenuList(menuItemType menuList[], int menuListSize)
  107. {
  108.     int itemNameCount = 0, itemPriceCount = 1; // Will help assign items in menuList to both .itemName & .itemPrice
  109.     const int MAX = (menuListSize*2);
  110.     string fullMenuList[MAX] = {"Plain Egg", "0", "Bacon and Egg", "0", "Muffin", "0", "French Toast", "0",
  111.                                 "Fruit Basket", "0", "Cereal", "0", "Coffee", "0", "Tea", "0"};
  112.     // Assigns values to items in class.
  113.     for (int i = 0; i < menuListSize; ++i)
  114. {
  115.         // FIXME4 (5 points) - fixed
  116.         // Initialize each menu item's name to A Tasty Item (DONE)
  117.         // Initialize each menu item's price to 0; (DONE)
  118.         menuList[i].itemName = fullMenuList[itemNameCount];
  119.         menuList[i].itemPrice = stod( fullMenuList[itemPriceCount] );
  120.  
  121.         //cout << "Debug #38571: " << menuList[i].itemName << "    " << menuList[i].itemPrice << "\n";
  122.         itemNameCount += 2;
  123.         itemPriceCount += 2;
  124.     }
  125. }
  126.  
  127. void readMenuData(ifstream& fin, menuItemType menuList[], int menuListSize)
  128. {
  129.     char ch;
  130.     for (int i = 0; i < menuListSize; i++)
  131.     {
  132.         // FIXME5 (10 points) - WIP
  133.         // 1. Read each item name into itemName field of menuList array
  134.         // 2. Read each item price into itemPrice field of menuList array
  135.         //cout << menuList[i].itemName << "\n";
  136.  
  137.     }
  138. }
  139.  
  140. void showMenu(menuItemType menuList[], int menuListSize)
  141. {
  142.     cout << "Welcome to Papi Joey's Kitchen" << endl;
  143.     cout << "----Today's Menu----" << endl;
  144.  
  145.     for (int i = 0; i < menuListSize; i++)
  146.     {
  147.         cout << i + 1 << ": " << left << setw(15) << menuList[i].itemName
  148.             << right << " $" << menuList[i].itemPrice << endl;
  149.     }
  150.     cout << endl;
  151. }
  152.  
  153. void printCheck(menuItemType menuList[], int menuListSize,
  154.                 int selectedList[], int selectedListLength)
  155. {
  156.     int i;
  157.     double salesTax;
  158.     double amountDue = 0;
  159.  
  160.     cout << "   Papi Joey's Kitchen" << endl;
  161.     cout << "       Guest Check    " << endl;
  162.     cout << setw(25) << setfill('=') << endl;
  163.     cout << setfill(' ') << endl;
  164.     for (i = 0; i < selectedListLength; i++)
  165.     {
  166.         cout << left << setw(15) << menuList[selectedList[i]].itemName
  167.             << right << " $" << setw(4) << menuList[selectedList[i]].itemPrice << endl;
  168.         amountDue += menuList[selectedList[i]].itemPrice;
  169.     }
  170.     cout << endl;
  171.     salesTax = amountDue * .07;
  172.     cout << left << setw(15) << "Tax " << right << " $"
  173.         << salesTax << endl;
  174.     amountDue = amountDue + salesTax;
  175.     cout << left << setw(15) << "Amount Due " << right
  176.         << " $" << amountDue << endl << endl;
  177.     cout << setw(25) << setfill('=') << ' ' << endl;
  178.     cout << setfill(' ') << endl;
  179.     cout << "     Thank you! " << endl;
  180.     // FIXME6 (10 points)
  181.     // 1. Prompt user to enter output filename to write receipt data.
  182.     // 2. Create the file and write the check info (as you see on the common output) into the file.
  183.     // 3. Provide feedback to the user, once done.
  184.     // 4. Close the file.
  185. }
  186.  
  187. void makeSelection(menuItemType menuList[], int menuListSize, int selectedList[],
  188.  int& selectedListLength)
  189. {
  190.     int selectionNo = 0;
  191.     int itemNo;
  192.     char response;
  193.     selectedListLength = 0;
  194.  
  195.     cout << "You can make up to " << menuListSize
  196.         << " single order selections" << endl;
  197.  
  198.     cout << "Do you want to make selection Y/y (Yes), N/n (No): ";
  199.     cin >> response;
  200.     cout << endl;
  201.  
  202.     while ((response == 'Y' || response == 'y') &&
  203.         selectedListLength < menuListSize)
  204.     {
  205.         cout << "Enter item number: ";
  206.         cin >> itemNo;
  207.         cout << endl;
  208.  
  209.         if (!isItemSelected(selectedList, selectedListLength, itemNo))
  210.             selectedList[selectedListLength++] = itemNo - 1;
  211.         else
  212.             cout << "Item already selected" << endl;
  213.  
  214.         cout << "Select another item Y/y (Yes), N/n (No): ";
  215.         cin >> response;
  216.         cout << endl;
  217.     }
  218. }
  219.  
  220. bool isItemSelected(int selectedList[], int selectedListLength, int itemNo)
  221. {
  222.     bool found = false;
  223.     /*
  224.     FIXME7 (10 points)
  225.     Algorithm steps:
  226.     1. Go through each item in selectedList and check if itemNo is in there.
  227.     2. If it is, return true, otherwise false
  228.     */
  229.     return found;
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement