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][T09-2]Cheah Zhi Kang #26

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ deploy:
provider: script
script: ./config/travis/deploy_github_pages.sh
on:
branch: master
all_branches: true

addons:
apt:
Expand Down
5 changes: 5 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Examples:
Shows a list of all persons, along with their non-private details, in the address book. +
Format: `list`

== Sorting all persons by name : `sort`

Sorts and displays a list of all persons, based on their names alphabetically, along with their non-private details, in the address book. +
Format: `sort`

cheahzk marked this conversation as resolved.
Show resolved Hide resolved
== Finding all persons containing any keyword in their name: `find`

Finds persons whose names contain any of the given keywords. +
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static String getMessageForPersonListShownSummary(List<? extends ReadOnly
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}

public static String getMessageForSortedPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_SORTED_OVERVIEW, personsDisplayed.size());
}

/**
* Executes the command and returns the result.
*/
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 @@ -19,6 +19,7 @@ public CommandResult execute() {
+ "\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
25 changes: 25 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.List;


/**
* Lists all persons in the address book to the user.
*/
cheahzk marked this conversation as resolved.
Show resolved Hide resolved
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Sorts and display all persons in the address book as a list with index numbers.\n"
+ "Example: " + COMMAND_WORD;


@Override
public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.sortAllPersons().immutableListView();
return new CommandResult(getMessageForSortedPersonListShownSummary(allPersons), allPersons);
}
}
1 change: 1 addition & 0 deletions src/seedu/addressbook/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Messages {
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
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_PERSONS_SORTED_OVERVIEW = "%1$d persons sorted!";
public static final String MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE = "Launch command format: " +
"java seedu.addressbook.Main [STORAGE_FILE_PATH]";
public static final String MESSAGE_WELCOME = "Welcome to your Address Book!";
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

public UniquePersonList sortAllPersons() {
allPersons.sort();
return new UniquePersonList(allPersons);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Address(String address, boolean isPrivate) throws IllegalValueException {
throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS);
}
this.value = trimmedAddress;

}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public List<String> getWordsInName() {
return Arrays.asList(fullName.split("\\s+"));
}

public int compareTo(Name target) {
int compare = this.fullName.compareTo(target.fullName);
return compare;
}

@Override
public String toString() {
return fullName;
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 java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -62,6 +63,15 @@ public Set<Tag> getTags() {
return new HashSet<>(tags);
}

/**
* Compare selected person's name with target's name
*/
public static Comparator<Person> compareByName = new Comparator<Person>() {
public int compare(Person selected, Person target) {
return selected.name.compareTo(target.name);
}
};

/**
* Replaces this person's tags with the tags in the argument tag set.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public void clear() {
internalList.clear();
}

/**
* Sorts the current list in alphabetical order of names
*/
public void sort() {
Collections.sort(internalList, Person.compareByName);
}

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand Down
18 changes: 7 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 @@ -76,6 +66,12 @@ public Command parseCommand(String userInput) {
case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);

case SortCommand.COMMAND_WORD:
if (arguments.isEmpty()) {
return new SortCommand();
}
return new HelpCommand();

case DeleteCommand.COMMAND_WORD:
return prepareDelete(arguments);

Expand Down
38 changes: 38 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
|| Example: find alice bob charlie
|| list: Displays all persons in the address book as a list with index numbers.
|| Example: list
|| sort: Sorts and display all persons in the address book as a list with index numbers.
|| Example: sort
|| view: Views the non-private details of the person identified by the index number in the last shown person listing.
|| Parameters: INDEX
|| Example: view 1
Expand Down Expand Up @@ -73,6 +75,33 @@
|| Enter command: || [Command entered: add Valid Name p/12345 e/[email protected] a/valid, address t/goodTag noPrefixTag]
|| Tags names should be alphanumeric
|| ===================================================
|| Enter command: || [Command entered: sort this]
|| add: Adds a person to the address book. Contact details can be marked private by prepending 'p' to the prefix.
|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...
|| Example: add John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney
|| delete: Deletes the person identified by the index number used in the last person listing.
|| Parameters: INDEX
|| Example: delete 1
|| Clears address book permanently.
|| Example: clear
|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers.
|| Parameters: KEYWORD [MORE_KEYWORDS]...
|| Example: find alice bob charlie
|| list: Displays all persons in the address book as a list with index numbers.
|| Example: list
|| sort: Sorts and display all persons in the address book as a list with index numbers.
|| Example: sort
|| view: Views the non-private details of the person identified by the index number in the last shown person listing.
|| Parameters: INDEX
|| Example: view 1
|| viewall: Views the non-private details of the person identified by the index number in the last shown person listing.
|| Parameters: INDEX
|| Example: viewall 1
|| help: Shows program usage instructions.
|| Example: help
|| exit: Exits the program.
|| Example: exit
|| ===================================================
|| Enter command: || [Command entered: add []\[;] p/12345 e/[email protected] a/valid, address]
|| Person names should be spaces or alphabetic characters
|| ===================================================
Expand Down Expand Up @@ -135,6 +164,15 @@
||
|| 5 persons listed!
|| ===================================================
|| Enter command: || [Command entered: sort]
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags:
|| 2. Betsy Choo Tags: [secretive]
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 5. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
||
|| 5 persons sorted!
|| ===================================================
cheahzk marked this conversation as resolved.
Show resolved Hide resolved
|| Enter command: || [Command entered: add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy]
|| This person already exists in the address book
|| ===================================================
Expand Down
4 changes: 3 additions & 1 deletion test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
list

##########################################################
# test add person command, setup state for further tests
# test add/sort person command, setup state for further tests
##########################################################

# should catch invalid args format
Expand All @@ -32,6 +32,7 @@
add Valid Name p/12345 [email protected] a/valid, address
add Valid Name p/12345 e/[email protected] valid, address
add Valid Name p/12345 e/[email protected] a/valid, address t/goodTag noPrefixTag
sort this

# should catch invalid person data
add []\[;] p/12345 e/[email protected] a/valid, address
Expand All @@ -50,6 +51,7 @@
list
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy
list
sort

Choose a reason for hiding this comment

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

Perhaps your input for the test case should not be added in a lexicographically sorted manner? You can consider changing the order of one or more of the test inputs to verify whether your command works as expected.


# should not allow adding duplicate persons
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy
Expand Down