Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. // Helper Strings
  8. string banner1 = "=====================================================";
  9. string banner2 = "============ Welcome to the Dining Hall =============";
  10. string voucherChoice = "Please Choose Your Dining Voucher: \n (1) $10, (2) $ 20, (3) $50, (4) $100 \n";
  11. string voucherBalance = "You've inserted: \n";
  12. string voucherMoreMoney = "Add more funds? (Y/N)";
  13. string voucherFoodMenu = "Please make a selection: \n (P)otato Chip $2.50, (H)am Burger $%.50, (C)hicken Rice , (B)rooklyn Pizza $4.50 \n";
  14. string voucherDrinkMenu = "Select your beverages: \n (A)quaVenna $1.50, (B)epsi $2.00, (C)ool Cola $2.00, (G)atorade $2.25, (N)o Beverage \n";
  15. string insufficientFunds = "Insufficient amount to make selection \n";
  16. string changeProvided = "Enjoy your meals \n";
  17.  
  18. // Helper Functions
  19. // Return the cost of the voucher food menu
  20. // parameters: voucherFoodMenu is p,h,c or b (upper or lower case)
  21. float getFoodCost(char voucherFoodMenu)
  22. {
  23.     if(voucherFoodMenu == 'P' || voucherFoodMenu == 'p')
  24.         return 1.50;
  25.     else if(voucherFoodMenu == 'B' || voucherFoodMenu == 'b' || voucherFoodMenu == 'C' || voucherFoodMenu == 'c')
  26.         return 2.00;
  27.     else if(voucherFoodMenu == 'G' || voucherFoodMenu == 'g')
  28.         return 2.25;
  29.     else if(voucherFoodMenu == 'N' || voucherFoodMenu == 'n')
  30.         return 0.0;
  31. }
  32.  
  33. // Return the cost of the voucher drink menu
  34. // parameters: voucherDrinkMenu is a,b,c,g or n (upper or lower case)
  35. float getDrinkCost(char voucherDrinkMenu)
  36. {
  37.  
  38.     if(voucherDrinkMenu == 'P' || voucherDrinkMenu == 'p')
  39.         return 2.50;
  40.     else if(voucherDrinkMenu == 'H' || voucherDrinkMenu == 'h' || voucherDrinkMenu == 'C' || voucherDrinkMenu == 'c')
  41.         return 5.50;
  42.     else if(voucherDrinkMenu == 'B' || voucherDrinkMenu == 'b')
  43.         return 4.50;
  44.     return 0.0;
  45. }
  46.  
  47. // Return true if the balance can cover the cost.
  48. // parameters:  balance is the user's balance.
  49. //              cost is the price of the item.
  50. bool hasSufficientFunds(float balance, float cost)
  51. {
  52.     if( balance >= cost )
  53.     {
  54.         return true;
  55.     }
  56.  
  57.     return false;
  58. }
  59.  
  60. // function to translate the selected number (1,2,3,4) to a dollar amount ($10, $20, $50, $100).
  61. float receiveMoney(char bill)
  62. {
  63.     float voucherMoneyInsterted = 0;
  64.  
  65.     // receive money
  66.     if (bill == '1')
  67.         voucherMoneyInsterted = 10.00;
  68.     else if  (bill == '2')
  69.         voucherMoneyInsterted = 20.00;
  70.     else if  (bill == '3')
  71.         voucherMoneyInsterted = 50.00;
  72.     else if  (bill == '4')
  73.         voucherMoneyInsterted = voucherMoneyInsterted + 100.00;
  74.     else {
  75.         std::cout << "Invalid input." << std::endl;
  76.         return 0;
  77.     }
  78.     return voucherMoneyInsterted;
  79. }
  80.  
  81. // Function to offer more voucherFoodMenu
  82. // Return true if user wants more, otherwise give change.
  83. bool offerMoreVoucherFood()
  84. {
  85.     // ask if user wants to purchase more drinks
  86.     cout << "Add more meals?\n";
  87.     char addMoreVoucherFoodYN;
  88.     cin >> addMoreVoucherFoodYN;
  89.  
  90.     if (addMoreVoucherFoodYN == 'N' || addMoreVoucherFoodYN == 'n')
  91.         return false;
  92.     return true;
  93. }
  94.  
  95. // Function to offer more voucherDrinkMenu
  96. // Return true if user wants more, otherwise give change.
  97. bool offerMoreVoucherDrinks()
  98. {
  99.     // ask if user wants to purchase more drinks
  100.     cout << "Add more drinks?\n";
  101.     char addMoreVoucherDrinksYN;
  102.     cin >> addMoreVoucherDrinksYN;
  103.  
  104.     if (addMoreVoucherDrinksYN == 'N' || addMoreVoucherDrinksYN == 'n')
  105.         return false;
  106.     return true;
  107. }
  108.  
  109. // Variables
  110. bool done = false;
  111. char bill;
  112. char voucherFoodSelected;
  113. float voucherMoneyInsterted (0);
  114. float voucherTotalMoney (0);
  115.  
  116.  
  117. int main()
  118. {
  119.  
  120. //banner
  121. cout<< "=====================================================" << endl;
  122. cout<< "============ Welcome to the Dining Hall =============" << endl;
  123. cout<< "=====================================================" << endl;
  124.  
  125.         //start
  126.         while (!done){
  127.  
  128.         // Accept Payment
  129.         bool addingMoney = true;
  130.         while (addingMoney)
  131.         {
  132.             char moreMoneyYN;
  133.  
  134.         //request money
  135.         cout<< voucherChoice << endl;
  136.             char bill;
  137.             cin >> bill;
  138.          // based on input, get the monetary value
  139.             voucherMoneyInsterted = receiveMoney(bill);
  140.  
  141.         //tell use how much money was inserted
  142.         cout << voucherBalance << voucherMoneyInsterted << endl;
  143.  
  144.         //ask for more money
  145.  
  146.         cout << "Add more funds? (Y/N)\n";
  147.             char addingMoneyYN;
  148.             cin >> addingMoneyYN;
  149.  
  150.             if(addingMoneyYN == 'N' || addingMoneyYN == 'n')
  151.             {
  152.         // if the user did not input enough for cheapest meal quit.
  153.             if( !hasSufficientFunds(voucherTotalMoney, 1.5) )
  154.             {
  155.                     cout << insufficientFunds << endl;
  156.                     return 0;
  157.             }
  158.  
  159.                 addingMoney = false;
  160.         }
  161.     }
  162.  
  163.         //tell user the balance
  164.         cout << voucherTotalMoney << endl;
  165.  
  166.             bool orderingVoucherFood = true;
  167.             while (orderingVoucherFood)
  168.         {
  169.         //accept selection
  170.         char voucherFoodMenu;
  171.         cout << voucherFoodMenu << endl;
  172.         cin >> voucherFoodMenu;
  173.         cout << "Meal selected: " << voucherFoodMenu << endl;
  174.  
  175.         //check if balance is sufficient to cover purchase of voucher food
  176.         float voucherFoodCost = getFoodCost(voucherFoodMenu);
  177.             bool enoughFunds = hasSufficientFunds(voucherTotalMoney, voucherFoodCost);
  178.  
  179.             if( enoughFunds )
  180.             {
  181.                 // Vend the drink and subtract payment
  182.                 // voucherTotalMoney = voucherTotalMoney - drinkCost;
  183.                 voucherTotalMoney -= voucherFoodCost;
  184.                 cout << "Coins/Bills: " << voucherTotalMoney << endl;
  185.  
  186.         //if enough funds for voucher food do this
  187.         if( !hasSufficientFunds(voucherTotalMoney, 1.5))
  188.                 {
  189.                     cout << voucherTotalMoney << endl;
  190.                     return 0;
  191.                 }
  192.             }
  193.             else
  194.             {
  195.                 cout << "Coins/Bills: " << voucherTotalMoney - voucherFoodCost << endl;
  196.                 cout << insufficientFunds << "\nInsufficient amount to make selection\n";
  197.                 cout << "Coins/Bills: " << voucherTotalMoney << endl;
  198.             }
  199.  
  200.             if(!offerMoreVoucherFood())
  201.             {
  202.                 cout << voucherTotalMoney << endl;
  203.                 done = true;
  204.             }
  205.         }
  206.  
  207.         done = true;
  208.     }
  209.  
  210.     cout << "Bye!\n";
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement