-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddMCQ.java
161 lines (135 loc) · 4.82 KB
/
AddMCQ.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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class AddMCQ extends JFrame implements ActionListener
{
JPanel p1, p2, p3, p4, p5, p6;
JLabel l1, l2, l3, l4, l5, l6;
JTextField t1, t2, t3, t4, t5, t6;
JButton b1, b2;
String subject;
public AddMCQ(String s)
{
subject=s;
setup(s);
setVisible(true);
subject=s;
}
public void setup(String s)
{
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();
setTitle(subject + ": Multiple Choice Question");
setSize(500, 500);
setLayout(new GridLayout(6, 1));
setLocationRelativeTo(null);
l1=new JLabel("Enter Question:");
t1=new JTextField(30);
p1.add(l1);
p1.add(t1);
l2=new JLabel("A)");
t2=new JTextField(10);
l3=new JLabel("B)");
t3=new JTextField(10);
p2.add(l2);
p2.add(t2);
p2.add(l3);
p2.add(t3);
l4=new JLabel("C)");
t4=new JTextField(10);
l5=new JLabel("D)");
t5=new JTextField(10);
p3.add(l4);
p3.add(t4);
p3.add(l5);
p3.add(t5);
l6=new JLabel("Enter correct option:");
t6=new JTextField(10);
p4.add(l6);
p4.add(t6);
b1=new JButton("Add");
b2=new JButton("Back");
b1.addActionListener(this);
b2.addActionListener(this);
p5.add(b1);
p6.add(b2);
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);
add(p6);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("Add"))
{
try
{
String question=t1.getText();
String optionA=t2.getText();
String optionB=t3.getText();
String optionC=t4.getText();
String optionD=t5.getText();
String answer=t6.getText();
if(question.equals("")||answer.equals("")||optionA.equals("")||optionB.equals("")||optionC.equals("")||optionD.equals(""))
{
JOptionPane.showMessageDialog(null, "One of the fields is empty", "Error", JOptionPane.WARNING_MESSAGE);
}
else if(!(answer.equals(optionA)||answer.equals(optionB)||answer.equals(optionC)||answer.equals(optionD)))
{
JOptionPane.showMessageDialog(null, "Answer does not match any of the options", "Error", JOptionPane.WARNING_MESSAGE);
}
else if(optionA.equals(optionB)||optionA.equals(optionC)||optionA.equals(optionD)||optionB.equals(optionC)||optionB.equals(optionD)||optionC.equals(optionD))
{
JOptionPane.showMessageDialog(null, "Two of the options are same", "Error", JOptionPane.WARNING_MESSAGE);
}
else
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
String file=subject+".txt";
FileWriter fout=new FileWriter(file, true);
BufferedReader br=new BufferedReader(new FileReader(subject+"Counter.txt"));
int count=Integer.parseInt(br.readLine());
count++;
FileWriter fout2=new FileWriter((subject+"Counter.txt"));
fout2.write(count+"\n");
fout.write("Question "+count +": "+question+"\n");
fout.write("A)" + optionA+" ");
fout.write("B)" + optionB+" ");
fout.write("C)" + optionC+" ");
fout.write("D)" + optionD+" \n");
fout.write("Answer: "+answer+"\n");
fout.close();
fout2.close();
JOptionPane.showMessageDialog(null, "Question added successfully", "Information", JOptionPane.PLAIN_MESSAGE);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
else
{
dispose();
new AddQuestion(subject);
}
}
public void main(String args[])
{
new AddMCQ(subject);
}
}