Skip to content

Commit 28dcb76

Browse files
committed
- Added goal event relevant classes for keeping track of flashcard count
1 parent 8fe143a commit 28dcb76

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package seedu.duke.calendar;
2+
3+
import java.time.LocalDateTime;
4+
5+
public class Goal extends Event{
6+
7+
private String name;
8+
private LocalDateTime to;
9+
private int goal;
10+
private int completed;
11+
12+
public Goal(String name, LocalDateTime to, int goal, int completed) {
13+
super(name, null, to);
14+
this.name = name;
15+
this.to = to;
16+
this.goal = goal;
17+
this.completed = completed;
18+
}
19+
20+
public int getGoal() {
21+
return goal;
22+
}
23+
24+
public Object getCompleted() {
25+
return completed;
26+
}
27+
28+
public void flashcardCompleted() {
29+
if (LocalDateTime.now().isBefore(to)) {
30+
completed += 1;
31+
}
32+
}
33+
34+
@Override
35+
public String getFrom() {
36+
return "null";
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "Goal '" + name + '\'' + " review " + goal + " flashcards by: " + to + " (Reviewed: " + completed + ")";
42+
}
43+
44+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)