-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManager.java
More file actions
108 lines (86 loc) · 3.15 KB
/
StudentManager.java
File metadata and controls
108 lines (86 loc) · 3.15 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
// StudentManager.java
// Handles all file operations and implements StudentsOperations
import java.io.*;
import java.util.*;
public class StudentManager implements StudentsOperations {
Scanner sc = new Scanner(System.in);
File file = new File("students.txt");
// addStudent() method
@Override
public void addStudent() throws IOException {
FileWriter fw = new FileWriter(file, true);
System.out.print("Enter Roll No.: ");
int rollNo = sc.nextInt();
sc.nextLine(); // consume leftover newline
System.out.print("Enter Student Name: ");
String name = sc.nextLine();
System.out.print("Enter Marks: ");
double marks = sc.nextDouble();
String data = rollNo + ", " + name + ", " + marks + "\n";
fw.write(data);
fw.close();
System.out.println("✅ Student Added Successfully!\n");
System.out.println(data);
}
// viewStudents() method
@Override
public void viewStudent() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
System.out.println("\nRoll | Name | Marks");
System.out.println("-------------------");
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
System.out.println(data[0].trim() + " | " + data[1].trim() + " | " + data[2].trim());
}
br.close();
System.out.println();
}
// searchStudent() method
@Override
public void searchStudent() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.print("Enter the roll number to search: ");
int rollNo = sc.nextInt();
String line;
boolean found = false;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (Integer.parseInt(data[0].trim()) == rollNo) {
System.out.println("✅ Found: " + data[1].trim() + " | Marks: " + data[2].trim());
found = true;
break;
}
}
if (!found)
System.out.println("❌ Student not found!");
br.close();
}
// deleteStudent() method
@Override
public void deleteStudent() throws IOException {
System.out.print("Enter Roll No to delete: ");
int r = sc.nextInt();
File tempFile = new File("temp.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
String line;
boolean deleted = false;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (Integer.parseInt(data[0].trim()) != r) {
bw.write(line + "\n");
} else {
deleted = true;
}
}
bw.close();
br.close();
file.delete();
tempFile.renameTo(file);
if (deleted)
System.out.println("🗑️ Record deleted successfully!");
else
System.out.println("❌ Roll number not found!");
}
}