Reputation: 31
I'm doing an assignment in which I have to have a Stamp store with a GUI, where users need to be able to add and remove items from a shopping card, then print an order receipt to a file when the customer checks out. I'm having a lot of issues with the shopping cart, as I'm not sure if items are being added correctly, since I can not display them. Currently, the code for the cart is
ShoppingCart.java
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart
{
static // creates arraylist for cart
List<CartItem> items = new ArrayList<CartItem>();
public void AddItem(CartItem store)
{
items.add(store);
}
public static void main(String[] args)
{
System.out.println(items.get(0));
}
}
Item.java
import java.util.ArrayList;
import java.util.List;
public class Item
{
ShoppingCart cart;
public void CartSelection()
{
CartItem items = new CartItem("Parcel", 12, "Italy", true, 10.00);
cart.AddItem(items);
}
}
CartItem.java
import java.util.ArrayList;
import java.util.List;
// creates a class to store items in cart arraylist
public class CartItem
{
public CartItem(
String Type,
Integer Weight,
String Destination,
Boolean NovDec,
Double Price)
{
}
}
For a start, the code was giving me an error saying The method get(int) is undefined for the type CartItem
.
After searching for the cause of the problem, I altered the code now I get
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at ShoppingCart.main(ShoppingCart.java:16)
Any help or pointers in the right direction would be greatly appreciated.
Upvotes: 3
Views: 5674
Reputation: 1
This is a good way to take input of two items and then print it.
public class ItemToPurchase
{
public String itemName;
public int itemPrice;
public int itemQuantity;
public ItemToPurchase()
{
itemName="none";
itemPrice=0;
itemQuantity=0;
}
public void setName(String name)
{
itemName = name;
}
public String getName()
{
return itemName;
}
}
Continue with other variables/ data members
Then the main method
import java.util.Scanner;
public class ShoppingCartPrinter
{
public static void main(String[] args)
{
ItemToPurchase item1= new ItemToPurchase();
ItemToPurchase item2= new ItemToPurchase();
Scanner kbdInput = new Scanner(System.in);
System.out.println("Item 1");
System.out.println( "Enter the item name: ");
item1.setName(kbdInput.nextLine());
etc...
kbdInput.nextLine();
...
System.out.println ( "Enter the item quantity: ");
item2.setQuantity(kbdInput.nextInt());
int totalItem1 = item1.getPrice()* item1.getQuantity();
System.out.println ("\n"+"TOTAL COST");
System.out.println (item1.getName()+" "+item1.getQuantity()+" "+" @ $" +totalItem1);
etc...
System.out.println ("Total: $"+ (totalItem1 + totalItem2));
}
}
Upvotes: 0
Reputation: 393936
It looks like your main method calls System.out.println(items.get(0));
before any item was added to the list. That explains the java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
you get.
You must check that the index is valid before accessing the list :
public static void main(String[] args)
{
if (items.size() > 0)
System.out.println(items.get(0));
}
This would prevent the exception, but I'm not sure how much closer it would bring you to whatever you are trying to implement.
Upvotes: 2