Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods for order and product class #38

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 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,82 @@
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 int getId(){
return this.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 changeQuantity(Product currProduct, int newQuantity) {
Quantity currQuantity = productMap.get(currProduct);
currQuantity.setQuantity(newQuantity);
}

public void deleteProduct(Product product) {
productMap.remove(product);
}

public void clearProductMap() {
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;
}
}
Loading