-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchit8.java
297 lines (265 loc) · 11.3 KB
/
chit8.java
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
//~ Implement a program for maintaining a database of student records using Files. Student has Student_id,name, Roll_no, Class, marks and address. Display the data for few students.
//^• Create Database • Display Database • Delete Records • Update Record • Search Record
import java.io.*;
import java.util.Scanner;
class Student {
// fields for student details
int studentId;
String name;
int rollNo;
String className;
int marks;
String address;
}
public class chit8 {
// method to create a new student record
public static void createRecord(Student student) throws IOException {
// open the file in append mode
FileWriter fw = new FileWriter("students.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
// write the student details to the file
pw.println(student.studentId);
pw.println(student.name);
pw.println(student.rollNo);
pw.println(student.className);
pw.println(student.marks);
pw.println(student.address);
// close the file
pw.close();
}
// method to display the student records
public static void displayRecords() throws IOException {
// open the file in read mode
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
// read the student details from the file and print them
int studentId = Integer.parseInt(line);
String name = br.readLine();
int rollNo = Integer.parseInt(br.readLine());
String className = br.readLine();
int marks = Integer.parseInt(br.readLine());
String address = br.readLine();
System.out.println("Student ID: " + studentId);
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Class: " + className);
System.out.println("Marks: " + marks);
System.out.println("Address: " + address);
System.out.println();
}
// close the file
br.close();
}
// method to delete a student record
public static void deleteRecord(int studentId) throws IOException {
// create a temporary file
File tempFile = new File("temp.txt");
// open the original file in read mode and the temporary file in write mode
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(tempFile);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
String line;
while ((line = br.readLine()) != null) {
int id = Integer.parseInt(line);
if (id != studentId) {
// if the student ID does not match, write the record to the temporary file
pw.println(line);
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
} else {
// if the student ID matches, skip the record
br.readLine();
br.readLine();
br.readLine();
br.readLine();
br.readLine();
}
}
// close the files
pw.close();
br.close();
// delete the original file
File originalFile = new File("students.txt");
originalFile.delete();
// rename the temporary file to the original file
tempFile.renameTo(originalFile);
}
// method to update a student record
public static void updateRecord(int studentId, Student updatedStudent) throws IOException {
// create a temporary file
File tempFile = new File("temp.txt");
// open the original file in read mode and the temporary file in write mode
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(tempFile);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
String line;
while ((line = br.readLine()) != null) {
int id = Integer.parseInt(line);
if (id == studentId) {
// if the student ID matches, write the updated student details to the temporary
// file
pw.println(updatedStudent.studentId);
pw.println(updatedStudent.name);
pw.println(updatedStudent.rollNo);
pw.println(updatedStudent.className);
pw.println(updatedStudent.marks);
pw.println(updatedStudent.address);
} else {
// if the student ID does not match, write the original record to the temporary
// file
pw.println(line);
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
pw.println(br.readLine());
}
}
// close the files
pw.close();
br.close();
// delete the original file
File originalFile = new File("students.txt");
originalFile.delete();
// rename the temporary file to the original file
tempFile.renameTo(originalFile);
}
// method to search for a student record
public static void searchRecord(int studentId) throws IOException {
// open the file in read mode
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
String line;
boolean found = false;
while ((line = br.readLine()) != null) {
int id = Integer.parseInt(line);
if (id == studentId) {
// if the student ID matches, print the student details
String name = br.readLine();
int rollNo = Integer.parseInt(br.readLine());
String className = br.readLine();
int marks = Integer.parseInt(br.readLine());
String address = br.readLine();
System.out.println("Student ID: " + studentId);
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Class: " + className);
System.out.println("Marks: " + marks);
System.out.println("Address: " + address);
System.out.println();
found = true;
break;
} else {
// if the student ID does not match, skip the record
br.readLine();
br.readLine();
br.readLine();
br.readLine();
br.readLine();
}
}
// close the file
br.close();
if (!found) {
System.out.println("No student found with ID: " + studentId);
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
// display the options
System.out.println("1. Create a new student record");
System.out.println("2. Display all student records");
System.out.println("3. Delete a student record");
System.out.println("4. Update a student record");
System.out.println("5. Search for a student record");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine(); // to consume the newline character
if (choice == 1) {
// create a new student record
System.out.print("Enter student ID: ");
int studentId = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter roll no: ");
int rollNo = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter class: ");
String className = sc.nextLine();
System.out.print("Enter marks: ");
int marks = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter address: ");
String address = sc.nextLine();
Student student = new Student();
student.studentId = studentId;
student.name = name;
student.rollNo = rollNo;
student.className = className;
student.marks = marks;
student.address = address;
createRecord(student);
System.out.println("Student record created successfully!");
} else if (choice == 2) {
// display all student records
displayRecords();
} else if (choice == 3) {
// delete a student record
System.out.print("Enter student ID: ");
int studentId = sc.nextInt();
deleteRecord(studentId);
System.out.println("Student record deleted successfully!");
} else if (choice == 4) {
// update a student record
System.out.print("Enter student ID: ");
int studentId = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter roll no: ");
int rollNo = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter class: ");
String className = sc.nextLine();
System.out.print("Enter marks: ");
int marks = sc.nextInt();
sc.nextLine(); // to consume the newline character
System.out.print("Enter address: ");
String address = sc.nextLine();
Student updatedStudent = new Student();
updatedStudent.studentId = studentId;
updatedStudent.name = name;
updatedStudent.rollNo = rollNo;
updatedStudent.className = className;
updatedStudent.marks = marks;
updatedStudent.address = address;
updateRecord(studentId, updatedStudent);
System.out.println("Student record updated successfully!");
} else if (choice == 5) {
// search for a student record
System.out.print("Enter student ID: ");
int studentId = sc.nextInt();
searchRecord(studentId);
} else if (choice == 6) {
// exit the program
break;
} else {
System.out.println("Invalid choice. Please try again.");
}
}
sc.close();
}
}