-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStudentdemo1.java
95 lines (79 loc) · 3.63 KB
/
Studentdemo1.java
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
package Array1;
import java.util.ArrayList;
import java.util.Scanner;
public class Studentdemo1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList<Student> studentList = new ArrayList<>(); // Create an ArrayList of Student objects
boolean a = true;
while (a) {
System.out.println("1. Add student\n2. Search ID\n3. Search Name\n4. Sort by ID\n5. Update name based on ID\n6. Display");
System.out.println("Enter your choice:");
int ch = s.nextInt();
switch (ch) {
case 1:
System.out.println("Enter student ID:");
int n = s.nextInt();
System.out.println("Enter student name:");
String s3 = s.next();
studentList.add(new Student(n, s3)); // Add Student object to the ArrayList
break;
case 2:
System.out.println("Enter the id to search");
int id1 = s.nextInt();
for (Student student : studentList) {
if (student.id == id1) {
System.out.println(student);
break; // Exit the loop after finding the student
}
}
break;
case 3:
System.out.println("Enter the name to search");
String h = s.next();
for (Student student : studentList) {
if (student.name.equals(h)) {
System.out.println(student);
}
}
break;
case 4:
// Implement sorting logic using a temporary variable and loops
for (int i = 0; i < studentList.size() - 1; i++) {
for (int j = 0; j < studentList.size() - i - 1; j++) {
if (studentList.get(j).id > studentList.get(j + 1).id) {
Student temp = studentList.get(j);
studentList.set(j, studentList.get(j + 1));
studentList.set(j + 1, temp);
}
}
}
System.out.println("Students sorted by ID:");
for (Student student : studentList) {
System.out.println(student);
}
break;
case 5:
System.out.println("Enter the id to search");
int id2 = s.nextInt();
System.out.println("Enter the name to update");
String h1 = s.next();
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i); // Get the student object at index i
if (student.id == id2) {
student.name = h1;
System.out.println("Student updated:");
System.out.println(student);
break; // Exit the loop after updating the student
}
}
break;
case 6:
for (Student student : studentList) {
System.out.println(student);
}
break;
}
}
}
}