From 01312ca9cff21044e283b930b65be5a1ed0bc6f3 Mon Sep 17 00:00:00 2001 From: Joelwang22 Date: Tue, 12 Mar 2024 02:00:59 +0800 Subject: [PATCH 1/2] add methods for order and product class add quantity class --- .../java/seedu/address/model/order/Order.java | 69 +++++++++++++++++++ .../seedu/address/model/order/Product.java | 46 +++++++++++++ .../seedu/address/model/order/Quantity.java | 45 ++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/main/java/seedu/address/model/order/Quantity.java diff --git a/src/main/java/seedu/address/model/order/Order.java b/src/main/java/seedu/address/model/order/Order.java index 2011078e075..8489e581cd0 100644 --- a/src/main/java/seedu/address/model/order/Order.java +++ b/src/main/java/seedu/address/model/order/Order.java @@ -1,4 +1,73 @@ package seedu.address.model.order; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + public class Order { + private int id; + private Map productMap; + + public Order() { + productMap = new HashMap<>(); + } + + public Order(int id) { + this.id = id; + productMap = new HashMap<>(); + } + + public void setID(int id) { + this.id = id; + } + + public Map getProductMap() { + return this.productMap; + } + + public void addProduct(Product newProduct, Quantity newQuantity) { + productMap.put(newProduct, newQuantity); + } + + public Quantity getQuantity(Product product) { + Quantity currQuantity = productMap.get(product); + return currQuantity; + } + + public int getQuantityValue(Product product) { + int value = productMap.get(product).getValue(); + return value; + } + + public void deleteProduct(Product product) { + productMap.remove(product); + } + + public void clearOrder() { + this.productMap.clear(); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof Order)) { + return false; + } + + Order otherOrder = (Order) other; + return (this.id == otherOrder.id) + && this.productMap.equals(otherOrder.productMap); + } + + @Override + public int hashCode() { + return Objects.hash(id, productMap); + } + + @Override + public String toString() { + return productMap.toString(); + } } diff --git a/src/main/java/seedu/address/model/order/Product.java b/src/main/java/seedu/address/model/order/Product.java index 8b0dd8d88a1..1c042de13cc 100644 --- a/src/main/java/seedu/address/model/order/Product.java +++ b/src/main/java/seedu/address/model/order/Product.java @@ -1,4 +1,50 @@ package seedu.address.model.order; public class Product { + + public static final String MESSAGE_CONSTRAINTS = + "Product names should only contain alphanumeric characters and spaces, and it should not be blank"; + + /* + * The first character of the product name must not be a whitespace, + * otherwise " " (a blank string) becomes a valid input. + */ + public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*"; + private String name; + + public Product(String name) { + this.name = name; + } + + @Override + public String toString() { + return this.name; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + if (!(other instanceof Product)){ + return false; + } + + Product otherProduct = (Product) other; + return this.name.equals(otherProduct.name); + } + + @Override + public int hashCode() { + return this.name.hashCode(); + } + + public String getName() { + return this.name; + } + + public void rename(String newName) { + this.name = newName; + } } diff --git a/src/main/java/seedu/address/model/order/Quantity.java b/src/main/java/seedu/address/model/order/Quantity.java new file mode 100644 index 00000000000..5143514627f --- /dev/null +++ b/src/main/java/seedu/address/model/order/Quantity.java @@ -0,0 +1,45 @@ +package seedu.address.model.order; + +public class Quantity { + public static final String MESSAGE_CONSTRAINTS = + "Product quantity should only be a number."; + public static final String VALIDATION_REGEX = "\\d"; + public int value; + + public Quantity(int value) { + this.value = value; + } + + @Override + public String toString() { + return Integer.toString(this.value); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof Quantity)) { + return false; + } + + Quantity otherQuantity = (Quantity) other; + return this.value == otherQuantity.value; + } + + @Override + public int hashCode() { + return Integer.hashCode(this.value); + } + + public int getValue() { + return this.value; + } + + public void setQuantity(int newQuantity) { + this.value = newQuantity; + } +} From 1e4965925d28554e269f3b197c5131d5d0d6ab05 Mon Sep 17 00:00:00 2001 From: Joelwang22 Date: Tue, 12 Mar 2024 02:08:23 +0800 Subject: [PATCH 2/2] fix order --- src/main/java/seedu/address/model/order/Order.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/order/Order.java b/src/main/java/seedu/address/model/order/Order.java index 8489e581cd0..de5b8034098 100644 --- a/src/main/java/seedu/address/model/order/Order.java +++ b/src/main/java/seedu/address/model/order/Order.java @@ -21,6 +21,10 @@ public void setID(int id) { this.id = id; } + public int getId(){ + return this.id; + } + public Map getProductMap() { return this.productMap; } @@ -39,11 +43,16 @@ public int getQuantityValue(Product product) { return value; } + public void changeQuantity(Product currProduct, int newQuantity) { + Quantity currQuantity = productMap.get(currProduct); + currQuantity.setQuantity(newQuantity); + } + public void deleteProduct(Product product) { productMap.remove(product); } - public void clearOrder() { + public void clearProductMap() { this.productMap.clear(); }