DamSi

Untitled

Jun 2nd, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. abstract class Item {
  4.     private int price;
  5.     public Item(){price=10;}
  6.     public Item(int price)
  7.     {
  8.         this.price= price;
  9.     }
  10.    
  11.     public int getPrice()
  12.     {  
  13.         return price;
  14.     }
  15.     public void setPrice(int p)
  16.     {
  17.         this.price=p;
  18.     }
  19.    
  20.     @Override
  21.     public int hashCode() {
  22.         final int prime = 31;
  23.         int result = 1;
  24.         result = prime * result + price;
  25.         return result;
  26.     }
  27.     @Override
  28.     public boolean equals(Object obj) {
  29.         if (this == obj)
  30.             return true;
  31.         if (obj == null)
  32.             return false;
  33.         if (getClass() != obj.getClass())
  34.             return false;
  35.         Item other = (Item) obj;
  36.         if (price != other.price)
  37.             return false;
  38.         return true;
  39.     }
  40.     @Override
  41.     abstract public String toString();
  42. }
  43.  
  44. class ExtraItem extends Item {
  45.     private String item;
  46.     public ExtraItem(String item)throws InvalidExtraTypeException
  47.     {
  48.         if(item.equals("Coke"))
  49.         {
  50.             this.item=item;
  51.             this.setPrice(5);
  52.         }
  53.         else if(item.equals("Ketchup"))
  54.         {
  55.             this.item=item;
  56.             this.setPrice(3);
  57.         }
  58.         else throw new InvalidExtraTypeException();
  59.     }
  60.     public String getItem()
  61.     {
  62.         return item;
  63.     }
  64.     @Override
  65.     public boolean equals(Object o)
  66.     {
  67.         ExtraItem e=(ExtraItem)o;
  68.         if(this.getItem().equals(e.getItem()))
  69.             return true;
  70.         return false;
  71.     }
  72.     public String toString()
  73.     {
  74.         return item;
  75.     }
  76. }
  77.  
  78. class PizzaItem extends Item {
  79.     private String pizzaItem;
  80.     public PizzaItem(String o) throws InvalidPizzaTypeException
  81.     {
  82.         if(o.equals("Standard"))
  83.         {
  84.             pizzaItem=o;
  85.             this.setPrice(10);
  86.         }
  87.         else if(o.equals("Pepperoni"))
  88.         {
  89.             pizzaItem=o;
  90.             this.setPrice(12);
  91.         }
  92.         else if(o.equals("Vegetarian"))
  93.         {
  94.             pizzaItem=o;
  95.             this.setPrice(8);
  96.         }
  97.         else throw new InvalidPizzaTypeException();
  98.     }
  99.     public boolean equals(Object o)
  100.     {
  101.         PizzaItem p=(PizzaItem)o;
  102.         if(this.pizzaItem.equals(p.pizzaItem)&&this.getPrice()==p.getPrice())
  103.         return true;
  104.         return false;
  105.     }
  106.     public String toString()
  107.     {
  108.         return pizzaItem;
  109.     }
  110. }
  111.  class Order {
  112.     private boolean isLocked;
  113.     private OrderItem naracki[];
  114.    
  115.     public Order()
  116.     {
  117.         isLocked=false;
  118.     }
  119.     public void addItem(Item item, int count) throws ItemOutOfStockException,OrderLockedException
  120.     {
  121.         if(isLocked) throw  new OrderLockedException();
  122.         if(count >10) throw  new ItemOutOfStockException();
  123.         if(naracki==null)
  124.         {
  125.             naracki=new OrderItem[1];
  126.             naracki[0]= new OrderItem(item,count);
  127.             //System.out.println(naracki.length);
  128.         }
  129.         else
  130.         {
  131.             boolean flag=false;
  132.             for(int i=0;i<naracki.length;i++)
  133.             {
  134.                 //System.out.println(naracki.length);
  135.                 if(naracki[i].getNaracka().getPrice() == item.getPrice())
  136.                 {
  137.                     naracki[i].setKolicina(count);
  138.                     flag=true;
  139.                 }
  140.             if(!flag)
  141.             {
  142.                 OrderItem[] tmp = new OrderItem[naracki.length+1];
  143.                 for(int j=0; j<naracki.length; j++)
  144.                     tmp[j] = naracki[j];
  145.                 tmp[naracki.length] = new OrderItem(item, count);
  146.                 naracki = tmp;
  147.             }
  148.         }
  149.         }
  150.        
  151.     }
  152.     public int getPrice()
  153.     {
  154.         int suma=0;
  155.         for(int i=0;i<naracki.length;i++)
  156.         {
  157.             suma+=naracki[i].getKolicina()*naracki[i].getNaracka().getPrice();
  158.             //System.out.println(suma);
  159.         }
  160.         return suma;
  161.     }
  162.     public void displayOrder()
  163.     {
  164.         System.out.println(this.getPrice());
  165.         for(int i=0;i<naracki.length;i++)
  166.         {
  167.             System.out.printf("%d.%15s x%d   %d$ ", i+1,naracki[i].getNaracka().toString(),naracki[i].getKolicina(),naracki[i].getNaracka().getPrice()*naracki[i].getKolicina());
  168.             System.out.println();
  169.         }
  170.        
  171.     }
  172.     public void removeItem(int idx) throws ArrayIndexOutOfBoundsException,OrderLockedException
  173.     {
  174.         if(idx<0 && idx>=naracki.length) throw new ArrayIndexOutOfBoundsException(idx);
  175.         if(isLocked) throw new OrderLockedException();
  176.         if(naracki!=null)
  177.         {
  178.             OrderItem[] pom=new OrderItem[naracki.length-1];
  179.             for(int i=0;i<idx;i++)
  180.                 pom[i]=naracki[i];
  181.             for(int i=idx+1;i<naracki.length;i++)
  182.                 pom[i-1]=naracki[i];
  183.         }
  184.     }
  185.     public void lock() throws EmptyOrderException,NoPizzaException
  186.     {
  187.         if(naracki.length == 0) throw new EmptyOrderException("1");
  188.         for(int i=0;i<naracki.length;i++)
  189.         {
  190.             if(naracki[i].getNaracka() instanceof PizzaItem)
  191.             {
  192.                 isLocked=true;
  193.                 break;
  194.             }
  195.         }
  196.         if(isLocked==false)
  197.         {
  198.             throw new NoPizzaException();
  199.         }
  200.        
  201.     }
  202.    
  203. }
  204.  class OrderItem {
  205.     private Item naracka;
  206.     private int kolicina;
  207.     public OrderItem()
  208.     {
  209.         naracka=null;
  210.         kolicina=0;
  211.     }
  212.     public OrderItem(Item item, int kolicina)
  213.     {
  214.         naracka=item;
  215.         this.kolicina=kolicina;
  216.     }
  217.     public Item getNaracka()
  218.     {
  219.         return naracka;
  220.     }
  221.     public int getKolicina()
  222.     {
  223.         return kolicina;
  224.     }
  225.     public void setKolicina(int kolicina)
  226.     {
  227.         this.kolicina=kolicina;
  228.     }
  229.     @Override
  230.     public int hashCode() {
  231.         final int prime = 31;
  232.         int result = 1;
  233.         result = prime * result + kolicina;
  234.         result = prime * result + ((naracka == null) ? 0 : naracka.hashCode());
  235.         return result;
  236.     }
  237.     @Override
  238.     public boolean equals(Object obj) {
  239.         if (this == obj)
  240.             return true;
  241.         if (obj == null)
  242.             return false;
  243.         if (getClass() != obj.getClass())
  244.             return false;
  245.         OrderItem other = (OrderItem) obj;
  246.         if (kolicina != other.kolicina)
  247.             return false;
  248.         if (naracka == null) {
  249.             if (other.naracka != null)
  250.                 return false;
  251.         } else if (!naracka.equals(other.naracka))
  252.             return false;
  253.         return true;
  254.     }
  255.     @Override
  256.     public String toString() {
  257.         return "OrderItem [naracka=" + naracka + ", kolicina=" + kolicina + "]";
  258.     }
  259.  
  260.    
  261. }
  262.  
  263.  class InvalidExtraTypeException extends Exception {
  264.     /**
  265.      * neam poima so e ova 1L;
  266.      */
  267.     private static final long serialVersionUID = 1L;
  268.     public InvalidExtraTypeException()
  269.     {
  270.         super("extraTypeException");
  271.     }
  272.     public InvalidExtraTypeException(String description)
  273.     {
  274.         super(description);
  275.     }
  276. }
  277. class InvalidPizzaTypeException extends Exception {
  278.  
  279.     /**
  280.      * i tuka isto taka neam poima so e 1L;
  281.      */
  282.     private static final long serialVersionUID = 1L;
  283.     public InvalidPizzaTypeException(){
  284.         super("pizzaTypeException");
  285.     }
  286.     public InvalidPizzaTypeException(String description)
  287.     {
  288.         super(description);
  289.     }
  290.    
  291. }
  292. class ItemOutOfStockException extends Exception {
  293.  
  294.     /**
  295.      *
  296.      */
  297.     private static final long serialVersionUID = 1L;
  298.     public ItemOutOfStockException()
  299.     {
  300.         super("OutOfStuck");
  301.     }
  302.     public ItemOutOfStockException(String description)
  303.     {
  304.         super(description);
  305.     }
  306. }
  307. class NoPizzaException extends Exception{
  308.  
  309.     /**
  310.      *
  311.      */
  312.     private static final long serialVersionUID = 1L;
  313.     public NoPizzaException()
  314.     {
  315.         super("NoPizza");
  316.     }
  317.     public NoPizzaException(String description)
  318.     {
  319.         super(description);
  320.     }
  321.  
  322.    
  323. }
  324.  
  325.  class OrderLockedException extends Exception {
  326.  
  327.     /**
  328.      *
  329.      */
  330.     private static final long serialVersionUID = 1L;
  331.     public OrderLockedException()
  332.     {
  333.         super("OrderLocked");
  334.     }
  335.     public OrderLockedException(String description)
  336.     {
  337.         super(description);
  338.     }
  339.  
  340.    
  341. }
  342. class EmptyOrderException extends Exception {
  343.  
  344.     /**
  345.      *
  346.      */
  347.     private static final long serialVersionUID = 1L;
  348.     public EmptyOrderException()
  349.     {
  350.         super("EmptyOrder");
  351.     }
  352.     public EmptyOrderException(String description)
  353.     {
  354.         super(description);
  355.     }
  356.  
  357.    
  358. }
  359. public class PizzaOrderTest {
  360.    
  361.     public static void main(String[] args) {
  362.         Scanner jin = new Scanner(System.in);
  363.         int k = jin.nextInt();
  364.         if ( k == 0 ) { //test Item
  365.             try {
  366.             String type = jin.next();
  367.             String name = jin.next();
  368.             Item item = null;
  369.             if ( type.equals("Pizza") ) item = new PizzaItem(name);
  370.             else item = new ExtraItem(name);
  371.             System.out.println(item.getPrice());
  372.             } catch (Exception e) {
  373.                 System.out.println(e.getClass().getSimpleName());
  374.             }
  375.         }
  376.         if ( k == 1 ) { // test simple order
  377.             Order order = new Order();
  378.             while ( true ) {       
  379.                 try {  
  380.                 String type = jin.next();
  381.                 String name = jin.next();
  382.                 Item item = null;
  383.                 if ( type.equals("Pizza") ) item = new PizzaItem(name);
  384.                 else item = new ExtraItem(name);
  385.                 if ( !jin.hasNextInt() ) break;
  386.                 order.addItem(item, jin.nextInt());
  387.                 } catch (Exception e) {
  388.                     System.out.println(e.getClass().getSimpleName());
  389.                 }
  390.             }
  391.             jin.next();
  392.             System.out.println(order.getPrice());
  393.             order.displayOrder();
  394.             while ( true ) {
  395.                 try {  
  396.                 String type = jin.next();
  397.                 String name = jin.next();
  398.                 Item item = null;
  399.                 if ( type.equals("Pizza") ) item = new PizzaItem(name);
  400.                 else item = new ExtraItem(name);
  401.                 if ( ! jin.hasNextInt() ) break;
  402.                 order.addItem(item, jin.nextInt());
  403.                 } catch (Exception e) {
  404.                     System.out.println(e.getClass().getSimpleName());
  405.                 }
  406.             }
  407.             System.out.println(order.getPrice());
  408.             order.displayOrder();
  409.         }
  410.         if ( k == 2 ) { // test order with removing
  411.             Order order = new Order();
  412.             while ( true ) {
  413.                        
  414.                 try {  
  415.                 String type = jin.next();
  416.                 String name = jin.next();
  417.                 Item item = null;
  418.                 if ( type.equals("Pizza") ) item = new PizzaItem(name);
  419.                 else item = new ExtraItem(name);
  420.                 if ( !jin.hasNextInt() ) break;
  421.                 order.addItem(item, jin.nextInt());
  422.                 } catch (Exception e) {
  423.                     System.out.println(e.getClass().getSimpleName());
  424.                 }
  425.             }
  426.             jin.next();
  427.             System.out.println(order.getPrice());
  428.             order.displayOrder();
  429.             while ( jin.hasNextInt() ) {
  430.                 try {  
  431.                 int idx = jin.nextInt();
  432.                 order.removeItem(idx);
  433.                 } catch (Exception e) {
  434.                     System.out.println(e.getClass().getSimpleName());
  435.                 }
  436.             }
  437.             System.out.println(order.getPrice());
  438.             order.displayOrder();
  439.         }
  440.         if ( k == 3 ) { //test locking & exceptions
  441.             Order order = new Order();
  442.             try {
  443.                 order.lock();
  444.             } catch (Exception e) {
  445.                 System.out.println(e.getClass().getSimpleName());
  446.             }
  447.             try {
  448.                 order.addItem(new ExtraItem("Coke"), 1);
  449.             } catch (Exception e) {
  450.                 System.out.println(e.getClass().getSimpleName());
  451.             }
  452.             try {
  453.                 order.lock();
  454.             } catch (Exception e) {
  455.                 System.out.println(e.getClass().getSimpleName());
  456.             }
  457.             try {
  458.                 order.addItem(new PizzaItem("Standard"), 1);
  459.             } catch (Exception e) {
  460.                 System.out.println(e.getClass().getSimpleName());
  461.             }
  462.             try {
  463.                 order.lock();
  464.             } catch (Exception e) {
  465.                 System.out.println(e.getClass().getSimpleName());
  466.             }
  467.             try {
  468.                 order.addItem(new PizzaItem("Standard"), 1);
  469.             } catch (Exception e) {
  470.                 System.out.println(e.getClass().getSimpleName());
  471.             }
  472.             try {
  473.                 order.removeItem(0);
  474.             } catch (Exception e) {
  475.                 System.out.println(e.getClass().getSimpleName());
  476.             }
  477.         }
  478.     }
  479.  
  480. }
Advertisement
Add Comment
Please, Sign In to add comment