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][W14-4]T Anandakkoomar #145

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
10 changes: 10 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ Views all details of the 1st person in the results of the `find` command.
Clears all entries from the address book. +
Format: `clear`

== Sorting the List : `sort`

Sorts the list according to the user preference using either name, email or address
Format: `sort TYPE_OF_SORT`

Example:

* `sort email`
Sorts the list according the email address of everybody

== Exiting the program : `exit`

Exits the program. +
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion src/seedu/addressbook/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import seedu.addressbook.storage.StorageFile.StorageOperationException;
import seedu.addressbook.ui.TextUi;


/**
* Entry point of the Address Book application.
* Initializes the application and starts the interaction with the user.
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class CommandResult {

/** The feedback message to be shown to the user. Contains a description of the execution result */
public final String feedbackToUser;
private final String feedbackToUser;

/** The list of persons that was produced by the command */
private final List<? extends ReadOnlyPerson> relevantPersons;
Expand All @@ -33,4 +33,8 @@ public Optional<List<? extends ReadOnlyPerson>> getRelevantPersons() {
return Optional.ofNullable(relevantPersons);
}

public String getfeedbackToUser(){
return this.feedbackToUser;
}

}
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CommandResult execute() {
+ "\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n" + HelpCommand.MESSAGE_USAGE
+ "\n" + ExitCommand.MESSAGE_USAGE
+ "\n" + SortCommand.MESSAGE_USAGE
);
}
}
36 changes: 36 additions & 0 deletions src/seedu/addressbook/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.addressbook.commands;

public class SortCommand extends Command {
public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts the list according to user preference. "
+ "List can be sorted by name, email or address.\n"
+ "Parameters: Type of sort[name/email/address]\n"
+ "Example: " + COMMAND_WORD
+ " name";

public static final String MESSAGE_SUCCESS = "The list is sorted!";
public static final String MESSAGE_INVALID_ARGUMENT = "Invalid Argument produced; please use valid arguments";
public static final String MESSAGE_ERROR_UNSORTABLE = "Bruh the list is empty man.......";

private String type;

public SortCommand(String type){
this.type = type.trim();
}

@Override
public CommandResult execute(){
if(type.equals("name") || type.equals("email") || type.equals("address")) {
boolean isSorted = addressBook.sort(this.type);

if(isSorted){
return new CommandResult(MESSAGE_SUCCESS);
}else{
return new CommandResult(MESSAGE_ERROR_UNSORTABLE);
}
}else{
return new CommandResult(MESSAGE_INVALID_ARGUMENT);
}
}
}
9 changes: 9 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public boolean equals(Object other) {
|| (other instanceof AddressBook // instanceof handles nulls
&& this.allPersons.equals(((AddressBook) other).allPersons));
}

public boolean sort(String type){
if(!allPersons.isEmpty()) {
allPersons.sort(type);
return true;
}else{
return false;
}
}
}
6 changes: 4 additions & 2 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import seedu.addressbook.data.exception.IllegalValueException;

import java.lang.String;

/**
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
*/
public class Address {
public class Address{

public static final String EXAMPLE = "123, some street";
public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses can be in any format";
public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses must in the format : a/BLOCK, STREET, UNIT, POSTAL CODE ";
public static final String ADDRESS_VALIDATION_REGEX = ".+";

public final String value;
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/data/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Represents a Person's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {
public class Email{

public static final String EXAMPLE = "[email protected]";
public static final String MESSAGE_EMAIL_CONSTRAINTS =
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/data/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Represents a Person's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)}
*/
public class Phone {
public class Phone{

public static final String EXAMPLE = "123456789";
public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers";
Expand Down
24 changes: 18 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.*;

import seedu.addressbook.common.Utils;
import seedu.addressbook.data.exception.DuplicateDataException;
Expand Down Expand Up @@ -129,6 +124,23 @@ public void clear() {
internalList.clear();
}

public void sort(String type){
if(type.equals("name"))
internalList.sort(Comparator.comparing((Person p)-> p.getName().toString()));
if(type.equals("email"))
internalList.sort(Comparator.comparing((Person p)-> p.getEmail().toString()));
if(type.equals("address"))
internalList.sort(Comparator.comparing((Person p)-> p.getAddress().toString()));
}

public boolean isEmpty(){
if(internalList.isEmpty()){
return true;
}else{
return false;
}
}

@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.commands.SortCommand;
import seedu.addressbook.data.exception.IllegalValueException;

/**
Expand Down Expand Up @@ -97,6 +98,9 @@ public Command parseCommand(String userInput) {
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand(arguments);

case HelpCommand.COMMAND_WORD: // Fallthrough
default:
return new HelpCommand();
Expand Down
2 changes: 1 addition & 1 deletion src/seedu/addressbook/ui/TextUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void showResultToUser(CommandResult result) {
if (resultPersons.isPresent()) {
showPersonListView(resultPersons.get());
}
showToUser(result.feedbackToUser, DIVIDER);
showToUser(result.getfeedbackToUser(), DIVIDER);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/java/seedu/addressbook/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void addCommand_emptyAddressBook_addressBookContainsPerson() {
assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
assertFalse(result.getRelevantPersons().isPresent());
assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, p), result.feedbackToUser);
assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, p), result.getfeedbackToUser());
}

@Test
Expand All @@ -139,7 +139,7 @@ public void addCommand_addressBookAlreadyContainsPerson_addressBookUnmodified()
CommandResult result = command.execute();

assertFalse(result.getRelevantPersons().isPresent());
assertEquals(AddCommand.MESSAGE_DUPLICATE_PERSON, result.feedbackToUser);
assertEquals(AddCommand.MESSAGE_DUPLICATE_PERSON, result.getfeedbackToUser());
UniquePersonList people = book.getAllPersons();
assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void assertCommandBehaviour(DeleteCommand deleteCommand, String expected

CommandResult result = deleteCommand.execute();

assertEquals(expectedMessage, result.feedbackToUser);
assertEquals(expectedMessage, result.getfeedbackToUser());
assertEquals(expectedAddressBook.getAllPersons(), actualAddressBook.getAllPersons());
}

Expand Down
2 changes: 1 addition & 1 deletion test/java/seedu/addressbook/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void assertFindCommandBehavior(String[] keywords, List<ReadOnlyPerson> e
FindCommand command = createFindCommand(keywords);
CommandResult result = command.execute();

assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser);
assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.getfeedbackToUser());
}

private FindCommand createFindCommand(String[] keywords) {
Expand Down
2 changes: 1 addition & 1 deletion test/java/seedu/addressbook/commands/ViewCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static void assertViewBehavior(Command viewCommand, AddressBook addressB
CommandResult result = viewCommand.execute();

// feedback message is as expected and there are no relevant persons returned.
assertEquals(expectedMessage, result.feedbackToUser);
assertEquals(expectedMessage, result.getfeedbackToUser());
assertEquals(Optional.empty(), result.getRelevantPersons());

// addressbook was not modified.
Expand Down