forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTusharHacktoberfest1
147 lines (124 loc) · 4.81 KB
/
TusharHacktoberfest1
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
******************************************************* School management Project in java ****************************************************************
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Student {
private String studentId;
private String name;
public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Student ID: " + studentId + ", Name: " + name;
}
}
class Course {
private String courseId;
private String courseName;
public Course(String courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
}
public String getCourseId() {
return courseId;
}
public String getCourseName() {
return courseName;
}
@Override
public String toString() {
return "Course ID: " + courseId + ", Name: " + courseName;
}
}
public class SchoolManagementSystem {
private static Map<String, Student> students = new HashMap<>();
private static Map<String, Course> courses = new HashMap<>();
private static Map<String, List<String>> enrollments = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("School Management System");
System.out.println("1. Add Student");
System.out.println("2. Add Course");
System.out.println("3. Enroll Student in Course");
System.out.println("4. List Enrolled Students in Course");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
addCourse(scanner);
break;
case 3:
enrollStudentInCourse(scanner);
break;
case 4:
listEnrolledStudentsInCourse(scanner);
break;
case 5:
System.out.println("Exiting the program.");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
private static void addStudent(Scanner scanner) {
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
System.out.print("Enter student name: ");
String name = scanner.nextLine();
Student student = new Student(studentId, name);
students.put(studentId, student);
System.out.println("Student added successfully.");
}
private static void addCourse(Scanner scanner) {
System.out.print("Enter course ID: ");
String courseId = scanner.nextLine();
System.out.print("Enter course name: ");
String courseName = scanner.nextLine();
Course course = new Course(courseId, courseName);
courses.put(courseId, course);
System.out.println("Course added successfully.");
}
private static void enrollStudentInCourse(Scanner scanner) {
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
System.out.print("Enter course ID: ");
String courseId = scanner.nextLine();
if (students.containsKey(studentId) && courses.containsKey(courseId)) {
enrollments.computeIfAbsent(courseId, k -> new ArrayList<>()).add(studentId);
System.out.println("Student enrolled in the course.");
} else {
System.out.println("Student or course not found. Please check the IDs.");
}
}
private static void listEnrolledStudentsInCourse(Scanner scanner) {
System.out.print("Enter course ID: ");
String courseId = scanner.nextLine();
if (courses.containsKey(courseId)) {
List<String> enrolledStudents = enrollments.getOrDefault(courseId, new ArrayList<>());
System.out.println("Enrolled students in the course:");
for (String studentId : enrolledStudents) {
System.out.println(students.get(studentId));
}
} else {
System.out.println("Course not found. Please check the course ID.");
}
}
}