Skip to content

Commit 5fe1564

Browse files
authored
Merge pull request #109 from solomonng2001/fix-birthday-sorting
Fix birthday sorting regardless of year bug
2 parents a6418ce + 78b1ce0 commit 5fe1564

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

src/main/java/seedu/address/model/AddressBook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ public ObservableList<Person> getSchedules() {
149149
* @return list of persons who have upcoming birthdays
150150
*/
151151
public ObservableList<Person> getPersonsWithUpcomingBirthdays() {
152-
return persons.filterHaveUpcomingBirthday();
152+
return persons.sortFilterHaveUpcomingBirthday();
153153
}
154154
}

src/main/java/seedu/address/model/person/Birthday.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,16 @@ public int hashCode() {
8080
public int compareTo(Birthday other) {
8181
return date.compareTo(other.date);
8282
}
83+
84+
/**
85+
* Compares the month and date of this birthday with another birthday.
86+
* Ignores the year.
87+
*
88+
* @param other The other birthday to compare with
89+
* @return A negative integer, zero, or a positive integer as this birthday is before, at the same time, or after
90+
*/
91+
public int compareByBirthdayMonthAndDateOnly(Birthday other) {
92+
int currentYear = LocalDate.now().getYear();
93+
return date.withYear(currentYear).compareTo(other.date.withYear(currentYear));
94+
}
8395
}

src/main/java/seedu/address/model/person/UniquePersonList.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import javafx.collections.FXCollections;
1111
import javafx.collections.ObservableList;
12+
import javafx.collections.transformation.FilteredList;
13+
import javafx.collections.transformation.SortedList;
1214
import seedu.address.model.person.exceptions.DuplicatePersonException;
1315
import seedu.address.model.person.exceptions.PersonNotFoundException;
1416

@@ -184,14 +186,13 @@ public ObservableList<Person> getSchedules() {
184186
* Returns the list of persons who have upcoming birthdays, sorted by birthday.
185187
* @return list of persons who have upcoming birthdays
186188
*/
187-
public ObservableList<Person> filterHaveUpcomingBirthday() {
188-
ObservableList<Person> personsWithUpcomingBirthdays = FXCollections.observableArrayList();
189-
for (Person person : internalList) {
190-
if (person.hasUpcomingBirthday()) {
191-
personsWithUpcomingBirthdays.add(person);
192-
}
193-
}
194-
personsWithUpcomingBirthdays.sort(Comparator.comparing(Person::getBirthday));
195-
return personsWithUpcomingBirthdays;
189+
public ObservableList<Person> sortFilterHaveUpcomingBirthday() {
190+
FilteredList<Person> filteredPersonsWithUpcomingBirthdays = new FilteredList<>(internalList);
191+
filteredPersonsWithUpcomingBirthdays.setPredicate(Person::hasUpcomingBirthday);
192+
SortedList<Person> sortedFilteredPersonsWithUpcomingBirthdays =
193+
new SortedList<>(filteredPersonsWithUpcomingBirthdays);
194+
sortedFilteredPersonsWithUpcomingBirthdays.setComparator(
195+
Comparator.comparing(Person::getBirthday, Birthday::compareByBirthdayMonthAndDateOnly));
196+
return sortedFilteredPersonsWithUpcomingBirthdays;
196197
}
197198
}

src/test/java/seedu/address/model/person/BirthdayTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package seedu.address.model.person;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56
import static seedu.address.testutil.Assert.assertThrows;
@@ -121,4 +122,24 @@ public void equals() {
121122
// different values -> returns false
122123
assertFalse(birthday.equals(new Birthday("1993-09-23")));
123124
}
125+
126+
@Test
127+
public void compareByBirthdayMonthAndDateOnly() {
128+
Birthday birthday = new Birthday("1993-09-22");
129+
130+
// same values -> returns 0 regardless of year
131+
assertEquals(0, birthday.compareByBirthdayMonthAndDateOnly(new Birthday("1998-09-22")));
132+
133+
// different month -> returns negative integer regardless of year
134+
assertTrue(birthday.compareByBirthdayMonthAndDateOnly(new Birthday("1992-10-22")) < 0);
135+
136+
// different day -> returns negative integer regardless of year
137+
assertTrue(birthday.compareByBirthdayMonthAndDateOnly(new Birthday("1991-09-23")) < 0);
138+
139+
// different month -> returns positive integer regardless of year
140+
assertTrue(birthday.compareByBirthdayMonthAndDateOnly(new Birthday("2023-08-22")) > 0);
141+
142+
// different day -> returns positive integer regardless of year
143+
assertTrue(birthday.compareByBirthdayMonthAndDateOnly(new Birthday("2008-09-01")) > 0);
144+
}
124145
}

src/test/java/seedu/address/model/person/UniquePersonListTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
88
import static seedu.address.testutil.Assert.assertThrows;
99
import static seedu.address.testutil.TypicalPersons.ALICE;
10+
import static seedu.address.testutil.TypicalPersons.BENSON;
1011
import static seedu.address.testutil.TypicalPersons.BOB;
1112

1213
import java.time.LocalDate;
@@ -178,6 +179,29 @@ public void toStringMethod() {
178179

179180
@Test
180181
public void filterHaveUpcomingBirthday() {
182+
Person personWithUpcomingBirthdaySecond = new PersonBuilder(ALICE).withBirthday(
183+
DateUtil.parseDateToString(LocalDate.now().plusWeeks(1)
184+
.minusYears(40))).build();
185+
Person personWithNoUpcomingBirthday = new PersonBuilder(BOB).withBirthday(
186+
DateUtil.parseDateToString(LocalDate.now().minusDays(1)
187+
.minusYears(30))).build();
188+
Person personWithUpcomingBirthdayFirst = new PersonBuilder(BENSON).withBirthday(
189+
DateUtil.parseDateToString(LocalDate.now().plusDays(3)
190+
.minusYears(30))).build();
191+
192+
UniquePersonList newPersonsList = new UniquePersonList();
193+
newPersonsList.add(personWithUpcomingBirthdaySecond);
194+
newPersonsList.add(personWithNoUpcomingBirthday);
195+
newPersonsList.add(personWithUpcomingBirthdayFirst);
196+
ObservableList<Person> birthdayList = newPersonsList.sortFilterHaveUpcomingBirthday();
197+
198+
assertEquals(2, birthdayList.size());
199+
assertEquals(personWithUpcomingBirthdayFirst, birthdayList.get(0));
200+
assertEquals(personWithUpcomingBirthdaySecond, birthdayList.get(1));
201+
}
202+
203+
@Test
204+
public void sortFilterHaveUpcomingBirthday() {
181205
Person personWithUpcomingBirthday = new PersonBuilder(ALICE).withBirthday(
182206
DateUtil.parseDateToString(LocalDate.now().plusWeeks(1)
183207
.minusYears(40))).build();
@@ -188,7 +212,7 @@ public void filterHaveUpcomingBirthday() {
188212
UniquePersonList newPersonsList = new UniquePersonList();
189213
newPersonsList.add(personWithUpcomingBirthday);
190214
newPersonsList.add(personWithNoUpcomingBirthday);
191-
ObservableList<Person> birthdayList = newPersonsList.filterHaveUpcomingBirthday();
215+
ObservableList<Person> birthdayList = newPersonsList.sortFilterHaveUpcomingBirthday();
192216

193217
assertEquals(1, birthdayList.size());
194218
assertTrue(birthdayList.contains(personWithUpcomingBirthday));

0 commit comments

Comments
 (0)