Skip to content

Commit

Permalink
Implement Find Order Command and Edit Order Command (#134)
Browse files Browse the repository at this point in the history
Closes #18 
Closes #28
  • Loading branch information
LiuMC-SG authored Mar 18, 2023
2 parents 1ebe037 + a0edeb9 commit e87e0a3
Show file tree
Hide file tree
Showing 19 changed files with 1,194 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/main/java/trackr/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public class Messages {
public static final String MESSAGE_TASKS_LISTED_OVERVIEW = "%1$d tasks listed!";
public static final String MESSAGE_INVALID_ORDER_DISPLAYED_INDEX = "The order index provided is invalid";
public static final String MESSAGE_ORDERS_LISTED_OVERVIEW = "%1$d orders listed!";

}
130 changes: 130 additions & 0 deletions src/main/java/trackr/logic/commands/EditOrderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package trackr.logic.commands;

import static java.util.Objects.requireNonNull;
import static trackr.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static trackr.logic.parser.CliSyntax.PREFIX_DEADLINE;
import static trackr.logic.parser.CliSyntax.PREFIX_NAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERNAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERQUANTITY;
import static trackr.logic.parser.CliSyntax.PREFIX_PHONE;
import static trackr.logic.parser.CliSyntax.PREFIX_STATUS;
import static trackr.model.Model.PREDICATE_SHOW_ALL_ORDERS;

import java.util.List;

import trackr.commons.core.Messages;
import trackr.commons.core.index.Index;
import trackr.logic.commands.exceptions.CommandException;
import trackr.model.Model;
import trackr.model.order.Order;
import trackr.model.order.OrderDeadline;
import trackr.model.order.OrderDescriptor;
import trackr.model.order.OrderName;
import trackr.model.order.OrderQuantity;
import trackr.model.order.OrderStatus;
import trackr.model.order.customer.Customer;
import trackr.model.order.customer.CustomerAddress;
import trackr.model.order.customer.CustomerName;
import trackr.model.order.customer.CustomerPhone;

/**
* Edits the details of an existing order in the order list.
*/
public class EditOrderCommand extends Command {
public static final String COMMAND_WORD = "edit_order";
public static final String COMMAND_WORD_SHORTCUT = "edit_o";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the order identified "
+ "by the index number used in the displayed order list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_ORDERNAME + "ORDER NAME] "
+ "[" + PREFIX_DEADLINE + "ORDER DEADLINE] "
+ "[" + PREFIX_STATUS + "ORDER STATUS]"
+ "[" + PREFIX_ORDERQUANTITY + "ORDER QUANTITY]"
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_ADDRESS + "ADDRESS "
+ "Example: " + COMMAND_WORD + " 2 "
+ PREFIX_NAME + "Birthday Cake"
+ PREFIX_STATUS + "D";

public static final String MESSAGE_EDIT_ORDER_SUCCESS = "Edited Order: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_ORDER = "This order already exists in the order list.";

private final Index index;
private final OrderDescriptor editOrderDescriptor;

/**
* @param index of the order in the filtered order list to edit
* @param editOrderDescriptor details to edit the order with
*/

public EditOrderCommand(Index index, OrderDescriptor editOrderDescriptor) {
requireNonNull(index);
requireNonNull(editOrderDescriptor);

this.index = index;
this.editOrderDescriptor = new OrderDescriptor(editOrderDescriptor);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Order> lastShownOrderList = model.getFilteredOrderList();

if (index.getZeroBased() >= lastShownOrderList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_ORDER_DISPLAYED_INDEX);
}

Order orderToEdit = lastShownOrderList.get(index.getZeroBased());
Order editedOrder = createEditedOrder(orderToEdit, editOrderDescriptor);

if (!orderToEdit.isSameOrder(editedOrder) && model.hasOrder(editedOrder)) {
throw new CommandException(MESSAGE_DUPLICATE_ORDER);
}

model.setOrder(orderToEdit, editedOrder);
model.updateFilteredOrderList(PREDICATE_SHOW_ALL_ORDERS);
return new CommandResult(String.format(MESSAGE_EDIT_ORDER_SUCCESS, editedOrder));
}


private static Order createEditedOrder(Order orderToEdit, OrderDescriptor editOrderDescriptor) {
assert orderToEdit != null;

OrderName updatedOrderName =
editOrderDescriptor.getOrderName().orElse(orderToEdit.getOrderName());
OrderDeadline updatedOrderDeadline =
editOrderDescriptor.getOrderDeadline().orElse(orderToEdit.getOrderDeadline());
OrderStatus updatedOrderStatus =
editOrderDescriptor.getOrderStatus().orElse(orderToEdit.getOrderStatus());
OrderQuantity updatedOrderQuantity =
editOrderDescriptor.getOrderQuantity().orElse(orderToEdit.getOrderQuantity());
CustomerName updatedCustomerName =
editOrderDescriptor.getCustomerName().orElse(orderToEdit.getCustomer().getCustomerName());
CustomerPhone updatedCustomerPhone =
editOrderDescriptor.getCustomerPhone().orElse(orderToEdit.getCustomer().getCustomerPhone());
CustomerAddress updatedCustomerAddress =
editOrderDescriptor.getCustomerAddress().orElse(orderToEdit.getCustomer().getCustomerAddress());
Customer updatedCustomer = new Customer(updatedCustomerName, updatedCustomerPhone, updatedCustomerAddress);

return new Order(updatedOrderName, updatedOrderDeadline, updatedOrderStatus,
updatedOrderQuantity, updatedCustomer);

}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof EditOrderCommand)) {
return false;
}
EditOrderCommand e = (EditOrderCommand) other;
return index.equals(e.index)
&& editOrderDescriptor.equals(e.editOrderDescriptor);
}
}
43 changes: 43 additions & 0 deletions src/main/java/trackr/logic/commands/FindOrderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package trackr.logic.commands;

