Java 在不同的时间多次向TableView添加相同的对象

Java 在不同的时间多次向TableView添加相同的对象,java,javafx,tableview,javafx-8,Java,Javafx,Tableview,Javafx 8,我正试图建立一个购物项目。该程序可以将所有添加的产品保存在SQLite数据库中,而不会出现任何问题 有一些限制 产品可以是主产品或标准产品。主要产品有自己的子产品。如果客户想要购买主产品,那么客户也必须购买其子产品 到目前为止,一切似乎都很好。在主屏幕上,我在顶部构建了2个tableview,用户可以看到所有产品的列表(tableProductsInfo),在桌子下面,您可以看到购物车桌子(tableShoppingBasket) 如果用户从tableProductsInfo中选择一个产品并单击

我正试图建立一个购物项目。该程序可以将所有添加的产品保存在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
    }

}
我的问题

当桌上的菜篮子是干净的

如果我想第一次添加主产品! 似乎没有问题。 您可以在此处看到主屏幕:

但是当我想第二次添加相同的产品时 有个问题,我的桌子好像是这样的

我不想在不同的行中列出相同的产品

如果之前添加了此产品,我想更新其数量。(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);
                                                        }
                    }
线程“JavaFX应用程序线程”java.lang.RuntimeException中的异常:java.lang.reflect.InvocationTargetException 原因:java.util.ConcurrentModificationException


我刚刚解决了我的问题。我发现了一些错误,如果你愿意,你可以找到我的运行代码

我最大的错误之一

shoppingBasket.contains(selectedItem);
如果项目数量不同,则不会返回正确的项目

通过在shoppingBasket上使用迭代器并比较产品ID,我已经解决了这个问题

