Java 2个不同的项目添加到ArrayList,但最新的项目是两次?
我有一个产品的表视图,其中有描述、产品编号和价格,当用户在表中选择产品并单击按钮时,它应该被添加到名为order的ArrayList中,然后被添加到名为shoppingCart的购物车中。请参见下面的按钮方法: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.
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);
}