java在不同的时间多次向TableView添加相同的对象
我正试图建立一个购物项目。该程序可以将所有添加的产品保存在SQLite数据库中,不会出现任何问题
有一些限制
产品可以是主产品或标准产品。主要产品有自己的子产品。如果客户想要购买主产品,那么客户也必须购买其子产品
直到现在一切似乎都很好。在主屏幕中,我在顶部构建了2个tableview,用户可以看到所有产品的列表(tableProductsInfo),在桌子下面,可以看到购物车桌子(tableShoppingBasket)
如果用户从tableProductsInfo中选择一个产品,并点击添加到购物车按钮,该产品将被添加到下面和购物篮(observableList)上,如图所示
这里是我的“添加到购物车”按钮代码
@FXML
public void addtoShoppingBasket() throws IOException {
if(tableProductInfo.getSelectionModel().getSelectedItem()!=null){ // eğer bir ürün seçiliyse // if any product selected?
Product selectedItem = (Product) tableProductInfo.getSelectionModel().getSelectedItem(); //
int inputQuantity = getInputDialogPane(); // Kaç tane ürün eklenmek istiyor? // how many products user wants to buy?
if(selectedItem.getStock()>=inputQuantity){ // STOK KONTROL eğer eklenmek istenen ürün stokta varsa // Checking stock if its avaliable for selling
selectedItem.setQuantity(inputQuantity);
//main ürün şuan alınabilir durumda (sub ürünü varsa henüz değil)
if (selectedItem.getSubProduct()==1){ // eğer sub ürünü varsa // if selected product has subProducts
//that means its main product and we need to check its sub products
//1)sub ürün listesini al, // get all subProducts of selectedItem
//2) sub ürün stok kontrollerini yap main.Quantity*sub.Quantity <= Sub.stock // check its stock
//3) eğer stokta varsa tabloya ekle yoksa uyarı ver // if any of subitems out of stock capasity then don't let user to buy the main product and its subProducts
ProductDialogPaneController productDialogPaneController = new ProductDialogPaneController();
ObservableList returnedList =FXCollections.observableArrayList();
returnedList=productDialogPaneController.getSubProducts(selectedItem.getProductID()); // getting all subproducts of main Product
Iterator<Product> productIterator = returnedList.iterator();
boolean inStock=true;
while (productIterator.hasNext()){
Product currentSubProduct = productIterator.next();
if((currentSubProduct.getQuantity()*selectedItem.getQuantity())>currentSubProduct.getStock()){
//eğer eklenmek istenen alt ürünün tanımlanan Miktarı*Alınmak istenen Main ürünün miktarı stok kapasitesinden fazla değilse
//sub products should multiply with main product quantity
inStock=false;
}
}
if(inStock==true){
System.out.println("in stock true");
// there is no stock problem for any sub product
//here we need to check if shoppingBasket contains selected item or its sub products
//if it contains then update/change its quantity
//else then add selected item and its subProducts into shoppingBasket
shoppingBasket.add(selectedItem);
shoppingBasket.addAll(returnedList);
}else{
System.out.println("ALMAK İSTEDİĞİNİZ MAİN ÜRÜNLE BİRLİKTE ALINMASI GEREKEN ALT ÜRÜNLERDEN BİRİ VEYA BİRKAÇI STOKTA YOK!");
}
}else{ //eğer sub ürünü yoksa (selectedItem.getSubProduct==0)
//if it's not the main product in another words if its standard Product
shoppingBasket.add(selectedItem);
}
}else{
System.out.println("ALMAK İSTEDİĞİNİZ MAİN ÜRÜN STOKTA YETERİ KADAR YOK");
//the product you wanted to buy is out of stock capacity
}
}else{ // eğer ürün listesinden bir ürün seçilmediyse
System.out.println("ALIŞ VERİŞ SEPETİNE EKLENECEK ÜRÜNÜ SEÇMEDİNİZ !");
//no item selected to buy
}
}
我像这样把我的商品放进购物桌
@FXML
public void initialize(){
tableShoppingBasket.setItems(shoppingBasket);
}
我的问题
当菜篮干净时
如果我想第一次添加主产品! 似乎没有问题。 您可以在此处看到主屏幕:
但是当我想第二次添加相同的产品时 有个问题,我的桌子好像是这样的
我不想在不同的行中列出相同的产品
如果之前添加了此产品,我想更新其数量。(oldProduct.getQuantity+newProduct.getQuantity)
我需要修正这个观点:
我想更新它的数量,并看到这样的表格行
标签名称价格数量
- 主产品200 2
- 子产品X 10 2
- 次级产品50 2
我试图用这些代码来解决我的问题,但它并没有按照我想要的那样工作。 我的意思是,它没有添加选定主产品的子产品
//checking for MAIN PRODUCT
if(shoppingBasket.contains(selectedItem)){
int index= shoppingBasket.indexOf(selectedItem);
Product existingItem=shoppingBasket.get(index);
shoppingBasket.remove(selectedItem); //removing old item
existingItem.setQuantity(existingItem.getQuantity()+inputQuantity);
shoppingBasket.add(index,existingItem); // adding updated item
}else{
shoppingBasket.add(selectedItem); // if if its adding first time
}
//Checking for Sub Products
Iterator subItemIterator=returnedList.iterator();
while (subItemIterator.hasNext()){
Product newSubItem = (Product) subItemIterator.next();
Iterator ShoppingIterator = shoppingBasket.iterator();
if(shoppingBasket!=null && !shoppingBasket.isEmpty()){
while (ShoppingIterator.hasNext()){
Product oldSubItem= (Product) ShoppingIterator.next();
if(oldSubItem.getProductID()==newSubItem.getProductID()){
oldSubItem.setQuantity(newSubItem.getQuantity()+oldSubItem.getQuantity());
}else{
shoppingBasket.add(newSubItem);
}
}
} else {shoppingBasket.add(newSubItem);
}
}
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException Caused by: java.util.ConcurrentModificationException
# 1 楼答案
我刚刚解决了我的问题。我发现了我的一些错误,如果你愿意,你可以找到我的运行代码
我最大的错误之一
如果商品数量不同,则不会返回正确的商品
通过在shoppingBasket上使用迭代器并比较产品ID,我解决了这个问题
@FXML public void addtoShoppingBasket()引发IOException{
}