forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods for order and product class
add quantity class
- Loading branch information
1 parent
fc71bf5
commit 01312ca
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Product, Quantity> 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<Product, Quantity> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |