-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeleteQuestion.java
231 lines (208 loc) · 6.68 KB
/
DeleteQuestion.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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DeleteQuestion extends JFrame implements ActionListener
{
String subject;
JLabel l1;
JButton b1, b2, b3;
JTextField text1;
FileWriter fw;
FileReader frcnt, fr;
BufferedWriter bw;
BufferedReader brcnt, br;
Scanner sc;
FileWriter fwcnt;
BufferedWriter bwcnt;
ArrayList<String>quesal, typeal, ansal;
public DeleteQuestion(String s)
{
subject = s;
setTitle("Choose the question to delete");
l1 = new JLabel("Enter question number");
l1.setBounds(50,50,150,30);
add(l1);
text1 = new JTextField();
text1.setBounds(200,50, 100,30);
add(text1);
b1 = new JButton("Click to delete");
b1.setBounds(100,170, 200,30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("Click to view");
b2.setBounds(100,220,200,30);
b2.addActionListener(this);
add(b2);
b3 = new JButton("Go back");
b3.setBounds(100,300,200,30);
b3.addActionListener(this);
add(b3);
setSize(400,400);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
quesal=new ArrayList<>();
typeal=new ArrayList<>();
ansal=new ArrayList<>();
if(e.getSource()==b1)
{
try
{
String numbertodelete=text1.getText();
frcnt=new FileReader(subject+"Counter.txt");
int quesnumber=0;
brcnt = new BufferedReader(frcnt);
String qu=brcnt.readLine();
quesnumber=Integer.parseInt(qu);
quesnumber--;
brcnt.close();
frcnt.close();
fr=new FileReader(subject+".txt");
br= new BufferedReader(fr);
int quescounter=1;
int flag =0;
String s=br.readLine();
while(s!=null)
{
int digitsofques=0;
int i=9;
String curr="";
while((int)s.charAt(i)>=48&&(int)s.charAt(i)<=57)
{
digitsofques++;
curr+=s.charAt(i);
i++;
}
String add=Integer.toString(quescounter);
String sa=s.substring(0, 9)+add+s.substring(digitsofques+9);
String sb=br.readLine();
String sc=br.readLine();
if(Integer.parseInt(curr)==Integer.parseInt(numbertodelete)&&flag==0)
{
flag=1;
s=br.readLine();
continue;
}
quesal.add(sa);
typeal.add(sb);
ansal.add(sc);
quescounter++;
s=br.readLine();
}
if(flag==0)
{
throw new Exception();
}
fr.close();
br.close();
fw=new FileWriter(subject+".txt", false);
bw=new BufferedWriter(fw);
for (int i = 0; i < quesal.size(); i++)
{
bw.write(quesal.get(i));
bw.newLine();
bw.write(typeal.get(i));
bw.newLine();
bw.write(ansal.get(i));
bw.newLine();
}
fwcnt=new FileWriter(subject+"Counter.txt", false);
bwcnt = new BufferedWriter(fwcnt);
bwcnt.write(Integer.toString(quesnumber));
JOptionPane.showMessageDialog(this,"Deletion successful");
bwcnt.close();
fwcnt.close();
bw.close();
fw.close();
}
catch(Exception e1)
{
System.out.println("Unable to delete question");
}
text1.setText("");
}
if(e.getSource()==b2)
{
try
{
String numbertodelete=text1.getText();
fr= new FileReader(subject+".txt");
br= new BufferedReader(fr);
int quescounter=1;
int flag =0;
String s=br.readLine();
String ans1="";
String ans2="";
String ans3="";
while(s!=null)
{
int digitsofques=0;
int i =9;
String curr="";
while((int)s.charAt(i)>=48&&(int)s.charAt(i)<=57)
{
digitsofques++;
curr+=s.charAt(i);
i++;
}
String add=Integer.toString(quescounter);
String sa=s.substring(0, 9)+add+s.substring(digitsofques+9);
String sb=br.readLine();
String sc=br.readLine();
if(Integer.parseInt(curr)==Integer.parseInt(numbertodelete)&&flag==0)
{
flag=1;
ans1=sa;
ans2=sb;
ans3=sc;
break;
}
quescounter++;
s=br.readLine();
}
if(flag==0)
throw new Exception();
else
JOptionPane.showMessageDialog(this, ans1+"\n"+ans2+"\n"+ans3);
fr.close();
br.close();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(this, "Unable to find question");
}
text1.setText("");
}
if(e.getSource()==b3)
{
dispose();
try
{
if(subject.equals("Physics"))
{
dispose();
new Physics();
}
else if(subject.equals("Chemistry"))
{
dispose();
new Chemistry();
}
else
{
dispose();
new Mathematics();
}
}
catch(Exception e1)
{
System.out.println(e1);
}
}
}
}