Skip to content

Commit

Permalink
Merge pull request #115 from dlathyun/dlathyun/sort-command
Browse files Browse the repository at this point in the history
Add sort command for LocalCourse
  • Loading branch information
alyssaongyx authored Oct 27, 2023
2 parents 3557ec5 + 15770d9 commit aec96af
Show file tree
Hide file tree
Showing 60 changed files with 1,454 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/SeplendidLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public interface SeplendidLogic {
*/
Path getLocalCourseCatalogueFilePath();

ObservableList<LocalCourse> getSortedLocalCourseCatalogue();

//=========== PartnerCourseCatalouge ============================================================================
ReadOnlyPartnerCourseCatalogue getPartnerCourseCatalogue();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/SeplendidLogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public Path getLocalCourseCatalogueFilePath() {
return model.getLocalCourseCatalogueFilePath();
}

@Override
public ObservableList<LocalCourse> getSortedLocalCourseCatalogue() {
return model.getSortedLocalCourseList();
}

//=========== PartnerCourseCatalouge ============================================================================
@Override
public ReadOnlyPartnerCourseCatalogue getPartnerCourseCatalogue() {
Expand Down
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();
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class CliSyntax {
public static final SeplendidParameter PARAMETER_PARTNERNAME = new SeplendidParameter("partnername");
public static final SeplendidParameter PARAMETER_PARTNERUNIT = new SeplendidParameter("partnerunit");
public static final SeplendidParameter PARAMETER_UNIVERSITY = new SeplendidParameter("university");
public static final SeplendidParameter PARAMETER_ATTRIBUTE = new SeplendidParameter("attribute");
public static final SeplendidParameter PARAMETER_UNIVERSITYNAME = new SeplendidParameter("university");
public static final SeplendidParameter PARAMETER_CONTENT = new SeplendidParameter("content");
public static final SeplendidParameter PARAMETER_TAGS = new SeplendidParameter("tags");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import seedu.address.model.localcourse.LocalUnit;

/**
* Parses input arguments and creates a new LocalCourse object.
* Parses input arguments and creates a new LocalCourseAddCommand object.
*/
public class LocalCourseAddCommandParser implements Parser<LocalCourseAddCommand> {

/**
* Parses the given {@code String} of arguments in the context of the LocalCourseAddCommand
* and returns an LocalCourseAddCommand object for execution.
* and returns a LocalCourseAddCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
Expand Down
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);
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.localcourse.LocalCode;
import seedu.address.model.localcourse.LocalCourseAttribute;
import seedu.address.model.localcourse.LocalName;
import seedu.address.model.localcourse.LocalUnit;
import seedu.address.model.mapping.MappingMiscInformation;
Expand Down Expand Up @@ -318,4 +319,33 @@ public static boolean areValuesEnclosedAndNonEmpty(String args) {
public static boolean areArgumentsPresent(SeplendidArgumentMap argumentMap, SeplendidParameter... parameters) {
return Stream.of(parameters).allMatch(parameter -> argumentMap.getValue(parameter).isPresent());
}

/**
* Parses a {@code String attribute}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code attribute} is invalid.
*/
public static LocalCourseAttribute parseLocalCourseAttribute(String attribute) throws ParseException {
requireNonNull(attribute);
String attributeLowerCase = attribute.toLowerCase();
String resultAttribute = attributeLowerCase;
switch(attributeLowerCase) {
case("localcode"):
resultAttribute = "LOCALCODE";
break;
case("localname"):
resultAttribute = "LOCALNAME";
break;
default:
break;
}

if (!LocalCourseAttribute.isValidAttribute(resultAttribute)) {
throw new ParseException(LocalCourseAttribute.MESSAGE_CONSTRAINTS);
}

return LocalCourseAttribute.valueOf(resultAttribute);

}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/parser/SeplendidParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.logic.commands.LocalCourseCommand;
import seedu.address.logic.commands.LocalCourseDeleteCommand;
import seedu.address.logic.commands.LocalCourseListCommand;
import seedu.address.logic.commands.LocalCourseSortCommand;
import seedu.address.logic.commands.MappingAddCommand;
import seedu.address.logic.commands.MappingCommand;
import seedu.address.logic.commands.MappingDeleteCommand;
Expand Down Expand Up @@ -143,6 +144,8 @@ private LocalCourseCommand getLocalCourseCommandWithArg(String userInput, String
return new LocalCourseAddCommandParser().parse(arguments);
case LocalCourseDeleteCommand.ACTION_WORD:
return new LocalCourseDeleteCommandParser().parse(arguments);
case LocalCourseSortCommand.ACTION_WORD:
return new LocalCourseSortCommandParser().parse(arguments);
default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/SeplendidModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model;

import java.nio.file.Path;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;

Expand Down Expand Up @@ -113,7 +114,11 @@ public interface SeplendidModel {
*/
void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCourse);

//=========== FilteredLocalCourseList Accessors =============================================================
//=========== Filtered/SortedLocalCourseList Accessors =============================================================

ObservableList<LocalCourse> getSortedLocalCourseList();

void updatedSortedLocalList(Comparator<LocalCourse> localCourseComparator);

/**
* Returns an unmodifiable view of the filtered local course list
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/seedu/address/model/SeplendidModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import static seedu.address.logic.commands.PartnerCourseDeleteCommand.MESSAGE_MAPPING_DEPENDENT_ON_PARTNER_COURSE;

import java.nio.file.Path;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.SeplendidLogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -35,6 +37,7 @@ public class SeplendidModelManager implements SeplendidModel {
private final LocalCourseCatalogue localCourseCatalogue;
private final UserPrefs userPrefs;
private final FilteredList<LocalCourse> filteredLocalCourseCatalogue;
private final SortedList<LocalCourse> sortedLocalCourseCatalogue;

private final UniversityCatalogue universityCatalogue;
private final FilteredList<University> filteredUniversityCatalogue;
Expand Down Expand Up @@ -71,6 +74,7 @@ public SeplendidModelManager(ReadOnlyLocalCourseCatalogue localCourseCatalogue,
this.localCourseCatalogue = new LocalCourseCatalogue(localCourseCatalogue);
this.userPrefs = new UserPrefs(userPrefs);
filteredLocalCourseCatalogue = new FilteredList<>(this.localCourseCatalogue.getLocalCourseList());
sortedLocalCourseCatalogue = new SortedList<>(this.localCourseCatalogue.getLocalCourseList());
this.partnerCourseCatalogue = new PartnerCourseCatalogue(partnerCourseCatalogue);
filteredPartnerCourseCatalogue = new FilteredList<>(this.partnerCourseCatalogue.getPartnerCourseList());
this.universityCatalogue = new UniversityCatalogue(universityCatalogue);
Expand All @@ -82,7 +86,7 @@ public SeplendidModelManager(ReadOnlyLocalCourseCatalogue localCourseCatalogue,
}

/**
* Contructs Seplendid Model Manager.
* Constructs Seplendid Model Manager.
*/
public SeplendidModelManager() {
this(new LocalCourseCatalogue(), new UserPrefs(), new PartnerCourseCatalogue(), new UniversityCatalogue(),
Expand Down Expand Up @@ -198,6 +202,16 @@ public void setLocalCourse(LocalCourse target, LocalCourse editedLocalCourse) {
localCourseCatalogue.setLocalCourse(target, editedLocalCourse);
}

@Override
public ObservableList<LocalCourse> getSortedLocalCourseList() {
return sortedLocalCourseCatalogue;
}

@Override
public void updatedSortedLocalList(Comparator<LocalCourse> localCourseComparator) {
sortedLocalCourseCatalogue.setComparator(localCourseComparator);
}

//=========== FilteredLocalCourseList Accessors =============================================================

/**
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/seedu/address/model/UniversityCatalogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import seedu.address.model.university.UniversityName;

/**
* Wraps universities data at the catalogue level
* Duplicates are not allowed (by .isSameUniversities comparison)
* Wraps universities data at the catalogue level.
* Duplicates are not allowed (by .isSameUniversities comparison).
*/

public class UniversityCatalogue implements ReadOnlyUniversityCatalogue {
Expand All @@ -28,7 +28,7 @@ public UniversityCatalogue() {
}

/**
* Creates an UniversityCatalogue using the Universities in the {@code toBeCopied}
* Creates an UniversityCatalogue using the Universities in the {@code toBeCopied}.
*/
public UniversityCatalogue(ReadOnlyUniversityCatalogue toBeCopied) {
this();
Expand All @@ -54,9 +54,18 @@ public Optional<University> getUniversityIfExists(UniversityName universityNameQ
return universities.getUniversityIfExists(universityNameQuery);
}

/**
* Adds university to university list.
* @param uN university to add.
*/
public void addUniversity(University uN) {
universities.add(uN);
}

public void removeUniversity(University uN) {
universities.remove(uN);
}

@Override
public String toString() {
return new ToStringBuilder(this)
Expand Down
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;
}
}
}
Loading

0 comments on commit aec96af

Please sign in to comment.