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][W09-3] Sivakumar Bavadharini #156

Open
wants to merge 3 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
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 alphabetically: `sort`

Shows a list of all persons, along with their non-private details, sorted alphabetically, in the address book. +
Format: `sort`

== Finding all persons containing any keyword in their name: `find`

Finds persons whose names contain any of the given keywords. +
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String getMessageForPersonListShownSummary(List<? extends ReadOnly
*/
public CommandResult execute(){
throw new UnsupportedOperationException("This method is to be implemented by child classes");
};
}

/**
* Supplies the data the command will operate on.
Expand Down
22 changes: 22 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
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{

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 (:

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);
}
}
6 changes: 6 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;

import java.util.List;

/**
* Represents the entire address book. Contains the data of the address book.
*/
Expand Down Expand Up @@ -68,6 +70,10 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

public List<ReadOnlyPerson> sortListByName() {
return allPersons.sortListByName().immutableListView();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
29 changes: 23 additions & 6 deletions src/seedu/addressbook/data/person/UniquePersonList.java
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.*;

Choose a reason for hiding this comment

The 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: Settings > Editor > Code Style > Java > Imports and set both class count and names count to 99 (or any large number)


import seedu.addressbook.common.Utils;
import seedu.addressbook.data.exception.DuplicateDataException;
Expand Down Expand Up @@ -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());

Choose a reason for hiding this comment

The 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() {
Expand Down
70 changes: 31 additions & 39 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 @@ -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:

Choose a reason for hiding this comment

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

Expand Down
31 changes: 20 additions & 11 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
|| ===================================================
Expand All @@ -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.
Expand Down Expand Up @@ -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!
|| ===================================================
Expand Down
13 changes: 10 additions & 3 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
##########################################################
Expand Down
4 changes: 2 additions & 2 deletions test/java/seedu/addressbook/commands/FindCommandTest.java
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;
Expand All @@ -15,6 +13,8 @@
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.util.TypicalPersons;

import static org.testng.AssertJUnit.assertEquals;

Choose a reason for hiding this comment

The 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();
Expand Down
1 change: 0 additions & 1 deletion test/java/seedu/addressbook/commands/ViewCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,4 @@ private static void assertViewBehavior(Command viewCommand, AddressBook addressB
// addressbook was not modified.
assertEquals(expectedAddressBook.getAllPersons(), addressBook.getAllPersons());
}

}