forked from nus-cs2103-AY2324S1/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 #176 from lamchenghou/lamchenghou/add-mapping-sort
Add mapping sort
- Loading branch information
Showing
15 changed files
with
428 additions
and
48 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
85 changes: 85 additions & 0 deletions
85
src/main/java/seedu/address/logic/commands/mapping/MappingSortCommand.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,85 @@ | ||
package seedu.address.logic.commands.mapping; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.SeplendidModel; | ||
import seedu.address.model.mapping.MappingComparatorByAttribute; | ||
import seedu.address.seplendidui.UiUtil; | ||
|
||
/** | ||
* Lists all mappings in the MappingCatalogue. | ||
*/ | ||
public class MappingSortCommand extends MappingCommand { | ||
|
||
public static final String MAPPING_SORT_MESSAGE_USAGE = COMMAND_WORD | ||
+ " sort [localcode/localname/partnercode/partnername/university/information]: " | ||
+ "Sorts all mappings based on the specified attribute."; | ||
public static final String ACTION_WORD = "sort"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Sorted mappings by %1$s."; | ||
|
||
private final MappingComparatorByAttribute mappingComparator; | ||
|
||
/** | ||
* Creates a MappingSortCommand to add the specified {@code mappingComparator} | ||
* | ||
* @param mappingComparator Search mappingComparator. | ||
*/ | ||
public MappingSortCommand(MappingComparatorByAttribute mappingComparator) { | ||
super(); | ||
this.mappingComparator = mappingComparator; | ||
} | ||
|
||
/** | ||
* TBD: This stub is to be removed after morphing is complete. | ||
* | ||
* @param model {@code Model} which the command should operate on. | ||
* @return Nothing. | ||
* @throws CommandException Always. | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
throw new CommandException("TBD: this is a stub and should be removed after morph."); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(SeplendidModel seplendidModel) throws CommandException { | ||
requireNonNull(seplendidModel); | ||
// Inject model functions into mappingComparator | ||
mappingComparator.initialiseGetLocalPartnercourse(seplendidModel::getLocalCourseIfExists, | ||
seplendidModel::getPartnerCourseIfExists); | ||
seplendidModel.updateSortedMappingList(mappingComparator); | ||
return new CommandResult( | ||
String.format(MESSAGE_SUCCESS, mappingComparator.getAttribute().toString().toLowerCase()), | ||
UiUtil.ListViewModel.MAPPING_SORT); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof MappingSortCommand)) { | ||
return false; | ||
} | ||
|
||
MappingSortCommand otherMappingSortCommand = (MappingSortCommand) other; | ||
return mappingComparator.equals(otherMappingSortCommand.mappingComparator); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("mappingComparator", mappingComparator) | ||
.toString(); | ||
} | ||
|
||
|
||
} |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/seedu/address/logic/parser/mapping/MappingSortCommandParser.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,52 @@ | ||
package seedu.address.logic.parser.mapping; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PARAMETER_MAPPINGATTRIBUTE; | ||
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty; | ||
import static seedu.address.logic.parser.ParserUtil.parseMappingAttribute; | ||
|
||
import seedu.address.logic.commands.mapping.MappingSortCommand; | ||
import seedu.address.logic.parser.Parser; | ||
import seedu.address.logic.parser.ParserUtil; | ||
import seedu.address.logic.parser.SeplendidArgumentMap; | ||
import seedu.address.logic.parser.SeplendidArgumentTokenizer; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.mapping.MappingAttributeEnum; | ||
import seedu.address.model.mapping.MappingComparatorByAttribute; | ||
|
||
/** | ||
* Parses input arguments and creates a new MappingSortCommandParser object. | ||
*/ | ||
public class MappingSortCommandParser implements Parser<MappingSortCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the MappingSortCommandParser | ||
* and returns a MappingSortCommandParser object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public MappingSortCommand parse(String args) throws ParseException { | ||
if (!areValuesEnclosedAndNonEmpty(args)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
MappingSortCommand.MAPPING_SORT_MESSAGE_USAGE)); | ||
} | ||
|
||
SeplendidArgumentMap parameterToArgMap = | ||
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_MAPPINGATTRIBUTE); | ||
|
||
|
||
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_MAPPINGATTRIBUTE)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
MappingSortCommand.MAPPING_SORT_MESSAGE_USAGE)); | ||
} | ||
|
||
// All arguments should be a non-empty {@code Optional} | ||
MappingAttributeEnum attributeEnumValue = parseMappingAttribute(parameterToArgMap.getValue( | ||
PARAMETER_MAPPINGATTRIBUTE).get().toLowerCase()); | ||
|
||
MappingComparatorByAttribute mappingComparator = new MappingComparatorByAttribute(attributeEnumValue); | ||
|
||
return new MappingSortCommand(mappingComparator); | ||
} | ||
|
||
} |
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.