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 #115 from dlathyun/dlathyun/sort-command
Add sort command for LocalCourse
- Loading branch information
Showing
60 changed files
with
1,454 additions
and
30 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
75 changes: 75 additions & 0 deletions
75
src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.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,75 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Comparator; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.SeplendidModel; | ||
import seedu.address.model.localcourse.LocalCourse; | ||
import seedu.address.seplendidui.UiUtil; | ||
|
||
/** | ||
* Sorts local course list. | ||
*/ | ||
public class LocalCourseSortCommand extends LocalCourseCommand { | ||
public static final String LOCAL_COURSE_SORT_MESSAGE_USAGE = COMMAND_WORD | ||
+ " sort: Sorts all local courses by attributes - localcode & localname"; | ||
public static final String ACTION_WORD = "sort"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Sorted all local courses"; | ||
|
||
private final Comparator<LocalCourse> comparator; | ||
|
||
/** | ||
* Creates a LocalCourseSortCommand to sort the local course list. | ||
* @param comparator Comparator used for sorting | ||
*/ | ||
public LocalCourseSortCommand(Comparator<LocalCourse> comparator) { | ||
this.comparator = comparator; | ||
} | ||
|
||
/** | ||
* 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); | ||
seplendidModel.updatedSortedLocalList(comparator); | ||
return new CommandResult(MESSAGE_SUCCESS, | ||
UiUtil.ListViewModel.LOCAL_COURSE_SORT); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof LocalCourseSortCommand)) { | ||
return false; | ||
} | ||
|
||
LocalCourseSortCommand otherLocalCourseSortCommand = (LocalCourseSortCommand) other; | ||
return comparator.equals(otherLocalCourseSortCommand.comparator); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("localCourseAttributeToSort", comparator) | ||
.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
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
57 changes: 57 additions & 0 deletions
57
src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.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,57 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PARAMETER_ATTRIBUTE; | ||
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty; | ||
|
||
import java.util.Comparator; | ||
|
||
import seedu.address.logic.commands.LocalCourseSortCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.localcourse.LocalCourse; | ||
import seedu.address.model.localcourse.LocalCourseAttribute; | ||
import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalCode; | ||
import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalName; | ||
|
||
/** | ||
* Parses input arguments and creates a new LocalCourseSortCommand object. | ||
*/ | ||
public class LocalCourseSortCommandParser implements Parser<LocalCourseSortCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the LocalCourseSortCommand | ||
* and returns a LocalCourseSortCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format. | ||
*/ | ||
public LocalCourseSortCommand parse(String args) throws ParseException { | ||
if (!areValuesEnclosedAndNonEmpty(args)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
LocalCourseSortCommand.LOCAL_COURSE_SORT_MESSAGE_USAGE)); | ||
} | ||
|
||
SeplendidArgumentMap parameterToArgMap = | ||
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_ATTRIBUTE); | ||
|
||
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_ATTRIBUTE)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
LocalCourseSortCommand.LOCAL_COURSE_SORT_MESSAGE_USAGE)); | ||
} | ||
|
||
Comparator<LocalCourse> localCourseComparator = | ||
parseLocalCourseComparator(parameterToArgMap.getValue(PARAMETER_ATTRIBUTE).get()); | ||
|
||
return new LocalCourseSortCommand(localCourseComparator); | ||
} | ||
|
||
private Comparator<LocalCourse> parseLocalCourseComparator(String args) throws ParseException { | ||
LocalCourseAttribute localCourseAttribute = ParserUtil.parseLocalCourseAttribute(args); | ||
switch(localCourseAttribute) { | ||
case LOCALCODE: | ||
return new LocalCourseComparatorByLocalCode(); | ||
case LOCALNAME: | ||
return new LocalCourseComparatorByLocalName(); | ||
default: | ||
throw new ParseException(LocalCourseAttribute.MESSAGE_CONSTRAINTS); | ||
} | ||
} | ||
} |
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
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
27 changes: 27 additions & 0 deletions
27
src/main/java/seedu/address/model/localcourse/LocalCourseAttribute.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,27 @@ | ||
package seedu.address.model.localcourse; | ||
|
||
/** | ||
* Represents the attribute which is related to local course. | ||
*/ | ||
public enum LocalCourseAttribute { | ||
LOCALCODE, | ||
LOCALNAME; | ||
|
||
public static final String MESSAGE_CONSTRAINTS = | ||
"There are only 2 attributes: localcode and localname."; | ||
|
||
/** | ||
* Returns true if the given attribute is valid. | ||
* @param test String to be tested | ||
* @return true if attribute is valid. | ||
*/ | ||
public static boolean isValidAttribute(String test) { | ||
switch(test) { | ||
case ("LOCALCODE"): | ||
case ("LOCALNAME"): | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.