Skip to content

Commit

Permalink
feat: update branch with addorder function
Browse files Browse the repository at this point in the history
  • Loading branch information
gavingoh99 committed Mar 14, 2024
2 parents a98b06b + 3ce7669 commit ddd12b9
Show file tree
Hide file tree
Showing 27 changed files with 922 additions and 28 deletions.
40 changes: 31 additions & 9 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ Examples:
* `find testing123 n/John` returns an error message as there should not be
prefixes before the first 'FIELD' and 'KEYWORD' pair

### Adding an order : `addorder`

Adds an order to a supplier.

Format: `addorder INDEX d/DATE r/REMARK`

* Adds an order to the supplier at the specified `INDEX`. The index refers to the index number shown in the displayed supplier list. The index **must be a positive integer, starting from 1** (1, 2, 3, …​)
* The date must be in the format `YYYY-MM-DD`. For example, `2020-12-31`.

<box type="tip" seamless>

**Note:** A person can have any number of orders (including 0)
</box>

Examples:
* `addorder 1 d/2020-01-01 r/100 chicken wings`
* `addorder 2 r/ 100 chicken wings d/ 2020-12-31`
* `addorder 3 d/2020-01-01 r/100 chicken wings`
* `addorder d/2020-01-01 r/100 chicken wings` returns an error as the index is not specified
* `addorder r/` or `addorder d/` or `addorder r/ d/` returns an error message as the 'KEYWORD' field cannot be empty

### Deleting a person : `delete`

Deletes the specified person from the address book.
Expand Down Expand Up @@ -253,13 +274,14 @@ _Details coming soon ..._

## Command summary

Action | Format, Examples
-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------
**Add** | `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague`
**Clear** | `clear`
**Delete** | `delete INDEX`<br> e.g., `delete 3`
**Edit** | `edit INDEX [n/NAME] [p/PHONE_NUMBER] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`<br> e.g.,`edit 2 n/James Lee e/[email protected]`
| Action | Format, Examples |
|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Add** | `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague` |
| **Clear** | `clear` |
| **Delete** | `delete INDEX`<br> e.g., `delete 3` |
| **Edit** | `edit INDEX [n/NAME] [p/PHONE_NUMBER] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`<br> e.g.,`edit 2 n/James Lee e/[email protected]` |
| **Find** | `find KEYWORD/ [KEYWORD]`<br> e.g., `find n/ James n/ T t/ friend t/ rich` |
| **Add Order** | `addorder INDEX d/DATE r/REMARK`<br> e.g., `addorder 1 d/ 2020-01-01 r/ 100 chicken wings` |
**Add Favourite** | `addfav [i/INDICES]`
**Find** | `find KEYWORD/ [KEYWORD]`<br> e.g., `find n/ James n/ T t/ friend t/ rich`
**List** | `list`
**Help** | `help`
| **List** | `list` |
| **Help** | `help` |
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public static String format(Person person) {
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append("; Orders: ");
person.getOrders().forEach(builder::append);
return builder.toString();
}

Expand Down
92 changes: 92 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,92 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.order.Date;
import seedu.address.model.person.Person;

/**
* Lists all persons in the address book to the user.
*/
public class AddOrderCommand extends Command {
public static final String COMMAND_WORD = "addorder";

public static final String MESSAGE_SUCCESS = "New order added: %1$s";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds an order to the person identified "
+ "by the index number used in the last person listing.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "d/ [DATE] r/ [REMARK] \n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "d/ 2024-01-01 r/ 100 chicken wings.";

private final Index index;
private final Date arrivalDate;
private final String remark;

/**
* Creates an AddOrderCommand to add the specified {@code Person}
*/
public AddOrderCommand(Index index, Date arrivalDate, String remark) {
requireNonNull(index);
requireNonNull(arrivalDate);
requireNonNull(remark);

this.index = index;
this.arrivalDate = arrivalDate;
this.remark = remark;
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

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

Person person = lastShownList.get(index.getZeroBased());

// TODO
person.addOrder(this.arrivalDate, this.remark);

model.setPerson(person, person);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(person)));
}

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

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

