Issue
I am working on a project where I create "products" that are made up of ObservableList "parts".
I create a product and add a part to it. You can see here, I add part "Screwdriver" to product TESTPROD1. Creating Product
Next, I create TESTPROD2 and add part "Wrench" to it. Creating Second Product
Finally, I go back to modify the original product (TESTPROD1), which should only contain "Screwdriver", but it contains both "Screwdriver" and "Wrench". Modify TESTPROD1
Here is where I am creating the product when the "Save" button is pressed.
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;
}
}
For some reason, creating a new product overwrites the ObservableList of parts associated with the previous product. I've been trying to figure out why this is for hours. Any help would be appreciated.
Solution
Your class Product
declares associatedParts
as being static
. This means there is only one list per JVM, rather than one list per product.
In other words: remove the static
keyword and each product will have its own associated parts.
Answered By - Jeroen Steenbeeke