import static java.util.Objects.requireNonNull;

import trackr.commons.core.Messages;
import trackr.model.Model;
import trackr.model.order.OrderContainsKeywordsPredicate;

/**
* Finds and lists all order in order list whose description contains any of the argument keywords.
* Keyword matching is case insensitive.
*/
public class FindOrderCommand extends Command {
public static final String COMMAND_WORD = "find_order";
public static final String COMMAND_WORD_SHORTCUT = "find_o";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all orders whose description contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " Chocolate cookie";

private final OrderContainsKeywordsPredicate predicate;

public FindOrderCommand(OrderContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredOrderList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_ORDERS_LISTED_OVERVIEW, model.getFilteredOrderList().size()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof FindOrderCommand // instanceof handles nulls
&& predicate.equals(((FindOrderCommand) other).predicate)); // state check
}

}
1 change: 0 additions & 1 deletion src/main/java/trackr/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class CliSyntax {

public static final Prefix PREFIX_DEADLINE = new Prefix("d/");
public static final Prefix PREFIX_STATUS = new Prefix("s/");

public static final Prefix PREFIX_ORDERNAME = new Prefix("on/");
public static final Prefix PREFIX_ORDERQUANTITY = new Prefix("q/");

Expand Down
84 changes: 84 additions & 0 deletions src/main/java/trackr/logic/parser/EditOrderCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package trackr.logic.parser;


import static java.util.Objects.requireNonNull;
import static trackr.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static trackr.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static trackr.logic.parser.CliSyntax.PREFIX_DEADLINE;
import static trackr.logic.parser.CliSyntax.PREFIX_NAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERNAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERQUANTITY;
import static trackr.logic.parser.CliSyntax.PREFIX_PHONE;
import static trackr.logic.parser.CliSyntax.PREFIX_STATUS;

import trackr.commons.core.index.Index;
import trackr.logic.commands.EditOrderCommand;
import trackr.logic.parser.exceptions.ParseException;
import trackr.model.order.OrderDescriptor;

/**
* Parses input arguments and creates a new EditOrderCommand object
*/
public class EditOrderCommandParser implements Parser<EditOrderCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditOrderCommand
* and returns an EditOrderCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public EditOrderCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_ORDERNAME, PREFIX_DEADLINE,
PREFIX_STATUS, PREFIX_ORDERQUANTITY, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS);

Index index;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditOrderCommand.MESSAGE_USAGE), pe);
}

OrderDescriptor editOrderDescriptor = new OrderDescriptor();
if (argMultimap.getValue(PREFIX_ORDERNAME).isPresent()) {
editOrderDescriptor.setOrderName(
ParserUtil.parseOrderName(argMultimap.getValue(PREFIX_ORDERNAME).get()));
}
if (argMultimap.getValue(PREFIX_DEADLINE).isPresent()) {
editOrderDescriptor.setOrderDeadline(
ParserUtil.parseOrderDeadline(argMultimap.getValue(PREFIX_DEADLINE).get()));
}
if (argMultimap.getValue(PREFIX_STATUS).isPresent()) {
editOrderDescriptor.setOrderStatus(
ParserUtil.parseOrderStatus(argMultimap.getValue(PREFIX_STATUS)));
}

if (argMultimap.getValue(PREFIX_ORDERQUANTITY).isPresent()) {
editOrderDescriptor.setOrderQuantity(
ParserUtil.parseOrderQuantity(argMultimap.getValue(PREFIX_ORDERQUANTITY).get()));
}

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editOrderDescriptor.setCustomerName(
ParserUtil.parseCustomerName(argMultimap.getValue(PREFIX_NAME).get()));
}

if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
editOrderDescriptor.setCustomerPhone(
ParserUtil.parseCustomerPhone(argMultimap.getValue(PREFIX_PHONE).get()));
}

if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editOrderDescriptor.setCustomerAddress(
ParserUtil.parseCustomerAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}

if (!editOrderDescriptor.isAnyFieldNonNull()) {
throw new ParseException(EditOrderCommand.MESSAGE_NOT_EDITED);
}

return new EditOrderCommand(index, editOrderDescriptor);
}
}
76 changes: 76 additions & 0 deletions src/main/java/trackr/logic/parser/FindOrderCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package trackr.logic.parser;

