Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify dateOfLastVisit to use LocalDate instead of String #171

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions src/main/java/seedu/address/model/person/DateOfLastVisit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,26 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;


import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

/**
* Represents a Person's last visited date by the social worker in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidDateOfLastVisit(String)}
*/
public class DateOfLastVisit implements Comparable<DateOfLastVisit> {

public static final String MESSAGE_CONSTRAINTS = "Date of last visit should be in dd-MM-yyyy format.";

// THIS VALIDATION REGEX WAS GENERATED BY CHATGPT - ACKNOWLEDGE IN DG
// date must be in the format dd-MM-yyyy
public static final String VALIDATION_REGEX =
"^((0[1-9]|[12][0-9]|3[01])-(01|03|05|07|08|10|12)-\\d{4})|"
+ "((0[1-9]|[12][0-9]|30)-(04|06|09|11)-\\d{4})|"
+ "((0[1-9]|1[0-9]|2[0-8])-02-\\d{4})|"
+ "(29-02-(?:(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26]))|(?:[0-9]{2}00)))$";
// 31st day of months with 31 days
// 30th day of months with 30 days
// 01-29 for February (ignores leap year handling)
// Days 01-31 for other months
// -------------------
public static final String MESSAGE_CONSTRAINTS = "Date of last visit should be in dd-MM-yyyy format.\n"
+ "Ensure the date entered is not later than today. ";

public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-uuuu")
.withResolverStyle(ResolverStyle.STRICT);

public final String value;

private final String[] dayMonthYear;
private final LocalDate localDate;

/**
* Constructs a {@code DateOfLastVisit}.
Expand All @@ -39,37 +33,37 @@ public DateOfLastVisit(String date) {
requireNonNull(date);
checkArgument(isValidDateOfLastVisit(date), MESSAGE_CONSTRAINTS);
value = date;
String[] init = {date.substring(0, 2), date.substring(3, 5), date.substring(6)};
this.dayMonthYear = init;
localDate = LocalDate.parse(date, DATE_TIME_FORMATTER);
}

/**
* Returns true if a given string is a valid date of last visit.
*/
public static boolean isValidDateOfLastVisit(String test) {
return test.matches(VALIDATION_REGEX);
public static boolean isValidDateOfLastVisit(String date) {
try {
LocalDate.parse(date, DATE_TIME_FORMATTER);
} catch (DateTimeException e) {
return false;
}
return isTodayOrEarlierThanToday(date);
}

/**
* Returns true if date is today or earlier than today's date.
*/
public static boolean isTodayOrEarlierThanToday(String date) {
LocalDate today = LocalDate.now();
return today.isEqual(LocalDate.parse(date, DATE_TIME_FORMATTER))
|| today.isAfter(LocalDate.parse(date, DATE_TIME_FORMATTER));
}

/**
* Compares two dates of last visit by year then month then day to find
* the right order.
*
* @param other the date to be compared to.
* @return an integer which specifies which date is earlier or if they are equal.
*/
@Override
public int compareTo(DateOfLastVisit other) {
int yearComparison = this.dayMonthYear[2].compareTo(other.dayMonthYear[2]);
if (yearComparison == 0) {
int monthComparsion = this.dayMonthYear[1].compareTo(other.dayMonthYear[1]);
if (monthComparsion == 0) {
return this.dayMonthYear[0].compareTo(other.dayMonthYear[0]);
} else {
return monthComparsion;
}
} else {
return yearComparison;
}
return localDate.compareTo(other.localDate);
}

@Override
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/seedu/address/model/person/PersonComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
public class PersonComparator {
public static final String NAME = "name";
public static final String DATE_OF_LAST_VISIT = "date of last visit";
public static final String EARLIEST_VALID_DATE = "01-01-0001";
public static final String LATEST_VALID_DATE = "31-12-9999";
private static final String SORT_EXCEPTION = "The specified parameter is invalid.";

public PersonComparator() {
Expand Down Expand Up @@ -40,7 +38,7 @@
if (isAscending) {
return new PersonDateOfLastVisitAscendingComparator();
} else {
return new PersonDateOfLastVisitDescendingComparator().reversed();
return new PersonDateOfLastVisitDescendingComparator();
}

default:
Expand Down Expand Up @@ -69,22 +67,36 @@
class PersonDateOfLastVisitAscendingComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.getDateOfLastVisit().orElse(new DateOfLastVisit(PersonComparator.LATEST_VALID_DATE))
.compareTo(p2.getDateOfLastVisit().orElse(new DateOfLastVisit(PersonComparator.LATEST_VALID_DATE)));
if (p1.getDateOfLastVisit().isEmpty() && p2.getDateOfLastVisit().isEmpty()) {
return 0;

Check warning on line 71 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L71

Added line #L71 was not covered by tests
}
if (p1.getDateOfLastVisit().isEmpty()) {
return 1;

Check warning on line 74 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L74

Added line #L74 was not covered by tests
}
if (p2.getDateOfLastVisit().isEmpty()) {
return -1;

Check warning on line 77 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L77

Added line #L77 was not covered by tests
}
return p1.getDateOfLastVisit().get().compareTo(p2.getDateOfLastVisit().get());
}
}

/**
* Represents a class for comparing persons by DateOfLastVisit.
* Where the person has no date of last visit they will be in the front
* of the list given ascending order, therefore reversing this comparator
* gives a descending order whereby persons with no date of last visit
* are in the back.
* Where the person has no date of last visit they will be in the back
* of the list given descending order.
*/
class PersonDateOfLastVisitDescendingComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.getDateOfLastVisit().orElse(new DateOfLastVisit(PersonComparator.EARLIEST_VALID_DATE))
.compareTo(p2.getDateOfLastVisit().orElse(new DateOfLastVisit(PersonComparator.EARLIEST_VALID_DATE)));
if (p1.getDateOfLastVisit().isEmpty() && p2.getDateOfLastVisit().isEmpty()) {
return 0;

Check warning on line 92 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L92

Added line #L92 was not covered by tests
}
if (p1.getDateOfLastVisit().isEmpty()) {
return 1;

Check warning on line 95 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L95

Added line #L95 was not covered by tests
}
if (p2.getDateOfLastVisit().isEmpty()) {
return -1;

Check warning on line 98 in src/main/java/seedu/address/model/person/PersonComparator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/PersonComparator.java#L98

Added line #L98 was not covered by tests
}
return p2.getDateOfLastVisit().get().compareTo(p1.getDateOfLastVisit().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"email" : "[email protected]",
"address" : "wall street",
"tags" : [ ],
"dateOfLastVisit" : "01-01-2025",
"dateOfLastVisit" : "01-04-2024",
"emergencyContact" : "",
"remark" : ""
}, {
Expand All @@ -33,7 +33,7 @@
"email" : "[email protected]",
"address" : "10th street",
"tags" : [ "friends" ],
"dateOfLastVisit" : "02-01-2024",
"dateOfLastVisit" : "02-04-2024",
"emergencyContact" : "",
"remark" : ""
}, {
Expand All @@ -42,7 +42,7 @@
"email" : "[email protected]",
"address" : "michegan ave",
"tags" : [ ],
"dateOfLastVisit" : "10-10-2024",
"dateOfLastVisit" : "10-07-2024",
"emergencyContact" : "",
"remark" : ""
}, {
Expand All @@ -60,7 +60,7 @@
"email" : "[email protected]",
"address" : "4th street",
"tags" : [ ],
"dateOfLastVisit" : "05-06-2024",
"dateOfLastVisit" : "05-09-2023",
"emergencyContact" : "",
"remark" : ""
} ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void executeSuccessfulSortTest(String sortParameter, boolean isAscending
String expectedMessage = String.format(SortCommand.MESSAGE_SUCCESS,
sortParameter, isAscending ? "ascending" : "descending");
Model expectedModel = new ModelManager(getTypicalAddressBook(sortParameter, isAscending), new UserPrefs());
assertCommandSuccess(sortCommand, modelDescendingName, expectedMessage, expectedModel);
assertCommandSuccess(sortCommand, model, expectedMessage, expectedModel);
}

@Test
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/model/person/DateOfLastVisitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

import java.time.LocalDate;

import org.junit.jupiter.api.Test;


public class DateOfLastVisitTest {

@Test
Expand Down Expand Up @@ -36,6 +39,14 @@ public void isValidDateOfLastVisit() {
assertFalse(DateOfLastVisit.isValidDateOfLastVisit("31-04-2024")); // 31st of even month
assertFalse(DateOfLastVisit.isValidDateOfLastVisit("29-02-2023")); // 29th feb of non leap year

// date later than today
String tomorrow = LocalDate.now().plusDays(1).format(DateOfLastVisit.DATE_TIME_FORMATTER);
assertFalse(DateOfLastVisit.isValidDateOfLastVisit(tomorrow)); // date is later than today

// date is today
String today = LocalDate.now().format(DateOfLastVisit.DATE_TIME_FORMATTER);
assertTrue(DateOfLastVisit.isValidDateOfLastVisit(today)); // date is equal to today

// valid dateOfLastVisits
assertTrue(DateOfLastVisit.isValidDateOfLastVisit("01-01-0000")); // year 0000
assertTrue(DateOfLastVisit.isValidDateOfLastVisit("31-01-2024")); // month with 31st
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/seedu/address/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public class TypicalPersons {
.withTags("owesMoney", "friends").withDateOfLastVisit("01-02-2024")
.withEmergencyContact("92173902").build();
public static final Person CARL = new PersonBuilder().withName("Carl Kurz").withPhone("95352563")
.withEmail("[email protected]").withAddress("wall street").withDateOfLastVisit("01-01-2025")
.withEmail("[email protected]").withAddress("wall street").withDateOfLastVisit("01-04-2024")
.withEmergencyContact().build();
public static final Person DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533")
.withEmail("[email protected]").withAddress("10th street").withTags("friends")
.withDateOfLastVisit("02-01-2024").withEmergencyContact().build();
.withDateOfLastVisit("02-04-2024").withEmergencyContact().build();
public static final Person ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("94822249")
.withEmail("[email protected]").withAddress("michegan ave").withDateOfLastVisit("10-10-2024")
.withEmail("[email protected]").withAddress("michegan ave").withDateOfLastVisit("10-07-2024")
.withEmergencyContact().build();
public static final Person FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("94824270")
.withEmail("[email protected]").withAddress("little tokyo").withDateOfLastVisit("23-08-2024")
.withEmergencyContact().build();
public static final Person GEORGE = new PersonBuilder().withName("George Best").withPhone("94824421")
.withEmail("[email protected]").withAddress("4th street").withDateOfLastVisit("05-06-2024")
.withEmail("[email protected]").withAddress("4th street").withDateOfLastVisit("05-09-2023")
.withEmergencyContact().build();

// Manually added
Expand Down Expand Up @@ -127,10 +127,10 @@ public static List<Person> getTypicalPersonsDescendingName() {
}

public static List<Person> getTypicalPersonsAscendingDateOfLastVisit() {
return new ArrayList<>(Arrays.asList(ALICE, DANIEL, BENSON, GEORGE, FIONA, ELLE, CARL));
return new ArrayList<>(Arrays.asList(GEORGE, ALICE, BENSON, CARL, DANIEL, ELLE, FIONA));
}

public static List<Person> getTypicalPersonsDescendingDateOfLastVisit() {
return new ArrayList<>(Arrays.asList(CARL, ELLE, FIONA, GEORGE, BENSON, DANIEL, ALICE));
return new ArrayList<>(Arrays.asList(FIONA, ELLE, DANIEL, CARL, BENSON, ALICE, GEORGE));
}
}