forked from Sparows/AP-Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.java
729 lines (635 loc) · 23.3 KB
/
portal.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package AP;
import java.util.ArrayList;
import java.util.Scanner;
class Vaccine {
private String name;
private int doses;// num od doses
private final int gap;
public Vaccine(String s, int d, int g) {// constructor //ip method
name = s;
doses = d;
gap = g;
}
public int getdoses() {
return doses;
}
public int getgap() {
return gap;
}
public String getname() {
return name;
}
public void verify() {// for outpur message at the end of add vaccine //op method
System.out.println("Vaccine Name: " + name + ", Number of Doses: " + doses + ",Gap Between Doses: " + gap);
}
}
class Hospitals {
private final int uniqID;
private final String name;
private final String pincode;
public int checkDuplicates(String n, String dpc) {
if (name.equals(n) && pincode.equals(dpc)) {
return 1;
} else {
return 0;
}
}
public Hospitals(int uid, String n, String pc) {
uniqID = uid;
name = n;
pincode = pc;
}
public int getid() {
return uniqID;
}
public String getpc() {
return pincode;
}
public void sbAreaInfo() {
System.out.println(uniqID + " " + name);
}
public void verify() {// for outpur message at the end //op method
System.out.println("Hospital Name: " + name + ", PinCode: " + pincode + ", Unique ID: " + uniqID);
}
}
class Citizen {
private String name;
private int age;
private final String id;
private String status;
private int doses;// num of doses taken
private String vac;// which vaccine
private int due;// next vaccine due date
public int checkDuplicates(String s) {
if (id.equals(s)) {
return 1;
} else {
return 0;
}
}
public Citizen(String n, int a, String idn) {
name = n;
age = a;
id = idn;
status = "REGISTERED";
doses = 0;
}
public int checkpid(String s) {
if (s.equals(id)) {
return 1;
}
return 0;
}
public void statusUpdate(String vname, Vaccine v) {
status = "PARTIALLY VACCINATED";
vac = vname;
doses++;
if (doses == v.getdoses()) {
status = "FULLY VACCINATED";
}
// auto update till vaccinated wala mod
}
public int checkStatus() {
if (status.equals("REGISTERED")) {
return 1;
} else if (status.equals("PARTIALLY VACCINATED")) {
return 2;
} else {
return 3;
}
}
public void displayStatus() {
System.out.println(status);
System.out.println("Vaccine Given: " + vac);
System.out.println("Number of Doses given: " + doses);
if (status.equals("PARTIALLY VACCINATED")) {
System.out.println("Next Dose due Date: " + due);
}
}
public void vacmsg() {
System.out.println(name + " vaccinated with " + vac);
}
public void setdue(int duedate) {
due = duedate;
}
public int getdue() {
return due;
}
public String getvac() {
return vac;
}
public void verify() {// for outpur message at the end //op method
System.out.println("Citizen Name: " + name + ", Age: " + age + ", Unique ID: " + id);
}
}
class Slots {
private int hID;
private int dnum;// day number
private int quan;// quantity
private String vacName;// name of vaccine
private String pincode;
private int vindex;
public Slots(int h, int d, int q, String vn, String pc, int vi) {
hID = h;
dnum = d;
quan = q;
vacName = vn;
pincode = pc;
vindex = vi;
}
public void modify() {
quan--;// check if quan not less than 0
}
public int gethid() {
return hID;
}
public String getpc() {
return pincode;
}
public int getday() {
return dnum;
}
public int getvindex() {
return vindex;
}
public int checkquan() {
if (quan == 0) {
return 0;
}
return 1;
}
public void slotInfo() {
System.out.println("Day: " + dnum + " Vaccine: " + vacName + " Available Qty: " + quan);
}
public void verify() {// for outpur message at the end //op method
System.out.println(" Slot added by Hospital " + hID + " for Day: " + dnum + ", Available Quantity: " + quan
+ " of Vaccine " + vacName);
}
// Slot added by Hospital 111111 for Day: 3, Available Quantity: 10 of Vaccine
// Covax
}
public class portal {
// static Vaccine vacs[] =new Vaccine[50];
// kind of databse for all classes
static int count = 1;
static ArrayList<Vaccine> vac = new ArrayList<Vaccine>();
static ArrayList<Hospitals> hos = new ArrayList<Hospitals>();
static ArrayList<Slots> slot = new ArrayList<Slots>();
static ArrayList<Citizen> cit = new ArrayList<Citizen>();
static int uniqIdGenerator() {
count++;
return 101010 + count;
}
static int checkHID(int hid) {// returns index at which match found else 0
for (int i = 0; i < hos.size(); i++) {
if (hid == hos.get(i).getid()) {
// System.out.println(hos.get(i).getid());
return i;
}
}
return -1;
}
static void sbArea(String pid) {// search by area
// searching for slots nearby
Scanner scan = new Scanner(System.in);
System.out.print("Enter Pincode: ");
String str = scan.nextLine();
if (str.length() != 6) {
System.out.println("Pincode length should be 6 digits!");
return;
}
// for(int i=0;i<slot.size();i++){//for pc check
// if(slot.get(i).getpc().equals(str)){
// hos.get(checkHID(slot.get(i).gethid())).sbAreaInfo();
// }
// }
int flag = 0;
// displaying found slots
for (int i = 0; i < hos.size(); i++) {// for pc check
if (hos.get(i).getpc().equals(str)) {
flag++;
hos.get(checkHID(slot.get(i).gethid())).sbAreaInfo();
}
}
if (flag == 0) {
System.out.println("No Hospital Found with Given Pincode !!");
return;
}
System.out.println("Enter Hospital ID: ");
int hosid = scan.nextInt();
if (hosid < 101010) {
System.out.println("Incorrect hospital ID");
return;
}
for (int i = 0; i < slot.size(); i++) {// for finding possible slots with matching hid
if (slot.get(i).gethid() == hosid) {
System.out.print(i + " ");// here i is the index at which we found hid
slot.get(i).slotInfo();
}
}
System.out.println("Choose Slot: ");
int opt = scan.nextInt();// now we know opt is the index
if (opt > slot.size() - 1) {
System.out.println("Incorrect Input");
return;
}
// if correct index
// search citizen by pid and check if earlier vaccinated or not
// add to citizens info if quan not 0 for chosen slot
if (slot.get(opt).checkquan() != 0) {
// citizen check
for (int m = 0; m < cit.size(); m++) {
if (cit.get(m).checkpid(pid) == 1) {
// update this citizens profile
if (cit.get(m).checkStatus() == 1) {
cit.get(m).statusUpdate(vac.get(slot.get(opt).getvindex()).getname(),
vac.get(slot.get(opt).getvindex()));
slot.get(opt).modify();
cit.get(m).setdue(slot.get(opt).getday() + vac.get(slot.get(opt).getvindex()).getgap());
cit.get(m).vacmsg();
} else if (cit.get(m).checkStatus() == 3) {
System.out.println(" Already Completely Vaccinated ,No further doses needed !");
} else {
if (cit.get(m).getdue() > slot.get(opt).getday()) {
System.out.println("Cannot be vaccinated before due date!");
} else {
if (cit.get(m).getvac().equals(vac.get(slot.get(opt).getvindex()).getname())) {/// no mix
cit.get(m).statusUpdate(vac.get(slot.get(opt).getvindex()).getname(),
vac.get(slot.get(opt).getvindex()));
slot.get(opt).modify();
cit.get(m).setdue(slot.get(opt).getday() + vac.get(slot.get(opt).getvindex()).getgap());
cit.get(m).vacmsg();
} else {
System.out.println("Cannot Choose different vaccine once Partially Vaccinated!");
return;
}
}
}
}
}
} else {
System.out.println("No Vaccine doses available for chosen vaccine name and slot!");
}
// remove vaccine from slot
}
static void sbVaccine(String pid) {// search by vaccine
Scanner scan = new Scanner(System.in);
System.out.print("Enter Vaccine name: ");
String str = scan.nextLine();
int flag = 0;
for (int i = 0; i < slot.size(); i++) {// for pc check
if (vac.get(slot.get(i).getvindex()).getname().equals(str)) {
flag++;
if (slot.get(i).checkquan() != 0) {
hos.get(checkHID(slot.get(i).gethid())).sbAreaInfo();
}
}
}
if (flag == 0) {
System.out.println("No Hospital Found with Given Vaccine name!!");
return;
}
// for(int i=0;i<hos.size();i++){//for pc check
// if(hos.get(i).getpc().equals(str)){
// hos.get(checkHID(slot.get(i).gethid())).sbAreaInfo();
// }
// }
System.out.println("Enter Hospital ID: ");
int hosid = scan.nextInt();
if (hosid < 101010) {
System.out.println("Incorrect hospital ID");
return;
}
for (int i = 0; i < slot.size(); i++) {// for finding possible slots with matching hid
if (slot.get(i).gethid() == hosid && vac.get(slot.get(i).getvindex()).getname().equals(str)) {
System.out.print(i + " ");// here i is the index at which we found hid
slot.get(i).slotInfo();
}
}
System.out.println("Choose Slot: ");
int opt = scan.nextInt();// now we know opt is the index
if (opt > slot.size() - 1) {
System.out.println("Incorrect Input");
return;
}
// if correct index
// search citizen by pid
// add to citizens info if quan not 0 for chosen slot
if (slot.get(opt).checkquan() != 0) {
for (int m = 0; m < cit.size(); m++) {
if (cit.get(m).checkpid(pid) == 1) {
// update this citizens profile
if (cit.get(m).checkStatus() == 1) {
cit.get(m).statusUpdate(vac.get(slot.get(opt).getvindex()).getname(),
vac.get(slot.get(opt).getvindex()));
slot.get(opt).modify();
cit.get(m).setdue(slot.get(opt).getday() + vac.get(slot.get(opt).getvindex()).getgap());
cit.get(m).vacmsg();
} else if (cit.get(m).checkStatus() == 3) {
System.out.println(" Already Completely Vaccinated ,No further doses needed !");
} else {
if (cit.get(m).getdue() > slot.get(opt).getday()) {
System.out.println("Cannot be vaccinated before due date!");
} else {
if (cit.get(m).getvac().equals(vac.get(slot.get(opt).getvindex()).getname())) {/// no mix
cit.get(m).statusUpdate(vac.get(slot.get(opt).getvindex()).getname(),
vac.get(slot.get(opt).getvindex()));
slot.get(opt).modify();
cit.get(m).setdue(slot.get(opt).getday() + vac.get(slot.get(opt).getvindex()).getgap());
cit.get(m).vacmsg();
} else {
System.out.println("Cannot Choose different vaccine once Partially Vaccinated!");
return;
}
}
}
// cit.get(m).statusUpdate(vac.get(slot.get(opt).getvindex()).getname(),
// vac.get(slot.get(opt).getvindex()));
// slot.get(opt).modify();
// cit.get(m).setdue(slot.get(opt).getday() +
// vac.get(slot.get(opt).getvindex()).getgap());
// cit.get(m).vacmsg();
}
}
} else {
System.out.println("No Vaccine doses available for chosen vaccine name and slot!");
}
// remove vaccine from slot
}
// functions for main class
static void addVaccine() {
Scanner scan = new Scanner(System.in);
System.out.print("Vaccine Name: ");
String str = scan.nextLine();
System.out.print("Number of Doses: ");
int dose = scan.nextInt();
int gap;// dose 0 and busshit wala error for later
if (dose == 1) {
gap = 0;
} else {
System.out.print("Gap between Doses: ");
gap = scan.nextInt();
}
for (int i = 0; i < vac.size(); i++) {
if (vac.get(i).getname().equals(str)) {
System.out.println("Vaccine Already added!");
return;
}
}
vac.add(new Vaccine(str, dose, gap));
vac.get(vac.size() - 1).verify();
// System.out.println(str);
/*
* so basically main function calls add vax and it takesinput and stores in in
* array list of vaccine details
*/
}
static void registerHospital() {
Scanner scan = new Scanner(System.in);
System.out.print("Hospital Name: ");
String str = scan.nextLine();
System.out.print("Pincode: ");
String pincode = scan.nextLine();
if (pincode.length() != 6) {
System.out.println("Pincode length should be 6 digits!");
return;
}
for (int i = 0; i < hos.size(); i++) {
if (hos.get(i).checkDuplicates(str, pincode) == 1) {
System.out.println("Hospital Already Registered!");
return;
}
}
int uid = uniqIdGenerator();
hos.add(new Hospitals(uid, str, pincode));
hos.get(hos.size() - 1).verify();
// just a simple add to database and verify program.
}
static void registerCitizen() {
Scanner scan = new Scanner(System.in);
System.out.print("Citizen Name: ");
String str = scan.nextLine();
System.out.print("Age: ");
int a = scan.nextInt();
System.out.print("Unique ID: ");
String uid = scan.next();
if (uid.length() != 12) {
System.out.println("Unique user ID should be of 12 Digits!");
return;
}
for (int i = 0; i < cit.size(); i++) {
if (cit.get(i).checkDuplicates(uid) == 1) {
System.out.println("Citizen Already Registered with unique ID!");
return;
}
}
cit.add(new Citizen(str, a, uid));
cit.get(cit.size() - 1).verify();
if (a < 19) {
cit.remove(cit.size() - 1);
System.out.println("Only above 18 are allowed");
}
}
// cannot repeat vaccine details once set wala error
static void createSlots() {
// regiser number of slots here as we dont need them in slot class
Scanner scan = new Scanner(System.in);
System.out.print("Enter Hospital ID: ");
int id = scan.nextInt();
if (id < 101010) {
System.out.println("Incorrect hospital ID");
return;
}
int flag = 0;
for (int i = 0; i < hos.size(); i++) {
if (hos.get(i).getid() == id) {
flag++;
}
}
if (flag == 0) {
System.out.println("Hospital NOT Registered on Portal!!");
return;
}
// System.out.println(checkHID(id));
// System.out.println(id);
String pc = hos.get(checkHID(id)).getpc();// added pc
// check hospital id
// func to check id correcteness using brute force
if (checkHID(id) != -1) {// return index at which found matching hid
System.out.print("Enter number of Slots to be added: ");
int snum = scan.nextInt();
for (int i = 0; i < snum; i++) {
System.out.print("Enter Day Number: ");
int dnum = scan.nextInt();
System.out.print("Enter Quantity: ");
int q = scan.nextInt();
System.out.println("Select Vaccine ");
for (int j = 0; j <= vac.size() - 1; j++) {
System.out.println(j + ". " + vac.get(j).getname());
}
int vacChoice = scan.nextInt();
if (vacChoice > vac.size() - 1) {
System.out.println("Incorrect Choice ! Fill Again");
return;
}
// loop back if choice of vaccine is not correct
// assuming correct choice lets fill slots
slot.add(new Slots(id, dnum, q, vac.get(vacChoice).getname(), pc, vacChoice));// added pc and vachoice
// as parameters later
slot.get(slot.size() - 1).verify();
}
} else {
System.out.println("Incorrect Hospital ID");
}
}
static void bookSlots() {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Patient Unique ID: ");
String pid = scan.nextLine();// patients id
if (pid.length() != 12) {
System.out.println("Unique user ID should be of 12 Digits!");
return;
}
int flag = 0;
for (int i = 0; i < cit.size(); i++) {
if (cit.get(i).checkpid(pid) == 1) {
flag++;
}
}
if (flag == 0) {
System.out.println("Citizen NOT Registered on Portal!!");
return;
}
// check if correct patient id
System.out.println("1. Search by Area\n2. Search by Vaccine\n3. Exit");
System.out.print("Enter option: ");
int opt = scan.nextInt();
if (opt == 1) {
sbArea(pid);
} else if (opt == 2) {
sbVaccine(pid);
} else if (opt == 3) {
return;
} else {
System.out.println("Wrong input!");
return;
}
// else wala drama if ip is dif
}
static void hslot() {// search by hospital id
System.out.print("Enter Hospitals ID: ");
Scanner scan = new Scanner(System.in);
int hid = scan.nextInt();
if (hid < 101010) {
System.out.println("Incorrect hospital ID");
return;
}
int flag = 0;
if (checkHID(hid) != -1) {// checks if correct id in records
for (int i = 0; i < slot.size(); i++) {
if (slot.get(i).gethid() == hid) {// checks if id matches in slots
slot.get(i).slotInfo();
flag++;
}
}
if (flag == 0) {
System.out.println("No slot found for given ID!");
}
} else {
System.out.println("Hospital ID NOT registered !!");
}
}
static void Vstatus() {
System.out.print("Enter Patient ID: ");
Scanner scan = new Scanner(System.in);
String pid = scan.nextLine();
if (pid.length() != 12) {
System.out.println("Unique user ID should be of 12 Digits!");
return;
}
int flag = 0;
for (int i = 0; i < cit.size(); i++) {
if (cit.get(i).checkpid(pid) == 1) {
cit.get(i).displayStatus();
flag++;
}
}
if (flag == 0) {
System.out.println("Incorrect Patient ID / Patient not Registered on portal!");
}
}
static void menu() {
System.out.println(
"1. Add Vaccine\n2. Register Hospital\n3. Register Citizen\n4. Add Slot for Vaccination\n5. Book Slot for Vaccination\n6. List all slots for a hospital\n7. Check Vaccination Status\n8. Exit\n");
}
static void separator() {
System.out.println("----------------------------------------");
}
// main class
public static void main(String[] args) {
System.out.println("CoWin Portal initialized");
separator();
menu();
separator();
Scanner scan = new Scanner(System.in);
int flag = 1;
while (flag == 1) {
int input = scan.nextInt();
if (input == 1) {
addVaccine();
} else if (input == 2) {
registerHospital();
} else if (input == 3) {
registerCitizen();
} else if (input == 4) {
if (vac.size() > 0) {// if hospitals empty then gives error
createSlots();
} else {
System.out.println("No Vaccine Data Added !! Add Vaccine data first");
separator();
// menu();
// separator();
// continue;
}
} else if (input == 5) {
if (slot.size() > 0) {
bookSlots();
} else {
System.out.println("No Slot Data Added !! Add Slots data first");
separator();
// menu();
// separator();
// continue;
}
} else if (input == 6) {
hslot();
} else if (input == 7) {
Vstatus();
} else if (input == 8) {
flag = 0;
continue;
} else {
System.out.println("Wrong Input!");
separator();
}
separator();
menu();
separator();
}
// addVaccine();//workes fine
// addVaccine();
// addVaccine();
// registerHospital();
// createSlots();
// registerCitizen();
// bookSlots();
// hslot();
// Vstatus();
// hospital cannot be registered before adding vax
// if same hospital is added again with same pincode then avoid providing new id
// and show text error
// vaccine mixing not allowed
// uniq ids have to be of specified digits
// slots only for registered hospitals and citizens
}
}