【发布时间】:2019-01-19 11:18:18
【问题描述】:
我正在尝试构建一个购物项目。该程序可以毫无问题地将所有添加的产品保存在 SQLite 数据库中。
有一些限制。
产品可以是主要产品或标准产品。主要产品有自己的子产品。如果客户想购买主产品,那么客户也必须购买它的子产品。
到目前为止,一切似乎都还不错。在 mainScreen 中,我在顶部用户上构建了 2 个 tableview,可以看到所有产品的列表(tableProductsInfo),在表格下方,您可以看到购物车表(tableShoppingBasket)。
如果用户从 tableProductsInfo 中选择一个产品并点击添加到购物车按钮,该产品将被添加到下面和图片中的 shoppingBasket(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)
我需要修复这个视图:
我想更新它的数量并看到像这样的表格行
标签名称价格数量
- 主mProduct 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
【问题讨论】:
-
目前还不清楚你在问什么。确切的问题是什么?链接到外部屏幕截图并不能说明问题所在。附带说明,请不要发布指向外部屏幕截图的链接;如有必要,将图片嵌入您的帖子中。
-
您可以看到我将文本样式设置为 bold 的问题。但让我再澄清一次。当用户在不同时间将相同产品添加到购物车中时,我想更新购物桌视图
-
“我试图像这样更新 shoppingBasket(observableList) 上的产品数量,但它没有按我的意愿工作。”该代码在我看来(大部分)是正确的。 “没有按我的意愿工作”是什么意思?该代码的具体结果是什么?
-
看来你需要做类似的事情。
if(checkouttable.contains(item){//Increment item quanity}else{//add new Item}. -
为什么要删除旧产品?为什么不只是提高他们的质量?
标签: java javafx tableview javafx-8