I tried to debug it but I have no clue what code I’m loosing. Mind checking up for me? I want to add Item Names, price and quantity into ITEM[] array but when I try to print , it doesn’t return anything
item.java
import java.text.NumberFormat;
public class Item {
private String name;
private double price;
private int quantity;
public Item(String itemName, double itemPrice, int numPurchased) {
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
+ fmt.format(price * quantity));
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
}
ShoppingCart.java
import java.text.NumberFormat;
public class ShoppingCart {
private int itemCount; // total number of items in the cart
private double totalPrice; // total price of items in the cart
private int capacity; // current cart capacity
private Item[] cart = new Item[capacity];
// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
public ShoppingCart()
{
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
}
// -------------------------------------------------------
// Adds an item to the shopping cart.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{if(itemCount==cart.length)
increaseSize();
else{
cart[itemCount]=new Item(itemName, price, quantity);
totalPrice += (totalPrice * quantity);
itemCount++;
}
}
// -------------------------------------------------------
// Returns the contents of the cart together with
// summary information.
// -------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String contents = "\nShopping Cart\n";
contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
for (int i = 0; i < itemCount; i++)
contents += cart[i].toString() + "\n";
contents += "\nTotal Price: " + fmt.format(totalPrice);
contents += "\n";
return contents;
}
// ---------------------------------------------------------
// Increases the capacity of the shopping cart by 3
// ---------------------------------------------------------
private void increaseSize()
{Item[] temp = new Item[cart.length + 3];
for (int num = 0; num < cart.length; num++)
{
temp[num] = cart[num];
cart = temp;
}
}
}
Shopping1.java
import java.util.Scanner;
public class Shopping1 {
public static void main(String[] args)
{
String answer="y";
ShoppingCart cart = new ShoppingCart();
while(answer=="y")
{
System.out.println("Enter the Name, Price, and Quantity of Item:");
Scanner scan = new Scanner (System.in);
String name = scan.next();
double price = scan.nextDouble();
int quantity = scan.nextInt();
cart.addToCart(name, price, quantity);
String printout = cart.toString();
System.out.println(printout);
System.out.println("Do you want to continue? y/n");
Scanner ans = new Scanner (System.in);
answer = ans.next();
}
System.out.println("thank you !");
}
}
My output was:
Enter the Name, Price, and Quantity of Item:
yuvin
3
2
Shopping Cart
Item Unit Price Quantity Total
Total Price: MYR0.00
Do you want to continue? y/n
n
thank you !
While I was expecting some return of the array which I just inserted
the line
is executed prior to the constructor, therefore capacity will be zero at that time and you will get an array without space for elements …
//edit:
additionally there is a flaw in that addToCart method …
if (itemCount==cart.length) is true, the item you are about to add is discarded, because you only increase the array size … you should increase the array size and then add the item …