Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- abstract class Item {
- private int price;
- public Item(){price=10;}
- public Item(int price)
- {
- this.price= price;
- }
- public int getPrice()
- {
- return price;
- }
- public void setPrice(int p)
- {
- this.price=p;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + price;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Item other = (Item) obj;
- if (price != other.price)
- return false;
- return true;
- }
- @Override
- abstract public String toString();
- }
- class ExtraItem extends Item {
- private String item;
- public ExtraItem(String item)throws InvalidExtraTypeException
- {
- if(item.equals("Coke"))
- {
- this.item=item;
- this.setPrice(5);
- }
- else if(item.equals("Ketchup"))
- {
- this.item=item;
- this.setPrice(3);
- }
- else throw new InvalidExtraTypeException();
- }
- public String getItem()
- {
- return item;
- }
- @Override
- public boolean equals(Object o)
- {
- ExtraItem e=(ExtraItem)o;
- if(this.getItem().equals(e.getItem()))
- return true;
- return false;
- }
- public String toString()
- {
- return item;
- }
- }
- class PizzaItem extends Item {
- private String pizzaItem;
- public PizzaItem(String o) throws InvalidPizzaTypeException
- {
- if(o.equals("Standard"))
- {
- pizzaItem=o;
- this.setPrice(10);
- }
- else if(o.equals("Pepperoni"))
- {
- pizzaItem=o;
- this.setPrice(12);
- }
- else if(o.equals("Vegetarian"))
- {
- pizzaItem=o;
- this.setPrice(8);
- }
- else throw new InvalidPizzaTypeException();
- }
- public boolean equals(Object o)
- {
- PizzaItem p=(PizzaItem)o;
- if(this.pizzaItem.equals(p.pizzaItem)&&this.getPrice()==p.getPrice())
- return true;
- return false;
- }
- public String toString()
- {
- return pizzaItem;
- }
- }
- class Order {
- private boolean isLocked;
- private OrderItem naracki[];
- public Order()
- {
- isLocked=false;
- }
- public void addItem(Item item, int count) throws ItemOutOfStockException,OrderLockedException
- {
- if(isLocked) throw new OrderLockedException();
- if(count >10) throw new ItemOutOfStockException();
- if(naracki==null)
- {
- naracki=new OrderItem[1];
- naracki[0]= new OrderItem(item,count);
- //System.out.println(naracki.length);
- }
- else
- {
- boolean flag=false;
- for(int i=0;i<naracki.length;i++)
- {
- //System.out.println(naracki.length);
- if(naracki[i].getNaracka().getPrice() == item.getPrice())
- {
- naracki[i].setKolicina(count);
- flag=true;
- }
- if(!flag)
- {
- OrderItem[] tmp = new OrderItem[naracki.length+1];
- for(int j=0; j<naracki.length; j++)
- tmp[j] = naracki[j];
- tmp[naracki.length] = new OrderItem(item, count);
- naracki = tmp;
- }
- }
- }
- }
- public int getPrice()
- {
- int suma=0;
- for(int i=0;i<naracki.length;i++)
- {
- suma+=naracki[i].getKolicina()*naracki[i].getNaracka().getPrice();
- //System.out.println(suma);
- }
- return suma;
- }
- public void displayOrder()
- {
- System.out.println(this.getPrice());
- for(int i=0;i<naracki.length;i++)
- {
- System.out.printf("%d.%15s x%d %d$ ", i+1,naracki[i].getNaracka().toString(),naracki[i].getKolicina(),naracki[i].getNaracka().getPrice()*naracki[i].getKolicina());
- System.out.println();
- }
- }
- public void removeItem(int idx) throws ArrayIndexOutOfBoundsException,OrderLockedException
- {
- if(idx<0 && idx>=naracki.length) throw new ArrayIndexOutOfBoundsException(idx);
- if(isLocked) throw new OrderLockedException();
- if(naracki!=null)
- {
- OrderItem[] pom=new OrderItem[naracki.length-1];
- for(int i=0;i<idx;i++)
- pom[i]=naracki[i];
- for(int i=idx+1;i<naracki.length;i++)
- pom[i-1]=naracki[i];
- }
- }
- public void lock() throws EmptyOrderException,NoPizzaException
- {
- if(naracki.length == 0) throw new EmptyOrderException("1");
- for(int i=0;i<naracki.length;i++)
- {
- if(naracki[i].getNaracka() instanceof PizzaItem)
- {
- isLocked=true;
- break;
- }
- }
- if(isLocked==false)
- {
- throw new NoPizzaException();
- }
- }
- }
- class OrderItem {
- private Item naracka;
- private int kolicina;
- public OrderItem()
- {
- naracka=null;
- kolicina=0;
- }
- public OrderItem(Item item, int kolicina)
- {
- naracka=item;
- this.kolicina=kolicina;
- }
- public Item getNaracka()
- {
- return naracka;
- }
- public int getKolicina()
- {
- return kolicina;
- }
- public void setKolicina(int kolicina)
- {
- this.kolicina=kolicina;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + kolicina;
- result = prime * result + ((naracka == null) ? 0 : naracka.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- OrderItem other = (OrderItem) obj;
- if (kolicina != other.kolicina)
- return false;
- if (naracka == null) {
- if (other.naracka != null)
- return false;
- } else if (!naracka.equals(other.naracka))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "OrderItem [naracka=" + naracka + ", kolicina=" + kolicina + "]";
- }
- }
- class InvalidExtraTypeException extends Exception {
- /**
- * neam poima so e ova 1L;
- */
- private static final long serialVersionUID = 1L;
- public InvalidExtraTypeException()
- {
- super("extraTypeException");
- }
- public InvalidExtraTypeException(String description)
- {
- super(description);
- }
- }
- class InvalidPizzaTypeException extends Exception {
- /**
- * i tuka isto taka neam poima so e 1L;
- */
- private static final long serialVersionUID = 1L;
- public InvalidPizzaTypeException(){
- super("pizzaTypeException");
- }
- public InvalidPizzaTypeException(String description)
- {
- super(description);
- }
- }
- class ItemOutOfStockException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public ItemOutOfStockException()
- {
- super("OutOfStuck");
- }
- public ItemOutOfStockException(String description)
- {
- super(description);
- }
- }
- class NoPizzaException extends Exception{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public NoPizzaException()
- {
- super("NoPizza");
- }
- public NoPizzaException(String description)
- {
- super(description);
- }
- }
- class OrderLockedException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public OrderLockedException()
- {
- super("OrderLocked");
- }
- public OrderLockedException(String description)
- {
- super(description);
- }
- }
- class EmptyOrderException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public EmptyOrderException()
- {
- super("EmptyOrder");
- }
- public EmptyOrderException(String description)
- {
- super(description);
- }
- }
- public class PizzaOrderTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) { //test Item
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if ( type.equals("Pizza") ) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- System.out.println(item.getPrice());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- if ( k == 1 ) { // test simple order
- Order order = new Order();
- while ( true ) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if ( type.equals("Pizza") ) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if ( !jin.hasNextInt() ) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- jin.next();
- System.out.println(order.getPrice());
- order.displayOrder();
- while ( true ) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if ( type.equals("Pizza") ) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if ( ! jin.hasNextInt() ) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- System.out.println(order.getPrice());
- order.displayOrder();
- }
- if ( k == 2 ) { // test order with removing
- Order order = new Order();
- while ( true ) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if ( type.equals("Pizza") ) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if ( !jin.hasNextInt() ) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- jin.next();
- System.out.println(order.getPrice());
- order.displayOrder();
- while ( jin.hasNextInt() ) {
- try {
- int idx = jin.nextInt();
- order.removeItem(idx);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- System.out.println(order.getPrice());
- order.displayOrder();
- }
- if ( k == 3 ) { //test locking & exceptions
- Order order = new Order();
- try {
- order.lock();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.addItem(new ExtraItem("Coke"), 1);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.lock();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.addItem(new PizzaItem("Standard"), 1);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.lock();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.addItem(new PizzaItem("Standard"), 1);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.removeItem(0);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment