Advertisement
Filip_Markoski

[NP] 3.1 Пицерија (Solved)

Oct 25th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.25 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.util.stream.IntStream;
  5.  
  6. interface Item {
  7.     public int getPrice();
  8.     public String getType();
  9. }
  10.  
  11. /* Item Exceptions */
  12. class InvalidExtraTypeException extends Exception{}
  13. class InvalidPizzaTypeException extends Exception{}
  14.  
  15. class ExtraItem implements Item {
  16.     private String type;
  17.     public ExtraItem(String type) throws InvalidExtraTypeException {
  18.         if (!type.equals("Coke") && !type.equals("Ketchup")) {
  19.             throw new InvalidExtraTypeException();
  20.         }
  21.         this.type = type;
  22.     }
  23.    
  24.     public String getType() { return type; }
  25.    
  26.     public ExtraItem(ExtraItem i){
  27.         type = i.type;
  28.     }
  29.     /*public ExtraItem(Item i){
  30.         type = i.type;
  31.     }*/
  32.     public int getPrice() {
  33.         if (type.equals("Coke")) {
  34.             return 5;
  35.         } else if (type.equals("Ketchup")) {
  36.             return 3;
  37.         } else { return -1; }
  38.     }
  39. }
  40.  
  41. class PizzaItem implements Item {
  42.     private String type;
  43.     public PizzaItem(String type) throws InvalidPizzaTypeException {
  44.         /*if(!type.equals("Standard") && !type.equals("Pepperoni") && !type.equals("Vegetarian")){
  45.             throw new InvalidPizzaTypeException();
  46.         }*/
  47.         String[] items = {"Standard", "Pepperoni", "Vegetarian"};
  48.         if (Arrays.stream(items).noneMatch(i -> i.equals(type))){
  49.             throw new InvalidPizzaTypeException();
  50.         }
  51.         this.type = type;
  52.     }
  53.  
  54.     public String getType() { return type; }
  55.    
  56.     public PizzaItem(PizzaItem i){
  57.         type = i.type;
  58.     }
  59.     /*public PizzaItem(Item i){
  60.         type = i.type;
  61.     }*/
  62.     public int getPrice(){
  63.         /*if(type.equals("Standard")){
  64.             return 10;
  65.         } else if(type.equals("Pepperoni")){
  66.             return 12;
  67.         } else if(type.equals("Vegetarian")){
  68.             return 8;
  69.         } else { return -1; }*/
  70.         switch (type) {
  71.             case "Standard": return 10;
  72.             case "Pepperoni": return 12;
  73.             case "Vegetarian": return 8;
  74.             default: return -1; // it never gets here anyway
  75.         }
  76.     }
  77. }
  78.  
  79. /* Exceptions */
  80. class ItemOutOfStockException extends Exception {
  81.     public ItemOutOfStockException(Item item) {
  82.         super(String.format("%s is out of stock", item));
  83.     }
  84. }
  85. class ArrayIndexOutOfBoundsException extends Exception {
  86.     public ArrayIndexOutOfBoundsException(int idx) {
  87.         super(String.format("%d is an invalid index", idx));
  88.     }
  89. }
  90. class EmptyOrder extends Exception {
  91.     public EmptyOrder() {
  92.         super(String.format("Empty order"));
  93.     }
  94. }
  95. class OrderLockedException extends Exception {
  96.     public OrderLockedException() {
  97.         super(String.format("Order locked"));
  98.     }
  99. }
  100.  
  101. class Order {
  102.     //private Item[] itemsArray;
  103.     private ArrayList<Item> items;
  104.     private boolean locked;
  105.     private ArrayList<Integer> counts;
  106.    
  107.     public Order(){
  108.         items = new ArrayList<Item>();
  109.         counts = new ArrayList<Integer>();
  110.         locked = false;
  111.     }
  112.     public void addItem(Item item, int count) throws ItemOutOfStockException,
  113.                                                     InvalidPizzaTypeException,
  114.                                                     InvalidExtraTypeException,
  115.                                                     OrderLockedException {
  116.         if(locked){
  117.             throw new OrderLockedException();
  118.         }
  119.         if(count > 10){
  120.             throw new ItemOutOfStockException(item);
  121.         }
  122.         /* Check if it has already been ordered */
  123.         for(int i = 0; i < items.size(); i++){
  124.             if(items.get(i).getType().equals(item.getType())){
  125.                 //counts.add(count);
  126.                 counts.set(i, count);
  127.                 return;
  128.             }
  129.         }
  130.        
  131.         /*itemsArray[0] = new PizzaItem(item.getType());
  132.         if (itemsArray[0].getType().equals(item.getType())) {
  133.            
  134.         }*/
  135.        
  136.         //System.out.println("Item Type: "+item.getType());
  137.        
  138.         /* Add the item */
  139.         String pizzaItems[] = new String[] {"Standard", "Pepperoni", "Vegetarian"};
  140.         String extraItems[] = new String[] {"Coke", "Ketchup"};
  141.         if (Arrays.stream(pizzaItems).anyMatch(pi -> pi.equals(item.getType()))) {
  142.             items.add(new PizzaItem(item.getType()));
  143.         } else if (Arrays.stream(extraItems).anyMatch(ei -> ei.equals(item.getType()))) {
  144.             items.add(new ExtraItem(item.getType()));
  145.         }
  146.         counts.add(count);
  147.     }
  148.     public int getPrice() {
  149.         int sum = 0;
  150.         int len = items.size();
  151.         for (int i = 0; i < len; i++) {
  152.             sum += counts.get(i) * items.get(i).getPrice();
  153.         }
  154.         return sum;
  155.         //return IntStream.range(0, items.size()).map(i -> counts.get(i) * items.get(i).getPrice()).sum();
  156.     }
  157.     public int linePrice(int index) {
  158.         int sum = 0;
  159.         int len = counts.get(index);
  160.         for (int i = 0; i < len; i++) {
  161.             sum += items.get(index).getPrice();
  162.         }
  163.         return sum;
  164.     }
  165.     public void displayOrder() {
  166.         //StringBuffer sb = new StringBuffer();
  167.         /*   1.Standard       x 2   20$ */
  168.         /* %3d . %s   %15d      x %2d %5d % */
  169.         IntStream.range(0, items.size()).
  170.             forEach(i -> System.out.format(
  171.                     "%3d.%-15sx%2d%5d$\n",
  172.                     i+1, items.get(i).getType(), counts.get(i), linePrice(i) ));
  173.        
  174.         //int len = items.size();
  175.         //for (int i = 0; i < len; i++) {
  176.             /*System.out.format("%3d.%sx%2d%5d$\n",
  177.                     i, items.get(i).getType(), counts.get(i), items.get(i).getPrice());
  178.             System.out.format("%d %s x %2d %5d $\n",
  179.                     i, items.get(i).getType(), counts.get(i), items.get(i).getPrice());*/
  180.             //System.out.println(i+" "+items.get(i).getType()+" "+counts.get(i)+" "+items.get(i).getPrice());
  181.         //}
  182.         System.out.format("Total:%21d$\n", this.getPrice());
  183.     }
  184.     public void removeItem(int idx) throws OrderLockedException, ArrayIndexOutOfBoundsException {
  185.         if (locked) { throw new OrderLockedException(); }
  186.         if (idx < 0 || idx > items.size()) { throw new ArrayIndexOutOfBoundsException(idx); }
  187.        
  188.         /* Find the item with index idx */
  189.         //IntStream.range(0, items.size()).anyMatch(i -> idx == items.get(i));
  190.        
  191.         /* Shifts to the left after element is removed */
  192.         items.remove(idx);
  193.     }
  194.    
  195.     public void lock() throws EmptyOrder {
  196.         int len = items.size();
  197.         if (len >= 1) {
  198.             locked = true;
  199.         } else { throw new EmptyOrder(); }
  200.     }
  201. }
  202.  
  203. public class PizzaOrderTest {
  204.  
  205.     public static void main(String[] args) {
  206.         Scanner jin = new Scanner(System.in);
  207.         int k = jin.nextInt();
  208.         if (k == 0) { // test Item
  209.             try {
  210.                 String type = jin.next();
  211.                 String name = jin.next();
  212.                 Item item = null;
  213.                 if (type.equals("Pizza"))
  214.                     item = new PizzaItem(name);
  215.                 else
  216.                     item = new ExtraItem(name);
  217.                 System.out.println(item.getPrice());
  218.             } catch (Exception e) {
  219.                 System.out.println(e.getClass().getSimpleName());
  220.             }
  221.         }
  222.         if (k == 1) { // test simple order
  223.             Order order = new Order();
  224.             while (true) {
  225.                 try {
  226.                     String type = jin.next();
  227.                     String name = jin.next();
  228.                     Item item = null;
  229.                     if (type.equals("Pizza"))
  230.                         item = new PizzaItem(name);
  231.                     else
  232.                         item = new ExtraItem(name);
  233.                     if (!jin.hasNextInt())
  234.                         break;
  235.                     order.addItem(item, jin.nextInt());
  236.                 } catch (Exception e) {
  237.                     System.out.println(e.getClass().getSimpleName());
  238.                 }
  239.             }
  240.             jin.next();
  241.             System.out.println(order.getPrice());
  242.             order.displayOrder();
  243.             while (true) {
  244.                 try {
  245.                     String type = jin.next();
  246.                     String name = jin.next();
  247.                     Item item = null;
  248.                     if (type.equals("Pizza"))
  249.                         item = new PizzaItem(name);
  250.                     else
  251.                         item = new ExtraItem(name);
  252.                     if (!jin.hasNextInt())
  253.                         break;
  254.                     order.addItem(item, jin.nextInt());
  255.                 } catch (Exception e) {
  256.                     System.out.println(e.getClass().getSimpleName());
  257.                 }
  258.             }
  259.             System.out.println(order.getPrice());
  260.             order.displayOrder();
  261.         }
  262.         if (k == 2) { // test order with removing
  263.             Order order = new Order();
  264.             while (true) {
  265.                 try {
  266.                     String type = jin.next();
  267.                     String name = jin.next();
  268.                     Item item = null;
  269.                     if (type.equals("Pizza"))
  270.                         item = new PizzaItem(name);
  271.                     else
  272.                         item = new ExtraItem(name);
  273.                     if (!jin.hasNextInt())
  274.                         break;
  275.                     order.addItem(item, jin.nextInt());
  276.                 } catch (Exception e) {
  277.                     System.out.println(e.getClass().getSimpleName());
  278.                 }
  279.             }
  280.             jin.next();
  281.             System.out.println(order.getPrice());
  282.             order.displayOrder();
  283.             while (jin.hasNextInt()) {
  284.                 try {
  285.                     int idx = jin.nextInt();
  286.                     order.removeItem(idx);
  287.                 } catch (Exception e) {
  288.                     System.out.println(e.getClass().getSimpleName());
  289.                 }
  290.             }
  291.             System.out.println(order.getPrice());
  292.             order.displayOrder();
  293.         }
  294.         if (k == 3) { // test locking & exceptions
  295.             Order order = new Order();
  296.             try {
  297.                 order.lock();
  298.             } catch (Exception e) {
  299.                 System.out.println(e.getClass().getSimpleName());
  300.             }
  301.             try {
  302.                 order.addItem(new ExtraItem("Coke"), 1);
  303.             } catch (Exception e) {
  304.                 System.out.println(e.getClass().getSimpleName());
  305.             }
  306.             try {
  307.                 order.lock();
  308.             } catch (Exception e) {
  309.                 System.out.println(e.getClass().getSimpleName());
  310.             }
  311.             try {
  312.                 order.removeItem(0);
  313.             } catch (Exception e) {
  314.                 System.out.println(e.getClass().getSimpleName());
  315.             }
  316.         }
  317.     }
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement