-
Notifications
You must be signed in to change notification settings - Fork 0
/
Administration_Helper.java
604 lines (479 loc) · 18.8 KB
/
Administration_Helper.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
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
/*
_______________________
| |
| Administration_Helper |
|_______________________|
@JEEL PATEL
*/
// Takes data from Studentdetail.txt & Collegedetail.txt file or same type of formatted datafile.
import java.util.*;
import java.io.*;
public class Administration_Helper {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Account ac = new Account();
ac.login();
}
}
// Student class inherits College class
// Student class has methods like getNstore, get, setPreference
// InsertIntoRanklist, listofallocation, allotment, getMyClg.
class Student extends College {
String name;
int id;
double marks;
String preference;
String myclg;
Student() {
super();
}
Student(int id, String name, double marks) {
super();
this.id = id;
this.name = name;
this.marks = marks;
this.preference = getdefault(); // default preference.
}
String getdefault() {
String temp = "";
for (int i = 1; i <= noc; i++) {
if (i > 1)
temp += ",";
temp += i;
}
return temp;
}
// to change preference from default to new preference.
public void setPreference(int myid) {
try {
String newPreference;
while (true) {
String t1 = s1[0].name, t2 = c1[0].ClgName;
System.out.println(
"Set your preference in this manner : Ex: 2,4,3,1,.. and the numbers between 1 to no. of college and no more than once.");
newPreference = sc.next();
newPreference += ',';
int map[] = new int[noc + 1];
int i;
for (i = 1; i <= noc; i++)
map[i] = 0;
String temp = "";
for (i = 0; i < newPreference.length(); i++) {
if (newPreference.charAt(i) == ',') {
int t = Integer.parseInt(temp);
if (t > noc) {
System.out.println("Invalid input type");
break;
}
map[t]++;
temp = "";
} else {
temp += newPreference.charAt(i);
}
}
if (i == newPreference.length())
break;
}
// this.preference = newPreference;
for (int i = 0; i < noc; i++) {
if (s[i].id == myid) {
s[i].preference = newPreference;
}
}
System.out.println("\n~~~Your Preference successfully Changed~~~\n");
} catch (Exception e) {
System.out.println("\n***This feature is not available yet.***\n");
}
}
// initilize the data value like Student name,Student id,total marks & store
// into obj array.
public void getNstore() {
File file = new File("C:\\Users\\JEEL\\OneDrive\\Desktop\\testing\\Studentdetail.txt");
try {
sc = new Scanner(file);
int it = 0;
String temp_name;
int temp_marks;
while ((sc.hasNextLine())) {
String[] ln = sc.nextLine().split(",", 2);
if (ln[0].length() == 0)
break;
temp_name = ln[0];
temp_marks = Integer.parseInt(ln[1]);
s1[it] = new Student(it + 1, temp_name, temp_marks);
it++;
}
N = it;
username[N] = "admin";
password[N] = "admin";
System.out.println("\n~~~Data successfully fatched.~~~\n");
} catch (Exception e) {
System.out.println("File is not available.");
}
for (int i = 0; i < N; i++) {
InsertIntoRankList(s1[i]);
}
int i = 0;
while (!Ranklist.isEmpty()) {
s[i] = new Student(i + 1, "a", 1);
s[i] = Ranklist.peek();
Ranklist.poll();
i++;
}
}
// add obj of Student in Priority Queue.
public void InsertIntoRankList(Student s) {
Ranklist.add(s);
}
// get detail of student Rankwise.
public void get() {
try {
System.out.println("----------------------------------------------------------------------------");
System.out.println("Students served in their merit order\n");
String space = " ";
System.out.println("Rank " + "ID " + "Name " + "Marks ");
for (int i = 0; i < N; i++) {
System.out.println((i + 1) + space.repeat(6 - (int) Math.log10(i + 1)) + s[i].id
+ space.repeat(3 - (int) Math.log10(s[i].id)) + s[i].name
+ space.repeat(18 - s[i].name.length()) + s[i].marks);
}
System.out.println("----------------------------------------------------------------------------");
} catch (NullPointerException exception) {
System.out.println("\n***Information is not available yet.***\n");
}
}
// get list of allocated college on merit-base.
// get detail of student Rankwise.
public void listofallocation() {
try {
String temp = s[0].myclg;
if (temp == null)
throw new NullPointerException("");
System.out.println("----------------------------------------------------------------------------");
System.out.println("College allocated to Students upon merit-base.\n");
String space = " ";
System.out.println("Rank " + "ID " + "Name " + "Marks " + " College name ");
for (int i = 0; i < N; i++) {
System.out.println((i + 1) + space.repeat(6 - (int) Math.log10(i + 1)) + s[i].id
+ space.repeat(3 - (int) Math.log10(s[i].id)) + s[i].name
+ space.repeat(18 - s[i].name.length()) + s[i].marks
+ space.repeat(6 - (int) Math.log10(s[i].marks)) + s[i].myclg);
}
System.out.println("----------------------------------------------------------------------------");
} catch (NullPointerException exception) {
System.out.println("\n***Information is not available yet.***\n");
}
}
// College Allotment
// fills data from file with well formated
void allotment() {
try {
int i = 0, j = 0;
for (i = 0; i < N; i++) {
String temp = "";
for (j = 0; j < s[i].preference.length(); j++) {
if (s[i].preference.charAt(j) == ',') {
int t = Integer.parseInt(temp);
t--;
if (c1[t].AvailableSeats > 0) {
c1[t].AvailableSeats--;
s[i].myclg = c1[t].ClgName;
s1[s[i].id - 1].myclg = c1[t].ClgName;
break;
}
temp = "";
} else {
temp += s[i].preference.charAt(j);
}
}
if (j >= s[i].preference.length()) {
s[i].myclg = "N/A";
s1[s[i].id - 1].myclg = "N/A";
}
}
System.out.println("\n~~~Task Completed~~~\n");
fillClgDetails();
} catch (Exception e) {
System.out.println("\n***You have to fill student before.***\n");
}
}
public void getMyClg(int myid) {
try {
String temp = s[myid - 1].myclg;
if (temp == null)
throw new NullPointerException("");
System.out.print("\nYour College Name : " + temp + "\n");
} catch (Exception e) {
System.out.println("\n***Information is not available yet.***\n");
}
}
}
// comparator for priority queue in descanding order of marks.
class DescOrder implements Comparator<Student> {
public int compare(Student s1, Student s2) {
if (s1.marks < s2.marks)
return 1;
else if (s1.marks > s2.marks)
return -1;
return 0;
}
}
// Account class which inherits Student class.
class Account extends Student {
Account() {
super();
for (int i = 0; i < N; i++) {
username[i] = "student" + (i + 1);
password[i] = "student" + (i + 1);
}
username[N] = "admin";
password[N] = "admin";
}
// login functionality.
public void login() {
while (true) {
int type = putPassword();
if (type == 0) {
System.out.println("\n~~~Successfully login with admin mode !~~~\n");
boolean loop = true;
while (loop)
loop = adminMenu();
} else if (type > 0) {
System.out.println("\n~~~Successfully login with student mode !~~~\n");
try {
System.out.println("Welcome " + s1[type - 1].name + ",");
} catch (Exception e) {
System.out.println("\n***Data is not available.***\n");
}
boolean loop = true;
while (loop)
loop = studentMenu(type);
} else if (type == -1) {
System.out.println("\n***Wrong password !***\n");
} else
System.out.println("\n***Username not found !***\n");
int WantToExit = 2;
while (true) {
WantToExit = Exit();
if (WantToExit == 0 || WantToExit == 1)
break;
}
if (WantToExit == 1) {
System.out.println("\n$$$ Thanks for visit. $$$");
System.out.println("____________________________________________________________________________\n");
break;
}
}
}
// It has 2 face.
// When logged from student's id.
static boolean studentMenu(int myid) {
Student temps = new Student();
System.out.println("Enter choice :");
System.out.println("1. get all students detail 2. for get college details 3. for set preference");
System.out.println("4. My allocated college 5. get list of allocation 6. Logout");
int x = getInt(1, 6);
switch (x) {
case 1:
temps.get();
break;
case 2:
temps.getClgDetail();
break;
case 3:
temps.setPreference(myid);
break;
case 4:
temps.getMyClg(myid);
break;
case 5:
temps.listofallocation();
break;
case 6:
return false;
}
return true;
}
// When logged from admin's id.
static boolean adminMenu() {
Student temps = new Student();
System.out.println("Enter choice :");
System.out.println("1. for fill student detail 2. for get student detail 3. for fill college details ");
System.out.println("4. for get college details 5. for change No. of seats 6. Allocate seats ");
System.out.println("7. get List of allocation 8. Logout ");
int x = getInt(1, 8);
switch (x) {
case 1:
temps.getNstore();
break;
case 2:
temps.get();
break;
case 3:
temps.fillClgDetails();
break;
case 4:
temps.getClgDetail();
break;
case 5:
temps.changeTotalSeats();
break;
case 6:
temps.allotment();
break;
case 7:
temps.listofallocation();
break;
case 8:
return false;
}
return true;
}
// Ask for username and password
public int putPassword() {
System.out.println("Enter username : ");
String tempuser = sc.next();
System.out.println("Enter password : ");
String temppass = sc.next();
return varify(tempuser, temppass);
}
// Ask for fully Exit from program.
public int Exit() {
try {
System.out.println("\nEnter choice: ");
System.out.println("Press 0. for Login ");
System.out.println("Press 1. for close the program \n ");
int WantToExit = sc.nextInt();
if (WantToExit < 0 && WantToExit > 1)
System.out.println("Please Enter valid input 0 or 1");
return WantToExit;
} catch (InputMismatchException exception) {
sc.next();
System.out.println("Please Enter valid input 0 or 1");
return 2;
}
}
// Varify valid username and password
int varify(String u, String p) {
for (int i = 0; i < N + 1; i++) {
if (u.compareTo(username[i]) == 0) {
if (p.compareTo(password[i]) == 0 && i == N)
return 0; // 1 for successfully login with admin mode!!
else if (p.compareTo(password[i]) == 0) {
int myid = Integer.parseInt(u.substring(7));
System.out.println(myid);
return myid; // 2 for successfully login with student mode!!
} else
return -1; // -1 for wrong password
}
}
return -2; // -2 for not found
}
}
// College class
class College {
public static int N = 100000, noc = 1000, f = 0; // N = no. of students noc=no. of colleges.
Scanner sc = new Scanner(System.in);
public static String[] username = new String[N + 2];
public static String[] password = new String[N + 2];
public static PriorityQueue<Student> Ranklist = new PriorityQueue<Student>(new DescOrder());
// object with inorder to id of students data
public static Student s1[] = new Student[N];
// object with order of merit.
public static Student s[] = new Student[N];
int ClgIndex;
String ClgName;
int AvailableSeats;
public static College c1[] = new College[noc];
College(int ClgIndex, String ClgName, int AvailableSeats) {
this.AvailableSeats = AvailableSeats;
this.ClgName = ClgName;
this.ClgIndex = ClgIndex;
}
College() {
;
}
// initilize c1[] obj of college class
// fills data from file with well formated
void fillClgDetails() {
int i = 0;
File file = new File("C:\\Users\\JEEL\\OneDrive\\Desktop\\testing\\Collegedetail.txt");
try {
sc = new Scanner(file);
String temp_name;
int temp_seats;
while ((sc.hasNextLine())) {
String[] ln = sc.nextLine().split(",", 2);
if (ln[0].length() == 0)
break;
temp_name = ln[0];
temp_seats = Integer.parseInt(ln[1]);
c1[i] = new College(i + 1, temp_name, temp_seats);
i++;
}
noc = i;
if (f == 0)
System.out.println("\n~~~Successfully Filled list of colleges.~~~\n");
f = 1;
} catch (Exception e) {
System.out.println("File is not available.");
}
}
// List of colleges with Available seats of Index no.
void getClgDetail() {
try {
int i = 0;
String space = " ", temp = c1[0].ClgName;
System.out.println("----------------------------------------------------------------------------");
System.out.println("Index No. " + "College Name " + "Available Seats");
for (i = 0; i < noc; i++) {
System.out.println(c1[i].ClgIndex + space.repeat(9 - (int) Math.log10(c1[i].ClgIndex)) + c1[i].ClgName
+ space.repeat(15 - c1[i].ClgName.length()) + c1[i].AvailableSeats);
}
System.out.println("----------------------------------------------------------------------------");
} catch (NullPointerException exception) {
System.out.println("\n***Information is not available yet***\n");
}
}
// Admin can change No. of Seats of Colleges.
void changeTotalSeats() {
try {
getClgDetail();
System.out.println("\nType College index which your want to edit :");
int x = getInt(1, 10);
System.out.println("\nEnter new available seats :");
int xx = getInt(0, 500);
c1[x - 1].AvailableSeats = xx;
System.out.println("\nSuccessfully changed detail.\n");
} catch (Exception e) {
System.out.println("\n***You have to fill college detail before.***\n");
}
}
// for scan valid integer in range
static int getInt(int l, int r) {
Scanner sc = new Scanner(System.in);
int x;
while (true) {
try {
x = sc.nextInt();
if (x >= l && x <= r)
break;
else
System.out.println("Please Enter valid input from " + l + " to " + r);
} catch (InputMismatchException exception) {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
sc.next();
System.out.println("Please Enter valid input from " + l + " to " + r);
}
}
return x;
}
}
/*
* To compile and run program -> javac Administration_Helper.java -> java
* Administration_Helper
*
* Login -> for admin mode :- username & password = admin -> for student mode :-
* username & password = student? (where ? is Id of student).
*
*/