-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMicrojava.java
More file actions
313 lines (296 loc) · 12.4 KB
/
myMicrojava.java
File metadata and controls
313 lines (296 loc) · 12.4 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import java.io.*;
import java.util.*;
class AMS
{
static Scanner scanner = new Scanner(System.in);
static ArrayList<Integer> rollNumbers = new ArrayList<>();
static ArrayList<String> statuses = new ArrayList<>();
static String subjectName, startTime, endTime, date;
public static void main(String[] args)
{
boolean exit = false;
while (!exit)
{
System.out.println("------------------------------------------------------------");
System.out.println("| Attendance System Menu: |");
System.out.println("| 1. Mark Attendance |");
System.out.println("| 2. View Attendance |");
System.out.println("| 3. Add Student |");
System.out.println("| 4. Edit Student |");
System.out.println("| 5. View All Students |");
System.out.println("| 6. Overall Attendance |");
System.out.println("| 7. Absent Days |");
System.out.println("| 8. Exit |");
System.out.println("------------------------------------------------------------");
System.out.print("\nEnter your choice: ");
int choice = scanner.nextInt();
switch (choice)
{
case 1:
markAttendance();
break;
case 2:
viewAttendance();
break;
case 3:
addStudent();
break;
case 4:
editStudent();
break;
case 5:
viewAllStudents();
break;
case 6:
overallAttendance();
break;
case 7:
absentDays();
break;
case 8:
saveAttendanceToFile();
exit = true;
break;
default:
System.out.println("Invalid choice! Please enter a number between 1 and 8.");
}
}
scanner.close();
}
public static void markAttendance()
{
System.out.print("| Enter the number of students: |");
int numStudents = scanner.nextInt();
System.out.print("| Enter the starting roll number: |");
int startingRollNo = scanner.nextInt();
System.out.print("| Enter the subject name: |");
subjectName = scanner.next();
System.out.print("| Enter the starting timing of the session: |");
startTime = scanner.next();
System.out.print("| Enter the ending timing of the session: |");
endTime = scanner.next();
System.out.print("| Enter the date: |");
date = scanner.next();
String fileName = "attend_" + date + ".txt";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)))
{
writer.write("| Session Details |\n");
writer.write("| Subject: " + subjectName + " |\n");
writer.write("| Timing: " + startTime + " - " + endTime + " |\n");
writer.write("| Date: " + date + " |\n");
for (int i = 0; i < numStudents; i++)
{
int rollNo = startingRollNo + i;
System.out.print("Student " + rollNo + ": Enter 'p' for present or 'a' for absent: ");
String attendance = scanner.next().toLowerCase();
if (attendance.equals("p") || attendance.equals("a"))
{
rollNumbers.add(rollNo);
statuses.add(attendance.equals("p") ? "Present" : "Absent");
writer.write("| " + date + " | " + startTime + " | " + endTime + " | " + rollNo + " | " + (attendance.equals("p") ? "Present" : "Absent") + " |\n");
}
else
{
System.out.println("Invalid input. Please enter 'p' for present or 'a' for absent.");
i--;
}
}
System.out.println("Attendance marked successfully.");
}
catch(IOException e)
{
System.out.println("Failed to mark attendance.");
}
}
public static void viewAttendance()
{
System.out.println("| Enter the date: |");
String datecheck = scanner.next();
String filecheck = "attend_" + datecheck + ".txt";
System.out.println("| Enter the student's roll number: |");
int rollNumber = scanner.nextInt();
try (BufferedReader reader = new BufferedReader(new FileReader(filecheck)))
{
String line;
boolean found = false;
while ((line = reader.readLine()) != null)
{
if (line.contains("| " + rollNumber + " |"))
{
found = true;
String[] parts = line.split("\\|");
String result = parts[5].trim();
System.out.println("Roll No is " + result);
break;
}
}
if (!found)
{
System.out.println("Roll No is Not Present");
}
}
catch (IOException e)
{
System.out.println("Error reading attendance file.");
}
}
public static void addStudent()
{
System.out.println("| Enter the student's roll number: |");
int rollNumber = scanner.nextInt();
if (!rollNumbers.contains(rollNumber))
{
rollNumbers.add(rollNumber);
statuses.add("Absent");
System.out.println("| Student with roll number " + rollNumber + " added successfully. |");
}
else
{
System.out.println("| Student with roll number " + rollNumber + " already exists. |");
}
}
public static void editStudent()
{
System.out.print("| Enter the student's roll number: |");
int rollNumber = scanner.nextInt();
int index = rollNumbers.indexOf(rollNumber);
if (index != -1)
{
System.out.print("| Enter 'p' for present or 'a' for absent: |");
String attendance = scanner.next().toLowerCase();
if (attendance.equals("p") || attendance.equals("a"))
{
statuses.set(index, attendance.equals("p") ? "Present" : "Absent");
System.out.println("| Attendance updated successfully for Student " + rollNumber + " |");
}
else
{
System.out.println("| Invalid input. Please enter 'p' for present or 'a' for absent. |");
}
}
else
{
System.out.println("| Student with roll number " + rollNumber + " does not exist. |");
}
}
public static void viewAllStudents()
{
System.out.println("|--------------------------------------------|");
System.out.println("| View All Students: |");
System.out.println("|--------------------------------------------|");
System.out.println("| 1. Present Students |");
System.out.println("| 2. Absent Students |");
System.out.println("| 3. All Students |");
System.out.println("|--------------------------------------------|");
System.out.print("| Enter your choice: ");
int choice = scanner.nextInt();
switch (choice)
{
case 1:
viewPresentStudents();
break;
case 2:
viewAbsentStudents();
break;
case 3:
viewAllStudentsAttendance();
break;
default:
System.out.println("|--------------------------------------------|");
System.out.println("| Invalid choice! Please enter a number between 1 and 3. |");
System.out.println("|--------------------------------------------|");
}
}
public static void viewPresentStudents()
{
System.out.println("|--------------------------------------------|");
System.out.println("| Present Students: |");
System.out.println("|--------------------------------------------|");
for (int i = 0; i < rollNumbers.size(); i++)
{
if (statuses.get(i).equals("Present"))
{
System.out.println("| Roll Number: " + rollNumbers.get(i) + " |");
}
}
}
public static void viewAbsentStudents()
{
System.out.println("|--------------------------------------------|");
System.out.println("| Absent Students |");
System.out.println("|--------------------------------------------|");
for (int i = 0; i < rollNumbers.size(); i++)
{
if (statuses.get(i).equals("Absent"))
{
System.out.println("| Roll Number: " + rollNumbers.get(i) + " |");
}
}
}
public static void viewAllStudentsAttendance()
{
System.out.println("|--------------------------------------------|");
System.out.println("| All Students Attendance: |");
System.out.println("|--------------------------------------------|");
for (int i = 0; i < rollNumbers.size(); i++)
{
System.out.println("| Roll Number: " + rollNumbers.get(i) + " | Status: " + statuses.get(i) + " |");
}
}
public static void overallAttendance()
{
int presentCount = 0;
for (String status : statuses)
{
if (status.equals("Present"))
{
presentCount++;
}
}
double percentage = (presentCount * 100.0)/rollNumbers.size();
System.out.println("|--------------------------------------------|");
System.out.println("| Overall Attendance Percentage: " + percentage + "% |");
System.out.println("|--------------------------------------------|");
}
public static void absentDays()
{
System.out.println("|--------------------------------------------|");
System.out.println("| Enter the student's roll number: |");
int rollNumber = scanner.nextInt();
int index = rollNumbers.indexOf(rollNumber);
if (index != -1)
{
int absentDays = statuses.get(index).equals("Absent") ? 1 : 0;
System.out.println("| Total Absent Days for Student " + rollNumber + ": " + absentDays + " |");
}
else
{
System.out.println("| Student with roll number " + rollNumber + " does not exist. |");
}
}
public static void saveAttendanceToFile()
{
try
{
System.out.print("| Enter the date: |");
date = scanner.next();
String fileName = "attend_" + date + ".txt";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)))
{
writer.write("| Session Details |\n");
writer.write("| Subject: " + subjectName + " |\n");
writer.write("| Timing: " + startTime + " - " + endTime + " |\n");
writer.write("| Date: " + date + " |\n");
for (int i = 0; i < rollNumbers.size(); i++)
{
writer.write("| " + date + " | " + startTime + " | " + endTime + " | " + rollNumbers.get(i) + " | " + statuses.get(i) + " |\n");
}
System.out.println("| Attendance records saved successfully. |");
}
}
catch (IOException e)
{
System.out.println("| Failed to save attendance records. |");
}
}
}