-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathStudentManager.java
More file actions
120 lines (101 loc) · 3.54 KB
/
StudentManager.java
File metadata and controls
120 lines (101 loc) · 3.54 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
//store and manage Student objects
package Programs.studentManagement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class StudentManager {
//HashMap to store students with ID as key for quick lookup
private final Map<String, Student> students = new HashMap<>();
//check existence by ID
public boolean studentExists(String id) {
if (id == null) return false;
return students.containsKey(id.trim());
}
public boolean addStudent(Student s) {
if ( s == null || s.getId() == null || s.getId().trim().isEmpty()) {
throw new IllegalArgumentException("Student or Student ID cannot be null/empty.");
}
String id = s.getId().trim();
if (students.containsKey(id) || findByEmail(s.getEmail()) != null){
return false; //student with same ID exists
}
//duplicate name (case-insensitive)
if (s.getName() != null && findByName(s.getName()) != null) {
return false;
}
String email = s.getEmail();
if ( email == null) {
return false;
} else {
students.put(id, s);
return true; //successfully added
}
}
public Student findById(String id) {
if ( id == null) return null;
return students.get(id);
}
//Linear search for email.Normalizes by trimming and lower-casting
public Student findByEmail(String email) {
if ( email == null) return null;
String norm = normalizedEmail(email);
for (Student s : students.values()) {
String sEmail = normalizedEmail(s.getEmail());
if (sEmail != null && norm.equals(normalizedEmail(sEmail))) {
return s;
}
}
return null;
}
public Student findByName(String name) {
if ( name == null) return null;
String norm = normalizedName(name);
if (norm == null) return null;
for (Student s : students.values()) {
String sName = s.getName();
if (sName != null && norm.equals(normalizedName(sName))) {
return s;
}
}
return null;
}
public boolean updateStudent(Student updated) {
if ( updated == null || updated.getId() == null ) {
throw new IllegalArgumentException("Student and id must not be empty.");
}
String id = updated.getId();
if (!students.containsKey(id)) {
return false;
}
Student updatedEmail = findByEmail(updated.getEmail());
if (updatedEmail != null && !updatedEmail.getId().equals(id)) {
return false; //another stude
}
//name conflict nt has this email
if (updated.getName() != null) {
Student ownName = findByName(updated.getName());
if (ownName != null && !ownName.getId().equals(id)) {
return false;
}
}
students.put(id, updated);
return true;
}
public boolean deleteStudents(String id) {
if ( id == null ) return false;
return students.remove(id) != null;
}
public List<Student> listAll() {
return new ArrayList<>(students.values());
}
//helpers method
private String normalizedEmail(String email) {
if ( email == null) return null;
return email.trim().toLowerCase();
}
public String normalizedName(String name) {
if (name == null) return null;
return name.trim().toLowerCase();
}
}