Skip to content

Commit

Permalink
Merge pull request #118 from victorpengmx/branch-fix-list-commands
Browse files Browse the repository at this point in the history
Fix list commands
  • Loading branch information
victorpengmx authored Nov 1, 2023
2 parents 0e00007 + dfd11d8 commit 0f49297
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/main/java/connectify/logic/commands/ListAllCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import static connectify.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static java.util.Objects.requireNonNull;

import java.util.Comparator;

import connectify.model.Model;
import connectify.model.person.Person;
import connectify.model.person.PersonNameComparator;

/**
* Lists all entities (both persons and companies) in the address book to the user.
Expand All @@ -22,6 +26,8 @@ public class ListAllCommand extends Command {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Comparator<Person> comparator = new PersonNameComparator();
model.updateSortedPersonList(comparator);
model.updateFilteredCompanyList(PREDICATE_SHOW_ALL_COMPANIES);
model.updateToAllEntities();
if (model.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import static connectify.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static java.util.Objects.requireNonNull;

import java.util.Comparator;

import connectify.model.Model;
import connectify.model.person.Person;
import connectify.model.person.PersonNameComparator;

/**
* Lists all persons in the address book to the user.
Expand All @@ -16,6 +20,8 @@ public class ListPeopleCommand extends Command {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Comparator<Person> comparator = new PersonNameComparator();
model.updateSortedPersonList(comparator);
Integer size = model.getNumberOfPeople();
if (size == 0) {
return new CommandResult(EMPTY_LIST_MESSAGE);
Expand All @@ -38,4 +44,5 @@ public boolean equals(Object other) {
public String toString() {
return "ListPeopleCommand";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import connectify.model.Model;
import connectify.model.person.Person;
import connectify.model.person.PersonPriorityComparator;

/**
* Lists all persons in the address book to the user according to their priority.
Expand All @@ -23,7 +24,7 @@ public class RankPersonCommand extends Command {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Comparator<Person> comparator = Comparator.comparing(Person::rank);
Comparator<Person> comparator = new PersonPriorityComparator();
model.updateSortedPersonList(comparator);
Integer size = model.getNumberOfPeople();
if (size == 0) {
Expand All @@ -49,4 +50,5 @@ public boolean equals(Object other) {
public String toString() {
return "RankPersonCommand";
}

}
14 changes: 3 additions & 11 deletions src/main/java/connectify/model/person/Person.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package connectify.model.person;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
Expand All @@ -15,7 +14,7 @@
* Represents a Person in the address book.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Person extends Entity implements Comparator<Person> {
public class Person extends Entity {

// Identity fields
private final PersonName name;
Expand Down Expand Up @@ -152,15 +151,8 @@ public String toString() {
.toString();
}

@Override
public int compare(Person o1, Person o2) {
if (o1.getPriority().getValue() < o2.getPriority().getValue()) {
return -1;
} else if (o1.getPriority().getValue() > o2.getPriority().getValue()) {
return 1;
} else {
return 0;
}
public int compareNameWith(Person o) {
return this.getName().compareTo(o.getName());
}

public int rank() {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/connectify/model/person/PersonName.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class PersonName extends Name {
public class PersonName extends Name implements Comparable<Name> {

/**
* Constructs a {@code Name}.
Expand All @@ -17,4 +17,9 @@ public PersonName(String name) {
super(name);
}

@Override
public int compareTo(Name o) {
return this.toString().toLowerCase().compareTo(o.toString().toLowerCase());
}

}
14 changes: 14 additions & 0 deletions src/main/java/connectify/model/person/PersonNameComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package connectify.model.person;

import java.util.Comparator;

/**
* Compares a {@code Person} to another using the alphabetical ordering of their {@code Name}.
*/
public class PersonNameComparator implements Comparator<Person> {

@Override
public int compare(Person o1, Person o2) {
return o1.compareNameWith(o2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package connectify.model.person;

import java.util.Comparator;

/**
* Compares a {@code Person} to another using their {@code Priority}.
*/
public class PersonPriorityComparator implements Comparator<Person> {

@Override
public int compare(Person o1, Person o2) {
if (o1.getPriority().getValue() < o2.getPriority().getValue()) {
return 1;
} else if (o1.getPriority().getValue() > o2.getPriority().getValue()) {
return -1;
} else {
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static connectify.logic.commands.CommandTestUtil.assertCommandSuccess;
import static connectify.logic.commands.CommandTestUtil.showPersonAtIndex;
import static connectify.testutil.RankedPersons.getRankedAddressBook;
import static connectify.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static connectify.testutil.TypicalPersons.getTypicalAddressBook;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -16,7 +17,7 @@
import connectify.model.UserPrefs;

/**
* Contains integration tests (interaction with the Model) and unit tests for ListPeopleCommand.
* Contains integration tests (interaction with the Model) and unit tests for RankPersonCommand.
*/
public class RankPersonCommandTest {

Expand All @@ -26,7 +27,7 @@ public class RankPersonCommandTest {
@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel = new ModelManager(getRankedAddressBook(), new UserPrefs());
}

@Test
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/connectify/model/person/NameTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package connectify.model.person;

import static connectify.testutil.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -57,4 +58,14 @@ public void equals() {
// different values -> returns false
assertFalse(name.equals(new PersonName("Other Valid Name")));
}

@Test
public void compareTo() {
PersonName name1 = new PersonName("Alice");
PersonName name2 = new PersonName("Bob");

assertEquals(name1.compareTo(name2), -1);
assertEquals(name1.compareTo(name1), 0);
assertEquals(name2.compareTo(name1), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package connectify.model.person;

import static connectify.testutil.TypicalPersons.ALICE;
import static connectify.testutil.TypicalPersons.BOB;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Comparator;

import org.junit.jupiter.api.Test;

/**
* Compares a {@code Person} to another using the alphabetical ordering of their {@code Name}.
*/
public class PersonNameComparatorTest {

@Test
public void compare() {
Comparator<Person> comparator = new PersonNameComparator();
assertEquals(comparator.compare(ALICE, BOB), -1);
assertEquals(comparator.compare(ALICE, ALICE), 0);
assertEquals(comparator.compare(BOB, ALICE), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package connectify.model.person;

import static connectify.testutil.TypicalPersons.ALICE;
import static connectify.testutil.TypicalPersons.BOB;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Comparator;

import org.junit.jupiter.api.Test;

/**
* Compares a {@code Person} to another using their {@code Priority}.
*/
public class PersonPriorityComparatorTest {

@Test
public void compare() {
Comparator<Person> comparator = new PersonPriorityComparator();
assertEquals(comparator.compare(ALICE, BOB), 1);
assertEquals(comparator.compare(ALICE, ALICE), 0);
assertEquals(comparator.compare(BOB, ALICE), -1);
}
}
8 changes: 4 additions & 4 deletions src/test/java/connectify/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public void toStringMethod() {
}

@Test
public void compare() {
assertEquals(ALICE.compare(ALICE, BOB), -1);
assertEquals(ALICE.compare(BOB, ALICE), 1);
assertEquals(HOON.compare(HOON, GEORGE), 0);
public void compareNameWith() {
assertEquals(ALICE.compareNameWith(BOB), -1);
assertEquals(ALICE.compareNameWith(ALICE), 0);
assertEquals(HOON.compareNameWith(GEORGE), 1);
}

@Test
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/connectify/testutil/RankedPersons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package connectify.testutil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import connectify.logic.commands.CommandTestUtil;
import connectify.model.AddressBook;
import connectify.model.company.Company;
import connectify.model.person.Person;

/**
* A utility class containing a list of {@code Person} objects to be used in tests.
*/
public class RankedPersons {

public static final Person ALICE = new PersonBuilder().withName("Alice Pauline")
.withAddress("123, Jurong West Ave 6, #08-111").withEmail("[email protected]")
.withPhone("94351253")
.withNote("")
.withTags("friends")
.withPriority("1")
.build();
public static final Person BENSON = new PersonBuilder().withName("Benson Meier")
.withAddress("311, Clementi Ave 2, #02-25")
.withEmail("[email protected]").withPhone("98765432").withNote("")
.withTags("owesMoney", "friends").withPriority("2").build();
public static final Person CARL = new PersonBuilder().withName("Carl Kurz").withPhone("95352563")
.withEmail("[email protected]").withAddress("wall street").withPriority("3").build();
public static final Person DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533")
.withEmail("[email protected]").withAddress("10th street").withPriority("4").withTags("friends").build();
public static final Person ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("9482224")
.withEmail("[email protected]").withAddress("michegan ave").withPriority("5").build();
public static final Person FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("9482427")
.withEmail("[email protected]").withAddress("little tokyo").withPriority("6").build();
public static final Person GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442")
.withEmail("[email protected]").withAddress("4th street").withPriority("7").build();

// Manually added
public static final Person HOON = new PersonBuilder().withName("Hoon Meier").withPhone("8482424")
.withEmail("[email protected]").withAddress("little india").withPriority("7").build();
public static final Person IDA = new PersonBuilder().withName("Ida Mueller").withPhone("8482131")
.withEmail("[email protected]").withAddress("chicago ave").withPriority("8").build();

// Manually added - Person's details found in {@code CommandTestUtil}
public static final Person AMY = new PersonBuilder().withName(CommandTestUtil.VALID_NAME_AMY)
.withPhone(CommandTestUtil.VALID_PHONE_AMY).withEmail(CommandTestUtil.VALID_EMAIL_AMY)
.withAddress(CommandTestUtil.VALID_ADDRESS_AMY).withTags(CommandTestUtil.VALID_TAG_FRIEND)
.withPriority(CommandTestUtil.VALID_PRIORITY_AMY).build();
public static final Person BOB = new PersonBuilder().withName(CommandTestUtil.VALID_NAME_BOB)
.withPhone(CommandTestUtil.VALID_PHONE_BOB).withEmail(CommandTestUtil.VALID_EMAIL_BOB)
.withAddress(CommandTestUtil.VALID_ADDRESS_BOB).withTags(CommandTestUtil.VALID_TAG_HUSBAND,
CommandTestUtil.VALID_TAG_FRIEND).withPriority(CommandTestUtil.VALID_PRIORITY_BOB).build();

public static final String KEYWORD_MATCHING_MEIER = "Meier"; // A keyword that matches MEIER

private RankedPersons() {} // prevents instantiation

/**
* Returns an {@code AddressBook} with all the ranked persons.
*/
public static AddressBook getRankedAddressBook() {
AddressBook ab = new AddressBook();
for (Person person : getRankedPersons()) {
ab.addPerson(person);
}
Company company1 = TypicalCompanies.DUMMY_COMPANY;
company1 = company1.addPersonToCompany(ALICE);
company1 = company1.addPersonToCompany(BENSON);
ab.addCompany(company1);

Company company2 = TypicalCompanies.COMPANY_1;
company2 = company2.addPersonToCompany(CARL);
company2 = company2.addPersonToCompany(DANIEL);
ab.addCompany(company2);

Company company3 = TypicalCompanies.COMPANY_2;
company3 = company3.addPersonToCompany(DANIEL);
company3 = company3.addPersonToCompany(FIONA);
ab.addCompany(company3);
return ab;
}

public static List<Person> getRankedPersons() {
return new ArrayList<>(Arrays.asList(GEORGE, FIONA, ELLE, DANIEL, CARL, BENSON, ALICE));
}
}
2 changes: 1 addition & 1 deletion src/test/java/connectify/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public static AddressBook getTypicalAddressBook() {
ab.addCompany(company2);

Company company3 = TypicalCompanies.COMPANY_2;
company3 = company3.addPersonToCompany(FIONA);
company3 = company3.addPersonToCompany(DANIEL);
company3 = company3.addPersonToCompany(FIONA);
ab.addCompany(company3);
return ab;
}
Expand Down

0 comments on commit 0f49297

Please sign in to comment.