Advertisement
dessislava_nik

Untitled

Aug 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. package Starbucks;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Starbucks {
  7.  
  8.  
  9. private static final String MAKE_AN_ORDER = "1";
  10. private static final String ORDERS = "2";
  11. private static final String BILL = "3";
  12. private static final String REMOVE = "4";
  13. private static final String EXIT = "0";
  14.  
  15. private static final String Choice = "Please choose an option:";
  16. private static final String Welcome = "***************WELCOME**************";
  17.  
  18. /*
  19. Done 1. When the customer order list is empty and he chose to see his current order - Print that he hasn't ordered anything yet.
  20. Done 2. Transform the multiple if/else to a switch case. Check examples how switch works.
  21. Done 3. There should not be any coma after the last item in the order list.
  22. Done 4. Add a new option in the menu called: I want to pay. It should print out the price. Each item costs 5.51 euro.
  23. Done 5. Move the project to a separate package
  24. */
  25.  
  26. /*
  27. Done 6. Learn how to format your code in IntelliJ - a keyboard shortcut combination
  28. Done 7. Optimize your code to use defined constants instead of the same String written many times
  29. Done 8. Every single item should have it's own price. ex. Espresso - 2.50 euro, Cappuccino - 3.50. Maybe use HashMap in a way ? Check examples and what is it used for?
  30. Done 9. The client should be able to remove an item from his order (This should reduce also the price. Just keep in mind when testing.)
  31. Done 10. The method payTheBill should not PRINT, it should directly return the price. Then the price should be print in the main method.
  32. */
  33.  
  34. /*
  35. Done 11. What is the difference between BREAK and RETURN in examples! Not stupid theory
  36. 12. Rename CurrentOrder to Item
  37. Done 13. Include also FOOD in the menu
  38. Done 14. What is the difference between Class and Object ? What is interface ?
  39. Done 15. What is an abstract method in interface ? How to implement abstract methods?
  40. Done 16. Define all of the food/drink as classes like: class Espresso / class Burger they will extend an interface
  41. Food with abstract methods: getPrice and getCalories which needs to be implemented in the classes who extends it.
  42. So the price should be defined in the method per different class. ex.Espresso
  43. Done 17. The HashMap should contain those objects as a Key and the value of the map should be the available count of the items.
  44. Like: map (Espresso, 10)
  45. 18. When the client orders the exiting count of our products should decrease
  46. 19. All the functionality should remain working.
  47. */
  48.  
  49. private static HashMap<Food, Integer> menu = new HashMap<>();
  50. private static List<CurrentOrder> currentOrderList = new ArrayList<>();
  51. public static double price = 0.0;
  52.  
  53. public static void main(String[] args) {
  54.  
  55. Cappuccino cappuccino = new Cappuccino();
  56. Cake cake = new Cake();
  57. Espresso espresso = new Espresso();
  58. Burger cheeseburger = new Burger();
  59.  
  60. menu.put(espresso, 10);
  61. menu.put(cappuccino, 30);
  62. menu.put(cheeseburger, 40);
  63. menu.put(cake, 25);
  64.  
  65. String customerChoice = "";
  66. while (!customerChoice.equalsIgnoreCase(EXIT)) {
  67. customerChoice = menu();
  68.  
  69. switch (customerChoice) {
  70.  
  71. case MAKE_AN_ORDER:
  72. makeAnOrder();
  73. break;
  74.  
  75. case ORDERS:
  76. if (currentOrderList.isEmpty()) {
  77. System.out.println("You haven`t ordered yet!");
  78. } else {
  79. System.out.println("Your current order is: ");
  80. printCurrentOrderList();
  81. }
  82. break;
  83.  
  84. case BILL:
  85. if (currentOrderList.isEmpty()) {
  86. System.out.println("You haven`t ordered yet!");
  87. return;
  88. } else {
  89. double bill = payTheBill();
  90. System.out.println("Your totalBill is: " + bill + " Euro.");
  91. }
  92. break;
  93.  
  94. case REMOVE:
  95. if (currentOrderList.isEmpty()) {
  96. System.out.println("You haven`t ordered yet!");
  97. return;
  98. } else {
  99. remove();
  100. }
  101. break;
  102.  
  103. case EXIT:
  104. System.out.println("Thank you for coming! Have a nice day!");
  105. break;
  106. }
  107. System.out.println();
  108. }
  109. }
  110.  
  111. private static void makeAnOrder() {
  112. System.out.println("Available drinks: ");
  113. System.out.println();
  114.  
  115. for (Map.Entry item : menu.entrySet()) {
  116. System.out.println(item.getKey() +":" );
  117. System.out.println(item.getKey() + ": " );
  118. System.out.println(item.getKey()+ ": " );
  119. System.out.println(item.getKey() + ": ");
  120. }
  121.  
  122. System.out.println("Choice:");
  123. Scanner scanner = new Scanner(System.in);
  124. String item = scanner.nextLine();
  125. currentOrderList.add(new CurrentOrder(item));
  126. }
  127.  
  128. private static double payTheBill() {
  129. double totalAmount = 0;
  130. for (CurrentOrder currentOrder : currentOrderList) {
  131. totalAmount += menu.get(currentOrder.getCurrentOrder());
  132. }
  133. return totalAmount;
  134. }
  135.  
  136. private static void printCurrentOrderList() {
  137. for (CurrentOrder currentOrder : currentOrderList) {
  138. CurrentOrder lastOrder = currentOrderList.get(currentOrderList.size() - 1);
  139. if (currentOrder.getCurrentOrder().equalsIgnoreCase(lastOrder.getCurrentOrder())) {
  140. System.out.print(currentOrder.getCurrentOrder());
  141. } else {
  142. System.out.print(currentOrder.getCurrentOrder() + ", ");
  143. }
  144. }
  145. }
  146.  
  147. private static void remove() {
  148. System.out.println("Please enter an item you want to remove: ");
  149. Scanner scanner = new Scanner(System.in);
  150. String itemToBeRemoved = scanner.nextLine();
  151. CurrentOrder foundItem = null;
  152. for (CurrentOrder order : currentOrderList) {
  153. if (order.getCurrentOrder().equalsIgnoreCase(itemToBeRemoved)) {
  154. foundItem = order;
  155. }
  156. }
  157. if (foundItem != null) {
  158. currentOrderList.remove(foundItem);
  159. }
  160. }
  161.  
  162. private static String menu() {
  163. System.out.println(Welcome);
  164. System.out.println("1.Make an order");
  165. System.out.println("2.Check your order");
  166. System.out.println("3.Pay the bill");
  167. System.out.println("4.Remove order item");
  168. System.out.println("0.Exit");
  169. System.out.print(Choice);
  170. Scanner yourChoice = new Scanner(System.in);
  171. return yourChoice.nextLine();
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement