-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelete.java
173 lines (147 loc) · 4.75 KB
/
Delete.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
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import static javax.swing.JOptionPane.showMessageDialog;
public class Delete extends JFrame implements ActionListener
{
private JLabel usnL;
private JTextField usnT;
private JButton delete,back;
Container con=null;
String usn="";
Delete()
{
super("Delete Record");
con = getContentPane();
con.setLayout(null);
Color lightBlue = new Color(0,255,255);
con.setBackground(lightBlue);
con.setSize(300,300);
con.setLayout(null);
con.setVisible(true);
Font font = new Font("Verdana", Font.BOLD, 16);
Color blue = new Color(30,144,255);
usnL=new JLabel("Enter USN of record to be deleted");
usnL.setBounds(400, 50, 700,150);
usnL.setFont(font);
usnL.setForeground(Color.BLACK);
usnT=new JTextField(200);
usnT.setBounds(725,100,250,50);
usnT.setFont(font);
usnT.setForeground(Color.BLACK);
delete = new JButton("Delete");
delete.setBounds(400,600,150,40);
delete.addActionListener(this);
delete.setFont(font);
Color pul = new Color(0,0,255);
Border bored = BorderFactory.createLineBorder(pul,5);
delete.setBorder(bored);
delete.setForeground(Color.WHITE);
delete.setBackground(blue);
back = new JButton("Go Back");
back.setBounds(600,600,150,40);
back.addActionListener(this);
back.setFont(font);
back.setBorder(bored);
back.setForeground(Color.WHITE);
back.setBackground(blue);
con.add(usnL);
con.add(usnT);
con.add(delete);
con.add(back);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==delete)
{
try{
String usn = usnT.getText();
String usn1="",r;
int count=0;
File file = new File("student.txt"); //student
BufferedReader br = new BufferedReader(new FileReader(file));
File file1 = new File("journal.txt"); //journal
BufferedReader br1 = new BufferedReader(new FileReader(file1));
File temp = new File("temp.txt"); //student temp
Boolean createNewFile = temp.createNewFile();
BufferedWriter pw = new BufferedWriter(new FileWriter(temp));
File temp1 = new File("temp1.txt"); //journal temp
Boolean createNewFile1 = temp.createNewFile();
BufferedWriter pw1 = new BufferedWriter(new FileWriter(temp1));
while((r= br.readLine()) !=null)
{
String[] result = r.split("\\|");
usn1=result[1];
if(usn1.equals(usn))
{
count=1;
continue;
}
else
{
pw.write(r);
pw.write("\n");
}
}
while((r= br1.readLine()) !=null)
{
String[] result = r.split("\\|");
usn1=result[0];
if(usn1.equals(usn))
{
continue;
}
else
{
pw1.write(r);
pw1.write("\n");
}
}
if(count == 0)
showMessageDialog(null, "Invalid USN");
else
showMessageDialog(null, "Record Deleted!");
pw.flush();
pw.close();
br.close();
pw1.close();
br1.close();
file.delete();
temp.renameTo(file);
file1.delete();
temp1.renameTo(file1);
}
catch(Exception e)
{
e.printStackTrace();
}
this.dispose();
Home h=new Home();
h.setSize(2300,790);
h.setVisible(true);
}
if(ae.getSource()==back)
{
try
{
this.dispose();
Home h=new Home();
h.setSize(2300,790);
h.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String args[])
{
Delete del=new Delete();
del.setSize(2300,790);
del.setVisible(true);
}
}