-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentService.java
More file actions
112 lines (76 loc) · 2.96 KB
/
Copy pathStudentService.java
File metadata and controls
112 lines (76 loc) · 2.96 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
import java.util.ArrayList;
public class StudentService {
private ArrayList<Student> students = new ArrayList<>();
// Add Student
public void addStudent(Student student) {
// Duplicate ID Check
for (Student s : students) {
if (s.getId() == student.getId()) {
System.out.println("\nStudent ID already exists!");
return;
}
}
students.add(student);
System.out.println("\nStudent Added Successfully!");
}
// View Students
public void viewStudents() {
if (students.isEmpty()) {
System.out.println("\nNo Students Found!");
return;
}
System.out.println("\n==================== STUDENT LIST ====================");
System.out.printf("%-6s %-20s %-12s %-8s %-15s%n",
"ID", "NAME", "BRANCH", "YEAR", "PHONE");
System.out.println("---------------------------------------------------------------");
for (Student s : students) {
System.out.printf("%-6d %-20s %-12s %-8d %-15s%n",
s.getId(),
s.getName(),
s.getBranch(),
s.getYear(),
s.getPhone());
}
System.out.println("---------------------------------------------------------------");
}
// Search Student
public void searchStudent(int id) {
for (Student s : students) {
if (s.getId() == id) {
System.out.println("\n=========== STUDENT FOUND ===========");
System.out.println("ID : " + s.getId());
System.out.println("Name : " + s.getName());
System.out.println("Branch : " + s.getBranch());
System.out.println("Year : " + s.getYear());
System.out.println("Phone : " + s.getPhone());
return;
}
}
System.out.println("\nStudent Not Found!");
}
// Update Student
public void updateStudent(int id, String name, String branch, int year, String phone) {
for (Student s : students) {
if (s.getId() == id) {
s.setName(name);
s.setBranch(branch);
s.setYear(year);
s.setPhone(phone);
System.out.println("\nStudent Updated Successfully!");
return;
}
}
System.out.println("\nStudent Not Found!");
}
// Delete Student
public void deleteStudent(int id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("\nStudent Deleted Successfully!");
return;
}
}
System.out.println("\nStudent Not Found!");
}
}