-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 8
More file actions
110 lines (105 loc) · 2.73 KB
/
Assignment 8
File metadata and controls
110 lines (105 loc) · 2.73 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
import java.io.*;
class StudentRecords
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public void addRecords() throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt",true)));
String Class, name, address, dob;
int age;
long telephoneNo;
String s;
boolean addMore = false;
do
{
System.out.print("\nEnter name: ");
name = br.readLine();
System.out.print("Address: ");
address = br.readLine();
System.out.print("Class: ");
Class = br.readLine();
System.out.print("Date of Birth (dd/mm/yy) : ");
dob = br.readLine();
System.out.print("Age: ");
age = Integer.parseInt(br.readLine());
System.out.print("Telephone No.: ");
telephoneNo = Long.parseLong(br.readLine());
pw.println(name);
pw.println(address);
pw.println(Class);
pw.println(dob);
pw.println(age);
pw.println(telephoneNo);
System.out.print("\nRecords added successfully !\n\nDo you want to add more records ? (y/n) :
");
s = br.readLine();
if(s.equalsIgnoreCase("y")) {
addMore = true;
System.out.println();
}
else
addMore = false;
}
while(addMore);
pw.close();
showMenu();
}
public void readRecords() throws IOException {
try{
BufferedReader file = new BufferedReader(new
FileReader("studentRecords.txt"));
String name;
int i=1;
while((name = file.readLine()) != null) {
System.out.println("S.No. : " +(i++));
System.out.println(" ------------ ");
System.out.println("\nName: " +name);
System.out.println("Address: "+file.readLine());
System.out.println("Class: "+file.readLine());
System.out.println("Date of Birth : "+file.readLine());
System.out.println("Age: "+Integer.parseInt(file.readLine()));
System.out.println("Tel. No.: "+Long.parseLong(file.readLine()));
System.out.println();
}
file.close();
showMenu();
}
catch(FileNotFoundException e){
System.out.println("\nERROR : File not Found !!!");
}
}
public void clear() throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt")));
pw.close();
System.out.println("\nAll Records cleared successfully !");
showMenu();
}
public void showMenu() throws IOException{
System.out.print("1 : Add Records\n2 : Display Records\n3 : Clear All Records\n4 : Exit\n\nYour
Choice : ");
int choice = Integer.parseInt(br.readLine());
switch(choice){
case 1:
addRecords();
break;
case 2:
readRecords();
break;
case 3:
clear();
break;
case 4:
System.exit(1);
break;
default:
System.out.println("\nInvalid Choice !");
break;
}
}
public static void main(String[] args) throws IOException {
StudentRecords call = new StudentRecords();
call.showMenu();
}
}