简体   繁体   中英

2 Different items added to ArrayList however the latest item is made twice?

I have a TableView of products that have a description, product number and price, when the user selects the product in the table and clicks the button it should then be added to an ArrayList called order which is then added to a cart called shoppingCart. See button method below:

        public void addToCartButtonClicked(){
        //Set the variables of the cart.
        //Set customer to the cart.
        shoppingCart.setShopper(newCustomer);
        //Set Cart ID
        shoppingCart.setCartId(cartID);

        //Get selected product from the table, add to order then add to cart.
        ObservableList<Product> allProducts;
        allProducts = shoppingTable.getItems();
        order.setProduct(shoppingTable.getSelectionModel().getSelectedItem());
        shoppingCart.addOrder(order);
        System.out.println(order);  
    }

I have used a System.out to try and understand the problem with no luck as it was previously working. When I add product 'Apple' it will successfully add it to shoppingCart with its attributes, when I then add a 'Melon' it will also sucessfuly add this to the cart but will then replace the product 'Apple' with a 'Melon' and its attributes also with the 'Melon's'

Here is the output of the system print:

Cart:[contents=[Order:[item=Product:[productCode=01, description=Apple, unitPrice =99], quantity=1]]

When the second product is added:

Cart:[contents=[Order:[item=Product:[productCode=03, description=Melon, unitPrice =77], quantity=1], Order:[item=Product:[productCode=03, description=Melon, unitPrice =77], quantity=1]]

Code that might help:

Cart class:

//constructors
public Cart() {
    contents = new ArrayList<Order>();
    shopper = new Customer();
    deliveryDate = new Date();
    cartId = "Not set";
}
    public void addOrder(Order o) {
    contents.add(o);
}

Order class:

    //constructors
public Order() {
    item = new Product();
    quantity = 1;
}

Product class:

    public Product(String productCode, String description, int unitPrice) {
    this.productCode = productCode;
    this.description = description;
    this.unitPrice = unitPrice;
}

You created a single Order instance, and in each call to addToCartButtonClicked() you change the product of that Order (by calling order.setProduct(shoppingTable.getSelectionModel().getSelectedItem()) ) and add that same Order to your List .

Therefore your List contains multiple references to the same Order instance.

You should put

Order order = new Order();

inside your addToCartButtonClicked() method, in order to add a different Order instances to the List .

 public void addToCartButtonClicked(){
    //Set the variables of the cart.
    //Set customer to the cart.
    shoppingCart.setShopper(newCustomer);
    //Set Cart ID
    shoppingCart.setCartId(cartID);

    //Get selected product from the table, add to order then add to cart.
    ObservableList<Product> allProducts = shoppingTable.getItems();
    Order order = new Order();
    order.setProduct(shoppingTable.getSelectionModel().getSelectedItem());
    shoppingCart.addOrder(order);
    System.out.println(order);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM