-
Notifications
You must be signed in to change notification settings - Fork 141
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.11r][T09-B3] Kang Anmin #730
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.common.Messages; | ||
import seedu.addressbook.data.person.Person; | ||
import seedu.addressbook.data.person.UniquePersonList; | ||
|
||
|
||
/** | ||
* Changes the phone number of a person identified using it's last displayed index from the address book. | ||
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. Good job for writing the header comment. |
||
*/ | ||
public class ChangeNumCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "changenum"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Changes the phone number of the person identified by the index number used in the last person listing.\n" | ||
+ "Parameters: INDEX NEW_NUMBER\n" | ||
+ "Example: " + COMMAND_WORD + " 1" + " 99997777"; | ||
|
||
public static final String MESSAGE_CHANGE_NUMBER_PERSON_SUCCESS = "Changed Person's Number: %1$s"; | ||
|
||
private String newNum; | ||
|
||
|
||
public ChangeNumCommand(int targetVisibleIndex, String num) { | ||
|
||
super(targetVisibleIndex); | ||
newNum = num; | ||
} | ||
|
||
|
||
@Override | ||
public CommandResult execute() { | ||
try { | ||
final Person target = (Person)getTargetPerson(); | ||
target.setPhone(newNum); | ||
return new CommandResult(String.format(MESSAGE_CHANGE_NUMBER_PERSON_SUCCESS, target)); | ||
|
||
} catch (IndexOutOfBoundsException ie) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ public class HelpCommand extends Command { | |
|
||
public static final String MESSAGE_ALL_USAGES = AddCommand.MESSAGE_USAGE | ||
+ "\n" + DeleteCommand.MESSAGE_USAGE | ||
+ "\n" + ChangeNumCommand.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. Good job for updating the help command. |
||
+ "\n" + ClearCommand.MESSAGE_USAGE | ||
+ "\n" + FindCommand.MESSAGE_USAGE | ||
+ "\n" + ListCommand.MESSAGE_USAGE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,13 @@ public void setTags(UniqueTagList replacement) { | |
tags.setTags(replacement); | ||
} | ||
|
||
/** | ||
* Updates the person's phone number to the one in the argument. | ||
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. For set and get methods, it's okay to remove the header comment. |
||
*/ | ||
public void setPhone(String num) { | ||
this.phone.setNum(num); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ public class Phone { | |
public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers"; | ||
public static final String PHONE_VALIDATION_REGEX = "\\d+"; | ||
|
||
public final String value; | ||
public String value; | ||
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. Can it be private? |
||
private boolean isPrivate; | ||
|
||
/** | ||
|
@@ -41,6 +41,10 @@ public String toString() { | |
return value; | ||
} | ||
|
||
public void setNum(String num) { | ||
this.value = num; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
import java.util.regex.Pattern; | ||
|
||
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; | ||
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PHONE_NUMBER; | ||
|
||
/** | ||
* Parses user input. | ||
|
@@ -26,6 +28,8 @@ public class Parser { | |
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)" | ||
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags | ||
|
||
public static final Pattern CHANGE_NUMBER_FORMAT = | ||
Pattern.compile("(?<index>.+)" + " (?<phone>[^/]+)"); | ||
|
||
/** | ||
* Signals that the user input could not be parsed. | ||
|
@@ -47,7 +51,7 @@ public static class ParseException extends Exception { | |
* @param userInput full user input string | ||
* @return the command based on the user input | ||
*/ | ||
public Command parseCommand(String userInput) { | ||
public Command parseCommand(String userInput) throws IllegalValueException { | ||
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); | ||
if (!matcher.matches()) { | ||
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); | ||
|
@@ -62,6 +66,9 @@ public Command parseCommand(String userInput) { | |
|
||
case DeleteCommand.COMMAND_WORD: | ||
return prepareDelete(arguments); | ||
|
||
case ChangeNumCommand.COMMAND_WORD: | ||
return prepareChangeNum(arguments); | ||
|
||
case ClearCommand.COMMAND_WORD: | ||
return new ClearCommand(); | ||
|
@@ -156,6 +163,43 @@ private Command prepareDelete(String args) { | |
} | ||
} | ||
|
||
/** | ||
* Checks whether this string only contains digits. | ||
* | ||
* @param str a string | ||
*/ | ||
private void checkIsPureDigitString (String str) throws IllegalValueException { | ||
if (!str.matches("[0-9]+")) { | ||
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. Good job for validating the string. |
||
throw new IllegalValueException("Person phone numbers should only contain numbers"); | ||
} | ||
} | ||
|
||
/** | ||
* Parses arguments in the context of the change num person command. | ||
* | ||
* @param args full command args string | ||
* @return the prepared command | ||
*/ | ||
private Command prepareChangeNum(String args) throws IllegalValueException { | ||
final Matcher matcher = CHANGE_NUMBER_FORMAT.matcher(args.trim()); | ||
|
||
// Validate arg string format | ||
if (!matcher.matches()) { | ||
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); | ||
} | ||
String delims = "[ ]+"; | ||
String[] tokens = args.trim().split(delims); | ||
|
||
try { | ||
checkIsPureDigitString(tokens[1]); | ||
return new ChangeNumCommand(Integer.parseInt(tokens[0]), tokens[1]); | ||
} catch (IllegalValueException ive) { | ||
return new IncorrectCommand(MESSAGE_INVALID_PHONE_NUMBER); | ||
} catch (NumberFormatException nfe) { | ||
return new IncorrectCommand(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
} | ||
|
||
/** | ||
* Parses arguments in the context of the view command. | ||
* | ||
|
@@ -227,4 +271,4 @@ private Command prepareFind(String args) { | |
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,6 +391,65 @@ public void execute_delete_missingInAddressBook() throws Exception { | |
threePersons); | ||
} | ||
|
||
@Test | ||
public void execute_changenum_invalidArgsFormat() throws Exception { | ||
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ChangeNumCommand.MESSAGE_USAGE); | ||
assertCommandBehavior("changenum ", expectedMessage); | ||
assertCommandBehavior("changenum arg not number", expectedMessage); | ||
} | ||
|
||
@Test | ||
public void execute_changenum_invalidIndex() throws Exception { | ||
assertInvalidIndexBehaviorForCommand("changenum"); | ||
} | ||
|
||
@Test | ||
public void execute_changenum_changesSuccessfully() throws Exception { | ||
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. Good job for testing the new command. |
||
TestDataHelper helper = new TestDataHelper(); | ||
Person p1 = helper.generatePerson(1, false); | ||
Person p2 = helper.adam(); | ||
Person p3 = helper.generatePerson(3, true); | ||
Person p4 = helper.adam(); | ||
|
||
List<Person> threePersons = helper.generatePersonList(p1, p2, p3); | ||
|
||
p4.setPhone("99998888"); | ||
List<Person> updatedThreePersons = helper.generatePersonList(p1, p4, p3); | ||
|
||
AddressBook expectedAB = helper.generateAddressBook(updatedThreePersons); | ||
|
||
helper.addToAddressBook(addressBook, threePersons); | ||
logic.setLastShownList(threePersons); | ||
|
||
assertCommandBehavior("changenum 2 99998888", | ||
String.format(ChangeNumCommand.MESSAGE_CHANGE_NUMBER_PERSON_SUCCESS, p4), | ||
expectedAB, | ||
false, | ||
updatedThreePersons); | ||
} | ||
|
||
@Test | ||
public void execute_changenum_invalid_index() throws Exception { | ||
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. Coding standard violation for the method name.. |
||
|
||
TestDataHelper helper = new TestDataHelper(); | ||
Person p1 = helper.generatePerson(1, false); | ||
Person p2 = helper.generatePerson(2, true); | ||
Person p3 = helper.generatePerson(3, true); | ||
|
||
List<Person> threePersons = helper.generatePersonList(p1, p2, p3); | ||
|
||
AddressBook expectedAB = helper.generateAddressBook(threePersons); | ||
|
||
helper.addToAddressBook(addressBook, threePersons); | ||
logic.setLastShownList(threePersons); | ||
|
||
assertCommandBehavior("changenum 4 99998888", | ||
Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, | ||
expectedAB, | ||
false, | ||
threePersons); | ||
} | ||
|
||
@Test | ||
public void execute_find_invalidArgsFormat() throws Exception { | ||
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE); | ||
|
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.
Good job for updating the UserGuide.