我正在尝试建立一个购物项目。该程序可以将所有添加的产品保留在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);
}
我的问题
当tableShoppingBasket清除时;
如果我要添加主产品第一次! 似乎没有问题。 您可以在此处看到主屏幕:
但是当我想添加相同的产品第二时间时 有问题,我的桌子好像是这样
我不想在不同的行中列出相同的产品。
如果此产品以前添加过,我想更新其数量。(oldProduct.getQuantity + newProduct.getQuantity)
我需要修正此观点:
我想更新其数量并查看表格行
标签名称价格数量
我试图用此代码解决我的问题,但是它没有按我想要的那样工作。 我的意思是,它没有添加所选主要产品的子产品。
//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 Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException 引起原因:java.util.ConcurrentModificationException
答案 0 :(得分:0)
我刚刚解决了我的问题,发现了一些错误,您可以根据需要找到运行代码。
我最大的错误之一
shoppingBasket.contains(selectedItem);
如果数量不同,则不会返回正确的项目。
我已经通过在shopBasket上使用迭代器并比较了产品ID来解决了这个问题
@FXML 公共无效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
}
}