import static java.util.Objects.requireNonNull;
import static trackr.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static trackr.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static trackr.logic.parser.CliSyntax.PREFIX_DEADLINE;
import static trackr.logic.parser.CliSyntax.PREFIX_NAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERNAME;
import static trackr.logic.parser.CliSyntax.PREFIX_ORDERQUANTITY;
import static trackr.logic.parser.CliSyntax.PREFIX_PHONE;
import static trackr.logic.parser.CliSyntax.PREFIX_STATUS;

import java.util.Arrays;

import trackr.logic.commands.FindOrderCommand;
import trackr.logic.parser.exceptions.ParseException;
import trackr.model.order.OrderContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindOrderCommand object
*/
public class FindOrderCommandParser implements Parser<FindOrderCommand> {
/**
* Parses the given {@code String} of arguments in the context of the FindOrderCommand
* and returns a FindOrderCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public FindOrderCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_ORDERNAME,
PREFIX_DEADLINE, PREFIX_STATUS, PREFIX_ORDERQUANTITY);

OrderContainsKeywordsPredicate predicate = new OrderContainsKeywordsPredicate();
if (argMultimap.getValue(PREFIX_ORDERNAME).isPresent()) {
String[] orderNameKeywords = argMultimap.getValue(PREFIX_ORDERNAME).get().trim().split("\\s+");
predicate.setOrderNameKeywords(Arrays.asList(orderNameKeywords));
}
if (argMultimap.getValue(PREFIX_DEADLINE).isPresent()) {
predicate.setOrderDeadline(
ParserUtil.parseOrderDeadline(argMultimap.getValue(PREFIX_DEADLINE).get()));
}
if (argMultimap.getValue(PREFIX_STATUS).isPresent()) {
predicate.setOrderStatus(
ParserUtil.parseOrderStatus(argMultimap.getValue(PREFIX_STATUS)));
}
if (argMultimap.getValue(PREFIX_ORDERQUANTITY).isPresent()) {
predicate.setOrderQuantity(
ParserUtil.parseOrderQuantity(argMultimap.getValue(PREFIX_ORDERQUANTITY).get()));
}
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
predicate.setCustomerName(
ParserUtil.parseCustomerName(argMultimap.getValue(PREFIX_NAME).get()));
}

if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
predicate.setCustomerPhone(
ParserUtil.parseCustomerPhone(argMultimap.getValue(PREFIX_PHONE).get()));
}

if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
predicate.setCustomerAddress(
ParserUtil.parseCustomerAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}


if (!predicate.isAnyFieldPresent()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindOrderCommand.MESSAGE_USAGE));
}

return new FindOrderCommand(predicate);
}

}
10 changes: 10 additions & 0 deletions src/main/java/trackr/logic/parser/TrackrParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import trackr.logic.commands.DeleteOrderCommand;
import trackr.logic.commands.DeleteSupplierCommand;
import trackr.logic.commands.DeleteTaskCommand;
import trackr.logic.commands.EditOrderCommand;
import trackr.logic.commands.EditSupplierCommand;
import trackr.logic.commands.EditTaskCommand;
import trackr.logic.commands.ExitCommand;
import trackr.logic.commands.FindOrderCommand;
import trackr.logic.commands.FindSupplierCommand;
import trackr.logic.commands.FindTaskCommand;
import trackr.logic.commands.HelpCommand;
Expand Down Expand Up @@ -77,6 +79,10 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteSupplierCommand.COMMAND_WORD:
case DeleteSupplierCommand.COMMAND_WORD_SHORTCUT:
return new DeleteSupplierCommandParser().parse(arguments);
case EditOrderCommand.COMMAND_WORD:
case EditOrderCommand.COMMAND_WORD_SHORTCUT:
return new EditOrderCommandParser().parse(arguments);


case DeleteTaskCommand.COMMAND_WORD:
case DeleteTaskCommand.COMMAND_WORD_SHORTCUT:
Expand Down Expand Up @@ -106,6 +112,10 @@ public Command parseCommand(String userInput) throws ParseException {
case FindTaskCommand.COMMAND_WORD_SHORTCUT:
return new FindTaskCommandParser().parse(arguments);

case FindOrderCommand.COMMAND_WORD:
case FindOrderCommand.COMMAND_WORD_SHORTCUT:
return new FindOrderCommandParser().parse(arguments);

case ListSupplierCommand.COMMAND_WORD:
case ListSupplierCommand.COMMAND_WORD_SHORTCUT:
return new ListSupplierCommand();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/trackr/model/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,4 @@ public boolean equals(Object other) {
public int hashCode() {
return orders.hashCode();
}

}
2 changes: 1 addition & 1 deletion src/main/java/trackr/model/order/OrderStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Represents an Order's status in the order list.
* Guaruntees: immutable; is valid as declared in {@link #isValidOrdeStatus(String)}
* Guaruntees: immutable; is valid as declared in {@link #isValidOrderStatus(String)}
*/
public class OrderStatus {

Expand Down
Loading

0 comments on commit e87e0a3

Please sign in to comment.