Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Exception;
- import java.util.ArrayList;
- class InvalidExtraTypeException extends Exception{
- public InvalidExtraTypeException(){
- super("InvalidExtraTypeException");
- }
- }
- class InvalidPizzaTypeException extends Exception {
- public InvalidPizzaTypeException(){
- super("InvalidPizzaTypeException");
- }
- }
- class ItemOutOfStockException extends Exception {
- private Item item;
- public ItemOutOfStockException(Item item){
- this.item = item;
- }
- }
- class ArrayIndexOutOfBоundsException extends Exception{
- public ArrayIndexOutOfBоundsException( int idx){
- super("InvalidExtraTypeException");
- }
- }
- class EmptyOrderException extends Exception {
- public EmptyOrderException(){
- super("EmptyOrderException");
- }
- }
- class OrderLockedException extends Exception {
- public OrderLockedException(){
- super("OrderLockedException");
- }
- }
- interface Item{
- public int getPrice();
- public String getType();
- public int getCount();
- public void setCount(int count);
- }
- class ExtraItem implements Item{
- private int count_extra;
- private String type;
- public ExtraItem(String type) throws InvalidExtraTypeException{
- if(type.equals("Coke") == false&&type.equals("Ketchup") == false){
- throw new InvalidExtraTypeException();
- }
- this.type = type;
- count_extra = 0;
- }
- public int getCount(){
- return count_extra;
- }
- public void setCount(int count){
- count_extra = count;
- }
- @Override
- public int getPrice() {
- if(type.equals("Ketchup"))
- return 3;
- if(type.equals("Coke"))
- return 5;
- else
- return 0;
- }
- @Override
- public String getType() {
- return type;
- }
- }
- class PizzaItem implements Item{
- private int count_pizza;
- private String type;
- public PizzaItem(String type) throws InvalidPizzaTypeException{
- if(!type.equals("Standard") && !type.equals("Pepperoni") && !type.equals("Veetarian")){
- throw new InvalidPizzaTypeException();
- }
- this.type = type;
- count_pizza = 0;
- }
- public int getCount(){
- return count_pizza;
- }
- public void setCount(int count){
- count_pizza = count;
- }
- @Override
- public int getPrice() {
- if(type.equals("Standard"))
- return 10;
- if(type.equals("Pepperoni"))
- return 12;
- if(type.equals("Vegetarian"))
- return 8;
- else
- return 0;
- }
- @Override
- public String getType() {
- return type;
- }
- }
- class Order{
- private ArrayList<Item> order;
- private int counter;
- private boolean lock;
- public Order(){
- order = null;
- counter = 0;
- lock=false;
- }
- public void addItem(Item item, int count) throws ItemOutOfStockException, OrderLockedException{
- if(lock==true)
- {
- throw new OrderLockedException();
- }
- else{
- if(count > 10)
- throw new ItemOutOfStockException(item);
- if(order.contains(item)){
- item.setCount(count);
- counter = count;
- }
- else if(!order.contains(item)){
- order.add(item);
- item.setCount(count);
- counter = count;
- }
- }
- }
- public int getPrice(){
- int total = 0;
- for(int i = 0; i < order.size(); i++){
- total += order.get(i).getPrice();
- }
- return total;
- }
- public void displayOrder(){
- for(int i = 0; i < order.size(); i++){
- System.out.printf("%3d", i+1);
- System.out.print(".");
- System.out.printf("%-15s", order.get(i).getType());
- System.out.print("x");
- System.out.printf("%2d", order.get(i).getCount());
- System.out.printf("%5d", order.get(i).getCount() * order.get(i).getPrice());
- System.out.print("$");
- System.out.println();
- }
- System.out.printf("%-22s", "Total:");
- System.out.printf("%5d", getPrice());
- System.out.print("$");
- System.out.println();
- }
- public void removeItem(int idx) throws ArrayIndexOutOfBоundsException, OrderLockedException{
- if(order.get(idx) == null)
- throw new ArrayIndexOutOfBоundsException(idx);
- if(lock==true)
- {
- throw new OrderLockedException();
- }
- else{
- order.remove(idx);
- }
- }
- public void lock() throws EmptyOrderException{
- if(order.get(0) == null)
- throw new EmptyOrderException();
- else
- lock = true;
- }
- }
- 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