-
Notifications
You must be signed in to change notification settings - Fork 113
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][W09-3] Sivakumar Bavadharini #156
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,22 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Sorts all persons in the address book by their name and lists them to the user. | ||
*/ | ||
public class SortCommand extends Command{ | ||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Displays all persons in the address book as a list sorted by name with index numbers.\n" | ||
+ "Example: " + COMMAND_WORD; | ||
|
||
@Override | ||
public CommandResult execute() { | ||
List <ReadOnlyPerson> sortedList = addressBook.sortListByName(); | ||
return new CommandResult(getMessageForPersonListShownSummary(sortedList), sortedList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
package seedu.addressbook.data.person; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.*; | ||
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. Wildcard imports violate the coding standard. Please use single imports and change your settings in Intellij: |
||
|
||
import seedu.addressbook.common.Utils; | ||
import seedu.addressbook.data.exception.DuplicateDataException; | ||
|
@@ -128,6 +123,28 @@ public void remove(ReadOnlyPerson toRemove) throws PersonNotFoundException { | |
public void clear() { | ||
internalList.clear(); | ||
} | ||
|
||
/** | ||
* Comparator used for sorting in sortListByName method. | ||
*/ | ||
class SortByName implements Comparator<ReadOnlyPerson> { | ||
@Override | ||
public int compare (ReadOnlyPerson person1, ReadOnlyPerson person2) { | ||
return person1.getName().toString().compareTo(person2.getName().toString()); | ||
} | ||
} | ||
|
||
public UniquePersonList sortListByName() { | ||
try { | ||
UniquePersonList sortedList = new UniquePersonList(this.internalList); | ||
sortedList.internalList.sort(new SortByName()); | ||
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 on modifying a copy of the data instead of the original data (: |
||
return sortedList; | ||
} catch (DuplicatePersonException e) { | ||
e.printStackTrace(); | ||
} | ||
// return empty list if there are no names to sort | ||
return new UniquePersonList(); | ||
} | ||
|
||
@Override | ||
public Iterator<Person> iterator() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
|
@@ -72,34 +62,36 @@ public Command parseCommand(String userInput) { | |
final String arguments = matcher.group("arguments"); | ||
|
||
switch (commandWord) { | ||
|
||
case AddCommand.COMMAND_WORD: | ||
return prepareAdd(arguments); | ||
|
||
case DeleteCommand.COMMAND_WORD: | ||
return prepareDelete(arguments); | ||
|
||
case ClearCommand.COMMAND_WORD: | ||
return new ClearCommand(); | ||
|
||
case FindCommand.COMMAND_WORD: | ||
return prepareFind(arguments); | ||
|
||
case ListCommand.COMMAND_WORD: | ||
return new ListCommand(); | ||
|
||
case ViewCommand.COMMAND_WORD: | ||
return prepareView(arguments); | ||
|
||
case ViewAllCommand.COMMAND_WORD: | ||
return prepareViewAll(arguments); | ||
|
||
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
return new HelpCommand(); | ||
case AddCommand.COMMAND_WORD: | ||
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 follow the indentation in the coding standard for switch statements (: |
||
return prepareAdd(arguments); | ||
|
||
case DeleteCommand.COMMAND_WORD: | ||
return prepareDelete(arguments); | ||
|
||
case ClearCommand.COMMAND_WORD: | ||
return new ClearCommand(); | ||
|
||
case FindCommand.COMMAND_WORD: | ||
return prepareFind(arguments); | ||
|
||
case ListCommand.COMMAND_WORD: | ||
return new ListCommand(); | ||
|
||
case SortCommand.COMMAND_WORD: | ||
return new SortCommand(); | ||
|
||
case ViewCommand.COMMAND_WORD: | ||
return prepareView(arguments); | ||
|
||
case ViewAllCommand.COMMAND_WORD: | ||
return prepareViewAll(arguments); | ||
|
||
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
return new HelpCommand(); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,24 +102,24 @@ | |
|| | ||
|| 2 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school] | ||
|| New person added: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] | ||
|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends] | ||
|| New person added: Dickson Ee Phone: 444444 Email: (private) dickson@nus.edu.sg Address: 444, delta street Tags: [friends] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 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] | ||
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| | ||
|| 3 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: add Dickson Ee p/444444 pe/dickson@nus.edu.sg a/444, delta street t/friends] | ||
|| New person added: Dickson Ee Phone: 444444 Email: (private) dickson@nus.edu.sg Address: 444, delta street Tags: [friends] | ||
|| Enter command: || [Command entered: add Charlie Dickson pp/333333 e/charlie.d@nus.edu.sg a/333, gamma street t/friends t/school] | ||
|| New person added: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 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] | ||
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 4. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| | ||
|| 4 persons listed! | ||
|| =================================================== | ||
|
@@ -129,15 +129,24 @@ | |
|| Enter command: || [Command entered: list] | ||
|| 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] | ||
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 4. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 5. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| 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 | ||
|| =================================================== | ||
|| 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 listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: view] | ||
|| Invalid command format! | ||
|| view: Views the non-private details of the person identified by the index number in the last shown person listing. | ||
|
@@ -222,8 +231,8 @@ | |
|| 1 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: find Dickson] | ||
|| 1. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 1. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 2. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| | ||
|| 2 persons listed! | ||
|| =================================================== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,16 +44,23 @@ | |
list | ||
add Betsy Choo pp/222222 pe/[email protected] pa/222, beta street t/secretive | ||
list | ||
add Charlie Dickson pp/333333 e/[email protected] a/333, gamma street t/friends t/school | ||
list | ||
add Dickson Ee p/444444 pe/[email protected] a/444, delta street t/friends | ||
list | ||
add Charlie Dickson pp/333333 e/[email protected] a/333, gamma street t/friends t/school | ||
list | ||
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy | ||
list | ||
|
||
# should not allow adding duplicate persons | ||
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy | ||
|
||
########################################################## | ||
# test sort command | ||
########################################################## | ||
|
||
# should display Charlie Dickson before Dickson Ee | ||
sort | ||
|
||
########################################################## | ||
# test view/viewall persons command | ||
########################################################## | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
|
@@ -15,6 +13,8 @@ | |
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.util.TypicalPersons; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
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 not to commit unrelated changes in this PR |
||
|
||
public class FindCommandTest { | ||
|
||
private final AddressBook addressBook = new TypicalPersons().getTypicalAddressBook(); | ||
|
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.
Remember to add a whitespace before the curly brace (: