C:\Users\notebook\Documents\NetBeansProjects\CS2\src\applications\Bistro.java
  1 /*
  2  * Bistro Assistant Program
  3  */
  4 package applications;
  5 
  6 import alist.AList;
  7 import alist.Pair;
  8 import java.util.ArrayList;
  9 import java.util.Scanner;
 10 
 11 /**
 12  *
 13  * @author notebook
 14  */
 15 public class Bistro {
 16 
 17     static private AList<String, Double> menu;
 18 
 19     /**
 20      * @param args the command line arguments
 21      */
 22     public static void main(String[] args) {
 23         establishMenu();
 24         interpretOrders();
 25     }
 26 
 27     private static void establishMenu() {
 28         menu = new AList<String, Double>();
 29         menu.add(new Pair("OYSTERS", 3.00));
 30         menu.add(new Pair("FRIES", 5.00));
 31         menu.add(new Pair("MEATBALLS", 8.00));
 32         menu.add(new Pair("SALAD", 5.00));
 33         menu.add(new Pair("BURGER", 10.00));
 34         menu.add(new Pair("RIBS", 18.00));
 35         menu.add(new Pair("STEAK", 20.00));
 36         menu.add(new Pair("MAC&CHEESE", 9.00));
 37         menu.add(new Pair("BAGEL", 3.00));
 38         menu.add(new Pair("CUPCAKE", 3.00));
 39         menu.add(new Pair("TIRAMISU", 5.50));
 40         menu.add(new Pair("SOUP", 4.00));
 41         menu.add(new Pair("MILKSHAKE", 4.00));
 42         menu.add(new Pair("COFFEE", 2.50));
 43         menu.add(new Pair("SODA", 1.50));
 44     }
 45 
 46     private static void interpretOrders() {
 47         System.out.println("Welcome to Bistro !");
 48         Scanner scanner = new Scanner(System.in);
 49         while (true) {
 50             System.out.print(">>> ");
 51             ArrayList<String> commands = new ArrayList<>();
 52             String input = scanner.nextLine();
 53             String[] splitedInput = input.split(" ");
 54             for (int a = 0; a < splitedInput.length; a = a + 1) {
 55                 commands.add(splitedInput[a]);
 56             }
 57             String command = commands.get(0);
 58             if (command.equalsIgnoreCase("menu")) {
 59                 menu.display();
 60             } else if (command.equalsIgnoreCase("order")) {
 61                 System.out.print("What do you want? ");
 62                 ArrayList<String> items = separateItems(scanner);
 63                 double cost = calculateCost(items);
 64                 System.out.println("Cost = $" + cost);
 65                 double withTax = cost * 1.08;
 66                 System.out.println("Cost + Tax = $" + withTax);
 67                 double withTip = withTax*1.15;
 68                 System.out.println("Cost + Tax + Tip = $" + withTip);
 69             } else if (command.equalsIgnoreCase("exit")) {
 70                 System.out.println("Thank you!");
 71                 break;
 72             } else if (command.equalsIgnoreCase("help")) {
 73                 System.out.println("MENU -- Display the Bistro menu");
 74                 System.out.println("ORDER -- Order items from the menu");
 75                 System.out.println("EXIT -- Exit the application");
 76                 System.out.println("HELP -- Display all available commands");
 77             } else {
 78                 System.out.println(
 79                         "Sorry, the command you entered is not available. "
 80                         + "You can use the \"HELP\" command to see all commands available.");
 81             }
 82         }
 83     }
 84 
 85     private static ArrayList<String> separateItems(Scanner scanner) {
 86         ArrayList<String> items = new ArrayList<>();
 87         String input = scanner.nextLine();
 88         String[] splitedInput = input.split(" ");
 89         for (int a = 0; a < splitedInput.length; a = a + 1) {
 90             items.add(splitedInput[a].toUpperCase());
 91         }
 92         return items;
 93     }
 94 
 95     private static double calculateCost(ArrayList<String> items) {
 96         double cost = 0.00;
 97         for (String item : items) {
 98             if (menu.member(item)) {
 99                 cost = cost + (double) menu.lookup(item);
100             } else {
101                 System.out.println("Sorry, " + item + " is not on our menu.");
102             }
103         }
104         return cost;
105     }
106 
107 }
108