-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManager.java
More file actions
76 lines (62 loc) · 2.1 KB
/
StudentManager.java
File metadata and controls
76 lines (62 loc) · 2.1 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
import java.util.*;
public class StudentManager {
private List<Student> students;
public StudentManager() {
students = FileHandler.loadFromFile();
}
public void addStudent(Student student) {
students.add(student);
FileHandler.saveToFile(students);
System.out.println("Student added successfully.");
}
public void displayAllStudents() {
if (students.isEmpty()) {
System.out.println("No student records found.");
} else {
for (Student s : students) {
System.out.println(s);
}
}
}
public Student searchStudentById(String id) {
for (Student s : students) {
if (s.getId().equalsIgnoreCase(id)) {
return s;
}
}
return null;
}
public void updateStudent(String id, Scanner sc) {
Student student = searchStudentById(id);
if (student != null) {
sc.nextLine(); // <-- Add this line to consume leftover newline before reading name
System.out.print("Enter new name: ");
String name = sc.nextLine();
student.setName(name);
System.out.print("Enter new age: ");
int age = sc.nextInt();
student.setAge(age);
sc.nextLine(); // <-- Add this line to consume leftover newline before reading course
System.out.print("Enter new course: ");
String course = sc.nextLine();
student.setCourse(course);
System.out.print("Enter new marks: ");
double marks = sc.nextDouble();
student.setMarks(marks);
FileHandler.saveToFile(students);
System.out.println("Student updated successfully.");
} else {
System.out.println("Student not found.");
}
}
public void deleteStudent(String id) {
Student student = searchStudentById(id);
if (student != null) {
students.remove(student);
FileHandler.saveToFile(students);
System.out.println("Student deleted successfully.");
} else {
System.out.println("Student not found.");
}
}
}