Alexis Drai 3 years ago
parent 0465ee3148
commit 0ce049eed0

@ -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<Product> 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,
int index = products.indexOf(toRemove);
if (index > -1) {
products.remove(index);
getSupport().fireIndexedPropertyChange(PROP_SHOP_RMV,
index,
product,
products.get(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;

Loading…
Cancel
Save