-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdmin.java
More file actions
457 lines (411 loc) · 15.6 KB
/
Copy pathAdmin.java
File metadata and controls
457 lines (411 loc) · 15.6 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
// include import files
import java.sql.*;
import java.lang.*;
import java.util.*;
import java.io.*;
public class Admin
{
public static void main(String args[]) throws Exception
{
Connection con = null;
Statement stmt = null;
Scanner in = new Scanner(System.in);
int ch;
Boolean AdminLoginResult = false;
try
{
//Class.forName("com.mysql.jdbc.Driver");
//this is the part where SQL is attached
String url = "jdbc:mysql://127.0.0.1/online_quiz?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String user = "root";
String pass = "";
//Trying to make connection with database
con = DriverManager.getConnection(url,user,pass);
stmt = con.createStatement();
System.out.println();
System.out.println();
System.out.println("Welcome To Online Quiz's Admin Portal");
System.out.println("-------------------------------------");
System.out.println();
Admin ad = new Admin();
AdminLoginResult = ad.login(stmt);
for(;;)
{
if( AdminLoginResult == true )
{
break;
}
else
{
AdminLoginResult = ad.login(stmt);
}
}
if(AdminLoginResult)
{
for(;;)
{
System.out.println();
System.out.println();
System.out.println("Enter Your Choice : ");
System.out.println("--------------------");
System.out.println("1 : Insert Question into Database \t 2 : View all the Questions in the Database");
System.out.println("3 : Create a new Admin User Account \t 4 : Alter Question");
System.out.println("5 : LogOut \t \t \t \t 6 : Exit");
System.out.println();
System.out.print("Choice : ");ch = in.nextInt();
System.out.println();
System.out.println("-------------------------------------------------------------------------------");
switch (ch)
{
case 1 :
ad.Insert(stmt);
break;
case 2 :
ad.viewQuestions(stmt);
break;
case 3 :
ad.AdminCreate(stmt);
break;
case 4 :
ad.ALterQuestions(stmt);
break;
case 5 :
System.out.println();
AdminLoginResult = false;
System.out.println(" Successfully Logged Out ");
System.out.println("------------------------------------------------------------------------------");
for(;;)
{
if( AdminLoginResult == true )
{
break;
}
else
{
AdminLoginResult = ad.login(stmt);
}
}
break;
case 6 :
System.out.println();
System.out.println("Good Bye!!");
System.exit(0);
break;
default:
System.out.println();
System.out.println("Wrong Input!");
System.out.println("Please Try Again");
System.out.println();
break;
}
}
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public Boolean login(Statement stmt) throws IOException, InterruptedException
{
Boolean val = false;
ResultSet rst = null;
String username = null;
Scanner in = new Scanner(System.in);
try
{
rst = stmt.executeQuery("select * from Admins");
//Asking The user to enter the Username for login
System.out.println();
System.out.println("Enter the Admin Username");
username=in.nextLine();
//Checking whether the Entered Username Exists in Admins Database
while(rst.next())
{
if(username.equals(rst.getString(1)))
{
//If Database has the username entered by the user then the Boolean "val" will be set to "True"
username = rst.getString(1);
val = true;
System.out.println();
System.out.println("Enter the Password for username : "+username);
String password=in.nextLine();
if(password.equals(rst.getString(2)))
{
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
System.out.println();
System.out.print("-------------------------------");
for(int i = 0; i < username.length(); i++)
{
System.out.print("-");
}
System.out.println();
System.out.println("Succefully Logged into Admin : "+username);
System.out.print("-------------------------------");
for(int i = 0; i < username.length(); i++)
{
System.out.print("-");
}
System.out.println();
return true;
}
// display "Retry" in case of wrong password.
else
{
System.out.println("Wrong Password");
System.out.println("Retry");
return false;
}
}
}
if(val == false)
{
return false;
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return false;
}
public void Insert(Statement stmt)
{
// takes question from the user
Admin add = new Admin();
Scanner sc = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
ResultSet rstt = null;
int n,i=0,choice,j;
int count=0;
System.out.println();
System.out.println("Enter The Question");
sb.append(sc.nextLine());
try
{
rstt = stmt.executeQuery("SELECT COUNT(Q_ID) FROM questions");
while(rstt.next())
{
count = Integer.parseInt(rstt.getString(1));
}
count = count + 1;
System.out.println();
System.out.println("Enter The number of Options for the Question");
n = sc.nextInt();
System.out.println();
System.out.println("Enter the Options for the Question");
for(i = 0 ; i < n ; i++ )
{
j = i + 1;
System.out.println();
System.out.print("Option "+j+" : ");
add.InsertQuestionIntoDataBase(stmt,count,sc);
}
System.out.println();
System.out.println("--------------------------------");
System.out.println();
rstt = stmt.executeQuery("SELECT * FROM ANSWERS WHERE Q_ID="+count+"");
i=1;
while(rstt.next())
{
System.out.println(rstt.getString(1)+" : "+rstt.getString(2));
}
System.out.println();
System.out.println("Enter the correct Answer for the Question : ' "+sb+" ' from the above mentioned choices");
choice = sc.nextInt();
stmt.executeUpdate("INSERT INTO QUESTIONS (QUESTION,ANS_ID)"+" VALUES ('"+sb+"',"+choice+")");
System.out.println();
System.out.println("Question inserted successfully!!");
System.out.println("--------------------------------");
System.out.println();
//}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
sb.delete(0,10000);
}
public void InsertQuestionIntoDataBase(Statement stmt, int count, Scanner sc)
{
DataInputStream br = new DataInputStream(System.in);
try
{
String option;
try
{
option = br.readLine();
stmt.executeUpdate("INSERT INTO ANSWERS (answers,q_id)"+" VALUES ('"+option+"',"+count+")");
}
catch (IOException e)
{
System.out.println(e);
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void viewQuestions(Statement stmt)
{
ResultSet rst = null;
try
{
rst = stmt.executeQuery("SELECT * FROM QUESTIONS");
System.out.println();
while (rst.next())
{
System.out.println(rst.getString(1)+" : "+rst.getString(2));
}
System.out.println();
System.out.println("--------------------------------------------------------------------------------------");
System.out.println();
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void AdminCreate(Statement stmt)
{
ResultSet rs = null;
String username,password;
Scanner sc = new Scanner(System.in);
Boolean val = true;
System.out.println();
System.out.println("NOTE : BEFORE YOU CAN CREATE A NEW ADMINISTRATOR HE/SHE MUST BE REGITERED WITH ONLINE QUIZ PORTAL");
try
{
System.out.println();
System.out.print("Enter the USERNAME_ID : ");
username = sc.nextLine();
rs = stmt.executeQuery("SELECT USER_ID FROM USERS");
while(rs.next())
{
if(username.equals(rs.getString(1)))
{
System.out.println();
System.out.println("Enter the PASSWORD : ");
password = sc.nextLine();
System.out.println();
stmt.executeUpdate("INSERT INTO ADMINS VALUES ('"+username+"','"+password+"')");
System.out.println(username+" is succefully added");
System.out.println("----------------------------------------------------------");
System.out.println();
val = false;
}
}
if(val)
{
System.out.println("Entered User Name Does not exist in our database");
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println();
}
}
public void ALterQuestions(Statement stmt)
{
ResultSet rst = null;
String alterQues;
Scanner sc = new Scanner(System.in);
int alterQID = -1 , typeCaste;
String ans;
Admin an = new Admin();
Boolean val = false;
DataInputStream br = new DataInputStream(System.in);
an.viewQuestions(stmt);
try
{
for(;;)
{
System.out.println();
System.out.print("Enter the Questions Number Which You Would Like To ALter : ");
alterQID = sc.nextInt();
System.out.println();
rst = stmt.executeQuery("SELECT * FROM QUESTIONS");
while (rst.next())
{
typeCaste = Integer.parseInt(rst.getString(1));
if(typeCaste == alterQID)
{
System.out.println();
System.out.println("Is the Question you would like to alter : ");
System.out.println(" "+rst.getString(1)+" : "+rst.getString(2));
System.out.println();
System.out.print("Type Yes(y) or No(n) : ");
ans = sc.next();
if(ans.equals("y"))
{
val = true;
break;
}
else
{
break;
}
}
}
if(val)
{
break;
}
else
{
continue;
}
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
System.out.println();
try
{
if(val)
{
System.out.println("Enter The Altered Question");
System.out.println();
alterQues = br.readLine();
System.out.println();
stmt.executeUpdate("UPDATE QUESTIONS SET QUESTION = '"+alterQues+"' WHERE Q_ID = "+alterQID+" ");
System.out.println();
System.out.println("Question Successfully Updated");
System.out.println("--------------------------------------------------------------------------");
}
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
catch (IOException e)
{
System.out.println(e);
}
}
}