Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update add command to MVP specifications #73

Merged
merged 19 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ SocialBook is a **desktop app for managing contacts, optimized for use via a Li
e.g. in `add n/NAME`, `NAME` is a parameter which can be used as `add n/John Doe`.

* Items in square brackets are optional.<br>
e.g `n/NAME [t/TAG]` can be used as `n/John Doe t/friend` or as `n/John Doe`.
e.g `n/NAME [t/TAG]` can be used as `n/John Doe t/friend` or as `n/John Doe`.<br>
e.g `n/NAME [a/ADDRESS]` can be used as `n/Jane Smith a/123 Hollywood Street 55` or as `n/Jane Smith`.

* Items with `…`​ after them can be used multiple times including zero times.<br>
e.g. `[t/TAG]…​` can be used as ` ` (i.e. 0 times), `t/friend`, `t/friend t/family` etc.
Expand All @@ -79,16 +80,23 @@ Format: `help`

Adds a person to the address book.

Format: `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​ d/DATE_OF_LAST_VISIT`
Format: `add n/NAME p/PHONE_NUMBER [e/EMAIL] [a/ADDRESS] [t/TAG]…​ d/DATE_OF_LAST_VISIT`

<box type="tip" seamless>

**Tip:** A person can have any number of tags (including 0)
**Tip:** A person can have any number of tags (including 0).<br>

</box>
<box type="tip" seamless>

**Tip:** The only required fields for a person are a name, a phone number, and a date of last visit, so you can create a contact with just those 3 fields. Providing an email, address, or tags is optional.

</box>

Examples:
* `add n/John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01 d/02-01-2024`
* `add n/Betsy Crowe t/friend e/[email protected] a/Newgate Prison p/12345678 t/criminal d/03-28-2024`
* `add p/12345678 n/Jane Smith d/01-01-2024`

### Listing all persons : `list`

Expand Down Expand Up @@ -210,7 +218,7 @@ _Details coming soon ..._

Action | Format, Examples
-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------
**Add** | `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​ d/DATE_OF_LAST_VISIT` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague d/07-23-2024`
**Add** | `add n/NAME p/PHONE_NUMBER [e/EMAIL] [a/ADDRESS] [t/TAG]…​ d/DATE_OF_LAST_VISIT` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague d/07-23-2024`
**Clear** | `clear`
**Delete** | `delete INDEX`<br> e.g., `delete 3`
**Edit** | `edit INDEX [n/NAME] [p/PHONE_NUMBER] [e/EMAIL] [a/ADDRESS] [t/TAG]…​ [d/DATE_OF_LAST_VISIT]`<br> e.g.,`edit 2 n/James Lee e/[email protected]`
Expand Down
49 changes: 38 additions & 11 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,45 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
*/
public static String format(Person person) {
final StringBuilder builder = new StringBuilder();
builder.append(person.getName())
.append("; Phone: ")
.append(person.getPhone())
.append("; Email: ")
.append(person.getEmail())
.append("; Address: ")
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append("; Last visit: ")
.append(person.getDateOfLastVisit());
addName(builder, person);
addPhone(builder, person);
addEmail(builder, person);
addAddress(builder, person);
addTags(builder, person);
addDateOfLastVisit(builder, person);

return builder.toString();
}

private static void addName(StringBuilder sb, Person person) {
sb.append(person.getName());
}

private static void addPhone(StringBuilder sb, Person person) {
sb.append("; Phone: ").append(person.getPhone());
}

private static void addDateOfLastVisit(StringBuilder sb, Person person) {
sb.append("; Last visit: ").append(person.getDateOfLastVisit());
}

private static void addEmail(StringBuilder sb, Person person) {
if (!person.hasEmail()) {
return;
}
sb.append("; Email: ").append(person.getEmail().get());
}

private static void addAddress(StringBuilder sb, Person person) {
if (!person.hasAddress()) {
return;
}
sb.append("; Address: ").append(person.getAddress().get());
}

