Skip to content

Commit

Permalink
Add addOrderCommand class
Browse files Browse the repository at this point in the history
Add addOrderCommandParser class
Add findPersonByPhoneNumber method to ModelManager
Add JsonAdaptedOrder
Add basic Ui implementation of order
  • Loading branch information
fubsywubsy committed Mar 13, 2024
1 parent 2a02b8f commit b8916e1
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class Messages {
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";

public static final String MESSAGE_INVALID_PHONE_NUMBER = "Phone number provided not found";

public static final String MESSAGE_PHONE_NUMBER_NOT_FOUND = "Phone number provided not found";

/**
* Returns an error message indicating the duplicate prefixes.
*/
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddOrderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package seedu.address.logic.commands;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.order.Order;
import seedu.address.model.order.Product;
import seedu.address.model.order.Quantity;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;

import java.util.Optional;

public class AddOrderCommand extends Command{
public static final String COMMAND_WORD = "order";
public static final String MESSAGE_ARGUMENTS = "Index: %1$d";
public static final String MESSAGE_ADD_ORDER_SUCCESS = "Added order to Person: %1$s";
public static final String MESSAGE_DELETE_ORDER_SUCCESS = "Removed order from Person: %1$s";

private final Phone phone;
private Order order;

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Edits the order of the person identified "
+ "by the phone number of person. "
+ "Existing orders will be overwritten by the input.\n"
+ "Parameters: phone number (must be a positive integer) "
+ "p/ [PHONE_NUMBER]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "p/ 87438807.";

public AddOrderCommand(Phone phone) {
requireAllNonNull(phone);

this.phone = phone;
this.order = new Order();
}

@Override
public CommandResult execute(Model model) throws CommandException {
if (!Phone.isValidPhone(phone.toString())) {
throw new CommandException(Messages.MESSAGE_INVALID_PHONE_NUMBER);
}

// To Check if phone number belongs to existing person
Optional<Person> maybeEditablePerson = model.findPersonByPhoneNumber(this.phone.value);
if (!maybeEditablePerson.isPresent()) {
throw new CommandException(Messages.MESSAGE_PHONE_NUMBER_NOT_FOUND);
}
Person personToEdit = maybeEditablePerson.get();
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), personToEdit.getTags());

// CHANGE THIS!!!! Need add functionality for users to input products
// THIS IS A PLACEHOLDER
this.order.addProduct(new Product("cupcake"), new Quantity(2));

editedPerson.addOrder(this.order);

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(editedPerson));
}

private String generateSuccessMessage(Person personToEdit) {
String message = !order.isEmpty() ? MESSAGE_ADD_ORDER_SUCCESS : MESSAGE_DELETE_ORDER_SUCCESS;
return String.format(message, personToEdit);
}


@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AddOrderCommand)) {
return false;
}

AddOrderCommand e = (AddOrderCommand) other;
return phone == e.phone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import seedu.address.commons.exceptions.IllegalValueException;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import seedu.address.logic.commands.AddOrderCommand;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Phone;


public class AddOrderCommandParser {
public AddOrderCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_PHONE);

Phone phone;
try {
phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
AddOrderCommand.MESSAGE_USAGE), ive);
}

return new AddOrderCommand(phone);
}
}
13 changes: 4 additions & 9 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.*;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -77,6 +69,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case AddOrderCommand.COMMAND_WORD:
return new AddOrderCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -84,4 +85,12 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Finds a person based on their phone number.
*
* @param phoneNumber The phone number to search for.
* @return An Optional containing the found Person, or an empty Optional if no person with the given phone number exists.
*/
Optional<Person> findPersonByPhoneNumber(String phoneNumber);
}
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -128,6 +129,17 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

@Override
public Optional<Person> findPersonByPhoneNumber(String phoneNumber) {
ObservableList<Person> persons = getFilteredPersonList();
for (Person person : persons) {
if (person.getPhone().value.equals(phoneNumber)) {
return Optional.of(person);
}
}
return Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/seedu/address/model/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import java.util.Objects;

public class Order {
public static final String MESSAGE_CONSTRAINTS = "Orders need to contain alphanumeric product names and numeric quantities";
private int id;
private Map<Product, Quantity> productMap;

public Order() {
productMap = new HashMap<>();
this.productMap = new HashMap<>();
}

public Order(int id) {
this.id = id;
productMap = new HashMap<>();
}

public Order(Map<Product, Quantity> map) {
productMap = map;
}

public void setID(int id) {
this.id = id;
}
Expand Down Expand Up @@ -56,6 +61,10 @@ public void clearProductMap() {
this.productMap.clear();
}

public boolean isEmpty() {
return productMap.isEmpty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.order.Order;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -25,6 +23,8 @@ public class Person {
private final Address address;
private final Set<Tag> tags = new HashSet<>();

private ArrayList<Order> orders= new ArrayList<>();

/**
* Every field must be present and not null.
*/
Expand Down Expand Up @@ -53,6 +53,22 @@ public Address getAddress() {
return address;
}

public ArrayList<Order> getOrders() {
return orders;
}

public void addOrder(Order order) {
this.orders.add(order);
}

public void deleteOrder(int index) {
this.orders.remove(index);
}

public void setOrders(ArrayList<Order> orders) {
this.orders = orders;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package seedu.address.storage;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.order.Order;
import seedu.address.model.order.Product;
import seedu.address.model.order.Quantity;

import java.util.Map;

public class JsonAdaptedOrder {
private final Map<Product, Quantity> order;

/**
* Constructs a {@code JsonAdaptedOrder} with the given {@code order}.
*/
@JsonCreator
public JsonAdaptedOrder(Map<Product, Quantity> order) {
this.order = order;
}

/**
* Converts a given {@code Order} into this class for Jackson use.
*/
public JsonAdaptedOrder(Order order) {
this.order = order.getProductMap();
}

@JsonValue
public String getOrder() {
return this.order.toString();
}

/**
* Converts this Jackson-friendly adapted tag object into the model's {@code Tag} object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted tag.
*/
public Order toModelType() throws IllegalValueException {
return new Order(this.order);
}
}
Loading

0 comments on commit b8916e1

Please sign in to comment.