-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefineCourseController.java
More file actions
96 lines (80 loc) · 2.91 KB
/
DefineCourseController.java
File metadata and controls
96 lines (80 loc) · 2.91 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
package s25.cs151.application.controller;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import s25.cs151.application.model.DataManager;
import java.io.IOException;
import java.util.List;
import s25.cs151.application.model.*;
public class DefineCourseController {
@FXML
private TextField courseCodeField;
@FXML
private TextField courseNameField;
@FXML
private TextField sectionNumberField;
@FXML
private Button saveButton;
@FXML
private Button cancelButton;
@FXML
private void onSave() {
// Validate inputs
if (courseCodeField.getText().trim().isEmpty() ||
courseNameField.getText().trim().isEmpty() ||
sectionNumberField.getText().trim().isEmpty()) {
showAlert("Please fill in all fields");
return;
}
String courseCode = courseCodeField.getText().trim();
String courseName = courseNameField.getText().trim();
String sectionNumber = sectionNumberField.getText().trim();
// Check for duplicates
List<Course> existingCourses = DataManager.loadAllCourses();
for (Course existingCourse : existingCourses) {
if (existingCourse.getCourseCode().equalsIgnoreCase(courseCode) &&
existingCourse.getCourseName().equalsIgnoreCase(courseName) &&
existingCourse.getSectionNumber().equalsIgnoreCase(sectionNumber)) {
showAlert("Error: This course already exists!");
return;
}
}
// Create and save new course
Course course = new Course(courseCode, courseName, sectionNumber);
DataManager.saveCourse(course);
showAlert("Course saved successfully!");
// Close the window
Stage stage = (Stage) saveButton.getScene().getWindow();
stage.close();
}
@FXML
private void onCancel() {
Stage stage = (Stage) cancelButton.getScene().getWindow();
stage.close();
}
@FXML
private void onViewCourses() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/s25/cs151/application/view/view-courses.fxml"));
Parent root = loader.load();
Stage viewStage = new Stage();
viewStage.initModality(Modality.APPLICATION_MODAL);
viewStage.setTitle("View Courses");
viewStage.setScene(new Scene(root, 600, 400));
viewStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}