Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Objects;
  3. import java.util.Scanner;
  4.  
  5. class InvalidExtraTypeException extends Exception {
  6. public InvalidExtraTypeException(String message) {
  7. super(message);
  8. }
  9. }
  10.  
  11. class InvalidPizzaTypeException extends Exception {
  12. public InvalidPizzaTypeException(String message) {
  13. super(message);
  14. }
  15. }
  16.  
  17.  
  18. class ItemOutOfStock extends Exception {
  19. public ItemOutOfStock(String message) {
  20. super(String.format("%s out of stock", message));
  21. }
  22. }
  23.  
  24. class EmptyOrder extends Exception {
  25. public EmptyOrder(String message) {
  26. super(message);
  27. }
  28. }
  29. class OrderLockedException extends Exception{
  30. public OrderLockedException(String message){
  31. super(message);
  32. }
  33.  
  34. }
  35.  
  36. interface Item {
  37. int getPrice();
  38.  
  39. String getType();
  40. }
  41.  
  42. class ExtraItem implements Item {
  43. String type;
  44.  
  45. ExtraItem(String type) throws InvalidExtraTypeException {
  46. if (type.equals("Coke") || type.equals("Ketchup")) {
  47. this.type = type;
  48. } else throw new InvalidExtraTypeException("Invalid Type Entered as Extra Item");
  49. }
  50.  
  51.  
  52. @Override
  53. public int getPrice() {
  54. if (type.equals("Coke")) {
  55. return 5;
  56. } else return 3;
  57. }
  58.  
  59. @Override
  60. public String getType() {
  61. return type;
  62. }
  63.  
  64. @Override
  65. public boolean equals(Object o) {
  66. if (this == o) return true;
  67. if (o == null || getClass() != o.getClass()) return false;
  68. ExtraItem extraItem = (ExtraItem) o;
  69. return Objects.equals(type, extraItem.type);
  70. }
  71.  
  72. @Override
  73. public int hashCode() {
  74.  
  75. return Objects.hash(type);
  76. }
  77. }
  78.  
  79. class PizzaItem implements Item {
  80.  
  81. String type;
  82.  
  83. PizzaItem(String type) throws InvalidPizzaTypeException {
  84. if (type.equals("Standard") || type.equals("Vegetarian") || type.equals("Pepperoni")) {
  85. this.type = type;
  86. } else throw new InvalidPizzaTypeException("Invalid Type Entered as Pizza Item");
  87. }
  88.  
  89. @Override
  90. public int getPrice() {
  91. if (type.equals("Standard")) {
  92. return 10;
  93. }
  94. else if (type.equals("Pepperoni")) {
  95. return 12;
  96. } else return 8;
  97. }
  98.  
  99. @Override
  100. public String getType() {
  101. return type;
  102. }
  103.  
  104. @Override
  105. public boolean equals(Object o) {
  106. if (this == o) return true;
  107. if (o == null || getClass() != o.getClass()) return false;
  108. PizzaItem pizzaItem = (PizzaItem) o;
  109. return Objects.equals(type, pizzaItem.type);
  110. }
  111.  
  112. @Override
  113. public int hashCode() {
  114.  
  115. return Objects.hash(type);
  116. }
  117. }
  118.  
  119. class Order {
  120. protected ArrayList<Item> items;
  121. protected ArrayList<Integer> count;
  122. boolean isLocked = false;
  123.  
  124. Order() {
  125. this.items = new ArrayList<>();
  126. this.count = new ArrayList<>();
  127. }
  128.  
  129. public void addItem(Item item, int count) throws ItemOutOfStock,OrderLockedException {
  130. if (!isLocked) {
  131. if (count > 10) throw new ItemOutOfStock(item.toString());
  132. if (items.contains(item)) {
  133. int index = items.indexOf(item);
  134. this.count.set(index, count);
  135. } else {
  136. this.items.add(item);
  137. this.count.add(count);
  138. }
  139. }
  140. else throw new OrderLockedException("Message");
  141. }
  142.  
  143. public int getPrice() {
  144. int sum = 0;
  145. for (int i = 0; i < items.size(); i++) {
  146. sum += (items.get(i).getPrice() * count.get(i));
  147. }
  148. return sum;
  149. }
  150.  
  151. ;
  152.  
  153. public void removeItem(int index) throws OrderLockedException {
  154. if (!isLocked) {
  155. if (index >items.size()){throw new ArrayIndexOutOfBoundsException(index);}
  156. else {
  157. items.remove(index);
  158. count.remove(index);
  159. };
  160. }
  161. else throw new OrderLockedException("Message");
  162. }
  163.  
  164. public void lock() throws EmptyOrder {
  165. if (items.size() != 0) {
  166. this.isLocked = true;
  167. } else throw new EmptyOrder("Array empty");
  168. }
  169.  
  170. public void displayOrder() {
  171. StringBuilder sb = new StringBuilder();
  172. for (int i=0; i<items.size(); i++){
  173. sb.append(String.format("%3d.%-15sx%2d%5d$\n", i+1,items.get(i).getType(),count.get(i),(items.get(i).getPrice()*count.get(i))));
  174. }
  175. sb.append(String.format("%-22s%5d$","Total:",getPrice()));
  176. System.out.println(sb.toString());
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement