Trash in Java

I am doing an assignment where I have to have a GUI Stamp store where users should be able to add and remove items from the merchant card and then print the order receipt to a file when the customer checks out. I am having a lot of problems with my shopping cart as I am not sure if the items are being added correctly as I am unable to display them. Currently shopping cart code

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)
    {

    }
}

      

To begin with, the code gave me an error message The method get(int) is undefined for the type CartItem

.

After looking for the cause of the problem, I changed 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.

+3


source to share


2 answers


It looks like your main method is being called System.out.println(items.get(0));

before any item is added to the list. This explains java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

what you get.

You should 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));
}

      

It would have prevented an exception, but I'm not sure how much closer it will get you to what you are trying to implement.

+2


source


This is a good way to enter data from two elements and then to print. In the meantime, there is no need to worry about 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 working with other variables / items



Then the main method is

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));         
 }
}

      

0


source







All Articles