-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystem.java
More file actions
96 lines (85 loc) · 3.01 KB
/
StudentManagementSystem.java
File metadata and controls
96 lines (85 loc) · 3.01 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
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
// Add a new student
public void addStudent() {
System.out.print("Enter ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Marks: ");
double marks = scanner.nextDouble();
students.add(new Student(id, name, marks));
System.out.println("Student added successfully!");
}
// View all students
public void viewStudents() {
if (students.isEmpty()) {
System.out.println("No students to display.");
return;
}
System.out.println("\n--- Student List ---");
for (Student s : students) {
s.display();
}
}
// Update student details
public void updateStudent() {
System.out.print("Enter Student ID to update: ");
int id = scanner.nextInt();
scanner.nextLine();
for (Student s : students) {
if (s.getId() == id) {
System.out.print("Enter new name: ");
s.setName(scanner.nextLine());
System.out.print("Enter new marks: ");
s.setMarks(scanner.nextDouble());
System.out.println("Student updated successfully!");
return;
}
}
System.out.println("Student not found!");
}
// Delete student
public void deleteStudent() {
System.out.print("Enter Student ID to delete: ");
int id = scanner.nextInt();
for (Student s : students) {
if (s.getId() == id) {
students.remove(s);
System.out.println("Student deleted successfully!");
return;
}
}
System.out.println("Student not found!");
}
// Menu loop
public void menu() {
int choice;
do {
System.out.println("\n=== Student Management System ===");
System.out.println("1. Add Student");
System.out.println("2. View Students");
System.out.println("3. Update Student");
System.out.println("4. Delete Student");
System.out.println("0. Exit");
System.out.print("Enter choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1: addStudent(); break;
case 2: viewStudents(); break;
case 3: updateStudent(); break;
case 4: deleteStudent(); break;
case 0: System.out.println("Exiting..."); break;
default: System.out.println("Invalid choice! Try again.");
}
} while (choice != 0);
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
sms.menu();
}
}