forked from nus-cs2103-AY2223S2/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.
Implement Find Order Command and Edit Order Command (#134)
- Loading branch information
Showing
19 changed files
with
1,194 additions
and
11 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
130 changes: 130 additions & 0 deletions
130
src/main/java/trackr/logic/commands/EditOrderCommand.java
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,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); | ||
} | ||
} |
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,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 | ||
} | ||
|
||
} |
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
84 changes: 84 additions & 0 deletions
84
src/main/java/trackr/logic/parser/EditOrderCommandParser.java
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,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
76
src/main/java/trackr/logic/parser/FindOrderCommandParser.java
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,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); | ||
} | ||
|
||
} |
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
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 |
---|---|---|
|
@@ -117,5 +117,4 @@ public boolean equals(Object other) { | |
public int hashCode() { | ||
return orders.hashCode(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.