-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleEntryController.java
More file actions
233 lines (201 loc) · 8.04 KB
/
ScheduleEntryController.java
File metadata and controls
233 lines (201 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package s25.cs151.application.controller;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.time.LocalDate;
import java.util.List;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import s25.cs151.application.model.DataManager;
import s25.cs151.application.model.OfficeHoursSchedule;
import s25.cs151.application.model.ScheduleEntry;
import s25.cs151.application.model.Timeslot;
import s25.cs151.application.model.*;
public class ScheduleEntryController {
@FXML
private TextField studentNameField;
@FXML
private DatePicker scheduleDatePicker;
@FXML
private ComboBox<String> timeSlotComboBox;
@FXML
private ComboBox<String> courseComboBox;
@FXML
private TextField reasonField;
@FXML
private TextArea commentField;
@FXML
private Button saveButton;
@FXML
private Button cancelButton;
private ScheduleEntry editingEntry = null;
private ViewScheduleEntriesController parentController = null;
// Store original values for matching
private String originalStudentName = null;
private LocalDate originalScheduleDate = null;
private String originalTimeSlot = null;
@FXML
private void initialize() {
// Set today's date as default for date picker
scheduleDatePicker.setValue(LocalDate.now());
// Load and populate timeslots
List<Timeslot> timeslots = DataManager.loadAllTimeslots();
ObservableList<String> timeSlotStrings = FXCollections.observableArrayList();
for (Timeslot slot : timeslots) {
timeSlotStrings.add(slot.getFormattedStartTime() + " - " + slot.getFormattedEndTime());
}
timeSlotComboBox.setItems(timeSlotStrings);
// Load and populate courses
List<Course> courses = DataManager.loadAllCourses();
ObservableList<String> courseStrings = FXCollections.observableArrayList();
for (Course course : courses) {
courseStrings.add(course.getCourseCode() + " - " + course.getCourseName() + " (Section " + course.getSectionNumber() + ")");
}
courseComboBox.setItems(courseStrings);
// Set prompt text for optional fields
reasonField.setPromptText("Enter reason (optional)");
commentField.setPromptText("Enter comment (optional)");
}
@FXML
private void onSave() {
if (validateInput()) {
if (editingEntry != null) {
// Update the existing entry
editingEntry.setStudentName(studentNameField.getText().trim());
editingEntry.setScheduleDate(scheduleDatePicker.getValue());
editingEntry.setTimeSlot(timeSlotComboBox.getValue());
editingEntry.setCourse(courseComboBox.getValue());
editingEntry.setReason(reasonField.getText().trim());
editingEntry.setComment(commentField.getText().trim());
saveEditedEntry(editingEntry);
if (parentController != null) parentController.refreshTable();
} else {
// Create schedule entry object
ScheduleEntry entry = new OfficeHoursSchedule(
studentNameField.getText().trim(),
scheduleDatePicker.getValue(),
timeSlotComboBox.getValue(),
courseComboBox.getValue(),
reasonField.getText().trim(),
commentField.getText().trim()
);
saveEntry(entry);
}
// Show success message
showSuccessAlert();
// Close the window
closeWindow();
}
}
private boolean validateInput() {
StringBuilder errorMessage = new StringBuilder();
// Check required fields
if (studentNameField.getText().trim().isEmpty()) {
errorMessage.append("Student name is required.\n");
}
if (scheduleDatePicker.getValue() == null) {
errorMessage.append("Schedule date is required.\n");
}
if (timeSlotComboBox.getValue() == null) {
errorMessage.append("Time slot selection is required.\n");
}
if (courseComboBox.getValue() == null) {
errorMessage.append("Course selection is required.\n");
}
if (errorMessage.length() > 0) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Required Fields");
alert.setHeaderText("Please fill in all required fields");
alert.setContentText(errorMessage.toString());
alert.showAndWait();
return false;
}
return true;
}
private void saveEntry(ScheduleEntry entry) {
try {
// Get existing entries
List<ScheduleEntry> entries = DataManager.loadScheduleEntries();
// Add new entry
entries.add(entry);
// Save updated list
DataManager.saveScheduleEntries(entries);
} catch (Exception e) {
showErrorAlert("Failed to save entry: " + e.getMessage());
}
}
private void saveEditedEntry(ScheduleEntry entry) {
try {
List<ScheduleEntry> entries = DataManager.loadScheduleEntries();
for (int i = 0; i < entries.size(); i++) {
ScheduleEntry e = entries.get(i);
// Match by original unique fields
if (e.getStudentName().equals(originalStudentName)
&& e.getScheduleDate().equals(originalScheduleDate)
&& e.getTimeSlot().equals(originalTimeSlot)) {
entries.set(i, entry);
break;
}
}
DataManager.saveScheduleEntries(entries);
} catch (Exception e) {
showErrorAlert("Failed to save edited entry: " + e.getMessage());
}
}
private void showSuccessAlert() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Success");
alert.setHeaderText(null);
alert.setContentText("Schedule entry has been saved successfully.");
alert.showAndWait();
}
private void showErrorAlert(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
@FXML
private void onCancel() {
closeWindow();
}
private void closeWindow() {
Stage stage = (Stage) cancelButton.getScene().getWindow();
stage.close();
}
@FXML
private void onViewEntries() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/s25/cs151/application/view/view-schedule-entries.fxml"));
Parent root = loader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("View Schedule Entries");
stage.setScene(new Scene(root, 800, 600));
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setEditingEntry(ScheduleEntry entry, ViewScheduleEntriesController parent) {
this.editingEntry = entry;
this.parentController = parent;
// Store original values for matching
this.originalStudentName = entry.getStudentName();
this.originalScheduleDate = entry.getScheduleDate();
this.originalTimeSlot = entry.getTimeSlot();
// Populate fields with entry data
studentNameField.setText(entry.getStudentName());
scheduleDatePicker.setValue(entry.getScheduleDate());
timeSlotComboBox.setValue(entry.getTimeSlot());
courseComboBox.setValue(entry.getCourse());
reasonField.setText(entry.getReason());
commentField.setText(entry.getComment());
}
}