Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
- at ShoppingCart.add(ShoppingCart.java:25)
- at ShoppingCart.main(ShoppingCart.java:99)
- import java.util.Scanner; //Used to read user input from keyboard
- import java.text.DecimalFormat; //Used to format output of decimal values
- public class IceCreamOrder
- {
- //Private instance variable declarations
- private String flavor;
- private String vessel;
- private String amount;
- private double unitPrice;
- private int quantity;
- //Constructor declarations
- public IceCreamOrder(String flavor, String vessel, String amount, double unitPrice, int quantity)
- {
- this.flavor = flavor;
- this.vessel = vessel;
- this.amount = amount;
- this.unitPrice = unitPrice;
- this.quantity = quantity;
- }
- public IceCreamOrder(String flavor, String vessel, String amount, double unitPrice)
- {
- this.flavor = flavor;
- this.vessel = vessel;
- this.amount = amount;
- this.unitPrice = unitPrice;
- this.quantity = 1;
- }
- public IceCreamOrder()
- {
- this.flavor = "";
- this.vessel = "";
- this.amount = "";
- this.unitPrice = 0.0;
- this.quantity = 0;
- }
- //Calculates the total price of order
- public double price()
- {
- return quantity * unitPrice;
- }
- //Accessor method declarations
- public String getFlavor()
- {
- return flavor;
- }
- public String getVessel()
- {
- return vessel;
- }
- public String getAmount()
- {
- return amount;
- }
- public double getUnitPrice()
- {
- return unitPrice;
- }
- public int getQuantity()
- {
- return quantity;
- }
- //Mutator method declarations
- public void setFlavor(String flavor)
- {
- this.flavor = flavor;
- }
- public void setVessel(String vessel)
- {
- this.vessel = vessel;
- }
- public void setAmount(String amount)
- {
- this.amount = amount;
- }
- public void setUnitPrice(double unitPrice)
- {
- this.unitPrice = unitPrice;
- }
- public void setQuantity(int quantity)
- {
- this.quantity = quantity;
- }
- //toString method declaration
- public String toString()
- {
- DecimalFormat pattern0dot00 = new DecimalFormat("$0.00");
- return (((getQuantity() == 1) ? (getQuantity() + " order") : (getQuantity() + " orders")) + " of " +
- getAmount() + " of " + getFlavor() + " ice cream in a " + getVessel() + " for " +
- pattern0dot00.format(price()) + " = " + getQuantity() + " x " + getUnitPrice());
- }
- public static void main(String[] args)
- {
- //Object declarations
- IceCreamOrder newOrder = new IceCreamOrder();
- Scanner keyboard = new Scanner(System.in);
- //Array declarations
- String[] flavorList = {"Avocado", "Banana", "Chocolate", "Hazelnut", "Lemon", "Mango", "Mocha", "Vanilla"};
- String[] vesselList = {"Cone", "Cup", "Sundae"};
- String[] amountList = {"Single Scoop", "Double Scoop", "Triple Scoop"};
- String[] quantityList = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
- System.out.println("Placing an order is as easy as ABC, and D.");
- System.out.println("Step A: Select your favorite flavour");
- String outputFlavor = "";
- for (int i = 1; i <= flavorList.length; i++)
- {
- outputFlavor = " (" + i + ") " + flavorList[i-1];
- System.out.println(outputFlavor);
- }
- System.out.print("?-> Enter an option number: ");
- int inputFlavor = keyboard.nextInt();
- String flavorString = "";
- switch (inputFlavor)
- {
- case 1:
- flavorString = flavorList[0];
- break;
- case 2:
- flavorString = flavorList[1];
- break;
- case 3:
- flavorString = flavorList[2];
- break;
- case 4:
- flavorString = flavorList[3];
- break;
- case 5:
- flavorString = flavorList[4];
- break;
- case 6:
- flavorString = flavorList[5];
- break;
- case 7:
- flavorString = flavorList[6];
- break;
- case 8:
- flavorString = flavorList[7];
- break;
- }
- newOrder.setFlavor(flavorString);
- System.out.println();
- System.out.println("Step B: Select a vessel for your ice cream:");
- String outputVessel = "";
- for (int i = 1; i <= vesselList.length; i++)
- {
- outputVessel = " (" + i + ") " + vesselList[i-1];
- System.out.println(outputVessel);
- }
- System.out.print("?-> Enter an option number: ");
- int inputVessel = keyboard.nextInt();
- String vesselString = "";
- switch (inputVessel)
- {
- case 1:
- vesselString = vesselList[0];
- break;
- case 2:
- vesselString = vesselList[1];
- break;
- case 3:
- vesselString = vesselList[2];
- break;
- }
- newOrder.setVessel(vesselString);
- System.out.println();
- System.out.println("Step C: How much ice cream?");
- String outputAmount = "";
- for (int i = 1; i <= amountList.length; i++)
- {
- outputAmount = " (" + i + ") " + amountList[i-1];
- System.out.println(outputAmount);
- }
- System.out.print("?-> Enter an option number: ");
- int inputAmount = keyboard.nextInt();
- String amountString = "";
- switch (inputAmount)
- {
- case 1:
- amountString = amountList[0];
- break;
- case 2:
- amountString = amountList[1];
- break;
- case 3:
- amountString = amountList[2];
- break;
- }
- newOrder.setAmount(amountString);
- System.out.println();
- System.out.println("Step D: How many orders of your current selection?");
- String outputQuantity = "";
- for (int i = 1; i <= quantityList.length; i++)
- {
- outputQuantity = " (" + i + ") " + quantityList[i-1];
- System.out.println(outputQuantity);
- }
- System.out.print("?-> Enter how many orders: ");
- int inputQuantity = keyboard.nextInt();
- newOrder.setQuantity(inputQuantity);
- System.out.println();
- if (newOrder.getAmount() == amountList[0])
- {
- if (newOrder.getVessel() == vesselList[0])
- {
- newOrder.setUnitPrice(2.99);
- }
- else if (newOrder.getVessel() == vesselList[1])
- {
- newOrder.setUnitPrice(3.49);
- }
- else
- {
- newOrder.setUnitPrice(4.25);
- }
- }
- else if (newOrder.getAmount() == amountList[1])
- {
- if (newOrder.getVessel() == vesselList[0])
- {
- newOrder.setUnitPrice(3.99);
- }
- else if (newOrder.getVessel() == vesselList[1])
- {
- newOrder.setUnitPrice(4.49);
- }
- else
- {
- newOrder.setUnitPrice(5.25);
- }
- }
- else
- {
- if (newOrder.getVessel() == vesselList[0])
- {
- newOrder.setUnitPrice(4.99);
- }
- else if (newOrder.getVessel() == vesselList[1])
- {
- newOrder.setUnitPrice(5.49);
- }
- else
- {
- newOrder.setUnitPrice(6.25);
- }
- }
- System.out.println(newOrder);
- }
- public class ShoppingCart
- {
- private IceCreamOrder[] shoppingCart;
- private int maxQuantity;
- private int orderTracker;
- //Constructor declarations
- public ShoppingCart()
- {
- this.shoppingCart = new IceCreamOrder[maxQuantity];
- this.maxQuantity = 5;
- this.orderTracker = 1;
- }
- public void add(IceCreamOrder order)
- {
- if (orderTracker > maxQuantity)
- {
- System.out.println("Shopping cart is full.");
- }
- else
- {
- shoppingCart[orderTracker - 1] = order;
- orderTracker++;
- }
- }
- //Method determines if shopping cart is empty
- public boolean isEmpty()
- {
- int orderCount = 0;
- for (int i = 0; i < shoppingCart.length; i++)
- {
- if (shoppingCart[i] != null)
- {
- orderCount++;
- }
- }
- return ((orderCount == 0) ? true : false);
- }
- //Method determines if shopping cart is full
- public boolean isFull()
- {
- int orderCount = 0;
- for (int i = 0; i < shoppingCart.length; i++)
- {
- if (shoppingCart[i] != null)
- {
- orderCount++;
- }
- }
- return ((orderCount == maxQuantity) ? true : false);
- }
- public IceCreamOrder get(int position)
- {
- return shoppingCart[position-1];
- }
- //Method determines the number of orders currently in shopping cart
- public int size()
- {
- int orderCount = 0;
- for (int i = 0; i < shoppingCart.length; i++)
- {
- if (shoppingCart[i] != null)
- {
- orderCount++;
- }
- }
- return orderCount;
- }
- public static void main(String[] args)
- {
- ShoppingCart newCart = new ShoppingCart();
- IceCreamOrder firstOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- IceCreamOrder secondOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- IceCreamOrder thirdOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- IceCreamOrder fourthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- IceCreamOrder fifthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- //IceCreamOrder sixthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
- newCart.add(firstOrder);
- newCart.add(secondOrder);
- newCart.add(thirdOrder);
- newCart.add(fourthOrder);
- newCart.add(fifthOrder);
- //newCart.add(sixthOrder);*/
- System.out.println(newCart.size());
- System.out.println(firstOrder);
- System.out.println(newCart.isEmpty());
- System.out.println(newCart.isFull());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement