Skip to content

Commit

Permalink
add methods for order and product class
Browse files Browse the repository at this point in the history
add quantity class
  • Loading branch information
fubsywubsy committed Mar 11, 2024
1 parent fc71bf5 commit 01312ca
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/seedu/address/model/order/Order.java
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();
}
}
46 changes: 46 additions & 0 deletions src/main/java/seedu/address/model/order/Product.java
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;
}
}
45 changes: 45 additions & 0 deletions src/main/java/seedu/address/model/order/Quantity.java
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;
}
}

0 comments on commit 01312ca

Please sign in to comment.