Skip to content

Commit

Permalink
Merge pull request #36 from highorbit25/feature-sort
Browse files Browse the repository at this point in the history
Add sort command to sort list by name
  • Loading branch information
highorbit25 authored Oct 10, 2022
2 parents b718fc3 + e8cac90 commit 4db8fa5
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class ListCommand extends Command {

public static final String MESSAGE_SUCCESS = "Listed all persons";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.model.Model;

/**
* Sorts the address book by name in alphabetic order.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_SUCCESS = "Address book has been sorted by name in alphabetical order!";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.sortFilteredPersonList();
return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.MarkCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.commands.UnmarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -84,6 +85,9 @@ public Command parseCommand(String userInput) throws ParseException {
case FilterCommand.COMMAND_WORD:
return new FilterCommandParser().parse(arguments);

case SortCommand.COMMAND_WORD:
return new SortCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class AddressBook implements ReadOnlyAddressBook {
persons = new UniquePersonList();
}

public AddressBook() {}
public AddressBook() {
}

/**
* Creates an AddressBook using the Persons in the {@code toBeCopied}
Expand Down Expand Up @@ -81,7 +82,6 @@ public void addPerson(Person p) {
*/
public void setPerson(Person target, Person editedPerson) {
requireNonNull(editedPerson);

persons.setPerson(target, editedPerson);
}

Expand All @@ -106,6 +106,10 @@ public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}

public void sort() {
persons.sort();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ public interface Model {
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/** Sorts filteredList by name in alphabetical order. */
void sortFilteredPersonList();

}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ public ObservableList<Person> getFilteredPersonList() {
return filteredPersons;
}

/**
*
*/
public void sortFilteredPersonList() {
this.addressBook.sort();
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
requireNonNull(predicate);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Name getName() {
return name;
}

public String getNameString() {
return name.toString();
}

public Phone getPhone() {
return phone;
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/seedu/address/model/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand All @@ -17,7 +18,7 @@
* persons uses Person#isSamePerson(Person) for equality so as to ensure that the person being added or updated is
* unique in terms of identity in the UniquePersonList. However, the removal of a person uses Person#equals(Object) so
* as to ensure that the person with exactly the same fields will be removed.
*
* <p>
* Supports a minimal set of list operations.
*
* @see Person#isSamePerson(Person)
Expand Down Expand Up @@ -48,6 +49,13 @@ public void add(Person toAdd) {
internalList.add(toAdd);
}

/**
* Sorts the list according to name in alphabetical order.
*/
public void sort() {
internalList.sort(Comparator.comparing(Person::getNameString));
}

/**
* Replaces the person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the list.
Expand Down Expand Up @@ -104,6 +112,7 @@ public ObservableList<Person> asUnmodifiableObservableList() {
return internalUnmodifiableList;
}


@Override
public Iterator<Person> iterator() {
return internalList.iterator();
Expand All @@ -113,7 +122,7 @@ public Iterator<Person> iterator() {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof UniquePersonList // instanceof handles nulls
&& internalList.equals(((UniquePersonList) other).internalList));
&& internalList.equals(((UniquePersonList) other).internalList));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public ObservableList<Person> getFilteredPersonList() {
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortFilteredPersonList() {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down

0 comments on commit 4db8fa5

Please sign in to comment.