forked from nus-cs2103-AY2324S2/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.
Add addOrderCommandParser class Add findPersonByPhoneNumber method to ModelManager Add JsonAdaptedOrder Add basic Ui implementation of order
- Loading branch information
1 parent
2a02b8f
commit b8916e1
Showing
13 changed files
with
274 additions
and
25 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
88 changes: 88 additions & 0 deletions
88
src/main/java/seedu/address/logic/commands/AddOrderCommand.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,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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/parser/AddOrderCommandParser.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,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); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.