Skip to content

Commit

Permalink
Revert "Add timeslot timing constraints"
Browse files Browse the repository at this point in the history
  • Loading branch information
joelgoh1 authored Apr 11, 2024
1 parent f480ce4 commit 5e04dad
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE_EDIT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIMESLOT_EDIT;
import static seedu.address.model.grade.Grade.isValidGrade;
import static seedu.address.model.timeslots.Timeslots.isStartTimeBeforeEndTime;
import static seedu.address.model.timeslots.Timeslots.isValidTimeslot;

import java.util.Arrays;
Expand Down Expand Up @@ -164,7 +163,7 @@ private Optional<Set<Timeslots>> parseTimeslotsForEdit(Collection<String> timesl
// Split by commas to handle multiple timeslots
String[] timeslotArray = trimmedTimeslotString.split(",");
for (String timeslot : timeslotArray) {
if (!isValidTimeslot(timeslot.trim()) || !isStartTimeBeforeEndTime(timeslot.trim())) {
if (!isValidTimeslot(timeslot.trim())) {
throw new ParseException(Timeslots.MESSAGE_CONSTRAINTS);
}
timeslotSet.add(new Timeslots(timeslot.trim()));
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.timeslots.Timeslots.isStartTimeBeforeEndTime;
import static seedu.address.model.timeslots.Timeslots.isValidTimeslot;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -107,7 +105,7 @@ public static Email parseEmail(String email) throws ParseException {
public static Timeslots parseTimeslot(String timeslot) throws ParseException {
requireNonNull(timeslot);
String trimmedTimeslot = timeslot.trim();
if (!isValidTimeslot(trimmedTimeslot) || !isStartTimeBeforeEndTime(trimmedTimeslot)) {
if (!Timeslots.isValidTimeslot(trimmedTimeslot)) {
throw new ParseException(Timeslots.MESSAGE_CONSTRAINTS);
}
return new Timeslots(trimmedTimeslot);
Expand Down
56 changes: 3 additions & 53 deletions src/main/java/seedu/address/model/timeslots/Timeslots.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Represents a Timeslot in the address book.
* Guarantees: immutable; timeslot is valid as declared in {@link #isValidTimeslot(String)}
Expand All @@ -16,19 +12,13 @@ public class Timeslots {
public static final String MESSAGE_CONSTRAINTS = "Timeslot should be of the format: "
+ "DayOfWeek StartTime-EndTime, and adhere to the following constraints:\n"
+ "1. The DayOfWeek is any day from Monday to Sunday.\n"
+ "2. StartTime and EndTime include hours and optional minutes in 12-hour format. \n"
+ "23. StartTime must be earlier than EndTime. "
+ "2. StartTime and EndTime include hours and optional minutes in 12-hour format. "
+ "Minutes, if included, should be separated from hours by a colon. \n"
+ "For example, 'Saturday 4pm-6pm', 'Tuesday 2:30pm-4:30pm'.\n";

public static final String VALIDATION_REGEX = "^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday) "
+ "(1[012]|[1-9])(:[0-5][0-9])?(am|pm)-(1[012]|[1-9])(:[0-5][0-9])?(am|pm)$";
private static final DateTimeFormatter[] TIME_FORMATTERS = {
DateTimeFormatter.ofPattern("h:mma"),
DateTimeFormatter.ofPattern("ha")
};
public final String timeslot;

public final String timeslot;

/**
* Constructs a {@code Timeslot}.
Expand All @@ -37,50 +27,10 @@ public class Timeslots {
*/
public Timeslots(String timeslot) {
requireNonNull(timeslot);
checkArgument(isValidTimeslot(timeslot) & isStartTimeBeforeEndTime(timeslot), MESSAGE_CONSTRAINTS);
checkArgument(isValidTimeslot(timeslot), MESSAGE_CONSTRAINTS);
this.timeslot = timeslot;
}

/**
* Helper Function to check if time entered is of valid time format.
*
* @param timeStr A time as String.
* @return parsed LocalTime if timeStr is of proper format.
*/
public static LocalTime parseTime(String timeStr) {
for (DateTimeFormatter formatter : TIME_FORMATTERS) {
try {
return LocalTime.parse(timeStr.toUpperCase(), formatter);
} catch (DateTimeParseException e) {
// Try the next formatter
}
}
throw new DateTimeParseException("Could not parse time: " + timeStr, timeStr, 0);
}

/**
* Checks if the start time is before the end time in the timeslot.
*
* @param timeslot A valid timeslot string.
* @return true if the start time is before the end time.
*/
public static boolean isStartTimeBeforeEndTime(String timeslot) {
String[] parts = timeslot.split("\\s+");
if (parts.length < 2) {
return false;
}
String timePart = parts[1];
String[] times = timePart.split("-");

try {
LocalTime startTime = parseTime(times[0]);
LocalTime endTime = parseTime(times[1]);
return startTime.isBefore(endTime);
} catch (DateTimeParseException e) {
return false;
}
}

/**
* Returns true if a given string is a valid timeslot.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getTimeslot() {
* @throws IllegalValueException if there were any data constraints violated in the adapted timeslot.
*/
public Timeslots toModelType() throws IllegalValueException {
if (!Timeslots.isValidTimeslot(timeslot) || !Timeslots.isStartTimeBeforeEndTime(timeslot)) {
if (!Timeslots.isValidTimeslot(timeslot)) {
throw new IllegalValueException(Timeslots.MESSAGE_CONSTRAINTS);
}
return new Timeslots(timeslot);
Expand Down

0 comments on commit 5e04dad

Please sign in to comment.