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

[W5.11r][T09-B3] Kang Anmin #730

Open
wants to merge 2 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
15 changes: 15 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ Examples:
`delete 1`<br>
Deletes the 1st person in the results of the `find` command.

### Changing a person's phone number : `changenum`
Changes the specified person's phone number from the address book. Irreversible.<br>
Format: `changenum INDEX NEW_NUMBER`

> Changes the person's phone number at the specified `INDEX`.
The index refers to the index number shown in the most recent listing.

Examples:
* `list`<br>
`changenum 2 99997777`<br>
Changes the phone number of the 2nd person in the address book to 99997777.
* `find Betsy`<br>
`changenum 1 88886666`<br>
Changes the phone number of the 1st person in the results of the `find` command to 88886666.

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.


## View non-private details of a person : `view`
Displays the non-private details of the specified person.<br>
Format: `view INDEX`
Expand Down
44 changes: 44 additions & 0 deletions src/seedu/addressbook/commands/ChangeNumCommand.java
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.

Choose a reason for hiding this comment

The 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);
}
}

}
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 help command.

+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Messages {

public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_PHONE_NUMBER = "Person phone numbers should only contain numbers";
public static final String MESSAGE_PERSON_NOT_IN_ADDRESSBOOK = "Person could not be found in address book";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE = "Launch command format: " +
Expand Down
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void setTags(UniqueTagList replacement) {
tags.setTags(replacement);
}

/**
* Updates the person's phone number to the one in the argument.

Choose a reason for hiding this comment

The 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
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/data/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be private?

private boolean isPrivate;

/**
Expand Down Expand Up @@ -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
Expand Down
48 changes: 46 additions & 2 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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));
Expand All @@ -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();
Expand Down Expand Up @@ -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]+")) {

Choose a reason for hiding this comment

The 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.
*
Expand Down Expand Up @@ -227,4 +271,4 @@ private Command prepareFind(String args) {
}


}
}
59 changes: 59 additions & 0 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Choose a reason for hiding this comment

The 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 {

Choose a reason for hiding this comment

The 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);
Expand Down
Loading