Skip to content

Commit

Permalink
Add comparators and static for consistent reference -- JUNI now works
Browse files Browse the repository at this point in the history
  • Loading branch information
whitesnowx committed Mar 15, 2024
1 parent 57eecdb commit 0ffa824
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 68 deletions.
36 changes: 9 additions & 27 deletions src/main/java/staffconnect/logic/parser/SortCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,39 +40,18 @@ public SortCommand parse(String args) throws ParseException {
private Comparator<Person> parseComparatorForKeywords(String keyword) throws ParseException {

if (keyword.equals(PREFIX_NAME.getPrefix())) {
return new Comparator<Person>() {
@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<Person>() {
@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<Person>() {
@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<Person>() {
@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));
}

}



}
}
Original file line number Diff line number Diff line change
@@ -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<Person> {

public static final ModuleComparator MODULE_COMPARATOR = new ModuleComparator();

@Override
public int compare(Person p1, Person p2) {
return p1.getModule().toString().compareToIgnoreCase(p2.getModule().toString());

Check warning on line 16 in src/main/java/staffconnect/model/person/comparators/ModuleComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/comparators/ModuleComparator.java#L16

Added line #L16 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -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<Person> {

public static final NameComparator NAME_COMPARATOR = new NameComparator();
@Override
public int compare(Person p1, Person p2) {
return p1.getName().toString().compareToIgnoreCase(p2.getName().toString());
}

Check warning on line 16 in src/main/java/staffconnect/model/person/comparators/NameComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/comparators/NameComparator.java#L15-L16

Added lines #L15 - L16 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -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<Person> {

public static final PhoneComparator PHONE_COMPARATOR = new PhoneComparator();

@Override
public int compare(Person p1, Person p2) {
return p1.getPhone().toString().compareToIgnoreCase(p2.getPhone().toString());
}
}
Original file line number Diff line number Diff line change
@@ -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<Person> {

public static final VenueComparator VENUE_COMPARATOR = new VenueComparator();

@Override
public int compare(Person p1, Person p2) {
return p1.getVenue().toString().compareToIgnoreCase(p2.getVenue().toString());

Check warning on line 16 in src/main/java/staffconnect/model/person/comparators/VenueComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/comparators/VenueComparator.java#L16

Added line #L16 was not covered by tests
}
}
14 changes: 4 additions & 10 deletions src/test/java/staffconnect/logic/parser/SortCommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -23,13 +21,9 @@ public void parse_emptyArg_throwsParseException() {

@Test
public void parse_validArgs_returnsSortCommand() {
Comparator<Person> temp = new Comparator<Person>() {
@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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Person> nameComparator = new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
return person1.getName().toString().compareToIgnoreCase(person2.getName().toString());
}
};
Comparator<Person> phoneComparator = new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
return person1.getPhone().toString().compareToIgnoreCase(person2.getPhone().toString());
}
};

Comparator<Person> venueComparator = new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
return person1.getVenue().toString().compareToIgnoreCase(person2.getVenue().toString());
}
};

Comparator<Person> moduleComparator = new Comparator<Person>() {
@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
Expand Down

0 comments on commit 0ffa824

Please sign in to comment.