forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from BunnyHoppp/branch-SortCommand
Branch sort command
- Loading branch information
Showing
47 changed files
with
1,049 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/seedu/address/logic/commands/SortCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.isNull; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Comparator; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.logic.parser.ParserUtil.SortAttribute; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.comparator.AddressCompare; | ||
import seedu.address.model.person.comparator.EcNameCompare; | ||
import seedu.address.model.person.comparator.EcNumberCompare; | ||
import seedu.address.model.person.comparator.EmailCompare; | ||
import seedu.address.model.person.comparator.NameCompare; | ||
import seedu.address.model.person.comparator.PhoneCompare; | ||
import seedu.address.model.person.comparator.RegisterNumberCompare; | ||
import seedu.address.model.person.comparator.SexCompare; | ||
import seedu.address.model.person.comparator.StudentClassCompare; | ||
|
||
/** | ||
* Sorts the list of contacts based on the attribute | ||
*/ | ||
public class SortCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts the list based on the attribute\n" | ||
+ "Attributes: name, phone, email, address, register number, sex, \n" | ||
+ "student class, emergency contact name, emergency contact number"; | ||
|
||
public static final String MESSAGE_SORTED_SUCCESS = "Sorted the list based on %1$s"; | ||
public static final String MESSAGE_UNSORTED_SUCCESS = "Your list is now unsorted and back to the original!"; | ||
private static final Logger logger = LogsCenter.getLogger(SortCommand.class); | ||
private final SortAttribute sortAttribute; | ||
private Comparator<Person> comparator; | ||
|
||
|
||
/** | ||
* @param sortAttribute attribute to sort the persons in the list | ||
*/ | ||
public SortCommand(SortAttribute sortAttribute) { | ||
requireNonNull(sortAttribute); | ||
this.sortAttribute = sortAttribute; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
this.comparator = this.getComparator(); | ||
model.sortFilteredPersonList(this.comparator); | ||
logger.log(Level.INFO, "SortCommand has been executed"); | ||
return new CommandResult(this.generateSuccessMessage()); | ||
} | ||
|
||
|
||
public String generateSuccessMessage() { | ||
return isNull(comparator) ? MESSAGE_UNSORTED_SUCCESS : String.format(MESSAGE_SORTED_SUCCESS, sortAttribute); | ||
} | ||
/** | ||
* Retrieves the comparator needed to sort the list. | ||
* Returns null to unsort. | ||
*/ | ||
public Comparator<Person> getComparator() { | ||
assert sortAttribute != null; | ||
switch (sortAttribute) { | ||
|
||
case NAME: | ||
return new NameCompare(); | ||
|
||
case PHONE: | ||
return new PhoneCompare(); | ||
|
||
case EMAIL: | ||
return new EmailCompare(); | ||
|
||
case ADDRESS: | ||
return new AddressCompare(); | ||
|
||
case REGISTERNUMBER: | ||
return new RegisterNumberCompare(); | ||
|
||
case SEX: | ||
return new SexCompare(); | ||
|
||
case STUDENTCLASS: | ||
return new StudentClassCompare(); | ||
|
||
case EMERGENCYCONTACTNAME: | ||
return new EcNameCompare(); | ||
|
||
case EMERGENCYCONTACTNUMBER: | ||
return new EcNumberCompare(); | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof SortCommand)) { | ||
return false; | ||
} | ||
|
||
SortCommand s = (SortCommand) other; | ||
return sortAttribute.equals(s.sortAttribute); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/seedu/address/logic/parser/SortCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.logic.commands.SortCommand; | ||
import seedu.address.logic.parser.ParserUtil.SortAttribute; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SortCommand object | ||
*/ | ||
public class SortCommandParser implements Parser<SortCommand> { | ||
|
||
private static final Logger logger = LogsCenter.getLogger(SortCommandParser.class); | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SortCommand | ||
* and returns a SortCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format. | ||
*/ | ||
public SortCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
SortAttribute sortAttribute; | ||
try { | ||
sortAttribute = ParserUtil.parseSortAttribute(args); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
SortCommand.MESSAGE_USAGE), ive); | ||
} | ||
assert sortAttribute != null; | ||
logger.log(Level.INFO, "parsed sort command without exception"); | ||
return new SortCommand(sortAttribute); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.