Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
127 lines (102 loc) · 4.15 KB
/
Copy pathMain.java
File metadata and controls
127 lines (102 loc) · 4.15 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
import java.util.*;
class Student{
private int id;
private String name;
private int age;
Student(int id, String name, int age){
this.id=id;
this.name=name;
this.age=age;
}
public int getId(){
return id;
}
public void update(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return "\nID: " + id + "\nName: " + name + "\nAge: " + age + "\n";
}
}
public class Main {
static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("\n===== Student Management System =====");
System.out.println("1. Add Student");
System.out.println("2. View Students");
System.out.println("3. Delete Student");
System.out.println("4. Search Student");
System.out.println("5. Update Student");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter ID: ");
int id =sc.nextInt();
sc.nextLine();
System.out.println("Enter Name: ");
String name = sc.nextLine();
System.out.println("Enter Age: ");
int age = sc.nextInt();
students.add(new Student(id, name, age));
System.out.println("\nStudent added successfully!\n");
break;
case 2:
System.out.println("\n--- Student List ---");
if(students.isEmpty()){
System.out.println("No students found.\n");
} else {
for(Student s : students){
System.out.println(s);
}
}
break;
case 3:
System.out.print("\nEnter ID to delete: ");
int deleteId = sc.nextInt();
boolean removed = students.removeIf(s -> s.getId() == deleteId);
System.out.println(removed ?
"\nStudent deleted successfully!\n" :
"\nStudent not found.\n");
break;
case 4:
System.out.print("\nEnter ID to search: ");
int searchId = sc.nextInt();
for (Student s : students) {
if (s.getId() == searchId) {
System.out.println("\nStudent Found:");
System.out.println(s);
break;
}
}
break;
case 5:
System.out.print("\nEnter ID to update: ");
int updateId = sc.nextInt();
sc.nextLine();
for (Student s : students) {
if (s.getId() == updateId) {
System.out.print("Enter new name: ");
String newName = sc.nextLine();
System.out.print("Enter new age: ");
int newAge = sc.nextInt();
s.update(newName, newAge);
System.out.println("\nStudent updated successfully!\n");
break;
}
}
break;
case 6:
sc.close();
System.out.println("\nExiting program... Thank you!\n");
return;
default:
System.out.println("\nInvalid choice. Try again.\n");
}
}
}
}