Skip to content

Commit

Permalink
Merge pull request #54 from SQwQ/master
Browse files Browse the repository at this point in the history
fix: addvisit command now correctly handles invalid inputs #52
  • Loading branch information
ReignOfComputer authored Oct 14, 2019
2 parents 4fac157 + 580a13b commit a6186fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ public AddVisitCommand parse(String args) throws ParseException {
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
// Take date from '/v' prefix or use current timing for report date.
date = argMultimap.getValue(PREFIX_VISIT)
.orElse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
date = ParserUtil.parseVisitReport(argMultimap.getValue(PREFIX_VISIT)
.orElse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))));
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddVisitCommand.MESSAGE_USAGE), ive);
}

//String date = argMultimap.getValue(PREFIX_VISIT).orElse("");

return new AddVisitCommand(index, date);
}
}
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.VisitReport;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -122,4 +123,15 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
return tagSet;
}

/**
* @throws ParseException if the given {@code date} is invalid.
*/
public static String parseVisitReport(String date) throws ParseException {
requireNonNull(date);
String trimmedDate = date.trim();
if (!VisitReport.isValidVisitDate(trimmedDate)) {
throw new ParseException(VisitReport.MESSAGE_CONSTRAINTS);
}
return trimmedDate;
}
}
18 changes: 15 additions & 3 deletions src/main/java/seedu/address/model/person/VisitReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static java.util.Objects.requireNonNull;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.Objects;


Expand All @@ -11,7 +15,6 @@
public class VisitReport {

public static final String MESSAGE_CONSTRAINTS = "Visit date should follow dd/mm/yyyy format";
public static final String VALIDATION_REGEX = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";

public final String date;

Expand Down Expand Up @@ -48,10 +51,19 @@ public String toString() {
}

/**
* Returns true if a given string is a valid tag name.
* Returns true if a given string is a valid date.
*/
public static boolean isValidVisitDate(String test) {
return test.matches(VALIDATION_REGEX);

//make sure month and day are valid and year is 2xxx
DateTimeFormatter dateFormatter =
DateTimeFormatter.ofPattern("dd/MM/2uuu").withResolverStyle(ResolverStyle.STRICT);
try {
LocalDate.parse(test, dateFormatter);
} catch (DateTimeParseException e) {
return false;
}
return true;
}

@Override
Expand Down

0 comments on commit a6186fc

Please sign in to comment.