Skip to content

Commit 8973b0e

Browse files
committed
Implement the Sort Command for People
Following additions of two new classes: * SortCommand.java * SortCommandParser.java And changes made to existing classes to support the sorting of people, * We are now able to sort the lists of people displayed by the lexicographical order of their names (e.g. Amanda would appear higher up in the person list as compared to Ben after sorting because the letter A appears earlier than B following alphabetical orders.
1 parent bb2e7dd commit 8973b0e

File tree

9 files changed

+108
-2
lines changed

9 files changed

+108
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import seedu.address.model.Model;
6+
7+
/**
8+
* Sorts all persons in the address book by an attribute of persons.
9+
*/
10+
public class SortCommand extends PersonCommand {
11+
12+
public static final String COMMAND_WORD = "sort";
13+
14+
public static final String MESSAGE_SUCCESS = "sorted all persons by name";
15+
16+
public static final String MESSAGE_USAGE = COMMAND_WORD
17+
+ ": sorts people according to an attribute and displays the sorted person list.\n"
18+
+ "Parameters: PREFIX (corresponds to each person's attribute e.g. /n for Name)\n"
19+
+ "Example: " + COMMAND_WORD + " n/";
20+
21+
private final String prefix;
22+
23+
/**
24+
* @param prefix referring to an attribute of persons to sort by
25+
*/
26+
public SortCommand(String prefix) {
27+
requireNonNull(prefix);
28+
this.prefix = prefix;
29+
}
30+
31+
32+
@Override
33+
public CommandResult execute(Model model) {
34+
requireNonNull(model);
35+
model.sortAddressBook(prefix);
36+
37+
return new CommandResult(MESSAGE_SUCCESS);
38+
}
39+
}

src/main/java/seedu/address/logic/parser/AddressBookParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import seedu.address.logic.commands.FindCommand;
1818
import seedu.address.logic.commands.HelpCommand;
1919
import seedu.address.logic.commands.ListCommand;
20+
import seedu.address.logic.commands.SortCommand;
2021
import seedu.address.logic.parser.exceptions.ParseException;
2122

2223
/**
@@ -75,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
7576
case ListCommand.COMMAND_WORD:
7677
return new ListCommand();
7778

79+
case SortCommand.COMMAND_WORD:
80+
return new SortCommandParser().parse(arguments);
81+
7882
case ExitCommand.COMMAND_WORD:
7983
return new ExitCommand();
8084

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
5+
6+
import seedu.address.logic.commands.SortCommand;
7+
import seedu.address.logic.parser.exceptions.ParseException;
8+
9+
/**
10+
* Parses input arguments and creates a new SortCommand object
11+
*/
12+
public class SortCommandParser implements Parser<SortCommand> {
13+
14+
/**
15+
* Parses the given {@code String} of arguments in the context of the SortCommand
16+
* and returns an SortCommand object for execution.
17+
* @throws ParseException if the user input does not conform the expected format
18+
*/
19+
public SortCommand parse(String args) throws ParseException {
20+
String prefix = args.trim();
21+
if (prefix.isEmpty() || !PREFIX_NAME.getPrefix().equals(prefix)) {
22+
throw new ParseException(
23+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
24+
}
25+
26+
return new SortCommand(prefix);
27+
}
28+
}

src/main/java/seedu/address/model/AddressBook.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public void removePerson(Person key) {
9494
persons.remove(key);
9595
}
9696

97+
/**
98+
* Sorts the persons in the address book by the attribute derived from a given prefix.
99+
*/
100+
public void sortAddressBook(String prefix) {
101+
persons.sortPersons(prefix);
102+
}
103+
97104
//// util methods
98105

99106
@Override

src/main/java/seedu/address/model/Model.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public interface Model {
8080
*/
8181
void setPerson(Person target, Person editedPerson);
8282

83+
void sortAddressBook(String prefix);
84+
8385
/** Returns an unmodifiable view of the filtered person list */
8486
ObservableList<Person> getFilteredPersonList();
8587

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public void setPerson(Person target, Person editedPerson) {
116116
addressBook.setPerson(target, editedPerson);
117117
}
118118

119+
@Override
120+
public void sortAddressBook(String prefix) {
121+
addressBook.sortAddressBook(prefix);
122+
}
123+
119124
//=========== Filtered Person List Accessors =============================================================
120125

121126
/**
@@ -169,7 +174,7 @@ public void setArticle(Article target, Article editedArticle) {
169174
articleBook.setArticle(target, editedArticle);
170175
}
171176

172-
//=========== Filtered Person List Accessors =============================================================
177+
//=========== Filtered Article List Accessors =============================================================
173178

174179
/**
175180
* Returns an unmodifiable view of the list of {@code Article} backed by the internal list of

src/main/java/seedu/address/model/person/Name.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Represents a Person's name in the address book.
88
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
99
*/
10-
public class Name {
10+
public class Name implements Comparable<Name> {
1111

1212
public static final String MESSAGE_CONSTRAINTS =
1313
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
@@ -44,6 +44,11 @@ public String toString() {
4444
return fullName;
4545
}
4646

47+
@Override
48+
public int compareTo(Name other) {
49+
return this.fullName.compareTo(other.fullName);
50+
}
51+
4752
@Override
4853
public boolean equals(Object other) {
4954
if (other == this) {

src/main/java/seedu/address/model/person/UniquePersonList.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static java.util.Objects.requireNonNull;
44
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
56

7+
import java.util.Comparator;
68
import java.util.Iterator;
79
import java.util.List;
810

@@ -97,6 +99,15 @@ public void setPersons(List<Person> persons) {
9799
internalList.setAll(persons);
98100
}
99101

102+
/**
103+
* Sorts the list of persons by the given prefix.
104+
*/
105+
public void sortPersons(String prefix) {
106+
if (PREFIX_NAME.getPrefix().equals(prefix)) {
107+
internalList.sort(Comparator.comparing(Person::getName));
108+
}
109+
}
110+
100111
/**
101112
* Returns the backing list as an unmodifiable {@code ObservableList}.
102113
*/

src/test/java/seedu/address/logic/commands/AddCommandTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public void setPerson(Person target, Person editedPerson) {
150150
throw new AssertionError("This method should not be called.");
151151
}
152152

153+
@Override
154+
public void sortAddressBook(String prefix) {
155+
throw new AssertionError("This method should not be called.");
156+
}
157+
153158
@Override
154159
public ObservableList<Person> getFilteredPersonList() {
155160
throw new AssertionError("This method should not be called.");

0 commit comments

Comments
 (0)