|
| 1 | +package seedu.duke.calendar.command; |
| 2 | + |
| 3 | +import seedu.duke.calendar.Event; |
| 4 | +import seedu.duke.calendar.EventList; |
| 5 | +import seedu.duke.calendar.Goal; |
| 6 | + |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.time.format.DateTimeFormatter; |
| 9 | +import java.time.format.DateTimeParseException; |
| 10 | +import java.util.Scanner; |
| 11 | + |
| 12 | +public class AddGoalEventCommand extends EventCommand{ |
| 13 | + |
| 14 | + //@@author kherlenbayasgalan-reused |
| 15 | + |
| 16 | + /** |
| 17 | + * The execute method is used to add an event goal to the calendar. It has two parameters (Scanner, EventList). |
| 18 | + * The EventList is used to add an event to the list. The scanner is used to get the user's event name input. |
| 19 | + * The method first takes the event name, then through parseDateTimeInput, it gets an acceptable date/time |
| 20 | + * from the user. If the user inserts acceptable inputs, the event goal will be added. If the user doesn't, |
| 21 | + * either one of DateTimeParseException or Invalid input exception. |
| 22 | + * @param scanner is used to get user's event name. |
| 23 | + * @param eventList is used to add an event to the list. |
| 24 | + */ |
| 25 | + |
| 26 | + public void execute(Scanner scanner, EventList eventList) { |
| 27 | + System.out.print("What's the goal event name?: "); |
| 28 | + String eventName = scanner.nextLine(); |
| 29 | + |
| 30 | + // checks if the acceptable format is given by the user to prevent program crash |
| 31 | + LocalDateTime endTime = parseDateTimeInput(scanner, "When does it end? (yyyy-MM-ddTHH:mm:ss) (e.g., 2023-12-20T12:30:30): "); |
| 32 | + int goal = parseIntegerInput(scanner, "How many flashcard to review by then?: "); |
| 33 | + |
| 34 | + Event event = new Goal(eventName, endTime, goal, 0); |
| 35 | + eventList.addEvent(event); |
| 36 | + System.out.println(event + " has been added to your Calendar"); |
| 37 | + } |
| 38 | + |
| 39 | + //@@author Cheezeblokz |
| 40 | + |
| 41 | + private int parseIntegerInput(Scanner scanner, String prompt) { |
| 42 | + while (true) { |
| 43 | + System.out.print(prompt); |
| 44 | + String userInput = scanner.nextLine(); |
| 45 | + try { |
| 46 | + // checks if the acceptable format is given by the user to prevent program crash |
| 47 | + return Integer.parseInt(userInput); |
| 48 | + } catch (NumberFormatException e) { |
| 49 | + System.out.println(" Invalid integer input. Please try again."); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + //@@author kherlenbayasgalan-reused |
| 55 | + |
| 56 | + /** |
| 57 | + * The parseDateTimeInput takes two parameters (Scanner , String) and through those parameters |
| 58 | + * the method gets user's input. Using exception handling of LocalDateTime, the user checks if the |
| 59 | + * input is in correct date/time format. If it is not in the specified "yyyy-MM-ddTHH:mm:ss" format |
| 60 | + * the method will throw DateTimeParseException. If it is in right format, the method will return |
| 61 | + * the input when it is called. |
| 62 | + * @param scanner is used to get user's date/time input. |
| 63 | + * @param prompt is given to instruct the user on an acceptable date/time format. |
| 64 | + * @return returns an acceptable time/date input. |
| 65 | + */ |
| 66 | + private LocalDateTime parseDateTimeInput(Scanner scanner, String prompt) { |
| 67 | + while (true) { |
| 68 | + System.out.print(prompt); |
| 69 | + String userInput = scanner.nextLine(); |
| 70 | + try { |
| 71 | + // checks if the acceptable format is given by the user to prevent program crash |
| 72 | + return LocalDateTime.parse(userInput); |
| 73 | + } catch (DateTimeParseException e) { |
| 74 | + System.out.println(" Invalid date and time format. Please try again."); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments