-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHome.java
More file actions
697 lines (635 loc) · 18.7 KB
/
Copy pathHome.java
File metadata and controls
697 lines (635 loc) · 18.7 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
public class Home extends JFrame {
private JPanel contentPane;
private JTextField stdNameText;
private JTextField emailText;
JButton submitBtn;
ExamData db;
Connection cn;
Statement st;
String sql,examName,ans4;
ResultSet rs;
JComboBox<String> examCombo;
JPanel minor,main;
int timecounter=60,ss=60;
long idg,eidg,qidg;
JLabel timeLabel,gg;
Random random;
JLabel questionLabel;
JRadioButton ans4Radio,ans3Radio,ans2Radio,ans1Radio;
long score=0,questionAttempted=0,correctAttempts=0,wrongAttempts=0,totalMarks=0,achivedMarks=0,correctAns=-1;
long totalQuestions,negtiveMarks,marksForEach,currentQno=0;
ButtonGroup Fgroup;
Timer timer;
private JButton admin;
boolean chk=false;
boolean firstIncheck=true;
boolean rchk=false;
JLabel mainLabel2,mainLabel;
public static void main(String[] args) {
Home frame = new Home();
frame.setVisible(true);
}
public Home() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setBounds(100, 100, 790, 443);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
main = new JPanel();
main.setBounds(0, 0, 764, 405);
contentPane.add(main);
main.setLayout(null);
mainLabel = new JLabel("Home");
mainLabel.setHorizontalAlignment(SwingConstants.CENTER);
mainLabel.setFont(new Font("Arial Black", Font.PLAIN, 20));
mainLabel.setBounds(172, 28, 283, 29);
main.add(mainLabel);
JLabel examLabel = new JLabel("Exam Name");
examLabel.setFont(new Font("Arial", Font.PLAIN, 15));
examLabel.setBounds(10, 66, 162, 32);
main.add(examLabel);
examCombo = new JComboBox<String>();
examCombo.setBounds(214, 67, 238, 32);
main.add(examCombo);
JLabel stdNameLabel = new JLabel("Student Name");
stdNameLabel.setFont(new Font("Arial", Font.PLAIN, 15));
stdNameLabel.setBounds(10, 110, 163, 32);
main.add(stdNameLabel);
stdNameText = new JTextField();
stdNameText.setColumns(10);
stdNameText.setBounds(214, 116, 238, 29);
main.add(stdNameText);
JLabel emailLabel = new JLabel("Email");
emailLabel.setFont(new Font("Arial", Font.PLAIN, 15));
emailLabel.setBounds(10, 170, 181, 32);
main.add(emailLabel);
emailText = new JTextField();
emailText.setColumns(10);
emailText.setBounds(214, 173, 238, 29);
main.add(emailText);
submitBtn = new JButton("Start Exam");
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
submitBtn.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(onSubmit())
{
setTime();
displayInst();
main.setVisible(false);
minor.setVisible(true);
loadThis();
//displayInst();
startTime();
//progress();
}
}
}
});
submitBtn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(onSubmit())
{
setTime();
displayInst();
main.setVisible(false);
minor.setVisible(true);
loadThis();
//displayInst();
startTime();
//progress();
}
}
});
submitBtn.setFont(new Font("Arial", Font.PLAIN, 12));
submitBtn.setBounds(203, 238, 126, 41);
main.add(submitBtn);
admin = new JButton("Admin");
admin.setForeground(Color.DARK_GRAY);
admin.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
new Admin().setVisible(true);
kill();
}
}
});
admin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
new Admin().setVisible(true);
kill();
}
});
admin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
admin.setFont(new Font("Arial", Font.PLAIN, 12));
admin.setBounds(0, 3, 126, 65);
main.add(admin);
JLabel lbldev = new JLabel("This project is devloped by Mukesh Kumar student of BCA at DAV college Abohar ");
lbldev.setFont(new Font("Arial Narrow", Font.PLAIN, 11));
lbldev.setBounds(136, 3, 425, 14);
main.add(lbldev);
//main.setVisible(false);
minor = new JPanel();
minor.setBounds(0, 0, 764, 405);
contentPane.add(minor);
minor.setLayout(null);
mainLabel2 = new JLabel("home");
mainLabel2.setHorizontalAlignment(SwingConstants.CENTER);
mainLabel2.setFont(new Font("Arial Black", Font.PLAIN, 20));
mainLabel2.setBounds(0, 0, 764, 42);
minor.add(mainLabel2);
questionLabel = new JLabel("Question ?");//,
questionLabel.setFont(new Font("Arial", Font.PLAIN, 15));
questionLabel.setBounds(11, 53, 617, 32);
minor.add(questionLabel);
ans1Radio = new JRadioButton("ans ");
ans1Radio.setFont(new Font("Arial Black", Font.PLAIN, 16));
ans1Radio.setBounds(20, 92, 534, 41);
minor.add(ans1Radio);
ans2Radio = new JRadioButton("");
ans2Radio.setFont(new Font("Arial Black", Font.PLAIN, 16));
ans2Radio.setBounds(20, 136, 534, 37);
minor.add(ans2Radio);
ans3Radio = new JRadioButton("");
ans3Radio.setFont(new Font("Arial Black", Font.PLAIN, 16));
ans3Radio.setBounds(20, 176, 534, 41);
minor.add(ans3Radio);
ans4Radio = new JRadioButton("");
ans4Radio.setFont(new Font("Arial Black", Font.PLAIN, 16));
ans4Radio.setBounds(20, 220, 534, 42);
minor.add(ans4Radio);
Fgroup=new ButtonGroup();
Fgroup.add(ans1Radio);
Fgroup.add(ans2Radio);
Fgroup.add(ans3Radio);
Fgroup.add(ans4Radio);
JButton button = new JButton("Submit");
button.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(rchk)
progress();
loadThis();
}
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
if(rchk)
progress();
loadThis();
}
});
button.setFont(new Font("Arial", Font.PLAIN, 12));
button.setBounds(232, 286, 126, 41);
minor.add(button);
JButton clearButton = new JButton("Clear ans");
clearButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
Fgroup.clearSelection();
}
}
});
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Fgroup.clearSelection();
}
});
clearButton.setFont(new Font("Arial", Font.PLAIN, 12));
clearButton.setBounds(46, 286, 126, 41);
minor.add(clearButton);
timeLabel = new JLabel("mm:ss");
timeLabel.setFont(new Font("Arial", Font.PLAIN, 15));
timeLabel.setBounds(667, 53, 60, 32);
minor.add(timeLabel);
gg = new JLabel("mm:ss");
gg.setFont(new Font("Arial", Font.PLAIN, 15));
gg.setBounds(667, 53, 60, 32);
minor.add(timeLabel);
JButton skipButton = new JButton("Skip");
skipButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
firstIncheck=true;
loadThis();
}
}
});
skipButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstIncheck=true;
loadThis();
}
});
skipButton.setFont(new Font("Arial", Font.PLAIN, 12));
skipButton.setBounds(398, 286, 126, 41);
minor.add(skipButton);
minor.setVisible(false);
random=new Random();
fload();
}
public void fload()
{
//form load//
score=questionAttempted=correctAttempts=wrongAttempts=totalMarks=achivedMarks=0;correctAns=-1;
totalQuestions=negtiveMarks=marksForEach=currentQno=0;ss=60;chk=false;
try
{
db=new ExamData();
cn=db.getConnection();
st = cn.createStatement();
ResultSet rs = st.executeQuery("Select exam_name from exam_setup order by exam_name");
examCombo.removeAllItems();
examCombo.addItem("");
stdNameText.setText("");//test();
emailText.setText("");
while (rs.next())
{
examCombo.addItem(rs.getString(1));
}
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}//end of fload();
public boolean onSubmit()
{
try
{
String msg;
if(examCombo.getSelectedItem().toString().equals(""))
{
msg="Please Enter The Exam Type...!";
examCombo.requestFocusInWindow();
JOptionPane.showMessageDialog(null,msg);
return false;
}
else if(stdNameText.getText().equals(""))
{
msg="Please Enter Student Name...!";
stdNameText.requestFocusInWindow();
JOptionPane.showMessageDialog(null,msg);
return false;
}
else if(emailText.getText().equals(""))
{
msg="Please Enter Email Addrs";
emailText.requestFocusInWindow();
JOptionPane.showMessageDialog(null,msg);
return false;
}
else
{
String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
Pattern p =Pattern.compile(ePattern);
Matcher m = p.matcher(emailText.getText());
boolean bm = m.matches();
if(bm == false)
{
msg="Please Enter a valid Email Addrs";
emailText.requestFocusInWindow();
JOptionPane.showMessageDialog(null,msg);
return false;
}
}
//long id;
st=cn.createStatement();
sql="Select id from student order by id DESC limit 1";
rs=st.executeQuery(sql);
if(rs.next())
idg=rs.getLong(1)+1;
else idg=1;
//st.executeUpdate(sql);
JOptionPane.showMessageDialog(null,stdNameText.getText().toUpperCase()+"! \nplease note your id for further use\nid="+idg);
mainLabel2.setText(stdNameText.getText()+" id:"+idg);
insLoad();
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
return true;
}
public void insLoad()
{
try
{
long tmp = 0;
sql="delete * from buffer";
st.executeUpdate(sql);
sql="select * from exam_setup where exam_name='"+examCombo.getSelectedItem().toString().toUpperCase()+"'";
System.out.println(sql);
rs=st.executeQuery(sql);
if(rs.next())
{
tmp=rs.getLong(2);
eidg=rs.getLong(1);
marksForEach=rs.getLong(4);
negtiveMarks=rs.getLong(5);
totalQuestions=rs.getLong(6);
examName=rs.getString(7);
totalMarks=marksForEach*totalQuestions;
System.out.println(eidg+" current exam details:->Time:"+rs.getLong(3)+"Marks for each q:"+marksForEach+"negtive marks:"+negtiveMarks+"total questions:"+totalQuestions+"exam name: "+examName);
}
sql="select id from exam_questions where c_id="+tmp+"";
System.out.println(sql);
rs=st.executeQuery(sql);
while(rs.next())
{
sql="insert into buffer(id)values("+rs.getLong(1)+")";
System.out.println(sql);
st.executeUpdate(sql);
}
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
public void startTime()
{
ActionListener timerListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(timecounter>0 || ss>0)
{
if(ss>0)
{
ss--;
}
else
{
ss=60;
timecounter--;
}
}
else
{
ss=0;
if(!chk)
{
JOptionPane.showMessageDialog(null,"time up");
finalResults();
homePanel();
}
else
{
finalResults();
homePanel();
}
timer.stop();
}
String s=Integer.toString(timecounter);
String p=Integer.toString(ss);
timeLabel.setText(s+":"+p);
}
};
timer = new Timer(1000, timerListener);
timer.setInitialDelay(0);
timer.start();
}
public void setTime()
{
try
{
sql="select time from exam_setup where id="+eidg+"";
System.out.println(sql);
rs=st.executeQuery(sql);
if(rs.next())
timecounter=rs.getInt(1)-1;
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
public void test()
{
}
public void loadThis()
{
try
{
if(totalQuestions==currentQno)
{
//finalResults();
timecounter=0;
ss=0;
chk=true;//time up hisab kitab
return;
}
int totalRec=0,ch=0;
int randomNumber=0;
if(firstIncheck)
{
firstIncheck=false;
System.out.println("this is first call");
}
else
{
rchk=false;
if(ans1Radio.isSelected())
rchk=true;
if(ans2Radio.isSelected())
rchk=true;
if(ans3Radio.isSelected())
rchk=true;
if(ans4Radio.isSelected())
rchk=true;
if(!rchk)
{
JOptionPane.showMessageDialog(null,"Please select an answer...!");
return;
}
}
Fgroup.clearSelection();
sql="Select COUNT(id) from buffer";
rs=st.executeQuery(sql);
if(rs.next())
{
totalRec=rs.getInt(1);
System.out.println("stuff i got-->"+totalRec);
}
if(totalRec<=1)
{
randomNumber=1;
}
else
randomNumber = random.nextInt(totalRec- 1) + 1;
System.out.println(randomNumber);
//loading the current generated stuff//
sql="select * from buffer";
rs=st.executeQuery(sql);
int lop=1;
while(lop<=randomNumber)
{
rs.next();
lop++;
}
// sql="SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY id) AS Rno,* buffer )T WHERE T.Rno = "+randomNumber+"";
// rs=st.executeQuery(sql);
ch=rs.getInt(1);
sql="select * from exam_questions where id="+ch+"";
rs=st.executeQuery(sql);
if(rs.next())
{
currentQno++;
qidg=rs.getLong(1);
correctAns=rs.getLong(8);
questionLabel.setText(currentQno+"."+rs.getString(3));
ans1Radio.setText(rs.getString(4));
ans2Radio.setText(rs.getString(5));
ans3Radio.setText(rs.getString(6));
ans4Radio.setText(rs.getString(7));
ans4=rs.getString(7);
System.out.println("genrated question number"+randomNumber);
System.out.println("correct ans in progress"+correctAns);
}
sql="delete * from buffer where id="+ch+"";
st.executeUpdate(sql);
}
catch(Exception ex)
{
System.err.println("Exception");
System.err.println(ex.getMessage());
}
}
public void progress()//calculating marks and stuff
{
try
{
if(ans1Radio.isSelected() && correctAns==1)
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 1");
}
else if(ans2Radio.isSelected() && correctAns==2)
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 2");
}
else if(ans3Radio.isSelected() && correctAns==3)
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 3");
}
else if(ans4Radio.isSelected() && correctAns==4)
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 4");
}
else if(ans4Radio.isSelected() && correctAns==5 && ans4.equals("ALL OF ABOVE"))
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 5");
}
else if(ans4Radio.isSelected() && correctAns==6 && ans4.equals("NONE OF ABOVE"))
{
score=score+marksForEach;
correctAttempts++;
System.out.println("my ans 6");
}
else
{
wrongAttempts++;
score=score-negtiveMarks;
}
System.out.println("score ="+score);
questionAttempted++;
}
catch(Exception ex)
{
System.err.println("Exception");
System.err.println(ex.getMessage());
}
}
public void finalResults()
{
String msg="Result!\n1.score="+score+"\n2.question attempted="+questionAttempted+"\n3.correct attempted="+correctAttempts+"\n4.wrong attempted="+wrongAttempts+"\n5.total marks="+totalMarks+"";
JOptionPane.showMessageDialog(null,msg);
System.out.print(msg);
try
{
sql="insert into student values("+idg+",'"+examCombo.getSelectedItem().toString().toUpperCase()+"','"+stdNameText.getText().toUpperCase()+"','"+emailText.getText().toUpperCase()+"',"+questionAttempted+","+correctAttempts+","+wrongAttempts+","+totalMarks+","+score+")";
System.out.println(sql);
st.executeUpdate(sql);
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
public void displayInst()
{ String msg="Important Instructions*\n1.You will have "+(timecounter+1)+" minutes\n2.There will be "+totalQuestions+" question\n3.Every question is mandatory\n4.You will get "+marksForEach+" Mark['s] for each correct attempt\n5."+negtiveMarks+" mark['s] will be deducted for each wrong attempt";
JOptionPane.showMessageDialog(null,msg);
}
public void homePanel()
{
int dialogButton = 0;
int dialogResult = JOptionPane.showConfirmDialog (null, "Click \"Yes\" to take another test...!","Delete",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION)
{
minor.setVisible(false);
main.setVisible(true);
fload();
}
else
System.exit(0);
}
public void kill()
{
this.dispose();
}
}