1

I am trying to add to an arraylist of arraylists. I have an arraylist called prod and 2d arraylist called shoppingBasket.

The main issue I have is that I want it to add several items to the basket (ie several prod lists to the list shoppingBasket) but instead of doing that it replaces the first item of the basket with the next (so there is only every one item in the basket). I am fairly new to java and am not sure how to correct this.

I have also tried making another class 'item' then create objects from here then add to the arraylist as ArrayList e.g

public class Item {
    private int bar;
    private String name;
    private String type;
    private String brand;
    private String colour;
    private String con;
    private int quantity;
    private float cost;
    private String addi;

    public Item (int bar, String name, String type, String brand, String colour, String con, int quantity,float cost, String addi) {
        this.bar = bar;
        this.name = name;
        this.type = type;
        this.brand = brand;
        this.colour = colour;
        this.con = con;
        this.quantity = quantity;
        this.cost = cost;
        this.addi = addi;
    }

  public class Basket {
    //arraylist for the shopping basket
    private ArrayList<Item> shoppingBasket;

    public Basket() {
        shoppingBasket = new ArrayList<>();
    }

    //function adding items to the basket
    public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {

         Item items = new Item (bar, name, type, brand, colour, con, quantity, cost, addi);
         shoppingBasket.add(items);
    }
  }

When I tried this method anad printed the arraylist to test, i would just show something like [Item@23455] and would still replace the item originally there instead of adding, plus i don't really understand how to do this properly so if anyone could explain that method to me (if it is easier to use than what i have already done) it would be greatly appreciated. Although I also would prefer if I didn't have to change my code too much.

The code in question: Function in one class that adds an item to the basket.

public class Basket {
    //arraylist for the shopping basket
    private ArrayList<ArrayList<String>> shoppingBasket;

    public Basket() {
        shoppingBasket = new ArrayList<>();
    }

    //function adding items to the basket
    public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {

          ArrayList<String> prod = new ArrayList<>();

          //adding one item to the basket shop.add(bar); shop.add(name);
          prod.add(Integer.toString(bar)); 
          prod.add(name); 
          prod.add(type); 
          prod.add(brand); 
          prod.add(colour); 
          prod.add(con);
          prod.add(Integer.toString(quantity));
          prod.add(Float.toString(cost)); 
          prod.add(addi);

          shoppingBasket.add(prod); 

        System.out.println(shoppingBasket);     

    }

n.b. the items are being added from a table using a listener in another table (each cell is a different variable):

selectionModel.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                int row = tableViewAll.getSelectedRow();

                viewAllModel=(DefaultTableModel) tableViewAll.getModel();

                int bar=Integer.parseInt(viewAllModel.getValueAt(row,0).toString());
                String name = viewAllModel.getValueAt(row,1).toString();
                String type = viewAllModel.getValueAt(row,2).toString();
                String brand = viewAllModel.getValueAt(row,3).toString();
                String colour= viewAllModel.getValueAt(row,4).toString();
                String con = viewAllModel.getValueAt(row,5).toString();
                int quantity=1;
                float cost=Float.parseFloat(viewAllModel.getValueAt(row,7).toString());
                String addi= viewAllModel.getValueAt(row,8).toString();

                Basket item = new Basket();
                item.addToBasket(bar, name, type, brand, colour, con, quantity, cost, addi);    

Thanks in advance :)

2 Answers 2

1

Problems with your design/code & solution:

  1. An Item shouldn't know how many numbers of it is present in the Basket. Its number should be passed to the basket while adding it. So, remove the attribute, quantity from Item.
  2. You need a Map instead of a List because when an existing item in the basket is added again, only its quantity should increase.
class Basket {
    private Map<String, Integer> basket = new HashMap<String, Integer>();

    public void addToBasket(Item item, int quantity) {
        if (item != null) {
            String key = item.getName();
            basket.put(key, basket.getOrDefault(key, 0) + quantity);
        }
    }

    public void showBasket() {
        for (Entry<String, Integer> entry : basket.entrySet()) {
            System.out.println("Item = " + entry.getKey() + ", Quantity = " + entry.getValue());
        }
    }
}
  1. The Basket is getting reset whenever valueChanged is invoked. Move the following code outside valueChanged and put it in the class instead of the method.
Basket item = new Basket();

It should be written like

Basket basket = new Basket();
selectionModel.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {

        int row = tableViewAll.getSelectedRow();

        viewAllModel=(DefaultTableModel) tableViewAll.getModel();

        int bar=Integer.parseInt(viewAllModel.getValueAt(row,0).toString());
        String name = viewAllModel.getValueAt(row,1).toString();
        String type = viewAllModel.getValueAt(row,2).toString();
        String brand = viewAllModel.getValueAt(row,3).toString();
        String colour= viewAllModel.getValueAt(row,4).toString();
        String con = viewAllModel.getValueAt(row,5).toString();
        float cost=Float.parseFloat(viewAllModel.getValueAt(row,7).toString());
        String addi= viewAllModel.getValueAt(row,8).toString();
        basket.addToBasket(new Item(bar, name, type, brand, colour, con, cost, addi), 1);    
        //...
    });

Once you will extend your UI to have an input field (textfield / dropdown) for quantity, you will get the quantity from the input field and pass the same to addToBasket instead of 1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much this was very detailed and helped a lot :D
0

You are creating a new Basket every time in your listener : Basket item = new Basket(); When you create a new instance everytime and you add an item there will be only one item in the "new" basket.

You need to create one instance (probably in your model) and use that instance to add the items.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.