Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2324S1#109 from jx124/fix-datetime-pa…
Browse files Browse the repository at this point in the history
…rsing-bug

Fix datetime parsing bug
  • Loading branch information
garylow2001 authored Oct 31, 2023
2 parents 69d9c4f + 5d94bf5 commit 6cf4dd3
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEY_MILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand All @@ -28,14 +28,15 @@ public class AddLeadCommand extends Command {
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_KEY_MILESTONE + "KEY_MILESTONE "
+ PREFIX_MEETING_TIME + "MEETING_TIME "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_KEYMILESTONE + "2023-10-20 "
+ PREFIX_KEY_MILESTONE + "01/12/2023 "
+ PREFIX_MEETING_TIME + "10/10/2023 14:30 "
+ PREFIX_TAG + "classmate";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEY_MILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand Down Expand Up @@ -43,8 +43,8 @@ public class EditLeadCommand extends EditCommand {
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_KEYMILESTONE + "KEY MILESTONE] "
+ "[" + PREFIX_MEETING_TIME + "MEETING TIME] "
+ "[" + PREFIX_KEY_MILESTONE + "KEY_MILESTONE] "
+ "[" + PREFIX_MEETING_TIME + "MEETING_TIME] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEY_MILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand Down Expand Up @@ -37,21 +37,21 @@ public class AddLeadCommandParser implements Parser<AddLeadCommand> {
public AddLeadCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_KEYMILESTONE, PREFIX_MEETING_TIME, PREFIX_TAG);
PREFIX_ADDRESS, PREFIX_KEY_MILESTONE, PREFIX_MEETING_TIME, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_KEYMILESTONE,
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_KEY_MILESTONE,
PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddLeadCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_MEETING_TIME, PREFIX_KEYMILESTONE);
PREFIX_MEETING_TIME, PREFIX_KEY_MILESTONE);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
KeyMilestone keyMilestone = ParserUtil.parseKeyMilestone(argMultimap.getValue(PREFIX_KEYMILESTONE).get());
KeyMilestone keyMilestone = ParserUtil.parseKeyMilestone(argMultimap.getValue(PREFIX_KEY_MILESTONE).get());
Optional<MeetingTime> meetingTime = argMultimap.getValue(PREFIX_MEETING_TIME).isPresent()
? Optional.of(ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_MEETING_TIME).get()))
: Optional.empty();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_MEETING_TIME = new Prefix("m/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_KEYMILESTONE = new Prefix("k/");
public static final Prefix PREFIX_KEY_MILESTONE = new Prefix("k/");

public static final Prefix PREFIX_NAME_TP = new Prefix("--name");
public static final Prefix PREFIX_PHONE_TP = new Prefix("--phone");
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEY_MILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand Down Expand Up @@ -38,7 +38,7 @@ public EditCommand parse(String args) throws ParseException {
boolean isLead = false;
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_KEYMILESTONE, PREFIX_MEETING_TIME, PREFIX_TAG);
PREFIX_KEY_MILESTONE, PREFIX_MEETING_TIME, PREFIX_TAG);

Index index;

Expand All @@ -48,14 +48,14 @@ public EditCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_KEYMILESTONE,
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_KEY_MILESTONE,
PREFIX_MEETING_TIME, PREFIX_ADDRESS);
EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
EditLeadDescriptor editLeadDescriptor = new EditLeadDescriptor();
if (argMultimap.getValue(PREFIX_KEYMILESTONE).isPresent()) {
if (argMultimap.getValue(PREFIX_KEY_MILESTONE).isPresent()) {
isLead = true;
editLeadDescriptor.setKeyMilestone(ParserUtil.parseKeyMilestone(
argMultimap.getValue(PREFIX_KEYMILESTONE).get()));
argMultimap.getValue(PREFIX_KEY_MILESTONE).get()));
}

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/seedu/address/model/person/KeyMilestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;

