-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagement.java
More file actions
157 lines (132 loc) · 5.29 KB
/
StudentManagement.java
File metadata and controls
157 lines (132 loc) · 5.29 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
package ManageStudents;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentManagement {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\nWELCOME TO STUDENT MANAGEMENT");
System.out.println("1. Create");
System.out.println("2. Find and Sort");
System.out.println("3. Update/Delete");
System.out.println("4. Report");
System.out.println("5. Exit");
System.out.print("Please choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
createStudent();
break;
case 2:
findAndSortStudents();
break;
case 3:
updateOrDeleteStudent();
break;
case 4:
generateReport();
break;
case 5:
System.out.println("Exiting program. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void createStudent() {
System.out.println("Creating a new student:");
System.out.print("Enter Student ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Student Name: ");
String name = scanner.nextLine();
System.out.print("Enter Semester: ");
int semester = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Course Name (Java, .Net, C/C++): ");
String course = scanner.nextLine();
Student student = new Student(id, name, semester, course);
students.add(student);
if (students.size() >= 10) {
System.out.print("Do you want to continue (Y/N)? ");
String continueChoice = scanner.nextLine();
if (!continueChoice.equalsIgnoreCase("Y")) {
return;
}
}
}
private static void findAndSortStudents() {
System.out.print("Enter a part of student name to search: ");
String searchName = scanner.nextLine();
ArrayList<Student> foundStudents = new ArrayList<>();
for (Student student : students) {
if (student.getName().toLowerCase().contains(searchName.toLowerCase())) {
foundStudents.add(student);
}
}
if (foundStudents.isEmpty()) {
System.out.println("No students found with the given search criteria.");
} else {
Collections.sort(foundStudents, (s1, s2) -> s1.getName().compareTo(s2.getName()));
System.out.println("Found students (sorted by name):");
for (Student student : foundStudents) {
System.out.println(student);
}
}
}
private static void updateOrDeleteStudent() {
System.out.print("Enter Student ID to find: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
Student foundStudent = null;
for (Student student : students) {
if (student.getId() == id) {
foundStudent = student;
break;
}
}
if (foundStudent == null) {
System.out.println("Student not found with the given ID.");
return;
}
System.out.print("Do you want to update (U) or delete (D) this student? ");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("U")) {
System.out.print("Enter new Student Name: ");
String newName = scanner.nextLine();
foundStudent = new Student(foundStudent.getId(), newName, foundStudent.getSemester(), foundStudent.getCourse());
System.out.println("Student information updated successfully.");
} else if (choice.equalsIgnoreCase("D")) {
students.remove(foundStudent);
System.out.println("Student deleted successfully.");
} else {
System.out.println("Invalid choice.");
}
}
private static void generateReport() {
System.out.println("Student Report:");
ArrayList<String> uniqueNames = new ArrayList<>();
for (Student student : students) {
String fullName = student.getName() + " | " + student.getCourse();
if (!uniqueNames.contains(fullName)) {
uniqueNames.add(fullName);
}
}
for (String fullName : uniqueNames) {
String[] parts = fullName.split(" \\| ");
String name = parts[0];
String course = parts[1];
int count = 0;
for (Student student : students) {
if (student.getName().equals(name) && student.getCourse().equals(course)) {
count++;
}
}
System.out.println(name + " | " + course + " | " + count);
}
}
}