Repository navigation
Expand file tree
/
Copy pathStudentCourses.java
More file actions
227 lines (195 loc) · 7.36 KB
/
Copy pathStudentCourses.java
File metadata and controls
227 lines (195 loc) · 7.36 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
import java.util.*;
class Course {
private String code;
private String title;
private String description;
private int capacity;
private String schedule;
private List<Student> students;
public Course(String code, String title, String description, int capacity, String schedule) {
this.code = code;
this.title = title;
this.description = description;
this.capacity = capacity;
this.schedule = schedule;
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
if (students.size() < capacity) {
students.add(student);
System.out.println(student.getName() + " has been registered for " + title);
} else {
System.out.println("Sorry, the course is full.");
}
}
public void removeStudent(Student student) {
if (students.contains(student)) {
students.remove(student);
System.out.println(student.getName() + " has been unenrolled from " + title);
} else {
System.out.println(student.getName() + " is not enrolled in this course.");
}
}
public String getTitle() {
return title;
}
public String getCode() {
return code;
}
public int getCapacity() {
return capacity;
}
public List<Student> getStudents() {
return students;
}
}
class Student {
private String studentId;
private String name;
private List<Course> courses;
public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
this.courses = new ArrayList<>();
}
public void registerCourse(Course course) {
courses.add(course);
course.addStudent(this);
System.out.println(name + " has registered for " + course.getTitle());
}
public void dropCourse(Course course) {
if (courses.contains(course)) {
courses.remove(course);
course.removeStudent(this);
System.out.println(name + " has dropped " + course.getTitle());
} else {
System.out.println(name + " is not enrolled in " + course.getTitle());
}
}
public String getName() {
return name;
}
public String getStudentId() {
return studentId;
}
public List<Course> getCourses() {
return courses;
}
}
class CourseDatabase {
private Map<String, Course> courses;
public CourseDatabase() {
this.courses = new HashMap<>();
}
public void addCourse(Course course) {
courses.put(course.getCode(), course);
}
public Course getCourseByCode(String code) {
return courses.get(code);
}
public Map<String, Course> getCourses() {
return courses;
}
}
class StudentDatabase {
private Map<String, Student> students;
public StudentDatabase() {
this.students = new HashMap<>();
}
public void addStudent(Student student) {
students.put(student.getStudentId(), student);
}
public Student getStudentById(String studentId) {
return students.get(studentId);
}
}
public class StudentCourses {
public static void displayCourseListing(CourseDatabase courseDatabase) {
System.out.println("Available Courses:");
for (Course course : courseDatabase.getCourses().values()) {
int availableSlots = course.getCapacity() - course.getStudents().size();
System.out.println("Code: " + course.getCode() + ", Title: " + course.getTitle() + ", Available Slots: "
+ availableSlots);
}
}
public static void main(String[] args) {
CourseDatabase courseDatabase = new CourseDatabase();
StudentDatabase studentDatabase = new StudentDatabase();
Scanner scanner = new Scanner(System.in);
// Adding some sample courses
Course course1 = new Course("CSCI101", "Introduction to Computer Science",
"An introductory course to programming", 50, "Mon/Wed 10:00 AM");
Course course2 = new Course("MATH201", "Calculus I", "Introduction to differential calculus", 40,
"Tue/Thu 2:00 PM");
Course course3 = new Course("PHYS101", "Physics I", "Classical mechanics and thermodynamics", 60,
"Mon/Wed/Fri 1:00 PM");
courseDatabase.addCourse(course1);
courseDatabase.addCourse(course2);
courseDatabase.addCourse(course3);
// Adding some sample students
Student student1 = new Student("S001", "Alice");
Student student2 = new Student("S002", "Bob");
studentDatabase.addStudent(student1);
studentDatabase.addStudent(student2);
int choice;
do {
System.out.println("\n1. Register Course");
System.out.println("2. Drop Course");
System.out.println("3. Display Course Listing");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
Student student = studentDatabase.getStudentById(studentId);
if (student == null) {
System.out.println("Student not found.");
break;
}
displayCourseListing(courseDatabase);
System.out.print("Enter course code to register: ");
String courseCode = scanner.nextLine();
Course course = courseDatabase.getCourseByCode(courseCode);
if (course == null) {
System.out.println("Course not found.");
break;
}
student.registerCourse(course);
break;
case 2:
System.out.print("Enter student ID: ");
studentId = scanner.nextLine();
student = studentDatabase.getStudentById(studentId);
if (student == null) {
System.out.println("Student not found.");
break;
}
System.out.println("Courses enrolled by " + student.getName() + ":");
for (Course c : student.getCourses()) {
System.out.println(c.getTitle());
}
System.out.print("Enter course code to drop: ");
courseCode = scanner.nextLine();
course = courseDatabase.getCourseByCode(courseCode);
if (course == null) {
System.out.println("Course not found.");
break;
}
student.dropCourse(course);
break;
case 3:
displayCourseListing(courseDatabase);
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
} while (choice != 4);
scanner.close();
}
}