Skip to content

Commit

Permalink
Merge pull request #136 from seetohjinwei/add-dateparser
Browse files Browse the repository at this point in the history
Implement DateParser class
  • Loading branch information
dexter-sim authored Oct 13, 2022
2 parents fce2944 + 83b38d9 commit cbeff84
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ If your changes to the data file makes its format invalid, AddressBook will disc

_Details coming soon ..._

## Miscellaneous Information

### Accepted Date Formats

The following date formats are accepted:

* yyyy-MM-dd (2022-10-31)
* MMM dd yyyy (Oct 31 2022)
* MM dd yyyy (10 31 2022)
* dd MMM yyyy (31 Oct 2022)

--------------------------------------------------------------------------------------------------------------------

## FAQ
Expand Down
1 change: 1 addition & 0 deletions src/main/java/taskbook/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Messages {
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_DATE_FORMAT = "Invalid date format!";

}
37 changes: 37 additions & 0 deletions src/main/java/taskbook/logic/parser/DateParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package taskbook.logic.parser;

import static taskbook.commons.core.Messages.MESSAGE_INVALID_DATE_FORMAT;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;

import taskbook.logic.parser.exceptions.ParseException;

/**
* Parses user input's date into internal Date object.
* Accepts multiple Date formats.
*/
public class DateParser {

private static final DateTimeFormatter PARSER_OPTIONAL_FORMATS = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE)
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
.appendOptional(DateTimeFormatter.ofPattern("MMM dd yyyy"))
.appendOptional(DateTimeFormatter.ofPattern("MM dd yyyy"))
.appendOptional(DateTimeFormatter.ofPattern("dd MMM yyyy"))
.toFormatter();

/**
* Parses {@code userInput} into a Date object and returns it.
* @throws ParseException if {@code userInput} does not conform the expected formats.
*/
public static LocalDate parse(String userInput) throws ParseException {
try {
return LocalDate.parse(userInput, PARSER_OPTIONAL_FORMATS);
} catch (DateTimeParseException e) {
throw new ParseException(MESSAGE_INVALID_DATE_FORMAT);
}
}
}
62 changes: 62 additions & 0 deletions src/test/java/taskbook/logic/parser/DateParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package taskbook.logic.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.LocalDate;

import org.junit.jupiter.api.Test;

import taskbook.logic.parser.exceptions.ParseException;

public class DateParserTest {

private final LocalDate expectedOutput = LocalDate.parse("2022-10-31");

@Test
public void parse_validArgument1_returnsCorrectOutput() throws ParseException {
LocalDate actual = DateParser.parse("2022-10-31");
assertEquals(actual, expectedOutput);
}

@Test
public void parse_validArgument2_returnsCorrectOutput() throws ParseException {
LocalDate actual = DateParser.parse("Oct 31 2022");
assertEquals(actual, expectedOutput);
}

@Test
public void parse_validArgument3_returnsCorrectOutput() throws ParseException {
LocalDate actual = DateParser.parse("10 31 2022");
assertEquals(actual, expectedOutput);
}

@Test
public void parse_validArgument4_returnsCorrectOutput() throws ParseException {
LocalDate actual = DateParser.parse("31 Oct 2022");
assertEquals(actual, expectedOutput);
}

@Test
public void parse_emptyArgument_throwsException() {
assertThrowsException("");
}

@Test
public void parse_invalidInput_throwsException() {
assertThrowsException("Halloween");
}

@Test
public void parse_invalidFormat_throwsException() {
assertThrowsException("31st October 2022");
}

public void assertThrowsException(String userInput) {
try {
DateParser.parse(userInput);
throw new AssertionError("The expected ParseException is not thrown.");
} catch (ParseException e) {
// intentionally empty catch block
}
}
}

0 comments on commit cbeff84

Please sign in to comment.