-
Notifications
You must be signed in to change notification settings - Fork 35
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
[W5][T11-2]Li Guanlong #34
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,26 @@ 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` | ||
|
||
== Updating a person: `update` | ||
|
||
Update an existing person in the address book. + | ||
Format: `update INDEX NEW_NAME [p]p/NEW_PHONE_NUMBER [p]e/NEW_EMAIL [p]a/NEW_ADDRESS [t/NEW_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 1 John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01` | ||
* `update 2 Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend` | ||
|
||
== Listing all persons : `list` | ||
|
||
Shows a list of all persons, along with their non-private details, in the address book. + | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.common.Messages; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.*; | ||
import seedu.addressbook.data.tag.Tag; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class UpdateCommand extends Command{ | ||
public static final String COMMAND_WORD = "update"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Update a contact\n" | ||
+ "Example: " + COMMAND_WORD + "1 Clive Li p/12345 e/[email protected] a/ABC DEF"; | ||
private final Person toAdd; | ||
public UpdateCommand(int targetVisibleIndex, | ||
String name, | ||
String phone, boolean isPhonePrivate, | ||
String email, boolean isEmailPrivate, | ||
String address, boolean isAddressPrivate, | ||
Set<String> tags) throws IllegalValueException { | ||
this.setTargetIndex(targetVisibleIndex); | ||
final Set<Tag> tagSet = new HashSet<>(); | ||
for (String tagName : tags) { | ||
tagSet.add(new Tag(tagName)); | ||
} | ||
this.toAdd = new Person( | ||
new Name(name), | ||
new Phone(phone, isPhonePrivate), | ||
new Email(email, isEmailPrivate), | ||
new Address(address, isAddressPrivate), | ||
tagSet | ||
); | ||
} | ||
public static final String MESSAGE_UPDATE_PERSON_SUCCESS = "Updated Person: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; | ||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to add blank lines to your code, it improves readability. |
||
public CommandResult execute() { | ||
try { | ||
final ReadOnlyPerson target = getTargetPerson(); | ||
addressBook.removePerson(target); | ||
addressBook.addPerson(toAdd); | ||
return new CommandResult(String.format(MESSAGE_UPDATE_PERSON_SUCCESS, target)); | ||
|
||
} catch (IndexOutOfBoundsException ie) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} catch (UniquePersonList.PersonNotFoundException pnfe) { | ||
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); | ||
} catch (UniquePersonList.DuplicatePersonException dpe) { | ||
return new CommandResult(MESSAGE_DUPLICATE_PERSON); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import seedu.addressbook.commands.ViewAllCommand; | ||
import seedu.addressbook.commands.ViewCommand; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.commands.UpdateCommand; | ||
|
||
/** | ||
* Parses user input. | ||
|
@@ -97,6 +98,9 @@ public Command parseCommand(String userInput) { | |
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
|
||
case UpdateCommand.COMMAND_WORD: | ||
return prepareUpdate(arguments); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
return new HelpCommand(); | ||
|
@@ -174,6 +178,44 @@ private Command prepareDelete(String args) { | |
} | ||
} | ||
|
||
private Command prepareUpdate(String args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing header comment. All non trivial methods should have javadoc format header comments. |
||
try{ | ||
String[] data = args.split(" "); | ||
String index = data[1]; | ||
String rest = ""; | ||
for(int i = 2; i < data.length; i++) | ||
{ | ||
rest = rest + data[i] + " "; | ||
} | ||
final int targetIndex = parseArgsAsDisplayedIndex(index); | ||
final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(rest.trim()); | ||
if (!matcher.matches()) { | ||
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using AddCommand's MESSAGE_USAGE, maybe try adding a separate "error message" to be more specific in the error handling process so when the system prints out the "message" you will be able to identify which exact part of your code it is running (instead of having the same message appear for 2 different situations). |
||
} | ||
return new UpdateCommand( | ||
targetIndex, | ||
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 (ParseException pe) { | ||
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE)); | ||
} catch (NumberFormatException nfe) { | ||
return new IncorrectCommand(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} catch (IllegalValueException ive) { | ||
return new IncorrectCommand(ive.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Parses arguments in the context of the view command. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Updates", remember to check for consistency in user guides because it is written by all your teammates, so there needs to be a uniform style of writing for the user guide to appear professional and neat. :)