From 0ce049eed0d2d3e668c8ec90b566e276b98ca26c Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Thu, 13 Oct 2022 21:06:45 +0200 Subject: [PATCH] :bug: --- src/model/Shop.java | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/model/Shop.java b/src/model/Shop.java index 9574c6a..bb5dc78 100644 --- a/src/model/Shop.java +++ b/src/model/Shop.java @@ -1,14 +1,11 @@ package model; -import data.Stub; - import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.UUID; public class Shop implements Serializable { public static final String PROP_SHOP_ADD = "model.shop.addProduct"; @@ -17,29 +14,24 @@ public class Shop implements Serializable { private final List products = new ArrayList<>(); public void addProduct(Product product) { + int index = 0; - products.add(0, product); - getSupport().fireIndexedPropertyChange( - PROP_SHOP_ADD, - 0, - products.size() > 1 ? products.get(1) : null, - product - ); - System.out.println("added " + product.getName() + " ($" + product.getPrice() + ")"); + products.add(index, product); + getSupport().fireIndexedPropertyChange(PROP_SHOP_ADD, + index, + getProducts().size() > index + 1 ? getProducts().get(index + 1) : null, + getProducts().get(index)); } - public void removeProduct(Product product) { + public void removeProduct(Product toRemove) { - System.out.println("removed " + product.getName() + " ($" + product.getPrice() + ")"); - int index = products.indexOf(product); - if (index != -1){ - products.remove(product); - getSupport().fireIndexedPropertyChange( - PROP_SHOP_RMV, - index, - product, - products.get(index) - ); + int index = products.indexOf(toRemove); + if (index > -1) { + products.remove(index); + getSupport().fireIndexedPropertyChange(PROP_SHOP_RMV, + index, + toRemove, + getProducts().size() >= index + 1 ? getProducts().get(index) : null); } } @@ -48,11 +40,10 @@ public class Shop implements Serializable { return Collections.unmodifiableList(products); } - private transient PropertyChangeSupport support; private PropertyChangeSupport getSupport() { - if(support == null) { + if (support == null) { support = new PropertyChangeSupport(this); } return support;