-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
193 lines (148 loc) · 5.97 KB
/
Main.java
File metadata and controls
193 lines (148 loc) · 5.97 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
import java.util.*;
public class Main {
static List<Student> students = FileManager.load("students.dat");
static List<Course> courses = FileManager.load("courses.dat");
static List<Enrollment> enrollments = FileManager.load("enrollments.dat");
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
students.stream().mapToInt(Student::getId).max()
.ifPresent(Student::setCounter);
courses.stream().mapToInt(Course::getCourseId).max()
.ifPresent(Course::setCounter);
while (true) {
try {
menu();
int choice = Integer.parseInt(sc.nextLine());
if (choice < 1 || choice > 10)
throw new InvalidChoiceException("Invalid choice");
switch (choice) {
case 1 -> addStudent();
case 2 -> addCourse();
case 3 -> enrollStudent();
case 4 -> students.forEach(System.out::println);
case 5 -> courses.forEach(System.out::println);
case 6 -> viewEnrollments();
case 7 -> searchStudent();
case 8 -> dropCourse();
case 9 -> showStudentCourses();
case 10 -> exitSystem();
}
} catch (InvalidChoiceException e) {
System.out.println("❌ " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("❌ Enter numeric value.");
}
}
}
static void menu() {
System.out.println("\n===== COURSE ENROLLMENT SYSTEM =====");
System.out.println("1. Add Student");
System.out.println("2. Add Course");
System.out.println("3. Enroll Student");
System.out.println("4. View Students");
System.out.println("5. View Courses");
System.out.println("6. View Enrollments");
System.out.println("7. Search Student");
System.out.println("8. Drop Course");
System.out.println("9. Show Student Courses");
System.out.println("10. Exit");
}
static int getInt(String msg) {
while (true) {
try {
System.out.print(msg);
return Integer.parseInt(sc.nextLine());
} catch (Exception e) {
System.out.println("❌ Invalid number");
}
}
}
static void addStudent() {
System.out.print("Enter name: ");
String name = sc.nextLine();
Student s = new Student(name);
students.add(s);
System.out.println("✅ Student added. ID = " + s.getId());
}
static void addCourse() {
System.out.print("Enter course name: ");
String name = sc.nextLine();
int cap = getInt("Enter capacity: ");
Course c = new Course(name, cap);
courses.add(c);
System.out.println("✅ Course added. ID = " + c.getCourseId());
}
static void enrollStudent() {
int sid = getInt("Enter student ID: ");
int cid = getInt("Enter course ID: ");
Course course = courses.stream()
.filter(c -> c.getCourseId() == cid)
.findFirst().orElse(null);
if (course == null) {
System.out.println("Course not found!");
return;
}
if (course.isFull()) {
System.out.println("Course is full!");
return;
}
boolean exists = enrollments.stream()
.anyMatch(e -> e.getStudentId() == sid &&
e.getCourseId() == cid);
if (exists) {
System.out.println("Already enrolled!");
return;
}
enrollments.add(new Enrollment(sid, cid));
course.incrementEnrolled();
System.out.println("✅ Enrollment successful!");
}
static void viewEnrollments() {
enrollments.forEach(e ->
System.out.println("Student " + e.getStudentId()
+ " → Course " + e.getCourseId()));
}
static void searchStudent() {
int id = getInt("Enter student ID: ");
students.stream()
.filter(s -> s.getId() == id)
.findFirst()
.ifPresentOrElse(
System.out::println,
() -> System.out.println("Not found"));
}
static void dropCourse() {
int sid = getInt("Enter student ID: ");
int cid = getInt("Enter course ID: ");
Iterator<Enrollment> it = enrollments.iterator();
while (it.hasNext()) {
Enrollment e = it.next();
if (e.getStudentId() == sid && e.getCourseId() == cid) {
it.remove();
courses.stream()
.filter(c -> c.getCourseId() == cid)
.forEach(Course::decrementEnrolled);
System.out.println("✅ Course dropped");
return;
}
}
System.out.println("Enrollment not found");
}
static void showStudentCourses() {
int sid = getInt("Enter student ID: ");
enrollments.stream()
.filter(e -> e.getStudentId() == sid)
.forEach(e ->
courses.stream()
.filter(c -> c.getCourseId() ==
e.getCourseId())
.forEach(System.out::println));
}
static void exitSystem() {
FileManager.save("students.dat", students);
FileManager.save("courses.dat", courses);
FileManager.save("enrollments.dat", enrollments);
System.out.println("💾 Data saved. Goodbye!");
System.exit(0);
}
}