gilolel7
gilolel7

Reputation: 3

Trying to add a array list to a cart object

I am trying to add stuff to a cart, so in my product class have made a cart like so:

private ArrayList<String> shoppingCart;

i made a basket adding function which also adds to this cart, like so:

public void addToBasket(String barcode, double price, int quantity) {
    List<String> temp = new ArrayList<String>(Arrays.asList());
    temp.add(barcode);
    temp.add(Double.toString(price));
    temp.add(Integer.toString(quantity));

    shoppingCart.add(temp);

however, the rest of the function works but I keep getting an error on this line:

shoppingCart.add(temp);

im not sure why this is happening.

Upvotes: 0

Views: 947

Answers (4)

Phill Alexakis
Phill Alexakis

Reputation: 1499

As correctly @Bashir pointed out, you are adding different types of variables to a specific type of the ArrayList<String> shoppingCart.

I'd like to explain a couple of things first, instead of copying-paste the solution they gave you , you have to understand the logic behind it.

The Object of this instance ArrayList<String> can only receive a String to it's add() method.

It doesn't let you add the price as well as the quantity because temp is an ArrayList<String> not a ArrayList<Integer> nor an ArrayList<Double>.

Your work around, is to convert it to a String and add it to the ArrayList<String> temp which is programmatically correct, but it's not Object Oriented Programming.

  • Why ArrayList<ArrayList<String>> shoppingCart; wont work for you:

Brief example:

Let's say you get the nested ArrayList<String> like this:

ArrayList<String> itemDetails = shoppingCart.get(0);
// which item belong to which shopping cart?

In order to make this work you have to count every 3 ArrayList Objects in order to get from a shoppingCart the contents.

Like so:

ArrayList<String> itemDetails = shoppingCart.get(0);
String details[] = new String[3];
details[0]=itemDetails.get(0);//barcode
details[1]=itemDetails.get(1);//price
details[2]=itemDetails.get(2);//quantity

Do you understand where i'm going with it?

The optimal way of doing it is as @Bashir pointed out.

Upvotes: 2

Clementine
Clementine

Reputation: 112

Your shoppingCart is a list of Strings, which means every element you want to put in should be a String.
Your temp is not a String, it's also an array of string, so it can't work. If your definition of temp is really what you want, then the definition of shoppingCart should be:

private ArrayList<ArrayList<String>> shoppingCart;

Upvotes: 0

Bashir
Bashir

Reputation: 2051

the problem occures because you are trying to add a List<String> to shoppingCart which is of type ArrayList<String>

To solve this I suggest to create a new class called Item for example, which will be the type of your temp instance

public class Item{
    private String barcode;
    private double price;
    private int quantity;

    public Item(String barcode, double price, int quantity){
        this.barcode = barcode;
        this.price = barcode;
        this.quantity = barcode;
    }

    //getters and setters
}

then, your shoppingCart will be an ArrayList of Items

private ArrayList<Item> shoppingCart;

and you have to change your method addToBasket as follow

public void addToBasket(String barcode, double price, int quantity) {
    Item temp = new Item(barcode,price,quantity);
    shoppingCart.add(temp);
}

and for the rest of your program, you have to make the needed changes, you have to remember that now your shoppingCart contains Objects of type Item

Upvotes: 2

Smutje
Smutje

Reputation: 18163

shoppingCart is of type ArrayList<String> but you are trying to add a List<String> to it. Either you need to change shoppingCart to a List<List<String>> or if you want to add all entries one after another to the list you have to rewrite shoppingCart.add(temp); to shoppingCart.addAll(temp);.

Upvotes: 1

Related Questions