创建新对象会覆盖与先前对象关联的 ObservableList

Creating new object overwrites ObservableList associated with previous object

我正在做一个项目,我在其中创建由 ObservableList“部分”组成的“产品”。

Main screen

我创建了一个产品并向其添加了一个部件。你可以在这里看到,我在产品 TESTPROD1 中添加了“螺丝刀”部分。 Creating Product

接下来,我创建 TESTPROD2 并向其中添加部件“Wrench”。 Creating Second Product

最后回去修改原产品(TESTPROD1),本来应该只有“Screwdriver”,却同时包含“Screwdriver”和“Wrench”。 Modify TESTPROD1

这是我在按下“保存”按钮时创建产品的地方。

private void addProductSaveHandler(ActionEvent event) throws IOException {
        
        Product p = new Product(
                Integer.parseInt(addProductIdTextField.getText()),
                addProductNameTextField.getText(),
                Double.parseDouble(addProductPriceTextField.getText()),
                Integer.parseInt(addProductInvTextField.getText()),
                Integer.parseInt(addProductMinTextField.getText()),
                Integer.parseInt(addProductMaxTextField.getText())
        );
        
        
        
        Inventory.addProduct(p);
        
        for (Part addedPs : tableSelectedParts){
            //Inventory.lookupProduct(p.getId()).addAssociatedPart(addedPs);
            Inventory.lookupProduct(Integer.parseInt(addProductIdTextField.getText())).addAssociatedPart(addedPs);
            
        }
        
        tableSelectedParts.clear();
        
        
        //Return to main screen
        stage = (Stage)((Button)event.getSource()).getScene().getWindow();
        scene = FXMLLoader.load(getClass().getResource("/view/MainForm.fxml"));
        stage.setScene(new Scene(scene));
        stage.show();
        
    }
@FXML
private void addPartHandler(ActionEvent event) {
        
        
        tableSelectedParts.add(addProductAllPartsTable.getSelectionModel().getSelectedItem());
        
    }
    
public class Inventory {
    
    private static ObservableList<Part> allParts = FXCollections.observableArrayList();
    private static ObservableList<Product> allProducts = FXCollections.observableArrayList();
    
    public static void addPart(Part newPart){
        allParts.add(newPart);
    }
    
    public static void addProduct(Product newProduct){
        allProducts.add(newProduct);
    }
    
    public static Part lookupPart(int partId){
        return allParts.get(partId);
    }
    
    public static Product lookupProduct(int productId){
        int productLocation = 0;
        for (int i = 0; i < allProducts.size(); i++){
            if (allProducts.get(i).getId() == productId){
                
                productLocation = i;
            }
            
        }
        return allProducts.get(productLocation);
        
        
    }
    public static ObservableList<Product> lookupProduct(String productName){
        ObservableList<Product> matchingProduct = FXCollections.observableArrayList();
        for (int i = 0; i < allProducts.size(); i++){
            if (allProducts.get(i).getName().contains(productName)){
                matchingProduct.add(allProducts.get(i));
            }
        }
        
        return matchingProduct;
        
    }
}
public class Product {
    private static ObservableList<Part> associatedParts = FXCollections.observableArrayList();
    private int id;
    private String name;
    private double price;
    private int stock;
    private int min;
    private int max;    
    public Product(int id, String name, double price, int stock, int min, int max) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.min = min;
        this.max = max;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
    
    /**
     * @return the stock
     */
    public int getStock() {
        return stock;
    }

    /**
     * @param stock the stock to set
     */
    public void setStock(int stock) {
        this.stock = stock;
    }

    /**
     * @return the min
     */
    public int getMin() {
        return min;
    }

    /**
     * @param min the min to set
     */
    public void setMin(int min) {
        this.min = min;
    }

    /**
     * @return the max
     */
    public int getMax() {
        return max;
    }

    /**
     * @param max the max to set
     */
    public void setMax(int max) {
        this.max = max;
    }
    
    public static void addAssociatedPart(Part part){
        associatedParts.add(part);
    }
    
    public boolean deleteAssociatedPart(Part part){
        associatedParts.remove(part);
        //not sure about this below - come back later
        return false;
    }
    
     public static ObservableList<Part> getAllAssociatedParts(){
         return associatedParts;
     }
}

出于某种原因,创建新产品会覆盖与先前产品关联的部件的 ObservableList。我一直在试图弄清楚为什么会这样几个小时。任何帮助将不胜感激。

您的 class Product 声明 associatedPartsstatic。这意味着每个 JVM 只有一个列表,而不是每个产品一个列表。

换句话说:删除 static 关键字,每个产品将有自己的关联部分。