-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystem.java
More file actions
135 lines (111 loc) · 3.74 KB
/
StudentManagementSystem.java
File metadata and controls
135 lines (111 loc) · 3.74 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
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private int id;
private String name;
private double marks;
// Constructor
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
// Getters & Setters
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getMarks() {
return marks;
}
public void setName(String name) {
this.name = name;
}
public void setMarks(double marks) {
this.marks = marks;
}
// Display student
public void display() {
System.out.println("ID: " + id + ", Name: " + name + ", Marks: " + marks);
}
}
public class StudentManagementSystem {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int choice;
do {
System.out.println("\n=== Student Record 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("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> addStudent();
case 2 -> viewStudents();
case 3 -> updateStudent();
case 4 -> deleteStudent();
case 5 -> System.out.println("Exiting the system. Goodbye!");
default -> System.out.println("Invalid choice. Try again.");
}
} while (choice != 5);
}
private static void addStudent() {
System.out.print("Enter student ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter student name: ");
String name = sc.nextLine();
System.out.print("Enter student marks: ");
double marks = sc.nextDouble();
students.add(new Student(id, name, marks));
System.out.println("Student added successfully.");
}
private static void viewStudents() {
if (students.isEmpty()) {
System.out.println("No student records available.");
} else {
System.out.println("\n--- Student Records ---");
for (Student s : students) {
s.display();
}
}
}
private static void updateStudent() {
System.out.print("Enter student ID to update: ");
int id = sc.nextInt();
boolean found = false;
for (Student s : students) {
if (s.getId() == id) {
sc.nextLine();
System.out.print("Enter new name: ");
String newName = sc.nextLine();
System.out.print("Enter new marks: ");
double newMarks = sc.nextDouble();
s.setName(newName);
s.setMarks(newMarks);
System.out.println("Student record updated.");
found = true;
break;
}
}
if (!found) {
System.out.println("Student with ID " + id + " not found.");
}
}
private static void deleteStudent() {
System.out.print("Enter student ID to delete: ");
int id = sc.nextInt();
boolean removed = students.removeIf(s -> s.getId() == id);
if (removed) {
System.out.println("Student deleted successfully.");
} else {
System.out.println("Student with ID " + id + " not found.");
}
}
}