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][W15-B3] Pham Vu Hung #775

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
55 changes: 55 additions & 0 deletions doc/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,61 @@ Use case ends.

> 3a1. AddressBook shows an error message <br>
Use case resumes at step 2

#### Use case: Delete person

**MSS**

1. User requests to list persons
2. AddressBook shows a list of persons
3. User requests to delete a specific person in the list
4. AddressBook deletes the person <br>
Use case ends.

**Extensions**

2a. The list is empty

> Use case ends

3a. The given index is invalid

> 3a1. AddressBook shows an error message <br>
Use case resumes at step 2

#### Use case: Edit person

**MSS**

1. User requests to list persons
2. AddressBook shows a list of persons
3. User requests to edit a specific person in the list
4. AddressBook edits the person <br>
Use case ends.

**Extensions**

2a. The list is empty

> Use case ends

3a. The given index is invalid

> 3a1. AddressBook shows an error message <br>
Use case resumes at step 2

4a. The given data has incorrect format

> 4a1. AddressBook shows an error message <br>
Use case resumes at step 2

#### Use case: Sort all persons

**MSS**

1. User requests to sort persons
2. AddressBook sort the list of all persons and display it
Use case ends.

## Appendix C : Non Functional Requirements

Expand Down
19 changes: 19 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,29 @@ 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`

## Editing a person : `edit`
Edits the specified person from the address book.<br>
Format: `edit INDEX [p]p/PHONE_NUMBER [p]e/EMAIL [p]a/ADDRESS [t/TAG]...`

> Edits the person at the specified `INDEX`.
The index refers to the index number shown in the most recent listing.

Examples:
* `list`<br>
`edit 2 pp/999 pe/[email protected] pa/somewhere t/secretive`<br>
Edits the 2nd person in the address book.
* `find Betsy`<br>
`edit 1 pp/999 pe/[email protected] pa/somewhere t/secretive`<br>
Edits the 1st person in the results of the `find` command.

## Listing all persons : `list`
Shows a list of all persons in the address book.<br>
Format: `list`

## Sorting all persons: `sort`
Sorts the address book by name in ascending order.<br>
Format: `sort`

## Finding all persons containing any keyword in their name: `find`
Finds persons whose names contain any of the given keywords.<br>
Format: `find KEYWORD [MORE_KEYWORDS]`
Expand Down
75 changes: 75 additions & 0 deletions src/seedu/addressbook/commands/EditCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.addressbook.commands;

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

import seedu.addressbook.common.Messages;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.*;

Choose a reason for hiding this comment

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

You should avoid importing by *

import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;


/**
* Edits a person identified using it's last displayed index from the address book.
*/
public class EditCommand extends Command {

public static final String COMMAND_WORD = "edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Edits the person identified by the index number used in the last person listing.\n\t"
+ "Parameters: INDEX [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...\n\t"
+ "Example: " + COMMAND_WORD
+ " 1 p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Person toEdit;

/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
*/
public EditCommand(int targetIndex,
String phone, boolean isPhonePrivate,
String email, boolean isEmailPrivate,
String address, boolean isAddressPrivate,
Set<String> tags) throws IllegalValueException {
super(targetIndex);
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
this.toEdit = new Person(
new Name("dummy"),

Choose a reason for hiding this comment

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

Could use the index to retrieve the name of toEdit.

new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
new UniqueTagList(tagSet)
);
}

public ReadOnlyPerson getPerson() {

Choose a reason for hiding this comment

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

Is this really necessary?

return toEdit;
}

@Override
public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
toEdit.changeName(target.getName());

Choose a reason for hiding this comment

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

There shouldn't be a random setter() just to cater for editting.

addressBook.editPerson(target, toEdit);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, toEdit));

} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
} catch (PersonNotFoundException pnfe) {
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK);
}
}

}
2 changes: 2 additions & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class HelpCommand extends Command {
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_ALL_USAGES = AddCommand.MESSAGE_USAGE
+ "\n" + EditCommand.MESSAGE_USAGE
+ "\n" + DeleteCommand.MESSAGE_USAGE
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
Expand Down
33 changes: 33 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

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

/**
* Sorts the address book by name in ascending order.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Sorts the address book by name in ascending order, then display it.\n\t"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Address book is sorted as above.\n\n";

@Override
public CommandResult execute() {
addressBook.sort();
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(MESSAGE_SUCCESS
+ getMessageForPersonListShownSummary(allPersons), allPersons);
}

}
13 changes: 13 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
}

public void editPerson(ReadOnlyPerson target, Person editedPerson) throws PersonNotFoundException {

Choose a reason for hiding this comment

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

Every public facing method should have a method header according to JavaDoc standards.

syncTagsWithMasterList(editedPerson);
allPersons.editPerson(target, editedPerson);
}


/**
* Sorts the address book by name in ascending order.
*/
public void sort() {
allPersons.sort();
}

/**
* Checks if an equivalent person exists in the address book.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.addressbook.data.person;

import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.tag.UniqueTagList;

import java.util.Objects;
Expand Down Expand Up @@ -39,6 +40,15 @@ public Name getName() {
return name;
}

/**
* Change the name of a person
*
* @param newName
*/
public void changeName(Name newName) {

Choose a reason for hiding this comment

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

This should not be allowed.

name = newName;
}

@Override
public Phone getPhone() {
return phone;
Expand Down
24 changes: 24 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException {
}
}

/**
* Edits the person {@code target} in the list into {@code editedPerson}.
*
* @throws PersonNotFoundException if {@code target} could not be found in the list.
*/
public void editPerson(ReadOnlyPerson target, Person editedPerson) throws PersonNotFoundException {
int index = internalList.indexOf(target);
if (index == -1) {
throw new PersonNotFoundException();
}
internalList.set(index, editedPerson);
}

/**
* Sorts the address book by name in ascending order.
*/
public void sort() {
internalList.sort(new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getName().toString().compareTo(p2.getName().toString());
}
});
}

/**
* Clears all persons in list.
*/
Expand Down
41 changes: 41 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public Command parseCommand(String userInput) {
case DeleteCommand.COMMAND_WORD:
return prepareDelete(arguments);

case EditCommand.COMMAND_WORD:
return prepareEdit(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand All @@ -72,6 +75,9 @@ public Command parseCommand(String userInput) {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand();

case ViewCommand.COMMAND_WORD:
return prepareView(arguments);

Expand Down Expand Up @@ -156,6 +162,41 @@ private Command prepareDelete(String args) {
}
}

/**
* Parses arguments in the context of the edit person command.
*
* @param args full command args string
* @return the prepared command
*/
private Command prepareEdit(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, EditCommand.MESSAGE_USAGE));
}
try {
final int targetIndex = parseArgsAsDisplayedIndex(matcher.group("name"));
return new EditCommand(
targetIndex,

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());
} catch (ParseException | NumberFormatException e) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
}
}

/**
* Parses arguments in the context of the view command.
*
Expand Down
Loading