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 feature #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 16 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ Examples:
* `add John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01`
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend`

### Adding a person: `update`
Updates a person's info in the address book<br>
Format: `update NAME [p]p/PHONE_NUMBER [p]e/EMAIL [p]a/ADDRESS [t/TAG]...`

> Words in `UPPER_CASE` are the parameters, items in `SQUARE_BRACKETS` are optional,
> items with `...` after them can have multiple instances. Order of parameters are fixed.
>
> Put a `p` before the phone / email / address prefixes to mark it as `private`. `private` details can only
> be seen using the `viewall` command.
>
> Persons can have any number of tags (including 0)

Examples:
* `Update John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01`
* `Update Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend`

### Listing all persons : `list`
Shows a list of all persons in the address book.<br>
Format: `list`
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public HelpCommand() {}
public CommandResult execute() {
return new CommandResult(
AddCommand.MESSAGE_USAGE
+ "\n" + UpdateCommand.MESSAGE_USAGE
+ "\n" + DeleteCommand.MESSAGE_USAGE
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
Expand Down
77 changes: 77 additions & 0 deletions src/seedu/addressbook/commands/UpdateCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package seedu.addressbook.commands;

import java.util.HashSet;
import java.util.Set;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

/**
* Update a person's info in the address book.
*/
public class UpdateCommand extends Command {

public static final String COMMAND_WORD = "update";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": updates a person's info in the address book. "
+ "Contact details can be marked private by prepending 'p' to the prefix.\n"
+ "Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...\n"
+ "Example: " + COMMAND_WORD
+ " John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney";

public static final String MESSAGE_SUCCESS = "person info updated: %1$s";
public static final String MESSAGE_NONEXIST_PERSON = "This person does not exist in the AddressBook";

private final Person toUpdate;

/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
*/
public UpdateCommand(String name,
String phone, boolean isPhonePrivate,
String email, boolean isEmailPrivate,
String address, boolean isAddressPrivate,
Set<String> tags) throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
this.toUpdate = new Person(
new Name(name),
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
new UniqueTagList(tagSet)
);
}

public UpdateCommand(Person toUpdate) {
this.toUpdate = toUpdate;
}

public ReadOnlyPerson getPerson() {
return toUpdate;
}

@Override
public CommandResult execute() {
try {
addressBook.updatePerson(toUpdate);
return new CommandResult(String.format(MESSAGE_SUCCESS, toUpdate));
} catch (AddressBook.PersonNonExistException pnee) {
return new CommandResult(MESSAGE_NONEXIST_PERSON);
}
}

}
40 changes: 40 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public AddressBook() {
allTags = new UniqueTagList();
}

/**
* Signals that an operation that try's to update a person who does not exist in the list
*/
public static class PersonNonExistException extends Exception {
public PersonNonExistException() {
super("Person does not exist in AddressBook");
}
}


/**
* Constructs an address book with the given data.
* Also updates the tag list with any missing tags found in any person.
Expand Down Expand Up @@ -83,6 +93,36 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
syncTagsWithMasterList(toAdd);
}


/**
* Updates a person's info in the address book.
* Also checks the new person's tags and updates {@link #allTags} with any new tags found,
* and updates the Tag objects in the person to point to those in {@link #allTags}.
*
* @throws PersonNonExistException if the person does not exist in the addressbook.
*/
public void updatePerson(Person toUpdate) throws PersonNonExistException{

boolean isPersonExist = false;

for (Person each : allPersons) {
if (each.getName().equals(toUpdate.getName())) {
each.setAddress(toUpdate.getAddress());
each.setEmail(toUpdate.getEmail());
each.setPhone(toUpdate.getPhone());
each.setTags(toUpdate.getTags());
isPersonExist = true;
}
}

if (!isPersonExist) {
throw new PersonNonExistException();
}

syncTagsWithMasterList(toUpdate);

}

/**
* Returns true if an equivalent person exists in the address book.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public Person(ReadOnlyPerson source) {
this(source.getName(), source.getPhone(), source.getEmail(), source.getAddress(), source.getTags());
}

public void setPhone(Phone phone) {
this.phone = phone;
}

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

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


@Override
public Name getName() {
return name;
Expand Down
47 changes: 36 additions & 11 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.addressbook.commands.AddCommand;
import seedu.addressbook.commands.ClearCommand;
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.DeleteCommand;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.commands.FindCommand;
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.commands.*;
import seedu.addressbook.data.exception.IllegalValueException;

/**
Expand Down Expand Up @@ -78,6 +68,9 @@ public Command parseCommand(String userInput) {
case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);

case UpdateCommand.COMMAND_WORD:
return prepareUpdate(arguments);

case DeleteCommand.COMMAND_WORD:
return prepareDelete(arguments);

Expand Down Expand Up @@ -137,6 +130,38 @@ private Command prepareAdd(String args) {
}
}

/**
* Parses arguments in the context of the update person command.
*
* @param args full command args string
* @return the prepared command
*/
private Command prepareUpdate(String args) {
final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(args.trim());
// Validate arg string format
if (!matcher.matches()) { ;
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UpdateCommand.MESSAGE_USAGE));
}
try {
return new UpdateCommand(
matcher.group("name"),

matcher.group("phone"),
isPrivatePrefixPresent(matcher.group("isPhonePrivate")),

matcher.group("email"),
isPrivatePrefixPresent(matcher.group("isEmailPrivate")),

matcher.group("address"),
isPrivatePrefixPresent(matcher.group("isAddressPrivate")),

getTagsFromArgs(matcher.group("tagArguments"))
);
} catch (IllegalValueException ive) {
return new IncorrectCommand(ive.getMessage());
}
}

/**
* Returns true if the private prefix is present for a contact detail in the add command's arguments string.
*/
Expand Down
Loading