Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The FastFood program implements an application that
- * simulates a fast-food-like run through, using user-driven input.
- *
- * Tinyurl: tinyurl.com/APCSYeezyFastFood
- * Pastebin Raw: https://pastebin.com/edit/XephUHe1
- *
- * @author Mario Figueroa
- * @version 1.0
- * @since 2017-10-20
- */
- import java.util.Scanner;
- public class FastFood {
- public int[] numberOfItems;
- public double total = 0;
- public String[] menu;
- public double[] prices;
- public double tax;
- public String nameOfRestaurant;
- /**
- * This constructor initializes a FastFood Restaurant
- * by setting up global variables.
- * @param nameOfRestaurant This is the name of the restaurant used for
- * the welcoming message.
- * @param menu This is the String Array with all of the restaurant's menu
- * items, matched with the prices array.
- * @param prices This is the Double Array with all of the restaurant's menu
- * item's prices, matched with the menu array.
- * @param tax This is the tax which is used in the finalizeOrder method
- * when calculating the total order price.
- */
- public FastFood(String nameOfRestaurant, String[] menu, double[] prices, double tax) {
- this.menu = menu;
- this.prices = prices;
- this.tax = tax;
- this.nameOfRestaurant = nameOfRestaurant;
- this.numberOfItems = new int[menu.length];
- }
- public FastFood(String nameOfRestaurant, String[] menu, double[] prices) {
- this.menu = menu;
- this.prices = prices;
- this.tax = 0;
- this.nameOfRestaurant = nameOfRestaurant;
- this.numberOfItems = new int[menu.length];
- }
- public FastFood(String[] menu, double[] prices, double tax) {
- this.menu = menu;
- this.prices = prices;
- this.tax = tax;
- this.nameOfRestaurant = "Null";
- this.numberOfItems = new int[menu.length];
- }
- /**
- * This method essentially boots up the whole FastFood program and ends
- * once the user has concluded his/her order.
- */
- public void simulateOrder() {
- String[] formattedMenu = formatMenu(menu, prices);
- System.out.println("Hello, Welcome to " + nameOfRestaurant);
- System.out.println(formattedMenu[0]);
- askForOrder();
- }
- /**
- * This method is used to ask the user what they would want.
- * The method is recursive meaning it will call itself again,
- * if and only if their response is NOT "None", which concludes
- * the order.
- */
- private void askForOrder() {
- System.out.println("What would you like?");
- Scanner userInput = new Scanner(System.in);
- String response = userInput.next();
- if (response.matches("None")) {
- System.out.println("Your total is, $" + finalizeOrder());
- } else if (processOrder(response) == true) {
- askForOrder();
- }
- }
- /**
- * This method is used to first check the order's validity, and
- * then ask how many of a particular item the user will want.
- * This number is then stored in the numberOfItems Array which
- * is used to calculate the final order price.
- * (Note: Check finalizeOrder() method).
- * @param response This is the user's input. i.e. "Cheeseburger"
- * @return boolean This is if the entire order is successful, meaning
- * the order is valid (Note: Check checkOrderValidity() Method).
- */
- private boolean processOrder(String response) {
- if (checkOrderValidity(response)) {
- Scanner userInput = new Scanner(System.in);
- System.out.println("How many would you like?");
- int numberOfItem = userInput.nextInt();
- int itemNumber = getItemNumber(response);
- numberOfItems[itemNumber] += numberOfItem;
- return true;
- }
- return false;
- }
- /**
- * This method is used to finalize a user's order by calculating
- * his/her order price. This is accomplished by looping through
- * each item in the menu and then multiplying the number of each
- * item that is stored in "numberOfItems" by the appropriate price.
- * Example: 4 Cheeseburgers, 4 * 1 Cheeseburger Price.
- * @return double The final calculated total price which includes tax.
- */
- private double finalizeOrder() {
- double orderTotal = 0;
- for (int counter = 0; counter < menu.length; counter++) {
- orderTotal += numberOfItems[counter] * prices[counter];
- }
- double taxTotal = ((1 + tax) * orderTotal);
- taxTotal = Math.round(taxTotal * 100.0) / 100.0;
- // Math.round(123.666666667);
- // Return -> 123.7
- // Math.round(123.666666667 * 100);
- // Return -> 12366.7
- // (double) Math.round(123.666666667 * 100) / 100;
- // Return -> 123.67
- return taxTotal;
- }
- // HELPER METHODS
- /**
- * This method is used to format a fast-food's menu
- * and prices. An extra comma is printed after the last
- * item in the menu/prices.
- * @param menu This is an array of all the menu items.
- * @param prices This is an array of all the menu item's prices.
- * @return String[] This returns a nicely printed version
- * of the menu with their corresponding prices in a
- * user-friendly/readable format.
- */
- private String[] formatMenu(String[] menu, double[] prices) {
- String[] formattedMenu = {"Menu:", "Price:"};
- if (menu.length == prices.length) {
- for (int counter = 0; counter < menu.length; counter++) {
- formattedMenu[0] += (" " + menu[counter] + " $" + prices[counter] + ",");
- // formattedMenu[1] += ();
- }
- return formattedMenu;
- } else {
- System.err.println("The amount of items on the menu, " +
- "don't match the amount of prices");
- return formattedMenu;
- }
- }
- /**
- * This method is used to find an item's number in accordance
- * to the placement inside the Menu/Prices array. It does so by
- * looping through the entire Menu array and finding when and where
- * it equals the menu item, which the for loop counter will take note
- * of where it landed.
- * @param response This is the user's input. i.e. "Cheeseburger"
- * @return int This is the item's number in the Menu/Prices Array
- */
- private int getItemNumber(String response) {
- for (int counter = 0; counter < menu.length; counter++) {
- if (response.equalsIgnoreCase(menu[counter])) {
- return counter;
- }
- }
- return -1;
- }
- /**
- * This method is used to check if the user has inputted a
- * valid item name. For example: if they enter "Cheeeseburger",
- * instead of "Cheeseburger" it will not recognize it and return false.
- * @param response This is the user's input. i.e. "Cheeseburger"
- * @return boolean If the user's input is a valid menu item, it
- * will return true, otherwise it will return false.
- */
- private boolean checkOrderValidity(String response) {
- for (String item : menu) {
- if (response.equalsIgnoreCase(item)) {
- return true;
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment