main
Alexis Drai 3 years ago
parent 0ce049eed0
commit ede570dd8a

@ -5,7 +5,6 @@
</component>
<component name="ChangeListManager">
<list default="true" id="39201d7d-d87b-4c10-b0b0-e462b4f0b033" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/data/Loader.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/data/Loader.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/data/Saver.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/data/Saver.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/model/Product.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/model/Product.java" afterDir="false" />
@ -93,7 +92,7 @@
<workItem from="1665590389139" duration="2198000" />
<workItem from="1665651119385" duration="53000" />
<workItem from="1665652739206" duration="91000" />
<workItem from="1665679173344" duration="5011000" />
<workItem from="1665679173344" duration="5345000" />
</task>
<task id="LOCAL-00001" summary=":truck: Set up main window">
<created>1665143728540</created>
@ -158,7 +157,14 @@
<option name="project" value="LOCAL" />
<updated>1665592583787</updated>
</task>
<option name="localTasksCounter" value="10" />
<task id="LOCAL-00010" summary=":bug:">
<created>1665687943184</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1665687943184</updated>
</task>
<option name="localTasksCounter" value="11" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -182,7 +188,8 @@
<MESSAGE value=":construction: WIP" />
<MESSAGE value=" Update gitignore" />
<MESSAGE value=":poop: Produce bad code that doesn't work&#10;&#10;I think we're looking at a restart in the near future" />
<option name="LAST_COMMIT_MESSAGE" value=":poop: Produce bad code that doesn't work&#10;&#10;I think we're looking at a restart in the near future" />
<MESSAGE value=":bug:" />
<option name="LAST_COMMIT_MESSAGE" value=":bug:" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>

@ -11,7 +11,6 @@ public class Loader implements Loadable {
@Override
public Shop load() throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("save.bin"))) {
System.out.println("...loading!...");
return (Shop) ois.readObject();
}
}

@ -11,7 +11,6 @@ public class Saver implements Savable {
@Override
public void save(Shop model) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("save.bin"))) {
System.out.println("...saving!...");
oos.writeObject(model);
}
}

@ -16,6 +16,7 @@ public class Product implements Serializable {
}
public Product() {
this(null, 0.0);
}
public String getName() {
@ -30,7 +31,6 @@ public class Product implements Serializable {
oldV,
getName()
);
System.out.println("set my name to " + name);
}
public double getPrice() {
@ -45,7 +45,6 @@ public class Product implements Serializable {
oldV,
getPrice()
);
System.out.println("set my price to " + price);
}
private PropertyChangeSupport support;
@ -64,16 +63,4 @@ public class Product implements Serializable {
getSupport().addPropertyChangeListener(listener);
}
@Override
public int hashCode() {
return getName().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof Product
&& ((Product) obj).getName() != null
&& ((Product) obj).getName().equals(getName());
}
}

@ -52,7 +52,7 @@ public class MainWindow {
@FXML
private void clickAddProduct() {
viewmodel.addGarmentVM(new GarmentVM("no_name", 0.0));
viewmodel.addGarmentVM(new GarmentVM());
}
@ -76,7 +76,7 @@ public class MainWindow {
new Alert(Alert.AlertType.ERROR,
"could not parse PRICE as a number, please try again",
ButtonType.OK)
.setHeaderText(null);
.show();
}
}
@ -99,10 +99,11 @@ public class MainWindow {
creationWindowStage.setScene(new Scene(loader.load()));
creationWindowStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
new Alert(Alert.AlertType.ERROR,
"error while opening product creation window",
ButtonType.OK)
.setHeaderText(null);
.show();
}
return controller;
}

@ -8,6 +8,14 @@ public class GarmentVM extends ProductVM {
private Garment model;
public GarmentVM(Object obj) {
super(obj);
}
public GarmentVM() {
super();
}
public GarmentVM(String name, Double price) {
super(name, price);
}

@ -1,7 +1,21 @@
package viewmodel;
import model.Perfume;
public class PerfumeVM extends ProductVM {
private Perfume model;
public PerfumeVM(Object obj) {
super(obj);
}
public PerfumeVM() {
super();
}
public PerfumeVM(String name, Double price) {
super(name, price);
}
}

@ -1,6 +1,9 @@
package viewmodel;
import javafx.beans.property.*;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import model.Product;
import java.beans.PropertyChangeEvent;
@ -19,12 +22,18 @@ public class ProductVM implements PropertyChangeListener {
private final StringProperty name = new SimpleStringProperty();
public ProductVM(String name, Double price) {
model = new Product();
public ProductVM(Object obj) {
//loads
if (obj instanceof Product) {
model = (Product) obj;
}
else {
model = new Product();
}
//loads / sets
setName(name == null ? model.getName() : name);
setPrice(price == null ? model.getPrice() : price);
setName(model.getName());
setPrice(model.getPrice());
//subscribes
model.addListener(this);
@ -34,9 +43,12 @@ public class ProductVM implements PropertyChangeListener {
priceProperty().addListener((__, ___, newV) -> model.setPrice((Double) newV));
}
public ProductVM(Object o) {
this(o != null ? ((Product) o).getName() : null,
o != null ? ((Product) o).getPrice() : null);
public ProductVM() {
this(null);
}
public ProductVM(String name, Double price) {
this(new Product(name, price));
}
public String getName() {
@ -61,7 +73,6 @@ public class ProductVM implements PropertyChangeListener {
public void setPrice(double price) {this.price.set(price);}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(PROP_PRODUCT_NAME)) {

@ -1,5 +1,6 @@
package viewmodel;
import data.Loadable;
import data.Loader;
import data.Saver;
import data.Stub;
@ -17,7 +18,7 @@ import java.io.IOException;
public class ShopVM implements PropertyChangeListener {
private Shop model;
private final Shop model;
public Shop getModel() {
return model;
@ -30,44 +31,42 @@ public class ShopVM implements PropertyChangeListener {
public ShopVM() {
Shop tmpModel;
Loadable loader;
try {
System.out.println("trying real load");
model = new Loader().load();
loader = new Loader();
tmpModel = loader.load();
} catch (IOException | ClassNotFoundException ex1) {
ex1.printStackTrace();
try {
System.out.println("trying stub load");
model = new Stub().load();
loader = new Stub();
tmpModel = loader.load();
} catch (IOException | ClassNotFoundException ex2) {
System.out.println("giving up load and creating new");
model = new Shop();
ex2.printStackTrace();
tmpModel = new Shop();
}
}
model = tmpModel;
//subscribes
model.addListener(this);
//loads
model.getProducts().forEach(p -> productsVMObs.add(new ProductVM(p.getName(), p.getPrice())));
model.getProducts().forEach(p -> productsVMObs.add(new ProductVM(p)));
//will update with its methods
}
public void removeProduct(ProductVM productVM) {
productsVMObs.remove(productVM);
getModel().removeProduct(productVM.getModel());
printModel();
}
public void addGarmentVM(GarmentVM garmentVM) {
productsVMObs.add(garmentVM);
getModel().addProduct(garmentVM.getModel());
printModel();
}
public void addPerfumeVM(PerfumeVM perfumeVM) {
productsVMObs.add(perfumeVM);
getModel().addProduct(perfumeVM.getModel());
printModel();
}
public void save() throws IOException {
@ -75,19 +74,13 @@ public class ShopVM implements PropertyChangeListener {
saver.save(getModel());
}
private void printModel() {
System.out.println("printing model.................");
getModel().getProducts().forEach(p -> System.out.println(p.getName() + " : " + p.getPrice()));
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Shop.PROP_SHOP_ADD)) {
ProductVM pvm = new ProductVM(evt.getNewValue());
productsVMObs.add(((IndexedPropertyChangeEvent) evt).getIndex(), pvm);
productsVMObs.add(((IndexedPropertyChangeEvent) evt).getIndex(), new ProductVM(evt.getNewValue()));
}
if (evt.getPropertyName().equals(Shop.PROP_SHOP_RMV)) {
productsVMObs.remove(new ProductVM(evt.getOldValue())); // redefined equals an toHash
productsVMObs.remove(((IndexedPropertyChangeEvent) evt).getIndex());
}
}
}

Loading…
Cancel
Save