Hey guys,
I'm working on arrays and I'm just not getting the hang of it. Every time my addToInvoice method is called and printed, it's not coming out correctly. Here's what the output is supposed to look like when you choose option 1 or 3.
This line serves customers with 5 or fewer items
------------------------------------------------
1. Buy an item
2. Modify the quantity of an item
3. Show the items purchased so far
4. Done
Your choice:
1
Enter the product name: Walnuts
Enter the price: 9.99
How many of Walnuts did you buy? Please enter the quantity: 2
1. Buy an item
2. Modify the quantity of an item
3. Show the items purchased so far
4. Done
Your choice:
3
Product: Walnuts Price: $9.99 Quantity: 2
Product: 1% Milk Price: $1.99 Quantity: 2
Running Total: $23.96
And here is what my output is giving me:
1
Enter the product name: milk
Enter the price: 2.99
How many of milk did you buy? Please enter the quantity: 2
1. Buy an item
2. Modify the quantity of an item
3. Show the items purchased so far
4. Done
Your choice:
1
Enter the product name: soup
Enter the price: 1.99
How many of soup did you buy? Please enter the quantity: 1
1. Buy an item
2. Modify the quantity of an item
3. Show the items purchased so far
4. Done
Your choice:
1
Enter the product name: sauce
Enter the price: 2.39
How many of sauce did you buy? Please enter the quantity: 1
1. Buy an item
2. Modify the quantity of an item
3. Show the items purchased so far
4. Done
Your choice:
3
null
null
Produt: Product: sauce Price: $2.39 Price: 0.0 Quantity: 1
null
null
Here is my product class:
public class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public String toString() { NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); return "Product: " + name + "\tPrice: " + n.format(price); } }
And my LineItem class:
public class LineItem { private Product p; private int quantity; private double amountOwed; private int q; private double price; public LineItem(Product p, int quantity) { this.p=p; this.quantity=quantity; } public double getAmount() { return quantity*price; } public void setQuantity(int q) { this.q=q; } public int getQuantity() { return quantity; } public Product getProduct() { return p; } public java.lang.String toString() { return "Produt: " + p + "\tPrice: " + price + "\tQuantity: " + quantity + " "; } }
And my invoice method:
public class Invoice { private LineItem[] collection; private int numItems; private double runningTotal=0; private double price; private int quantity; public Invoice() { numItems=0; } public void addToInvoice(LineItem item) { collection=new LineItem[5]; collection[numItems]=item; numItems++; } public int getNumItems() { return numItems; } public double getRunningTotal() { return runningTotal; } public void print() { for (int i=0; i<5; i++) { System.out.println(collection[i] + "\n"); runningTotal = runningTotal + price*quantity; } getRunningTotal(); } public void update(java.lang.String name, int quantity) { } }
AND FINALLY my main method:
public class Cashier { public static void main(String args[]) { final int MAXITEMS = 5; Scanner keyboard = new Scanner(System.in); NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); System.out.println("Welcome to CS 140 Store Express Line"); System.out.println("This line serves customers with " + MAXITEMS + " or fewer items"); System.out.println("------------------------------------------------\n"); Invoice invoice = new Invoice(); do { System.out.println("\t1. Buy an item"); System.out.println("\t2. Modify the quantity of an item"); System.out.println("\t3. Show the items purchased so far"); System.out.println("\t4. Done"); System.out.println("\nYour choice: "); int choice = Integer.parseInt(keyboard.nextLine()); switch(choice) { case 1: if (invoice.getNumItems() < MAXITEMS) { System.out.print("Enter the product name: "); String name = keyboard.nextLine(); System.out.print("Enter the price: "); double price = Double.parseDouble(keyboard.nextLine() ); System.out.print("How many of " + name + " did you buy? Please enter the quantity: "); int quantity = Integer.parseInt(keyboard.nextLine() ); LineItem item = new LineItem(new Product(name, price), quantity); invoice.addToInvoice(item); } else System.out.println("This line is customers for <= " + MAXITEMS + " items only"); break; case 2: System.out.println("Enter the name of the product"); String name = keyboard.nextLine(); System.out.println("Enter the new quantity: "); int quantity = Integer.parseInt(keyboard.nextLine() ); invoice.update(name, quantity); break; case 3: invoice.print(); break; case 4: System.out.println("Thanks for using CS 140 store"); System.out.println("Here is your invoice information"); System.out.println("--------------------------------"); invoice.print(); System.out.println("\nPlease come back again. Have a nice day!"); System.exit(0); default: System.out.println("Wrong choice. Choose a valid option"); } // end of switch } while (true); // end of do - while loop } // end of main method } // end of Cashier class
can anyone tell me what I'm doing wrong?