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

[W7.6][W12-3]Ler Wei Sheng #195

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions docs/DeveloperGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ OSes. Convert the actual.txt to the format used by your OS using some https://kb
* Problem: Test fails during the very first time. +
Solution: The output of the very first test run could be slightly different because the program
creates a new storage file. Tests should pass from the 2nd run onwards.

=== Adding more sort functions
Refer to `UniquePersonList` class and `Person` class. +
Add on to custom comparator
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`

== Listing all persons sorted by name : `listSorted`
Shows a list of all persons in sorted order, along with their non-private details, in the address book. +
Format: `listSorted`


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

Finds persons whose names contain any of the given keywords. +
Expand Down
22 changes: 22 additions & 0 deletions src/seedu/addressbook/commands/ListSortedCommand.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.Collections;
import java.util.List;

public class ListSortedCommand extends Command {
public static final String COMMAND_WORD = "listSorted";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Displays all persons in the address book as a list with index numbers in their sorted order by name.\n"
+ "Example: " + COMMAND_WORD;


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

}
6 changes: 6 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

public UniquePersonList getAllPersonsSorted() {
UniquePersonList uniquePersons = new UniquePersonList(allPersons);
uniquePersons.sort(); // Call to sort the list
return uniquePersons;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
6 changes: 6 additions & 0 deletions src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ public int hashCode() {
return fullName.hashCode();
}

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


}
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 @@ -88,4 +89,13 @@ public String toString() {
return getAsTextShowAll();
}

// Custom comparator to compare two person objects
public static Comparator<Person> COMPARE_BY_NAME = new Comparator<Person>() {
public int compare(Person one, Person other) {
return one.name.compareTo(other.name);
}
};



}
8 changes: 8 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public UniquePersonList(UniquePersonList source) {
internalList.addAll(source.internalList);
}

/**
* Sorts the list
*/
public void sort () {
Collections.sort(internalList, Person.COMPARE_BY_NAME);
}


/**
* Returns an unmodifiable java List view with elements cast as immutable {@link ReadOnlyPerson}s.
* For use with other methods/libraries.
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 @@ -20,6 +20,7 @@
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ListSortedCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.data.exception.IllegalValueException;
Expand Down Expand Up @@ -99,6 +100,9 @@ public Command parseCommand(String userInput) {
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case ListSortedCommand.COMMAND_WORD:
return new ListSortedCommand();

case HelpCommand.COMMAND_WORD: // Fallthrough
default:
return new HelpCommand();
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
Expand All @@ -15,6 +17,8 @@
*/
public class StorageFile {

private static final Logger logger = Logger.getLogger("StorageFile");

/** Default file path used if the user doesn't provide the file name. */
public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt";

Expand Down Expand Up @@ -78,7 +82,7 @@ public void save(AddressBook addressBook) throws StorageOperationException {
List<String> encodedAddressBook = AddressBookEncoder.encodeAddressBook(addressBook);
Files.write(path, encodedAddressBook);
} catch (IOException ioe) {
throw new StorageOperationException("Error writing to file: " + path);
logger.log(Level.SEVERE, "File is read only!");
}
}

Expand Down
30 changes: 19 additions & 11 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: listSorted]
|| 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: delete]
|| Invalid command format!
|| delete: Deletes the person identified by the index number used in the last person listing.
Expand All @@ -249,10 +258,10 @@
|| The person index provided is invalid
|| ===================================================
|| Enter command: || [Command entered: delete 3]
|| The person index provided is invalid
|| Deleted Person: Charlie Dickson Phone: (private) 333333 Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| ===================================================
|| Enter command: || [Command entered: delete 2]
|| Deleted Person: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends]
|| Deleted Person: Betsy Choo Phone: (private) 222222 Email: (private) benchoo@nus.edu.sg Address: (private) 222, beta street Tags: [secretive]
|| ===================================================
|| Enter command: || [Command entered: delete 2]
|| Person could not be found in address book
Expand All @@ -265,28 +274,27 @@
|| ===================================================
|| Enter command: || [Command entered: list]
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags:
|| 2. Betsy Choo Tags: [secretive]
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 4. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 3. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
||
|| 4 persons listed!
|| 3 persons listed!
|| ===================================================
|| Enter command: || [Command entered: delete 4]
|| Deleted Person: Esther Potato Phone: 555555 Email: [email protected] Address: (private) 555, epsilon street Tags: [tubers][starchy]
|| The person index provided is invalid
|| ===================================================
|| Enter command: || [Command entered: list]
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags:
|| 2. Betsy Choo Tags: [secretive]
|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 3. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
||
|| 3 persons listed!
|| ===================================================
|| Enter command: || [Command entered: delete 1]
|| Deleted Person: Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags:
|| ===================================================
|| Enter command: || [Command entered: list]
|| 1. Betsy Choo Tags: [secretive]
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 1. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
|| 2. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy]
||
|| 2 persons listed!
|| ===================================================
Expand Down
7 changes: 7 additions & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@
# find multiple with some keywords
find Charlie Betsy

##########################################################
# test list sorted command
##########################################################
# Lists in correct order
listSorted


##########################################################
# test delete person command
##########################################################
Expand Down
11 changes: 11 additions & 0 deletions test/java/seedu/addressbook/common/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Arrays;
import java.util.List;

import jdk.jshell.execution.Util;
import org.junit.Test;

public class UtilsTest {
Expand Down Expand Up @@ -43,4 +44,14 @@ private void assertAreUnique(Object... objects) {
private void assertNotUnique(Object... objects) {
assertFalse(Utils.elementsAreUnique(Arrays.asList(objects)));
}


@Test
public void isAnyNullTest () {
Object item = null; // Temporary null object
assertTrue(Utils.isAnyNull(item)); // Contains null object

item = "NonNull";
assertFalse(Utils.isAnyNull(item)); // Contains non null object
}
}