Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- interface Item {
- public int getPrice();
- public String getType();
- }
- /* Item Exceptions */
- class InvalidExtraTypeException extends Exception{}
- class InvalidPizzaTypeException extends Exception{}
- class ExtraItem implements Item {
- private String type;
- public ExtraItem(String type) throws InvalidExtraTypeException {
- if (!type.equals("Coke") && !type.equals("Ketchup")) {
- throw new InvalidExtraTypeException();
- }
- this.type = type;
- }
- public String getType() { return type; }
- public ExtraItem(ExtraItem i){
- type = i.type;
- }
- /*public ExtraItem(Item i){
- type = i.type;
- }*/
- public int getPrice() {
- if (type.equals("Coke")) {
- return 5;
- } else if (type.equals("Ketchup")) {
- return 3;
- } else { return -1; }
- }
- }
- class PizzaItem implements Item {
- private String type;
- public PizzaItem(String type) throws InvalidPizzaTypeException {
- /*if(!type.equals("Standard") && !type.equals("Pepperoni") && !type.equals("Vegetarian")){
- throw new InvalidPizzaTypeException();
- }*/
- String[] items = {"Standard", "Pepperoni", "Vegetarian"};
- if (Arrays.stream(items).noneMatch(i -> i.equals(type))){
- throw new InvalidPizzaTypeException();
- }
- this.type = type;
- }
- public String getType() { return type; }
- public PizzaItem(PizzaItem i){
- type = i.type;
- }
- /*public PizzaItem(Item i){
- type = i.type;
- }*/
- public int getPrice(){
- /*if(type.equals("Standard")){
- return 10;
- } else if(type.equals("Pepperoni")){
- return 12;
- } else if(type.equals("Vegetarian")){
- return 8;
- } else { return -1; }*/
- switch (type) {
- case "Standard": return 10;
- case "Pepperoni": return 12;
- case "Vegetarian": return 8;
- default: return -1; // it never gets here anyway
- }
- }
- }
- /* Exceptions */
- class ItemOutOfStockException extends Exception {
- public ItemOutOfStockException(Item item) {
- super(String.format("%s is out of stock", item));
- }
- }
- class ArrayIndexOutOfBoundsException extends Exception {
- public ArrayIndexOutOfBoundsException(int idx) {
- super(String.format("%d is an invalid index", idx));
- }
- }
- class EmptyOrder extends Exception {
- public EmptyOrder() {
- super(String.format("Empty order"));
- }
- }
- class OrderLockedException extends Exception {
- public OrderLockedException() {
- super(String.format("Order locked"));
- }
- }
- class Order {
- //private Item[] itemsArray;
- private ArrayList<Item> items;
- private boolean locked;
- private ArrayList<Integer> counts;
- public Order(){
- items = new ArrayList<Item>();
- counts = new ArrayList<Integer>();
- locked = false;
- }
- public void addItem(Item item, int count) throws ItemOutOfStockException,
- InvalidPizzaTypeException,
- InvalidExtraTypeException,
- OrderLockedException {
- if(locked){
- throw new OrderLockedException();
- }
- if(count > 10){
- throw new ItemOutOfStockException(item);
- }
- /* Check if it has already been ordered */
- for(int i = 0; i < items.size(); i++){
- if(items.get(i).getType().equals(item.getType())){
- //counts.add(count);
- counts.set(i, count);
- return;
- }
- }
- /*itemsArray[0] = new PizzaItem(item.getType());
- if (itemsArray[0].getType().equals(item.getType())) {
- }*/
- //System.out.println("Item Type: "+item.getType());
- /* Add the item */
- String pizzaItems[] = new String[] {"Standard", "Pepperoni", "Vegetarian"};
- String extraItems[] = new String[] {"Coke", "Ketchup"};
- if (Arrays.stream(pizzaItems).anyMatch(pi -> pi.equals(item.getType()))) {
- items.add(new PizzaItem(item.getType()));
- } else if (Arrays.stream(extraItems).anyMatch(ei -> ei.equals(item.getType()))) {
- items.add(new ExtraItem(item.getType()));
- }
- counts.add(count);
- }
- public int getPrice() {
- int sum = 0;
- int len = items.size();
- for (int i = 0; i < len; i++) {
- sum += counts.get(i) * items.get(i).getPrice();
- }
- return sum;
- //return IntStream.range(0, items.size()).map(i -> counts.get(i) * items.get(i).getPrice()).sum();
- }
- public int linePrice(int index) {
- int sum = 0;
- int len = counts.get(index);
- for (int i = 0; i < len; i++) {
- sum += items.get(index).getPrice();
- }
- return sum;
- }
- public void displayOrder() {
- //StringBuffer sb = new StringBuffer();
- /* 1.Standard x 2 20$ */
- /* %3d . %s %15d x %2d %5d % */
- IntStream.range(0, items.size()).
- forEach(i -> System.out.format(
- "%3d.%-15sx%2d%5d$\n",
- i+1, items.get(i).getType(), counts.get(i), linePrice(i) ));
- //int len = items.size();
- //for (int i = 0; i < len; i++) {
- /*System.out.format("%3d.%sx%2d%5d$\n",
- i, items.get(i).getType(), counts.get(i), items.get(i).getPrice());
- System.out.format("%d %s x %2d %5d $\n",
- i, items.get(i).getType(), counts.get(i), items.get(i).getPrice());*/
- //System.out.println(i+" "+items.get(i).getType()+" "+counts.get(i)+" "+items.get(i).getPrice());
- //}
- System.out.format("Total:%21d$\n", this.getPrice());
- }
- public void removeItem(int idx) throws OrderLockedException, ArrayIndexOutOfBoundsException {
- if (locked) { throw new OrderLockedException(); }
- if (idx < 0 || idx > items.size()) { throw new ArrayIndexOutOfBoundsException(idx); }
- /* Find the item with index idx */
- //IntStream.range(0, items.size()).anyMatch(i -> idx == items.get(i));
- /* Shifts to the left after element is removed */
- items.remove(idx);
- }
- public void lock() throws EmptyOrder {
- int len = items.size();
- if (len >= 1) {
- locked = true;
- } else { throw new EmptyOrder(); }
- }
- }
- 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.removeItem(0);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement