Repository navigation
Expand file tree
/
Copy pathStudents.java
More file actions
204 lines (182 loc) · 6.58 KB
/
Copy pathStudents.java
File metadata and controls
204 lines (182 loc) · 6.58 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
import java.util.Scanner;
class Students {
private String firstName;
private String lastName;
private int studentID;
private String major;
private int year;
private String[] coursesRegistered;
public Students(int studentID, String firstName, String lastName, String major, int year) {
this.firstName = firstName;
this.lastName = lastName;
this.studentID = studentID;
this.major = major;
this.year = year;
this.coursesRegistered = new String[4];
}
public String getStudentID() {
return studentID + "";
}
public String[] getCoursesRegistered() {
return coursesRegistered;
}
}
class Course {
private String professor;
private String course;
private String courseID;
private int maxStudents;
private String[] studentList;
private int credits;
public Course(String professor, String course, String courseID, int maxStudents, int credits) {
this.professor = professor;
this.course = course;
this.courseID = courseID;
this.maxStudents = maxStudents;
this.studentList = new String[maxStudents];
this.credits = credits;
}
public String[] getStudentList() {
return studentList;
}
public String getCourseID() {
return courseID;
}
}
class University {
private Students[] students;
private Course[] courses;
public University() {
this.students = new Students[100];
this.courses = new Course[50];
}
public void addStudent(Students student) {
for (int i = 0; i < students.length; i++) {
if (students[i] == null) {
students[i] = student;
break;
}
}
}
public void addCourse(Course course) {
for (int i = 0; i < courses.length; i++) {
if (courses[i] == null) {
courses[i] = course;
break;
}
}
}
public void registerStudentForCourse(String studentID, String courseID) {
Students student = null;
Course course = null;
for (Students s : students) {
if (s != null && s.getStudentID().equals(studentID)) {
student = s;
break;
}
}
for (Course c : courses) {
if (c != null && c.getCourseID().equals(courseID)) {
course = c;
break;
}
}
if (student == null || course == null) {
System.out.println("Student or course not found.");
return;
}
String[] studentList = course.getStudentList();
for (int i = 0; i < studentList.length; i++) {
if (studentList[i] == null) {
studentList[i] = studentID;
System.out.println(studentID + " successfully registered for " + courseID);
return;
}
}
System.out.println("Course is full");
}
public void listStudentsByCourse(String courseID) {
for (Course c : courses) {
if (c != null && c.getCourseID().equals(courseID)) {
for (String studentID : c.getStudentList()) {
if (studentID != null) {
System.out.println("Student ID: " + studentID);
}
}
return;
}
}
System.out.println("Course not found");
}
public void listCoursesByStudent(String studentID) {
for (Students s : students) {
if (s != null && s.getStudentID().equals(studentID)) {
for (String course : s.getCoursesRegistered()) {
if (course != null) {
System.out.println("Course ID: " + course);
}
}
return;
}
}
System.out.println("Student not found");
}
}
class MainCourses {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
University university = new University();
while (true) {
System.out.println("1 - add student, 2 - add course, 3 - register students for course, 4 - list students by course, 5 - list courses by student, 6 - exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter student ID: ");
int studentID = sc.nextInt();
System.out.println("Enter student name: ");
String name = sc.next();
System.out.println("Enter student last name: ");
String lastName = sc.next();
System.out.println("Enter student's major: ");
String major = sc.next();
System.out.println("Enter year: ");
int year = sc.nextInt();
university.addStudent(new Students(studentID, name, lastName, major, year));
break;
case 2:
System.out.println("Enter professor name: ");
String profName = sc.next();
System.out.println("Enter course name: ");
String courseName = sc.next();
System.out.println("Enter course ID: ");
String courseID = sc.next();
System.out.println("Enter max students: ");
int maxStudents = sc.nextInt();
System.out.println("Enter credits: ");
int credits = sc.nextInt();
university.addCourse(new Course(profName, courseName, courseID, maxStudents, credits));
break;
case 3:
System.out.println("Enter student ID: ");
String studentId = sc.next();
System.out.println("Enter course ID: ");
String courseId = sc.next();
university.registerStudentForCourse(studentId, courseId);
break;
case 4:
System.out.println("Enter course ID: ");
String cid = sc.next();
university.listStudentsByCourse(cid);
break;
case 5:
System.out.println("Enter student ID: ");
String sid = sc.next();
university.listCoursesByStudent(sid);
break;
case 6:
sc.close();
return;
}
}
}
}