-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.java
More file actions
260 lines (197 loc) · 7.57 KB
/
Copy pathProject.java
File metadata and controls
260 lines (197 loc) · 7.57 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import java.io.*;
import java.util.ArrayList;
class Student {
int id;
String name;
int totalClasses;
int attendedClasses;
int marks;
Student(int id, String name, int totalClasses, int attendedClasses, int marks) {
this.id = id;
this.name = name;
this.totalClasses = totalClasses;
this.attendedClasses = attendedClasses;
this.marks = marks;
}
double getAttendancePercentage() {
if (totalClasses == 0) return 0;
return (attendedClasses * 100.0) / totalClasses;
}
}
public class Project {
static void displayStudent(Student s) {
double percentage = s.getAttendancePercentage();
System.out.println("\nID: " + s.id);
System.out.println("Name: " + s.name);
System.out.println("Marks: " + s.marks);
System.out.printf("Attendance: %.2f%%\n", percentage);
if (percentage < 75)
System.out.println("Status: Warning (Low Attendance)");
else
System.out.println("Status: OK");
}
static void addStudent(ArrayList<Student> students, BufferedReader br) throws IOException {
System.out.print("Enter ID: ");
int id = Integer.parseInt(br.readLine());
for (int i = 0; i < students.size(); i++) {
if (students.get(i).id == id) {
System.out.println("ID already exists!");
return;
}
}
System.out.print("Enter Name: ");
String name = br.readLine();
System.out.print("Total Classes: ");
int total = Integer.parseInt(br.readLine());
System.out.print("Attended Classes: ");
int attended = Integer.parseInt(br.readLine());
System.out.print("Marks: ");
int marks = Integer.parseInt(br.readLine());
students.add(new Student(id, name, total, attended, marks));
System.out.println("Student added.");
}
static void deleteStudent(ArrayList<Student> students, int id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).id == id) {
students.remove(i);
System.out.println("Student deleted.");
return;
}
}
System.out.println("Student not found.");
}
static void updateStudent(ArrayList<Student> students, BufferedReader br, int id) throws IOException {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).id == id) {
Student s = students.get(i);
System.out.print("Enter new name: ");
s.name = br.readLine();
System.out.print("Enter new total classes: ");
s.totalClasses = Integer.parseInt(br.readLine());
System.out.print("Enter new attended classes: ");
s.attendedClasses = Integer.parseInt(br.readLine());
System.out.print("Enter new marks: ");
s.marks = Integer.parseInt(br.readLine());
System.out.println("Student updated.");
return;
}
}
System.out.println("Student not found.");
}
static void viewAll(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data.");
return;
}
for (int i = 0; i < students.size(); i++) {
displayStudent(students.get(i));
}
}
static void showStars(ArrayList<Student> students) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.print(s.name + " : ");
int bars = s.marks / 10;
for (int j = 0; j < bars; j++) {
System.out.print("#");
}
System.out.println(" (" + s.marks + ")");
}
}
static void saveToFile(ArrayList<Student> students) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("students.csv"));
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
bw.write(s.id + "," + s.name + "," + s.totalClasses + "," + s.attendedClasses + "," + s.marks);
bw.newLine();
}
bw.close();
}
static void loadFromFile(ArrayList<Student> students) throws IOException {
File file = new File("students.csv");
if (!file.exists()) return;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] d = line.split(",");
students.add(new Student(
Integer.parseInt(d[0]),
d[1],
Integer.parseInt(d[2]),
Integer.parseInt(d[3]),
Integer.parseInt(d[4])
));
}
br.close();
}
static void viewCSV() throws IOException {
File file = new File("students.csv");
if (!file.exists()) {
System.out.println("File not found.");
return;
}
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
System.out.println("\n--- CSV FILE DATA ---");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Student> students = new ArrayList<>();
loadFromFile(students);
while (true) {
System.out.println("\n===== MENU =====");
System.out.println("1. Add Student");
System.out.println("2. View All");
System.out.println("3. Search Student");
System.out.println("4. Show Stars");
System.out.println("5. Delete Student");
System.out.println("6. Update Student");
System.out.println("7. View CSV File");
System.out.println("8. Exit");
System.out.print("Enter choice: ");
int ch = Integer.parseInt(br.readLine());
switch (ch) {
case 1:
addStudent(students, br);
break;
case 2:
viewAll(students);
break;
case 3:
System.out.print("Enter ID: ");
int id = Integer.parseInt(br.readLine());
for (int i = 0; i < students.size(); i++) {
if (students.get(i).id == id) {
displayStudent(students.get(i));
break;
}
}
break;
case 4:
showStars(students);
break;
case 5:
System.out.print("Enter ID to delete: ");
deleteStudent(students, Integer.parseInt(br.readLine()));
break;
case 6:
System.out.print("Enter ID to update: ");
updateStudent(students, br, Integer.parseInt(br.readLine()));
break;
case 7:
viewCSV();
break;
case 8:
saveToFile(students);
System.out.println("Saved & Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}