AddOrderCommand otherAddOrderCommand = (AddOrderCommand) other;
return index.equals(otherAddOrderCommand.index)
&& arrivalDate.equals(otherAddOrderCommand.arrivalDate)
&& remark.equals(otherAddOrderCommand.remark);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("arrivalDate", arrivalDate)
.add("remark", remark)
.toString();
}
}
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -21,6 +22,7 @@
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.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -106,8 +108,10 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
ArrayList<Order> updatedOrders = personToEdit.getOrders();

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags,
updatedOrders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddOrderCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.order.Date;

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

/**
* Parses the given {@code String} of arguments in the context of the AddOrderCommand
* and returns an AddOrderCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddOrderCommand parse(String args) throws ParseException {
requireNonNull(args);

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DATE, PREFIX_REMARK);

if (!argMultimap.arePrefixesPresent(PREFIX_DATE, PREFIX_REMARK)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddOrderCommand.MESSAGE_USAGE));
}

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

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_DATE, PREFIX_REMARK);

Date arrivalDate = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());

String remark = argMultimap.getValue(PREFIX_REMARK).get();

return new AddOrderCommand(index, arrivalDate, remark);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
<<<<<<< HEAD
import seedu.address.logic.commands.AddFavouriteCommand;
=======
import seedu.address.logic.commands.AddOrderCommand;
>>>>>>> master
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -72,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddFavouriteCommand.COMMAND_WORD:
return new AddFavouriteCommandParser().parse(arguments);

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

case ListCommand.COMMAND_WORD:
return new ListCommand();

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/logic/parser/ArgumentMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import seedu.address.logic.Messages;
import seedu.address.logic.parser.exceptions.ParseException;



/**
* Stores mapping of prefixes to their respective arguments.
* Each key may be associated with multiple argument values.
Expand Down Expand Up @@ -109,4 +107,12 @@ private void checkValuesAlpha(List<String> values, Prefix prefix) throws ParseEx
throw new ParseException(String.format(Messages.MESSAGE_ALPHABET_ONLY, prefix.getPrefix()));
}
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
public boolean arePrefixesPresent(Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> getValue(prefix).isPresent());
}
}
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_INDICES = new Prefix("i/");
public static final Prefix PREFIX_DATE = new Prefix("d/");
public static final Prefix PREFIX_REMARK = new Prefix("r/");

}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.order.Date;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -95,6 +96,21 @@ public static Email parseEmail(String email) throws ParseException {
return new Email(trimmedEmail);
}

/**
* Parses a {@code String date} into an {@code Date}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code date} is invalid.
*/
public static Date parseDate(String date) throws ParseException {
requireNonNull(date);
String trimmedDate = date.trim();
if (!Date.isValidDate(trimmedDate)) {
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}
return new Date(trimmedDate);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/seedu/address/model/order/Date.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.address.model.order;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Represents a date of an order in the address book.
*/
public class Date {

public static final String MESSAGE_CONSTRAINTS = "Date format should be YYYY-MM-DD, and it should not be blank";

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

public final LocalDate date;

/**
* Constructs an {@code Date}.
*
* @param date a valid date string.
*/
public Date(String date) {
requireNonNull(date);
checkArgument(isValidDate(date), MESSAGE_CONSTRAINTS);
this.date = LocalDate.parse(date, DATE_TIME_FORMATTER);
}

/**
* Returns true if a given string is a valid date.
*/
public static boolean isValidDate(String test) {
try {
LocalDate.parse(test, DATE_TIME_FORMATTER);
return true;
} catch (DateTimeParseException e) {
return false;
}
}

@Override
public String toString() {
return date.toString();
}

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

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

Date otherDate = (Date) other;
return date.equals(otherDate.date);
}

@Override
public int hashCode() {
return date.hashCode();
}

}
Loading

0 comments on commit ddd12b9

Please sign in to comment.