/**
* Represents a lead's key milestone in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidKeyMilestone(String)}
*/
public class KeyMilestone {
public static final String DATE_FORMAT = "dd/MM/uuuu";

// Replace uuuu in format to yyyy to not confuse users
public static final String MESSAGE_CONSTRAINTS =
"Key milestone is the date of Lead's milestone moment, should be in the format of yyyy-MM-dd";
"Key milestone is the date of Lead's milestone moment, should be in the format of "
+ DATE_FORMAT.replace("u", "y");

public final String value;

/**
* Constructs an {@code KeyMilestone}.
*
Expand All @@ -31,7 +38,10 @@ public KeyMilestone(String keyMilestone) {
*/
public static boolean isValidKeyMilestone(String test) {
try {
LocalDate.parse(test, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate.parse(test,
DateTimeFormatter
.ofPattern(DATE_FORMAT)
.withResolverStyle(ResolverStyle.STRICT));
return true;
} catch (DateTimeParseException e) {
return false;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/model/person/MeetingTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
*/
public class MeetingTime {

public static final String MESSAGE_CONSTRAINTS = "Meeting time should be in the format of " + DATE_TIME_FORMAT;
// Replace uuuu in format to yyyy to not confuse users
public static final String MESSAGE_CONSTRAINTS = "Meeting time should be in the format of "
+ DATE_TIME_FORMAT.replace("u", "y");

public final LocalDateTime value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;

/**
* Helper class for handling meeting time formatting and parsing.
*/
public class MeetingTimeFormatter {

public static final String DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm";
public static final String DATE_TIME_FORMAT = "dd/MM/uuuu HH:mm";
private static DateTimeFormatter getFormatter() {
return DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
return DateTimeFormatter
.ofPattern(DATE_TIME_FORMAT)
.withResolverStyle(ResolverStyle.STRICT);
}

public static LocalDateTime parse(String meetingTime) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public static Person[] getSamplePersons() {
getTagSet("neighbours")),
new Lead(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
new KeyMilestone("2022-12-01"), Optional.of(new MeetingTime("10/10/2023 14:30")),
new KeyMilestone("01/12/2023"), Optional.of(new MeetingTime("10/10/2023 14:30")),
getTagSet("family")),
new Lead(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
new KeyMilestone("2022-12-01"),
new KeyMilestone("01/12/2023"),
Optional.of(new MeetingTime("10/10/2023 14:30")),
getTagSet("classmates")),
new Lead(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
new KeyMilestone("2022-12-01"), Optional.of(new MeetingTime("10/10/2023 14:30")),
new KeyMilestone("01/12/2023"), Optional.of(new MeetingTime("10/10/2023 14:30")),
getTagSet("colleagues"))
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"email": "[email protected]",
"type": "lead",
"address": "123, Jurong West Ave 6, #08-111",
"keyMilestone": "2022-12-01",
"keyMilestone": "01/12/2023",
"meetingTime" : "25/10/2023 11:21",
"tags": [ "Client", "friends" ]
}, {
Expand All @@ -14,7 +14,7 @@
"email": "[email protected]",
"type": "lead",
"address": "4th street",
"keyMilestone": "2022-12-01",
"keyMilestone": "01/12/2023",
"meetingTime" : "25/10/2023 11:21",
"tags": [ "Client" ]
} ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"phone" : "9482224",
"email" : "[email protected]",
"type": "lead",
"keyMilestone": "2022-12-01",
"keyMilestone": "01/12/2023",
"address" : "michegan ave",
"meetingTime" : null,
"tags" : []
Expand All @@ -50,7 +50,7 @@
"phone" : "9482427",
"email" : "[email protected]",
"type": "lead",
"keyMilestone": "2022-12-01",
"keyMilestone": "01/12/2023",
"address" : "little tokyo",
"meetingTime" : null,
"tags" : []
Expand All @@ -59,7 +59,7 @@
"phone" : "9482442",
"email" : "[email protected]",
"type": "lead",
"keyMilestone": "2022-12-01",
"keyMilestone": "01/12/2023",
"address" : "4th street",
"meetingTime" : null,
"tags" : []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_KEY_MILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand Down Expand Up @@ -40,8 +40,8 @@ public class CommandTestUtil {
public static final String VALID_MEETING_TIME_BOB = "16/12/2023 11:00";
public static final String VALID_TAG_HUSBAND = "husband";

public static final String VALID_KEYMILESTONE_AMY = "2022-12-01";
public static final String VALID_KEYMILESTONE_BOB = "2022-12-01";
public static final String VALID_KEY_MILESTONE_AMY = "01/12/2023";
public static final String VALID_KEY_MILESTONE_BOB = "01/12/2023";
public static final String VALID_TAG_FRIEND = "friend";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
Expand All @@ -52,7 +52,7 @@ public class CommandTestUtil {
public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB;
public static final String ADDRESS_DESC_AMY = " " + PREFIX_ADDRESS + VALID_ADDRESS_AMY;
public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB;
public static final String KEYMILESTONE_DESC_BOB = " " + PREFIX_KEYMILESTONE + VALID_KEYMILESTONE_BOB;
public static final String KEY_MILESTONE_DESC_BOB = " " + PREFIX_KEY_MILESTONE + VALID_KEY_MILESTONE_BOB;
public static final String MEETING_TIME_DESC_AMY = " " + PREFIX_MEETING_TIME + VALID_MEETING_TIME_AMY;
public static final String MEETING_TIME_DESC_BOB = " " + PREFIX_MEETING_TIME + VALID_MEETING_TIME_BOB;
public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {

PersonBuilder personInList = new PersonBuilder(lastPerson);
Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB)
.withTags(VALID_TAG_HUSBAND).withKeyMilestone("2022-12-01").buildLead();
.withTags(VALID_TAG_HUSBAND).withKeyMilestone("01/12/2023").buildLead();

EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static seedu.address.logic.commands.CommandTestUtil.DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_KEYMILESTONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_KEY_MILESTONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY;
Expand Down Expand Up @@ -60,10 +60,10 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {

PersonBuilder personInList = new PersonBuilder(lastPerson);
Lead editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB)
.withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEYMILESTONE_BOB).buildLead();
.withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEY_MILESTONE_BOB).buildLead();

EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEYMILESTONE_BOB)
.withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEY_MILESTONE_BOB)
.build();

EditLeadCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor);
Expand Down Expand Up @@ -98,7 +98,7 @@ public void execute_filteredList_success() {
Lead editedLead = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).buildLead();
EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON,
new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB)
.withKeyMilestone(VALID_KEYMILESTONE_BOB).build());
.withKeyMilestone(VALID_KEY_MILESTONE_BOB).build());

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedLead));

Expand Down
Loading

0 comments on commit 6cf4dd3

Please sign in to comment.