Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2个不同的项目添加到ArrayList,但最新的项目是两次?_Java_Arraylist_Javafx_Observablelist - Fatal编程技术网

Java 2个不同的项目添加到ArrayList,但最新的项目是两次?

Java 2个不同的项目添加到ArrayList,但最新的项目是两次?,java,arraylist,javafx,observablelist,Java,Arraylist,Javafx,Observablelist,我有一个产品的表视图,其中有描述、产品编号和价格,当用户在表中选择产品并单击按钮时,它应该被添加到名为order的ArrayList中,然后被添加到名为shoppingCart的购物车中。请参见下面的按钮方法: public void addToCartButtonClicked(){ //Set the variables of the cart. //Set customer to the cart. shoppingCart.

我有一个产品的表视图,其中有描述、产品编号和价格,当用户在表中选择产品并单击按钮时,它应该被添加到名为order的ArrayList中,然后被添加到名为shoppingCart的购物车中。请参见下面的按钮方法:

        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);  
    }
添加第二种产品时:

Cart:[contents=[Order:[item=Product:[productCode=03, description=Melon, unitPrice =77], quantity=1], Order:[item=Product:[productCode=03, description=Melon, unitPrice =77], quantity=1]]
可能有帮助的代码:

购物车类别:

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

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

您创建了一个
Order
实例,在每次调用
addToCartButtonClicked()
时,您可以更改该
订单的产品(通过调用
Order.setProduct(shoppingTable.getSelectionModel().getSelectedItem())
)并将相同的
订单添加到
列表中

 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);  
}
因此,您的
列表
包含对同一
订单
实例的多个引用

你应该把

Order order = new Order();
addToCartButtonClicked()
方法中,将不同的
顺序
实例添加到
列表

 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);  
}
public void addToCartButtonClicked(){
//设置购物车的变量。
//将客户设置为购物车。
shoppingCart.setShopper(新客户);
//设置购物车ID
shoppingCart.setCartId(cartID);
//从表中获取所选产品,添加到订单,然后添加到购物车。
ObservableList allProducts=shoppingTable.getItems();
订单=新订单();
setProduct(shoppingTable.getSelectionModel().getSelectedItem());
shoppingCart.addOrder(订单);
系统输出打印项次(订单);
}

@Eran Order是在公共类视图扩展应用程序下面的代码开头声明的,它是通过:Order Order=new Order()完成的;
 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);  
}