private static void addTags(StringBuilder sb, Person person) {
sb.append("; Tags: ");
person.getTags().forEach(sb::append);
}

}
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class AddCommand extends Command {
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_TAG + "TAG]... "
+ PREFIX_DATEOFLASTVISIT + "DATEOFLASTVISIT \n"
+ "Example: " + COMMAND_WORD + " "
Expand All @@ -38,7 +38,7 @@ public class AddCommand extends Command {
+ PREFIX_TAG + "owesMoney "
+ PREFIX_DATEOFLASTVISIT + "01-03-2024";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_SUCCESS = "Person successfully added. New person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Person toAdd;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Optional<Email> updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Optional<Address> updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
DateOfLastVisit dateOfLastVisit = editPersonDescriptor.getDateOfLastVisit()
.orElse(personToEdit.getDateOfLastVisit());
Expand Down Expand Up @@ -140,8 +140,8 @@ public String toString() {
public static class EditPersonDescriptor {
private Name name;
private Phone phone;
private Email email;
private Address address;
private Optional<Email> email;
private Optional<Address> address;
private Set<Tag> tags;
private DateOfLastVisit dateOfLastVisit;

Expand Down Expand Up @@ -183,19 +183,19 @@ public Optional<Phone> getPhone() {
return Optional.ofNullable(phone);
}

public void setEmail(Email email) {
public void setEmail(Optional<Email> email) {
this.email = email;
}

public Optional<Email> getEmail() {
public Optional<Optional<Email>> getEmail() {
return Optional.ofNullable(email);
}

public void setAddress(Address address) {
public void setAddress(Optional<Address> address) {
this.address = address;
}

public Optional<Address> getAddress() {
public Optional<Optional<Address>> getAddress() {
return Optional.ofNullable(address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -36,8 +37,7 @@ public AddCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_TAG, PREFIX_DATEOFLASTVISIT);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_DATEOFLASTVISIT)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_DATEOFLASTVISIT)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
Expand All @@ -46,8 +46,8 @@ public AddCommand parse(String args) throws ParseException {
PREFIX_ADDRESS, PREFIX_DATEOFLASTVISIT);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Optional<Email> email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL));
Optional<Address> address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
DateOfLastVisit dateOfLastVisit = ParserUtil
.parseDateOfLastVisit(argMultimap.getValue(PREFIX_DATEOFLASTVISIT).get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public EditCommand parse(String args) throws ParseException {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL)));
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS)));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
if (argMultimap.getValue(PREFIX_DATEOFLASTVISIT).isPresent()) {
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
Expand Down Expand Up @@ -67,33 +68,44 @@ public static Phone parsePhone(String phone) throws ParseException {
}

/**
* Parses a {@code String address} into an {@code Address}.
* Parses a {@code Optional<String> address} into an {@code Optional<Address>}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code address} is invalid.
*/
public static Address parseAddress(String address) throws ParseException {
public static Optional<Address> parseAddress(Optional<String> address) throws ParseException {
requireNonNull(address);
String trimmedAddress = address.trim();

if (address.isEmpty()) {
// if an address prefix was never entered by the user
return Optional.empty();
}

String trimmedAddress = address.get().trim();
if (!Address.isValidAddress(trimmedAddress)) {
throw new ParseException(Address.MESSAGE_CONSTRAINTS);
}
return new Address(trimmedAddress);
return Optional.of(new Address(trimmedAddress));
}

/**
* Parses a {@code String email} into an {@code Email}.
* Parses a {@code Optional<String> email} into an {@code Optional<Email>}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
public static Optional<Email> parseEmail(Optional<String> email) throws ParseException {
requireNonNull(email);
String trimmedEmail = email.trim();

if (email.isEmpty()) {
// if an email prefix was never entered by the user
return Optional.empty();
}
String trimmedEmail = email.get().trim();
if (!Email.isValidEmail(trimmedEmail)) {
throw new ParseException(Email.MESSAGE_CONSTRAINTS);
}
return new Email(trimmedEmail);
return Optional.of(new Email(trimmedEmail));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public AddressContainsKeywordsPredicate(List<String> keywords) {

@Override
public boolean test(Person person) {
// Returns false if the person does not have an address.
if (!person.hasAddress()) {
return false;
}

// Checks if the string i.e (address) contains a keyword, allowing partial matching of address via find command
return keywords.stream()
.anyMatch(keyword -> person.getAddress().value.toLowerCase().contains(keyword.toLowerCase()));
.anyMatch(keyword -> person.getAddress().get().value.toLowerCase().contains(keyword.toLowerCase()));
}

@Override
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -19,19 +20,19 @@ public class Person {
// Identity fields
private final Name name;
private final Phone phone;
private final Email email;

// Data fields
private final Address address;
private final Optional<Address> address;
private final Optional<Email> email;
private final Set<Tag> tags = new HashSet<>();
private final DateOfLastVisit dateOfLastVisit;

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address,
public Person(Name name, Phone phone, Optional<Email> email, Optional<Address> address,
Set<Tag> tags, DateOfLastVisit dateOfLastVisit) {
requireAllNonNull(name, phone, email, address, tags);
requireAllNonNull(name, phone, email, address, tags, dateOfLastVisit);
this.name = name;
this.phone = phone;
this.email = email;
Expand All @@ -48,11 +49,25 @@ public Phone getPhone() {
return phone;
}

public Email getEmail() {
/**
* Returns whether the Person has an email.
*/
public boolean hasEmail() {
return email.isPresent();
}

public Optional<Email> getEmail() {
return email;
}

public Address getAddress() {
/**
* Returns whether the Person has an address.
*/
public boolean hasAddress() {
return address.isPresent();
}

public Optional<Address> getAddress() {
return address;
}

Expand Down
32 changes: 17 additions & 15 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model.util;

import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -20,23 +21,24 @@
public class SampleDataUtil {
public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends"), new DateOfLastVisit("01-01-2024")),
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends"), new DateOfLastVisit("02-02-2024")),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours"), new DateOfLastVisit("03-03-2024")),
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
new Person(new Name("Alex Yeoh"), new Phone("87438807"), Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 30 Geylang Street 29, #06-40")), getTagSet("friends"),
new DateOfLastVisit("01-01-2024")),
new Person(new Name("Bernice Yu"), new Phone("99272758"), Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18")),
getTagSet("colleagues", "friends"), new DateOfLastVisit("02-02-2024")),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"),
Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 11 Ang Mo Kio Street 74, #11-04")),
getTagSet("neighbours"), new DateOfLastVisit("03-03-2024")),
new Person(new Name("David Li"), new Phone("91031282"), Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 436 Serangoon Gardens Street 26, #16-43")),
getTagSet("family"), new DateOfLastVisit("04-04-2024")),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 47 Tampines Street 20, #17-35")),
getTagSet("classmates"), new DateOfLastVisit("05-05-2024")),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), Optional.of(new Email("[email protected]")),
Optional.of(new Address("Blk 45 Aljunied Street 85, #11-31")),
getTagSet("colleagues"), new DateOfLastVisit("06-06-2024"))
};
}
Expand Down
Loading