-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
195 lines (166 loc) · 6.72 KB
/
Main.java
File metadata and controls
195 lines (166 loc) · 6.72 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.util.List;
import java.util.Scanner;
public class Main {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
StudentStore store = new StudentStore("data/students.csv");
List<Student> students = store.loadAll();
while (true) {
System.out.println("\n=== STUDENT MANAGEMENT SYSTEM ===");
System.out.println("1) Add student");
System.out.println("2) List students");
System.out.println("3) Search by ID");
System.out.println("4) Update student");
System.out.println("5) Delete student");
System.out.println("6) Exit");
System.out.print("Choose: ");
String choice = sc.nextLine().trim();
switch (choice) {
case "1":
addStudent(store, students);
break;
case "2":
listStudents(students);
break;
case "3":
searchById(store, students);
break;
case "4":
updateStudent(store, students);
break;
case "5":
deleteStudent(store, students);
break;
case "6":
System.out.println("Bye!");
return;
default:
System.out.println("Invalid choice. Try again.");
break;
}
}
}
private static void addStudent(StudentStore store, List<Student> students) {
int id = store.nextId(students);
System.out.print("Name: ");
String name = mustNotEmpty(sc.nextLine(), "Name");
int age = readInt("Age (10-80): ", 10, 80);
System.out.print("Email: ");
String email = mustContain(sc.nextLine(), "@", "Email must contain @");
System.out.print("Program (e.g., Software Engineering): ");
String program = mustNotEmpty(sc.nextLine(), "Program");
double gpa = readDouble("GPA (0.0 - 4.0): ", 0.0, 4.0);
Student s = new Student(id, name, age, email, program, gpa);
students.add(s);
store.saveAll(students);
System.out.println("✅ Added: " + s);
}
private static void listStudents(List<Student> students) {
if (students.isEmpty()) {
System.out.println("No students found.");
return;
}
System.out.println("\n--- Students ---");
for (Student s : students) {
System.out.println(s);
}
System.out.println("Total: " + students.size());
}
private static void searchById(StudentStore store, List<Student> students) {
int id = readInt("Enter student ID: ", 1, 1000000);
int idx = store.findIndexById(students, id);
if (idx == -1) {
System.out.println("Not found.");
return;
}
System.out.println("✅ Found: " + students.get(idx));
}
private static void updateStudent(StudentStore store, List<Student> students) {
int id = readInt("Enter student ID to update: ", 1, 1000000);
int idx = store.findIndexById(students, id);
if (idx == -1) {
System.out.println("Not found.");
return;
}
Student s = students.get(idx);
System.out.println("Current: " + s);
System.out.println("Leave blank to keep current value.");
System.out.print("New name: ");
String name = sc.nextLine().trim();
if (!name.isEmpty()) s.setName(name);
System.out.print("New age: ");
String ageStr = sc.nextLine().trim();
if (!ageStr.isEmpty()) s.setAge(parseIntRange(ageStr, 10, 80));
System.out.print("New email: ");
String email = sc.nextLine().trim();
if (!email.isEmpty()) {
if (!email.contains("@")) {
System.out.println("Email must contain @ (skipped).");
} else {
s.setEmail(email);
}
}
System.out.print("New program: ");
String program = sc.nextLine().trim();
if (!program.isEmpty()) s.setProgram(program);
System.out.print("New GPA: ");
String gpaStr = sc.nextLine().trim();
if (!gpaStr.isEmpty()) s.setGpa(parseDoubleRange(gpaStr, 0.0, 4.0));
store.saveAll(students);
System.out.println("✅ Updated: " + s);
}
private static void deleteStudent(StudentStore store, List<Student> students) {
int id = readInt("Enter student ID to delete: ", 1, 1000000);
int idx = store.findIndexById(students, id);
if (idx == -1) {
System.out.println("Not found.");
return;
}
Student removed = students.remove(idx);
store.saveAll(students);
System.out.println("🗑️ Deleted: " + removed);
}
// helpers
private static String mustNotEmpty(String s, String fieldName) {
s = s.trim();
if (s.isEmpty()) throw new IllegalArgumentException(fieldName + " cannot be empty.");
return s;
}
private static String mustContain(String s, String token, String msg) {
s = s.trim();
if (!s.contains(token)) throw new IllegalArgumentException(msg);
return s;
}
private static int readInt(String prompt, int min, int max) {
while (true) {
System.out.print(prompt);
String s = sc.nextLine().trim();
try {
return parseIntRange(s, min, max);
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
}
}
private static double readDouble(String prompt, double min, double max) {
while (true) {
System.out.print(prompt);
String s = sc.nextLine().trim();
try {
return parseDoubleRange(s, min, max);
} catch (Exception e) {
System.out.println("❌ " + e.getMessage());
}
}
}
private static int parseIntRange(String s, int min, int max) {
int v = Integer.parseInt(s);
if (v < min || v > max) throw new IllegalArgumentException("Value must be between " + min + " and " + max);
return v;
}
private static double parseDoubleRange(String s, double min, double max) {
double v = Double.parseDouble(s);
if (v < min || v > max) throw new IllegalArgumentException("Value must be between " + min + " and " + max);
return v;
}
}