-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentCourseManagementSystem.java
More file actions
485 lines (412 loc) · 17.3 KB
/
Copy pathStudentCourseManagementSystem.java
File metadata and controls
485 lines (412 loc) · 17.3 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
class Student {
private String name;
private String studentId;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
}
public String getName() {
return name;
}
public String getStudentId() {
return studentId;
}
}
// AVLNode class for the AVL tree
class AVLNode {
Course course;
int height;
AVLNode left;
AVLNode right;
public AVLNode(Course course) {
this.course = course;
this.height = 1;
this.left = null;
this.right = null;
}
}
// AVLTree class for managing courses
class AVLTree {
private AVLNode root;
// Constructor
public AVLTree() {
this.root = null;
}
// Get height of a node
private int getHeight(AVLNode node) {
if (node == null)
return 0;
return node.height;
}
// Get balance factor of a node
private int getBalanceFactor(AVLNode node) {
if (node == null)
return 0;
return getHeight(node.left) - getHeight(node.right);
}
// Update height of a node
private void updateHeight(AVLNode node) {
if (node != null)
node.height = Math.max(getHeight(node.left), getHeight(node.right)) + 1;
}
// Rotate right
private AVLNode rotateRight(AVLNode y) {
AVLNode x = y.left;
AVLNode T2 = x.right;
x.right = y;
y.left = T2;
updateHeight(y);
updateHeight(x);
return x;
}
// Rotate left
private AVLNode rotateLeft(AVLNode x) {
AVLNode y = x.right;
AVLNode T2 = y.left;
y.left = x;
x.right = T2;
updateHeight(x);
updateHeight(y);
return y;
}
// Insert a course into the AVL tree
public AVLNode insert(AVLNode node, Course course) {
if (node == null)
return new AVLNode(course);
if (course.getCourseCode().compareTo(node.course.getCourseCode()) < 0)
node.left = insert(node.left, course);
else if (course.getCourseCode().compareTo(node.course.getCourseCode()) > 0)
node.right = insert(node.right, course);
else // Duplicate course codes not allowed
return node;
updateHeight(node);
int balance = getBalanceFactor(node);
// Left Heavy
if (balance > 1) {
if (course.getCourseCode().compareTo(node.left.course.getCourseCode()) < 0) {
return rotateRight(node);
} else {
node.left = rotateLeft(node.left);
return rotateRight(node);
}
}
// Right Heavy
if (balance < -1) {
if (course.getCourseCode().compareTo(node.right.course.getCourseCode()) > 0) {
return rotateLeft(node);
} else {
node.right = rotateRight(node.right);
return rotateLeft(node);
}
}
return node;
}
// Wrapper function to insert a course
public void insert(Course course) {
root = insert(root, course);
}
// Search for a course by course code
public Course search(String courseCode) {
return search(root, courseCode);
}
// Helper function to search for a course
private Course search(AVLNode node, String courseCode) {
if (node == null)
return null;
if (courseCode.compareTo(node.course.getCourseCode()) < 0)
return search(node.left, courseCode);
else if (courseCode.compareTo(node.course.getCourseCode()) > 0)
return search(node.right, courseCode);
else
return node.course;
}
// Inorder traversal to display courses
public void inorder() {
inorder(root);
}
private void inorder(AVLNode node) {
if (node != null) {
inorder(node.left);
System.out.println("Course Code: " + node.course.getCourseCode());
System.out.println("Course Name: " + node.course.getName());
System.out.println("Schedule: " + node.course.getSchedule());
System.out.println();
inorder(node.right);
}
}
}
class Course {
private String courseCode;
private String name;
private String schedule;
private int maxEnrollment;
private List<String> enrolledStudents;
public Course(String courseCode, String name, String schedule, int maxEnrollment) {
this.courseCode = courseCode;
this.name = name;
this.schedule = schedule;
this.maxEnrollment = maxEnrollment;
this.enrolledStudents = new ArrayList<>();
}
public String getCourseCode() {
return courseCode;
}
public String getName() {
return name;
}
public String getSchedule() {
return schedule;
}
public int getMaxEnrollment() {
return maxEnrollment;
}
public List<String> getEnrolledStudents() {
return enrolledStudents;
}
}
public class StudentCourseManagementSystem {
private HashMap<String, Student> students;
private AVLTree courseTree; // AVL tree for courses
private HashMap<String, List<String>> studentSchedules;
public StudentCourseManagementSystem() {
students = new HashMap<>();
courseTree = new AVLTree();
studentSchedules = new HashMap<>();
}
public void addStudent(String studentId, String name) {
Student student = new Student(name, studentId);
students.put(studentId, student);
}
public void addCourse(String courseCode, String name, String schedule, int maxEnrollment) {
Course course = new Course(courseCode, name, schedule, maxEnrollment);
courseTree.insert(course);
}
public void enrollStudentInCourse(String studentId, String courseCode) {
Student student = students.get(studentId);
Course course = courseTree.search(courseCode);
if (student != null && course != null) {
if (!studentSchedules.containsKey(studentId)) {
studentSchedules.put(studentId, new ArrayList<>());
}
List<String> studentSchedule = studentSchedules.get(studentId);
if (!studentSchedule.contains(courseCode)) {
boolean hasScheduleConflict = checkScheduleConflict(studentId, course);
if (!hasScheduleConflict) {
if (course.getEnrolledStudents().size() < course.getMaxEnrollment()) {
studentSchedule.add(courseCode);
course.getEnrolledStudents().add(studentId);
System.out.println("Enrollment successful: Student " + student.getName() +
" enrolled in course " + course.getName());
} else {
System.out.println("Enrollment failed: Course " + course.getName() + " is full.");
}
} else {
System.out.println("Enrollment failed: Schedule conflict with another course.");
}
} else {
System.out.println("Enrollment failed: Student " + student.getName() +
" is already enrolled in course " + course.getName());
}
} else {
System.out.println("Enrollment failed: Student or course not found.");
}
}
public void dropStudentFromCourse(String studentId, String courseCode) {
Student student = students.get(studentId);
Course course = courseTree.search(courseCode);
if (student != null && course != null) {
if (studentSchedules.containsKey(studentId)) {
List<String> studentSchedule = studentSchedules.get(studentId);
if (studentSchedule.contains(courseCode)) {
if (course.getEnrolledStudents().remove(studentId)) {
studentSchedule.remove(courseCode);
System.out.println("Course dropped: Student " + student.getName() +
" dropped course " + course.getName());
} else {
System.out.println("Drop failed: Student " + student.getName() +
" was not enrolled in course " + course.getName());
}
} else {
System.out.println("Drop failed: Student " + student.getName() +
" is not enrolled in course " + course.getName());
}
} else {
System.out.println("Drop failed: Student " + student.getName() +
" is not enrolled in any courses.");
}
} else {
System.out.println("Drop failed: Student or course not found.");
}
}
public void viewStudentSchedule(String studentId) {
Student student = students.get(studentId);
if (student != null) {
if (studentSchedules.containsKey(studentId)) {
List<String> studentSchedule = studentSchedules.get(studentId);
if (!studentSchedule.isEmpty()) {
System.out.println("Schedule for Student " + student.getName() + ":");
for (String courseCode : studentSchedule) {
Course course = courseTree.search(courseCode);
if (course != null) {
System.out.println("Course Code: " + courseCode);
System.out.println("Course Name: " + course.getName());
System.out.println("Schedule: " + course.getSchedule());
System.out.println();
} else {
System.out.println("Course information not found for code: " + courseCode);
}
}
} else {
System.out.println("Student " + student.getName() + " is not enrolled in any courses.");
}
} else {
System.out.println("Student " + student.getName() + " is not enrolled in any courses.");
}
} else {
System.out.println("Student not found.");
}
}
// Helper method to check for schedule conflicts
private boolean checkScheduleConflict(String studentId, Course newCourse) {
List<String> studentSchedule = studentSchedules.get(studentId);
for (String courseCode : studentSchedule) {
Course enrolledCourse = courseTree.search(courseCode);
if (enrolledCourse != null && haveScheduleConflict(newCourse, enrolledCourse)) {
return true; // Schedule conflict found
}
}
return false; // No schedule conflicts
}
// Helper method to check if two courses have schedule conflicts
private boolean haveScheduleConflict(Course course1, Course course2) {
// Split the schedule strings to extract day and time
String[] schedule1Parts = course1.getSchedule().split(" ");
String[] schedule2Parts = course2.getSchedule().split(" ");
if (schedule1Parts.length != 3 || schedule2Parts.length != 3) {
return false; // Invalid schedule format
}
String day1 = schedule1Parts[0];
String time1 = schedule1Parts[1] + " " + schedule1Parts[2];
String day2 = schedule2Parts[0];
String time2 = schedule2Parts[1] + " " + schedule2Parts[2];
// Check if the courses are on the same day and the times overlap
return day1.equals(day2) && doTimesOverlap(time1, time2);
}
// Helper method to check if two times overlap
private boolean doTimesOverlap(String time1, String time2) {
// Parse time strings into Date objects for comparison
try {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
Date startTime1 = sdf.parse(time1);
Date endTime1 = new Date(startTime1.getTime() + 60 * 60 * 1000); // Add 1 hour
Date startTime2 = sdf.parse(time2);
Date endTime2 = new Date(startTime2.getTime() + 60 * 60 * 1000); // Add 1 hour
// Check for overlap
return startTime1.before(endTime2) && startTime2.before(endTime1);
} catch (ParseException e) {
e.printStackTrace();
return false; // Invalid time format
}
}
public void generateCourseRosters() {
for (Map.Entry<String, Student> studentEntry : students.entrySet()) {
String studentId = studentEntry.getKey();
Student student = studentEntry.getValue();
if (studentSchedules.containsKey(studentId)) {
List<String> studentSchedule = studentSchedules.get(studentId);
for (String courseCode : studentSchedule) {
Course course = courseTree.search(courseCode);
if (course != null) {
System.out.println("Course Code: " + courseCode);
System.out.println("Course Name: " + course.getName());
System.out.println("Enrolled Students:");
if (!course.getEnrolledStudents().isEmpty()) {
for (String enrolledStudentId : course.getEnrolledStudents()) {
Student enrolledStudent = students.get(enrolledStudentId);
if (enrolledStudent != null) {
System.out.println("Student ID: " + enrolledStudentId);
System.out.println("Student Name: " + enrolledStudent.getName());
System.out.println();
}
}
}
}
}
}
else {
System.out.println("No Students enrolled in this course");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentCourseManagementSystem system = new StudentCourseManagementSystem();
while (true) {
System.out.println("\nStudent Course Management System");
System.out.println("1. Add Student ID");
System.out.println("2. Add Course");
System.out.println("3. Enroll Student in Course");
System.out.println("4. Drop Student from Course");
System.out.println("5. View Student Schedule");
System.out.println("6. Generate Course Rosters");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
System.out.print("Enter student name: ");
String studentName = scanner.nextLine();
system.addStudent(studentId, studentName);
System.out.println("Student added.");
break;
case 2:
System.out.print("Enter course code: ");
String courseCode = scanner.nextLine();
System.out.print("Enter course name: ");
String courseName = scanner.nextLine();
System.out.print("Enter course schedule (e.g., 'Monday 9:00 AM'): ");
String courseSchedule = scanner.nextLine();
System.out.print("Enter maximum enrollment for the course: ");
int maxEnrollment = scanner.nextInt();
scanner.nextLine(); // Consume newline
system.addCourse(courseCode, courseName, courseSchedule, maxEnrollment);
System.out.println("Course added.");
break;
case 3:
System.out.print("Enter student ID: ");
studentId = scanner.nextLine();
System.out.print("Enter course code: ");
courseCode = scanner.nextLine();
system.enrollStudentInCourse(studentId, courseCode);
break;
case 4:
System.out.print("Enter student ID: ");
studentId = scanner.nextLine();
System.out.print("Enter course code: ");
courseCode = scanner.nextLine();
system.dropStudentFromCourse(studentId, courseCode);
break;
case 5:
System.out.print("Enter student ID: ");
studentId = scanner.nextLine();
system.viewStudentSchedule(studentId);
break;
case 6:
system.generateCourseRosters();
break;
case 7:
System.out.println("Exiting the program.");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}
}