-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourseEnrollment.java
More file actions
122 lines (114 loc) · 4.06 KB
/
courseEnrollment.java
File metadata and controls
122 lines (114 loc) · 4.06 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
package assign_1;
import java.util.*;
class Course{
private int courseId;
private String courseName;
private int credits;
public Course(int courseID, String courseName, int credits) {
this.courseId = courseID;
this.courseName = courseName;
this.credits = credits;
}
public int getCourseId(){
return courseId;
}
public String getCourseName(){
return courseName;
}
public int getCredits(){
return credits;
}
public String toString(){
return "courseId : " + getCourseId() + " Course name : " + getCourseName() + " Credits " + getCredits();
}
}
class Enrollment{
private int[][] studentCourses;
private int count;
public Enrollment(int maxStudents,int maxCourses){
studentCourses = new int[maxStudents][maxCourses];
count=0;
}
public void enroll(int studentId, int courseID){
studentCourses[studentId][count] = courseID;
count++;
}
public void drop(int studentID, int courseID) {
for (int i = 0; i < count; i++) {
if (studentCourses[studentID][i] == courseID) {
studentCourses[studentID][i] = studentCourses[studentID][count - 1];
studentCourses[studentID][count - 1] = 0;
count--;
break;
}
}
}
private Course getCourseByID(int courseID, Course[] courseCatalog) {
for (Course course : courseCatalog) {
if (course.getCourseId() == courseID) {
return course;
}
}
return null;
}
public void getEnrolledCourses(int studentID, Course[] courseCatalog) {
System.out.println("Courses enrolled by student " + studentID + ":");
for (int courseID : studentCourses[studentID]) {
if (courseID != 0) {
Course course = getCourseByID(courseID, courseCatalog);
if (course != null) {
System.out.println(course);
}
}
}
}
}
public class courseEnrollment {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Course[] courses = {
new Course(1, "Math", 3),
new Course(2, "Science", 4),
new Course(3, "History", 2)
};
Enrollment enrollment = new Enrollment(100, 10);
int n,stuId,courseId;
char choice;
do {
System.out.println("1 : enroll course ");
System.out.println("2 : getEnrolled course ");
System.out.println("3 : Drop");
System.out.println("4 : exit");
System.out.print("Enter your choice : ");
// System.out.println("4 : get enrolled course");
n = scanner.nextInt();
switch(n){
case 1:
System.out.print("Enter student id : ");
stuId = scanner.nextInt();
System.out.print("Enter course id : ");
courseId = scanner.nextInt();
enrollment.enroll(stuId,courseId);
break;
case 2:
System.out.print("Enter student Id : ");
stuId = scanner.nextInt();
enrollment.getEnrolledCourses(stuId, courses);
break;
case 3:
System.out.print("Enter student id : ");
stuId = scanner.nextInt();
System.out.print("Enter course id : ");
courseId = scanner.nextInt();
enrollment.drop(stuId , courseId);
break;
case 4:
return;
default:
System.out.println("Enter write choice");
}
// System.out.print("Do you want another(y/n)? : ");
// choice = scanner.next().charAt(0);
} while (n!=4);
}
}