EliteByte

Fast Food

Oct 20th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 KB | None | 0 0
  1.  
  2. /**
  3.  * The FastFood program implements an application that
  4.  * simulates a fast-food-like run through, using user-driven input.
  5.  *
  6.  * Tinyurl: tinyurl.com/APCSYeezyFastFood
  7.  * Pastebin Raw: https://pastebin.com/edit/XephUHe1
  8.  *
  9.  * @author  Mario Figueroa
  10.  * @version 1.0
  11.  * @since   2017-10-20
  12.  */
  13.  
  14. import java.util.Scanner;
  15.  
  16. public class FastFood {
  17.  
  18.  
  19.     public int[] numberOfItems;
  20.     public double total = 0;
  21.     public String[] menu;
  22.     public double[] prices;
  23.     public double tax;
  24.     public String nameOfRestaurant;
  25.  
  26.     /**
  27.      * This constructor initializes a FastFood Restaurant
  28.      * by setting up global variables.
  29.      * @param nameOfRestaurant This is the name of the restaurant used for
  30.      *                         the welcoming message.
  31.      * @param menu This is the String Array with all of the restaurant's menu
  32.      *             items, matched with the prices array.
  33.      * @param prices This is the Double Array with all of the restaurant's menu
  34.      *               item's prices, matched with the menu array.
  35.      * @param tax This is the tax which is used in the finalizeOrder method
  36.      *            when calculating the total order price.
  37.      */
  38.     public FastFood(String nameOfRestaurant, String[] menu, double[] prices, double tax) {
  39.         this.menu = menu;
  40.         this.prices = prices;
  41.         this.tax = tax;
  42.         this.nameOfRestaurant = nameOfRestaurant;
  43.         this.numberOfItems = new int[menu.length];
  44.     }
  45.  
  46.     public FastFood(String nameOfRestaurant, String[] menu, double[] prices) {
  47.         this.menu = menu;
  48.         this.prices = prices;
  49.         this.tax = 0;
  50.         this.nameOfRestaurant = nameOfRestaurant;
  51.         this.numberOfItems = new int[menu.length];
  52.     }
  53.  
  54.     public FastFood(String[] menu, double[] prices, double tax) {
  55.         this.menu = menu;
  56.         this.prices = prices;
  57.         this.tax = tax;
  58.         this.nameOfRestaurant = "Null";
  59.         this.numberOfItems = new int[menu.length];
  60.     }
  61.  
  62.  
  63.     /**
  64.      * This method essentially boots up the whole FastFood program and ends
  65.      * once the user has concluded his/her order.
  66.      */
  67.     public void simulateOrder() {
  68.  
  69.         String[] formattedMenu = formatMenu(menu, prices);
  70.  
  71.         System.out.println("Hello, Welcome to " + nameOfRestaurant);
  72.         System.out.println(formattedMenu[0]);
  73.  
  74.         askForOrder();
  75.  
  76.     }
  77.  
  78.     /**
  79.      * This method is used to ask the user what they would want.
  80.      * The method is recursive meaning it will call itself again,
  81.      * if and only if their response is NOT "None", which concludes
  82.      * the order.
  83.      */
  84.     private void askForOrder() {
  85.  
  86.         System.out.println("What would you like?");
  87.  
  88.         Scanner userInput = new Scanner(System.in);
  89.         String response = userInput.next();
  90.  
  91.         if (response.matches("None")) {
  92.             System.out.println("Your total is, $" + finalizeOrder());
  93.         } else if (processOrder(response) == true) {
  94.             askForOrder();
  95.         }
  96.  
  97.     }
  98.  
  99.  
  100.     /**
  101.      * This method is used to first check the order's validity, and
  102.      * then ask how many of a particular item the user will want.
  103.      * This number is then stored in the numberOfItems Array which
  104.      * is used to calculate the final order price.
  105.      * (Note: Check finalizeOrder() method).
  106.      * @param response This is the user's input. i.e. "Cheeseburger"
  107.      * @return boolean This is if the entire order is successful, meaning
  108.      * the order is valid (Note: Check checkOrderValidity() Method).
  109.      */
  110.     private boolean processOrder(String response) {
  111.  
  112.         if (checkOrderValidity(response)) {
  113.             Scanner userInput = new Scanner(System.in);
  114.  
  115.             System.out.println("How many would you like?");
  116.             int numberOfItem = userInput.nextInt();
  117.  
  118.             int itemNumber = getItemNumber(response);
  119.             numberOfItems[itemNumber] += numberOfItem;
  120.             return true;
  121.         }
  122.  
  123.         return false;
  124.     }
  125.  
  126.  
  127.     /**
  128.      * This method is used to finalize a user's order by calculating
  129.      * his/her order price. This is accomplished by looping through
  130.      * each item in the menu and then multiplying the number of each
  131.      * item that is stored in "numberOfItems" by the appropriate price.
  132.      * Example: 4 Cheeseburgers, 4 * 1 Cheeseburger Price.
  133.      * @return double The final calculated total price which includes tax.
  134.      */
  135.     private double finalizeOrder() {
  136.  
  137.         double orderTotal = 0;
  138.  
  139.         for (int counter = 0; counter < menu.length; counter++) {
  140.             orderTotal += numberOfItems[counter] * prices[counter];
  141.         }
  142.  
  143.         double taxTotal = ((1 + tax) * orderTotal);
  144.         taxTotal = Math.round(taxTotal * 100.0) / 100.0;
  145.  
  146.         // Math.round(123.666666667);
  147.         // Return -> 123.7
  148.         // Math.round(123.666666667 * 100);
  149.         // Return -> 12366.7
  150.         // (double) Math.round(123.666666667 * 100) / 100;
  151.         // Return -> 123.67
  152.  
  153.         return taxTotal;
  154.     }
  155.  
  156.  
  157.     // HELPER METHODS
  158.  
  159.     /**
  160.      * This method is used to format a fast-food's menu
  161.      * and prices. An extra comma is printed after the last
  162.      * item in the menu/prices.
  163.      * @param menu This is an array of all the menu items.
  164.      * @param prices This is an array of all the menu item's prices.
  165.      * @return String[] This returns a nicely printed version
  166.      * of the menu with their corresponding prices in a
  167.      * user-friendly/readable format.
  168.      */
  169.     private String[] formatMenu(String[] menu, double[] prices) {
  170.  
  171.         String[] formattedMenu = {"Menu:", "Price:"};
  172.         if (menu.length == prices.length) {
  173.             for (int counter = 0; counter < menu.length; counter++) {
  174.                 formattedMenu[0] += (" " + menu[counter] + " $" + prices[counter] + ",");
  175. //                formattedMenu[1] += ();
  176.             }
  177.  
  178.             return formattedMenu;
  179.         } else {
  180.             System.err.println("The amount of items on the menu, " +
  181.                     "don't match the amount of prices");
  182.             return formattedMenu;
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * This method is used to find an item's number in accordance
  188.      * to the placement inside the Menu/Prices array. It does so by
  189.      * looping through the entire Menu array and finding when and where
  190.      * it equals the menu item, which the for loop counter will take note
  191.      * of where it landed.
  192.      * @param response This is the user's input. i.e. "Cheeseburger"
  193.      * @return int This is the item's number in the Menu/Prices Array
  194.      */
  195.     private int getItemNumber(String response) {
  196.  
  197.         for (int counter = 0; counter < menu.length; counter++) {
  198.             if (response.equalsIgnoreCase(menu[counter])) {
  199.                 return counter;
  200.             }
  201.         }
  202.  
  203.         return -1;
  204.     }
  205.  
  206.     /**
  207.      * This method is used to check if the user has inputted a
  208.      * valid item name. For example: if they enter "Cheeeseburger",
  209.      * instead of "Cheeseburger" it will not recognize it and return false.
  210.      * @param response This is the user's input. i.e. "Cheeseburger"
  211.      * @return boolean If the user's input is a valid menu item, it
  212.      * will return true, otherwise it will return false.
  213.      */
  214.     private boolean checkOrderValidity(String response) {
  215.  
  216.         for (String item : menu) {
  217.             if (response.equalsIgnoreCase(item)) {
  218.                 return true;
  219.             }
  220.         }
  221.  
  222.         return false;
  223.     }
  224.  
  225. }
Add Comment
Please, Sign In to add comment