@FXML public void addtoShoppingBasket()引发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?
        int totalQ=returnTotalQuantity(selectedItem); // if this item added already in the shopping basket we are subsraction added quantity

        if(selectedItem.getStock()-totalQ>=inputQuantity){ // STOK KONTROL eğer eklenmek istenen ürün stokta varsa // Checking stock if its avaliable for selling
            //selectedItem.setQuantity(inputQuantity);

            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();
                    int totalSubQ=returnTotalQuantity(currentSubProduct);
                if((currentSubProduct.getQuantity()*inputQuantity)>currentSubProduct.getStock()-totalSubQ){
                    //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){
                    if(shoppingBasket!=null && !shoppingBasket.isEmpty()){
                    Iterator mainIterator=shoppingBasket.iterator();
                    while (mainIterator.hasNext()){
                        Product oldMain= (Product) mainIterator.next();
                        if(oldMain.getProductID()==selectedItem.getProductID()){
                            oldMain.setQuantity(oldMain.getQuantity()+inputQuantity);
                        }
                    }
                        Iterator subItemIterator=returnedList.iterator();
                    while (subItemIterator.hasNext()){
                        Product newSubItem = (Product) subItemIterator.next();
                        Iterator ShoppingIterator = shoppingBasket.iterator();
                        while (ShoppingIterator.hasNext()){
                            Product oldSubItem= (Product) ShoppingIterator.next();
                            if(oldSubItem.getProductID()==newSubItem.getProductID() && oldSubItem.getSubProduct()!=1){
                                oldSubItem.setQuantity((newSubItem.getQuantity()*inputQuantity)+oldSubItem.getQuantity());
                                calculateTotalPriceofShoppingBasket();
                            }
                        }
                        }
                            }else{
                        selectedItem.setQuantity(inputQuantity);
                        shoppingBasket.add(selectedItem);
                        // we should multiply subItem Quantity with new InputQuantity
                       Iterator iterator = returnedList.iterator();
                       while (iterator.hasNext()){
                           Product newQforSubItem= (Product) iterator.next();
                           newQforSubItem.setQuantity(inputQuantity*newQforSubItem.getQuantity());
                       }
                        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!");
                            //one of sub product out of its stock capacity
                }
            }else{ //eğer sub ürünü yoksa (selectedItem.getSubProduct==0)
                //if its not main product in other word if its standart Product
                selectedItem.setQuantity(inputQuantity);
                Iterator standartProduct = shoppingBasket.iterator();
                if(!shoppingBasket.isEmpty()&&shoppingBasket!=null) {
                    while (standartProduct.hasNext()) {
                        Product standart = (Product) standartProduct.next();
                        if (standart.getProductID() == selectedItem.getProductID()) {
                            standart.setQuantity(standart.getQuantity() + inputQuantity);
                            calculateTotalPriceofShoppingBasket();
                        }
                    }
                }else { // shoppingbasket is empty
                    selectedItem.setQuantity(inputQuantity);
                    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
    }
if(tableProductInfo.getSelectionModel().getSelectedItem()!=null){//eğer birürün seçiliyse//?
Product selectedItem=(Product)tableProductInfo.getSelectionModel().getSelectedItem()//
int inputQuantity=getInputDialogPane();//Kaçtaneürün eklenmek istiyor?//用户想要购买多少产品?
int totalQ=returnTotalQuantity(selectedItem);//如果此商品已添加到购物篮中,则我们将包含添加的数量
如果(selectedItem.getStock()-totalQ>=inputQuantity){//STOK KONTROL eğer eklenmek istenenürün stokta varsa//检查股票是否可出售
//选择编辑项设置数量(输入数量);
if(selectedItem.getSubProduct()==1){//eğer subürünüvarsa//如果所选产品具有子产品
//这意味着它的主要产品,我们需要检查它的子产品
//1) subürün Listsini al,//获取selectedItem的所有子管道

//2) subürün stok kontrollerini yap main.Quantity*sub.Quantity你问的问题很不清楚。确切的问题是什么?链接到外部截图并不表明问题是什么。请注意,不要发布外部截图的链接;如果需要,将图像嵌入到你的帖子中。你可以在我想的地方看到我的问题将我的文字改为粗体。但让我再次澄清一下。当用户在不同时间将同一产品添加到购物车时,我想更新购物表视图“我试图像这样更新shoppingBasket(observableList)上的产品数量,但它没有按我所希望的那样工作。”该代码看起来(大部分)对我来说是正确的。这是怎么回事“没有按我想要的那样工作”的意思是?那代码的结果到底是什么?看起来你需要做这样的事情。
if(checkouttable.contains(item){//Increment item quantity}或者{//add new item}
。你为什么要删除旧产品?为什么不只是提高它们的质量?你总是可以创建自己的
equals()
hashcode()
方法来比较ID,而不是使用循环。然后仍然可以使用
contains()
方法。
    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?
        int totalQ=returnTotalQuantity(selectedItem); // if this item added already in the shopping basket we are subsraction added quantity

        if(selectedItem.getStock()-totalQ>=inputQuantity){ // STOK KONTROL eğer eklenmek istenen ürün stokta varsa // Checking stock if its avaliable for selling
            //selectedItem.setQuantity(inputQuantity);

            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();
                    int totalSubQ=returnTotalQuantity(currentSubProduct);
                if((currentSubProduct.getQuantity()*inputQuantity)>currentSubProduct.getStock()-totalSubQ){
                    //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){
                    if(shoppingBasket!=null && !shoppingBasket.isEmpty()){
                    Iterator mainIterator=shoppingBasket.iterator();
                    while (mainIterator.hasNext()){
                        Product oldMain= (Product) mainIterator.next();
                        if(oldMain.getProductID()==selectedItem.getProductID()){
                            oldMain.setQuantity(oldMain.getQuantity()+inputQuantity);
                        }
                    }
                        Iterator subItemIterator=returnedList.iterator();
                    while (subItemIterator.hasNext()){
                        Product newSubItem = (Product) subItemIterator.next();
                        Iterator ShoppingIterator = shoppingBasket.iterator();
                        while (ShoppingIterator.hasNext()){
                            Product oldSubItem= (Product) ShoppingIterator.next();
                            if(oldSubItem.getProductID()==newSubItem.getProductID() && oldSubItem.getSubProduct()!=1){
                                oldSubItem.setQuantity((newSubItem.getQuantity()*inputQuantity)+oldSubItem.getQuantity());
                                calculateTotalPriceofShoppingBasket();
                            }
                        }
                        }
                            }else{
                        selectedItem.setQuantity(inputQuantity);
                        shoppingBasket.add(selectedItem);
                        // we should multiply subItem Quantity with new InputQuantity
                       Iterator iterator = returnedList.iterator();
                       while (iterator.hasNext()){
                           Product newQforSubItem= (Product) iterator.next();
                           newQforSubItem.setQuantity(inputQuantity*newQforSubItem.getQuantity());
                       }
                        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!");
                            //one of sub product out of its stock capacity
                }
            }else{ //eğer sub ürünü yoksa (selectedItem.getSubProduct==0)
                //if its not main product in other word if its standart Product
                selectedItem.setQuantity(inputQuantity);
                Iterator standartProduct = shoppingBasket.iterator();
                if(!shoppingBasket.isEmpty()&&shoppingBasket!=null) {
                    while (standartProduct.hasNext()) {
                        Product standart = (Product) standartProduct.next();
                        if (standart.getProductID() == selectedItem.getProductID()) {
                            standart.setQuantity(standart.getQuantity() + inputQuantity);
                            calculateTotalPriceofShoppingBasket();
                        }
                    }
                }else { // shoppingbasket is empty
                    selectedItem.setQuantity(inputQuantity);
                    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
    }