diff --git a/src/main/java/staffconnect/logic/parser/SortCommandParser.java b/src/main/java/staffconnect/logic/parser/SortCommandParser.java index 8864ea5bb93..6da01a05bc6 100644 --- a/src/main/java/staffconnect/logic/parser/SortCommandParser.java +++ b/src/main/java/staffconnect/logic/parser/SortCommandParser.java @@ -11,7 +11,10 @@ import staffconnect.logic.commands.SortCommand; import staffconnect.logic.parser.exceptions.ParseException; import staffconnect.model.person.Person; - +import staffconnect.model.person.comparators.ModuleComparator; +import staffconnect.model.person.comparators.NameComparator; +import staffconnect.model.person.comparators.PhoneComparator; +import staffconnect.model.person.comparators.VenueComparator; /** * Parses input arguments and creates a new SortCommand object @@ -37,39 +40,18 @@ public SortCommand parse(String args) throws ParseException { private Comparator parseComparatorForKeywords(String keyword) throws ParseException { if (keyword.equals(PREFIX_NAME.getPrefix())) { - return new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getName().toString().compareToIgnoreCase(person2.getName().toString()); - } - }; + return NameComparator.NAME_COMPARATOR; } else if (keyword.equals(PREFIX_PHONE.getPrefix())) { - return new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getPhone().toString().compareToIgnoreCase(person2.getPhone().toString()); - } - }; + return PhoneComparator.PHONE_COMPARATOR; } else if (keyword.equals(PREFIX_MODULE.getPrefix())) { - return new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getModule().toString().compareToIgnoreCase(person2.getModule().toString()); - } - }; + return ModuleComparator.MODULE_COMPARATOR; } else if (keyword.equals(PREFIX_VENUE.getPrefix())) { - return new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getVenue().toString().compareToIgnoreCase(person2.getVenue().toString()); - } - }; + return VenueComparator.VENUE_COMPARATOR; } else { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); } + } - - } } diff --git a/src/main/java/staffconnect/model/person/comparators/ModuleComparator.java b/src/main/java/staffconnect/model/person/comparators/ModuleComparator.java new file mode 100644 index 00000000000..5c14dc386b8 --- /dev/null +++ b/src/main/java/staffconnect/model/person/comparators/ModuleComparator.java @@ -0,0 +1,18 @@ +package staffconnect.model.person.comparators; + +import java.util.Comparator; + +import staffconnect.model.person.Person; + +/** + * Represents a Comparator for a Person's Module code in the staff book. + */ +public class ModuleComparator implements Comparator { + + public static final ModuleComparator MODULE_COMPARATOR = new ModuleComparator(); + + @Override + public int compare(Person p1, Person p2) { + return p1.getModule().toString().compareToIgnoreCase(p2.getModule().toString()); + } +} diff --git a/src/main/java/staffconnect/model/person/comparators/NameComparator.java b/src/main/java/staffconnect/model/person/comparators/NameComparator.java new file mode 100644 index 00000000000..ea9685aeac0 --- /dev/null +++ b/src/main/java/staffconnect/model/person/comparators/NameComparator.java @@ -0,0 +1,17 @@ +package staffconnect.model.person.comparators; + +import java.util.Comparator; + +import staffconnect.model.person.Person; + +/** + * Represents a Comparator for a Person's Name in the staff book. + */ +public class NameComparator implements Comparator { + + public static final NameComparator NAME_COMPARATOR = new NameComparator(); + @Override + public int compare(Person p1, Person p2) { + return p1.getName().toString().compareToIgnoreCase(p2.getName().toString()); + } +} diff --git a/src/main/java/staffconnect/model/person/comparators/PhoneComparator.java b/src/main/java/staffconnect/model/person/comparators/PhoneComparator.java new file mode 100644 index 00000000000..ea833f9d85c --- /dev/null +++ b/src/main/java/staffconnect/model/person/comparators/PhoneComparator.java @@ -0,0 +1,18 @@ +package staffconnect.model.person.comparators; + +import java.util.Comparator; + +import staffconnect.model.person.Person; + +/** + * Represents a Comparator for a Person's Phone number in the staff book. + */ +public class PhoneComparator implements Comparator { + + public static final PhoneComparator PHONE_COMPARATOR = new PhoneComparator(); + + @Override + public int compare(Person p1, Person p2) { + return p1.getPhone().toString().compareToIgnoreCase(p2.getPhone().toString()); + } +} diff --git a/src/main/java/staffconnect/model/person/comparators/VenueComparator.java b/src/main/java/staffconnect/model/person/comparators/VenueComparator.java new file mode 100644 index 00000000000..32abe427110 --- /dev/null +++ b/src/main/java/staffconnect/model/person/comparators/VenueComparator.java @@ -0,0 +1,18 @@ +package staffconnect.model.person.comparators; + +import java.util.Comparator; + +import staffconnect.model.person.Person; + +/** + * Represents a Comparator for a Person's Venue in the staff book. + */ +public class VenueComparator implements Comparator { + + public static final VenueComparator VENUE_COMPARATOR = new VenueComparator(); + + @Override + public int compare(Person p1, Person p2) { + return p1.getVenue().toString().compareToIgnoreCase(p2.getVenue().toString()); + } +} diff --git a/src/test/java/staffconnect/logic/parser/SortCommandParserTest.java b/src/test/java/staffconnect/logic/parser/SortCommandParserTest.java index 7c1ff11c1b0..a2b01504600 100644 --- a/src/test/java/staffconnect/logic/parser/SortCommandParserTest.java +++ b/src/test/java/staffconnect/logic/parser/SortCommandParserTest.java @@ -5,12 +5,10 @@ import static staffconnect.logic.parser.CommandParserTestUtil.assertParseFailure; import static staffconnect.logic.parser.CommandParserTestUtil.assertParseSuccess; -import java.util.Comparator; - import org.junit.jupiter.api.Test; import staffconnect.logic.commands.SortCommand; -import staffconnect.model.person.Person; +import staffconnect.model.person.comparators.VenueComparator; public class SortCommandParserTest { @@ -23,13 +21,9 @@ public void parse_emptyArg_throwsParseException() { @Test public void parse_validArgs_returnsSortCommand() { - Comparator temp = new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getVenue().toString().compareToIgnoreCase(person2.getVenue().toString()); - } - }; - SortCommand expectedSortCommand = new SortCommand(temp); + + SortCommand expectedSortCommand = new SortCommand(VenueComparator.VENUE_COMPARATOR); + // no leading and trailing whitespaces assertParseSuccess(parser, " v/ ", expectedSortCommand); diff --git a/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java b/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java index efd8375a3cb..c786c8303dc 100644 --- a/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java +++ b/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java @@ -8,7 +8,6 @@ import static staffconnect.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import java.util.Arrays; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -31,6 +30,10 @@ import staffconnect.model.person.NameContainsKeywordsPredicate; import staffconnect.model.person.Person; import staffconnect.model.person.PersonHasTagsPredicate; +import staffconnect.model.person.comparators.ModuleComparator; +import staffconnect.model.person.comparators.NameComparator; +import staffconnect.model.person.comparators.PhoneComparator; +import staffconnect.model.person.comparators.VenueComparator; import staffconnect.model.tag.Tag; import staffconnect.testutil.EditPersonDescriptorBuilder; import staffconnect.testutil.PersonBuilder; @@ -98,36 +101,10 @@ public void parseCommand_sort() throws Exception { SortCommand phoneSortCommand = (SortCommand) parser.parseCommand(SortCommand.COMMAND_WORD + " " + "p/"); SortCommand venueSortCommand = (SortCommand) parser.parseCommand(SortCommand.COMMAND_WORD + " " + "v/"); SortCommand moduleSortCommand = (SortCommand) parser.parseCommand(SortCommand.COMMAND_WORD + " " + "m/"); - Comparator nameComparator = new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getName().toString().compareToIgnoreCase(person2.getName().toString()); - } - }; - Comparator phoneComparator = new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getPhone().toString().compareToIgnoreCase(person2.getPhone().toString()); - } - }; - - Comparator venueComparator = new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getVenue().toString().compareToIgnoreCase(person2.getVenue().toString()); - } - }; - - Comparator moduleComparator = new Comparator() { - @Override - public int compare(Person person1, Person person2) { - return person1.getModule().toString().compareToIgnoreCase(person2.getModule().toString()); - } - }; - // assertEquals(new SortCommand(nameComparator), nameSortCommand); // name - assertEquals(new SortCommand(phoneComparator), phoneSortCommand); // phone - assertEquals(new SortCommand(venueComparator), venueSortCommand); // venue - assertEquals(new SortCommand(moduleComparator), moduleSortCommand); // module + assertEquals(new SortCommand(NameComparator.NAME_COMPARATOR), nameSortCommand); // name + assertEquals(new SortCommand(PhoneComparator.PHONE_COMPARATOR), phoneSortCommand); // phone + assertEquals(new SortCommand(VenueComparator.VENUE_COMPARATOR), venueSortCommand); // venue + assertEquals(new SortCommand(ModuleComparator.MODULE_COMPARATOR), moduleSortCommand); // module } @Test