Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- class InvalidPizzaTypeException extends Exception {
- public InvalidPizzaTypeException() {
- super("InvalidPizzaTypeException");
- }
- }
- class InvalidExtraTypeException extends Exception {
- public InvalidExtraTypeException() {
- super("InvalidExtraTypeException");
- }
- }
- class ItemOutOfStockException extends Exception {
- Item item;
- public ItemOutOfStockException(Item item) {
- this.item = item;
- }
- }
- class ArrayIndexOutOfBоundsException extends Exception {
- int idx;
- public ArrayIndexOutOfBоundsException(int idx) {
- this.idx = idx;
- }
- }
- class Item {
- protected int cena;
- protected String type;
- public int getPrice() {
- return cena;
- }
- }
- class ExtraItem extends Item {
- ExtraItem(String type) throws InvalidExtraTypeException {
- if (type.equals("Coke") || type.equals("Ketchup")) {
- if (type.equals("Coke"))
- cena = 5;
- else
- cena = 3;
- this.type = type;
- } else
- throw new InvalidExtraTypeException();
- }
- }
- class PizzaItem extends Item {
- public PizzaItem(String type) throws Exception {
- if (type.equals("Standard") || type.equals("Pepperoni")
- || type.equals("Vegetarian")) {
- this.type = type;
- if (type.equals("Standard"))
- cena = 10;
- else if (type.equals("Pepperoni"))
- cena = 12;
- else if (type.equals("Vegetarian"))
- cena = 8;
- } else
- throw new InvalidPizzaTypeException();
- }
- }
- class Order {
- List<Item> Nitem;
- List<Integer> counts;
- public Order() {
- Nitem = new ArrayList<Item>();
- counts = new ArrayList<Integer>();
- }
- void addItem(Item item, int count) throws ItemOutOfStockException {
- if (count <= 10) {
- int index = 0;
- boolean flag = false;
- for (int i = 0; i < Nitem.size(); i++) {
- if (Nitem.get(i).type.equals(item.type) == true) {
- flag = true;
- index = i;
- }
- }
- if (flag == true) {
- Nitem.set(index, item);
- counts.set(index, count);
- } else {
- Nitem.add(item);
- counts.add(count);
- }
- } else
- throw new ItemOutOfStockException(item);
- }
- public int getPrice() {
- int sum = 0;
- for (int i = 0; i < Nitem.size(); i++) {
- if (Nitem.get(i).type.equals("Coke"))
- sum += Nitem.get(i).getPrice() * counts.get(i);
- else if (Nitem.get(i).type.equals("Pepperoni"))
- sum += Nitem.get(i).getPrice() * counts.get(i);
- else if (Nitem.get(i).type.equals("Vegetarian"))
- sum += Nitem.get(i).getPrice() * counts.get(i);
- else if (Nitem.get(i).type.equals("Standard"))
- sum += Nitem.get(i).getPrice() * counts.get(i);
- else if (Nitem.get(i).type.equals("Ketchup"))
- sum += Nitem.get(i).getPrice() * counts.get(i);
- }
- return sum;
- }
- public void displayOrder() {
- int x = 1;
- for (int i = 0; i < Nitem.size(); i++) {
- System.out.format("%d.%-15sx%-3d%d$\n", x, Nitem.get(i).type,
- counts.get(i), (Nitem.get(i).cena * counts.get(i)));
- x++;
- }
- String str="Total";
- System.out.format("%-20s%d$\n",str, getPrice());
- }
- public void removeItem(int idx) throws Exception {
- if (idx >= 0&&idx < Nitem.size()) {
- Nitem.remove(idx);
- counts.remove(idx);
- } else
- throw new ArrayIndexOutOfBоundsException(idx);
- }
- public void lock() {
- System.out.println("Treba da se implementira");
- }
- }
- 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
Advertisement