-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.java
243 lines (201 loc) · 5.76 KB
/
Student.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
import java.util.Scanner;
import java.io.*;
public class Student {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String studentname; //name of student
int rollno, mob_no; //roll number and mobile number
float marks;//marks
//method to create a new file
void create() {
try {
File file = new File("Student.txt");
//File named Student.txt is created
boolean x = file.createNewFile();
if (x) {
System.out.println("File has been created successfully! ");
//Message of successful creation after new file is created
} else {
System.out.println("File already present");
//message if file already exists
}
} catch (IOException e) {
System.out.println("Exception Occurred:");
}
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Student.txt", true)));
int x = 1;
do {
Scanner sc = new Scanner(System.in);
int y;
//take input from user for name,roll no and mobile no.
System.out.print("\nEnter Student Name: ");
studentname = br.readLine();
System.out.print("Student Roll no: ");
rollno = Integer.parseInt(br.readLine());
System.out.print("Mobile Number: ");
mob_no = Integer.parseInt(br.readLine());
System.out.print("Marks : ");
marks = Float.parseFloat(br.readLine());
//display student data
pw.println(studentname + "\t" + rollno + "\t" + marks + "\t" + mob_no);
System.out.println("Do you want to enter more records? \n1) Yes\n2) No");
y = sc.nextInt();
if(y == 2) {
x = 0;
}
}while(x==1); //loop to add more records
pw.close();
}catch(IOException e) {
System.out.println(e);
}
}
void display() {
try {
// Open the file
BufferedReader file = new BufferedReader(new FileReader("Student.txt"));
String name;
int i = 1;
// Read records from the file
while ((name = file.readLine()) != null) {
System.out.println(name);
System.out.println("");
}
file.close();
} catch (FileNotFoundException e) {
System.out.println("\nERROR : File not Found !!!");
}catch(IOException e) {
System.out.println(e);
}
}
void search() {
try {
// Open the file
BufferedReader file = new BufferedReader(new FileReader("Student.txt"));
String name;
int flag = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter an id of the student you want to search: ");
String searchname = sc.next();
// Read records from the file
while ((name = file.readLine()) != null) {
String[] line = name.split("\t");
//System.out.println(line[0]);
if (searchname.equalsIgnoreCase(line[1])) {
System.out.println("Record found");
System.out.println(name);
System.out.println("");
flag = 1;
break;
}
}
if (flag == 0)
System.out.println("Record not found");
file.close();
} catch (FileNotFoundException e) {
System.out.println("\nERROR : File not Found !!!");
}catch (IOException e) {
System.out.println(e);
}
}
void clearRecords() {
// Create a blank file
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Student.txt")));
pw.close();
System.out.println("\nAll Records cleared successfully !");
for (int i = 0; i < 999999999; i++);
}catch (IOException e) {
System.out.println(e);
}
}
void modify() {
try {
// Open the file
BufferedReader file1 = new BufferedReader(new FileReader("Student.txt"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Student.txt", true)));
String name;
int flag = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the student you want to update: ");
String searchname = sc.next();
// Read records from the file
while ((name = file1.readLine()) != null) {
String[] line = name.split("\t");
System.out.println(line[0]);
if (!searchname.equalsIgnoreCase(line[0])) {
pw.println(name);
flag = 0;
} else {
System.out.println("Record found");
System.out.println("Enter updated marks:");
String up_mark = sc.next();
pw.println(line[0] + "\t" + line[1] + "\t" + up_mark+ "\t" + line[3]);
flag = 1;
}
}
file1.close();
pw.close();
File delName = new File("Student.txt");
File oldName = new File("StudentDb.txt");
File newName = new File("Student.txt");
if (delName.delete())
System.out.println("record updated successfully");
else
System.out.println("Error");
if (oldName.renameTo(newName))
System.out.println("Renamed successfully");
else
System.out.println("Error");
} catch (FileNotFoundException e) {
System.out.println("\nERROR : File not Found !!!");
}catch (IOException e) {
System.out.println(e);
}
}
void delete() {
File file = new File("Student.txt");
if (file.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Delete failed: File didn't delete");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student();
int x = 1;
int ch;
Scanner sc = new Scanner(System.in);
do {
System.out.println("\n\n1. Create Database" + "\n2. Display Database" + "\n3. Search Records" + "\n4. Modify Records" + "\n5. Clear Records" + "\n6. Delete file" + "\n7. Exit");
System.out.println("Enter Choice:: ");
ch = sc.nextInt();
switch(ch) {
case 1:
s.create();
// s.write();
break;
case 2:
s.display();
break;
case 3:
s.search();
break;
case 4:
s.modify();
break;
case 5:
s.clearRecords();
break;
case 6:
s.delete();
break;
case 7:
x = 1;
break;
default:
System.out.println("Invalid choice!");
}
}while(x==1);
}
}