Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Objects;
- import java.util.Scanner;
- class InvalidExtraTypeException extends Exception {
- public InvalidExtraTypeException(String message) {
- super(message);
- }
- }
- class InvalidPizzaTypeException extends Exception {
- public InvalidPizzaTypeException(String message) {
- super(message);
- }
- }
- class ItemOutOfStock extends Exception {
- public ItemOutOfStock(String message) {
- super(String.format("%s out of stock", message));
- }
- }
- class EmptyOrder extends Exception {
- public EmptyOrder(String message) {
- super(message);
- }
- }
- class OrderLockedException extends Exception{
- public OrderLockedException(String message){
- super(message);
- }
- }
- interface Item {
- int getPrice();
- String getType();
- }
- class ExtraItem implements Item {
- String type;
- ExtraItem(String type) throws InvalidExtraTypeException {
- if (type.equals("Coke") || type.equals("Ketchup")) {
- this.type = type;
- } else throw new InvalidExtraTypeException("Invalid Type Entered as Extra Item");
- }
- @Override
- public int getPrice() {
- if (type.equals("Coke")) {
- return 5;
- } else return 3;
- }
- @Override
- public String getType() {
- return type;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- ExtraItem extraItem = (ExtraItem) o;
- return Objects.equals(type, extraItem.type);
- }
- @Override
- public int hashCode() {
- return Objects.hash(type);
- }
- }
- class PizzaItem implements Item {
- String type;
- PizzaItem(String type) throws InvalidPizzaTypeException {
- if (type.equals("Standard") || type.equals("Vegetarian") || type.equals("Pepperoni")) {
- this.type = type;
- } else throw new InvalidPizzaTypeException("Invalid Type Entered as Pizza Item");
- }
- @Override
- public int getPrice() {
- if (type.equals("Standard")) {
- return 10;
- }
- else if (type.equals("Pepperoni")) {
- return 12;
- } else return 8;
- }
- @Override
- public String getType() {
- return type;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- PizzaItem pizzaItem = (PizzaItem) o;
- return Objects.equals(type, pizzaItem.type);
- }
- @Override
- public int hashCode() {
- return Objects.hash(type);
- }
- }
- class Order {
- protected ArrayList<Item> items;
- protected ArrayList<Integer> count;
- boolean isLocked = false;
- Order() {
- this.items = new ArrayList<>();
- this.count = new ArrayList<>();
- }
- public void addItem(Item item, int count) throws ItemOutOfStock,OrderLockedException {
- if (!isLocked) {
- if (count > 10) throw new ItemOutOfStock(item.toString());
- if (items.contains(item)) {
- int index = items.indexOf(item);
- this.count.set(index, count);
- } else {
- this.items.add(item);
- this.count.add(count);
- }
- }
- else throw new OrderLockedException("Message");
- }
- public int getPrice() {
- int sum = 0;
- for (int i = 0; i < items.size(); i++) {
- sum += (items.get(i).getPrice() * count.get(i));
- }
- return sum;
- }
- ;
- public void removeItem(int index) throws OrderLockedException {
- if (!isLocked) {
- if (index >items.size()){throw new ArrayIndexOutOfBoundsException(index);}
- else {
- items.remove(index);
- count.remove(index);
- };
- }
- else throw new OrderLockedException("Message");
- }
- public void lock() throws EmptyOrder {
- if (items.size() != 0) {
- this.isLocked = true;
- } else throw new EmptyOrder("Array empty");
- }
- public void displayOrder() {
- StringBuilder sb = new StringBuilder();
- for (int i=0; i<items.size(); i++){
- 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))));
- }
- sb.append(String.format("%-22s%5d$","Total:",getPrice()));
- System.out.println(sb.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement