/
Nurse.java
650 lines (511 loc) · 21 KB
/
Nurse.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
package clinicsystem;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Nurse {
static ArrayList<Patient> patientList = new ArrayList<>();
static ArrayList<Patient> patientQueue = new ArrayList<>();
static ArrayList<MedicineQueue> medicineQueue = new ArrayList<>();
static ArrayList<Medicine> medicineList = new ArrayList<>();
static ArrayList<Illness> illnessList = new ArrayList<>();
static boolean connectedToChat = true;
static int writeStatusForChat;
public static void main(String[] args) throws InterruptedException {
Socket socket = null;
OutputStream out = null;
DataOutputStream dos = null;
InputStream in = null;
DataInputStream dis = null;
String host = "localhost";
int port = 1111;
int choice = 0;
System.out.println("Connecting to server..");
try {
socket = new Socket(host, port);
System.out.println("Connected to server.");
out = socket.getOutputStream();
dos = new DataOutputStream(out);
in = socket.getInputStream();
dis = new DataInputStream(in);
dos.writeInt(1); //to let the server know that a nurse connected
System.out.println("Loading patient data..");
patientList = getPatientList(socket, patientList);
System.out.println("Loading medicine..");
getMedicineList(socket);
System.out.println("Loading illness..");
loadIllnessList(socket);
System.out.println("Loaded!");
} catch (IOException e) {
System.out.println("Cannot connect to server.");
}
while (choice != 8) {
choice = mainMenu();
switch (choice) {
case 1: {
registerPatients(socket);
break;
}
case 2: {
viewPatients(patientList);
break;
}
case 3: {
viewQueue(socket);
break;
}
case 4: {
startChat(socket);
break;
}
case 5: {
viewMedicineQueue(socket);
break;
}
case 6: {
refreshMedicineQueue(socket);
break;
}
case 7: {
searchIllness();
break;
}
case 8: {
System.out.println("Goodbye!");
System.exit(-1);
break;
}
}
}
}
public static int mainMenu() {
int choice = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("\nWelcome to system!");
System.out.println("1. Register patients.");
System.out.println("2. View patients list.");
System.out.println("3. View patient queue.");
System.out.println("4. Chat with doctor.");
System.out.println("5. View medicine queue.");
System.out.println("6. Refresh medicine queue.");
System.out.println("7. Search illness.");
System.out.println("8. Exit.");
System.out.println("\nYou have " + patientQueue.size() + " patients in queue.");
System.out.println("You have " + medicineQueue.size() + " medicine to prescribe in queue.");
try {
choice = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please enter only an integer. Please try again.");
}
return choice;
}
public static ArrayList<Patient> getPatientList(Socket socket, ArrayList<Patient> patientList) {
int id;
String ic;
String fname;
String lname;
int age;
int cnumber;
try {
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
InputStream input = socket.getInputStream();
DataInputStream dis = new DataInputStream(input);
dos.writeInt(2);
int numberOfPatients = dis.readInt();
for (int i = 0; i < numberOfPatients; i++) {
id = dis.readInt();
ic = dis.readUTF();
fname = dis.readUTF();
lname = dis.readUTF();
age = dis.readInt();
cnumber = dis.readInt();
patientList.add(new Patient(id, ic, fname, lname, age, cnumber));
}
} catch (IOException e) {
System.out.println("Cannot get IO streams.");
}
return patientList;
}
public static void registerPatients(Socket socket) {
Scanner scanner = new Scanner(System.in);
long ICint = 0;
String IC = null;
int count = 0;
try {
System.out.println("Enter IC : ");
ICint = scanner.nextLong();
}
catch (InputMismatchException e) {
System.out.println("Please enter only integers.");
return;
}
IC = String.valueOf(ICint);
System.out.println("Searching for patient..");
for (int i = 0; i < patientList.size(); i++) {
if (patientList.get(i).IC.equals(IC)) {
count++;
System.out.println("Patient found! Added to queue.\n\n");
patientQueue.add(patientList.get(i));
}
}
if (count == 0) {
System.out.println("Patient not found. Registering new entry.\n");
registerNewPatient(IC, socket);
}
}
public static ArrayList<Patient> registerNewPatient(String IC, Socket socket) {
String fname;
String lname;
int age;
int cnumber;
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter first name : ");
fname = scanner.next();
System.out.println("Enter last name : ");
lname = scanner.next();
System.out.println("Enter age : ");
age = scanner.nextInt();
System.out.println("Enter contact number : ");
cnumber = scanner.nextInt();
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
InputStream input = socket.getInputStream();
DataInputStream dis = new DataInputStream(input);
dos.writeInt(1); //to let server know nurse is registering new patient
dos.writeUTF(IC);
dos.writeUTF(fname);
dos.writeUTF(lname);
dos.writeInt(age);
dos.writeInt(cnumber);
System.out.println("Adding entry..");
int id = dis.readInt();
patientList.add(new Patient(id, IC, fname, lname, age, cnumber));
patientQueue.add(new Patient(id, IC, fname, lname, age, cnumber));
System.out.println("Added new entry!\n\n");
} catch (IOException e) {
System.out.println("Cannot connect to server in register new patient for nurse.");
}
catch (InputMismatchException e) {
System.out.println("Please enter only integers and text in the appropriate fields.");
}
return patientQueue;
}
public static void viewPatients(ArrayList<Patient> patientList) {
for (int i = 0; i < patientList.size(); i++) {
System.out.println(patientList.get(i).toString());
}
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any key to go back to main menu.");
scanner.next();
}
public static void viewQueue(Socket socket) {
Scanner scanner = new Scanner(System.in);
int patientNumber ;
if (patientQueue.isEmpty()) {
System.out.println("There is no patient in the queue.");
return;
}
for (int i = 0; i < patientQueue.size(); i++) {
System.out.println((i + 1) + ". Patient ID : " + patientQueue.get(i).PatientID
+ "\nName : " + patientQueue.get(i).firstName + "\n");
}
System.out.println("Select patient to send to doctor. Press 0 to return to main menu.");
try {
patientNumber = scanner.nextInt();
if (patientNumber == 0) {
return;
}
patientNumber -= 1;
}
catch (InputMismatchException e) {
System.out.println("Please enter only integers.");
return;
}
if ((patientNumber >= 0) && (patientNumber < patientQueue.size())) {
if (patientNumber != -1) {
try {
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
dos.writeInt(4);
dos.writeInt((patientQueue.get(patientNumber).PatientID));
patientQueue.remove(patientNumber);
System.out.println("Patient " + (patientNumber + 1) + " sent to doctor! Removed from queue.");
} catch (IOException e) {
System.out.println("Cannot create streams in view queue.");
}
} else {
return;
}
} else {
System.out.println("Number entered is not in queue. Please try again.");
}
}
public static void startChat(Socket socket) {
InputStream input;
DataInputStream dis = null;
OutputStream output;
DataOutputStream dos = null;
try {
input = socket.getInputStream();
dis = new DataInputStream(input);
output = socket.getOutputStream();
dos = new DataOutputStream(output);
dos.writeInt(3); //send 5 as request to server to request for chat
} catch (IOException e) {
System.out.println("Cannot send requests to server.");
}
System.out.println("Enter your message and hit enter to send.\n");
connectedToChat = true;
Thread readThread = new Thread(new Read(dis));
Thread writeThread = new Thread(new Write(dos));
readThread.start();
writeThread.start();
while ((writeThread.isAlive()) && (readThread.isAlive())) {
}
System.out.println("Disconnected from chat. Enter anything to return to menu.");
if (!writeThread.isAlive()) {
Scanner scanner = new Scanner(System.in);
scanner.next();
}
while (writeThread.isAlive()) {
}
}
private static void viewMedicineQueue(Socket socket) {
int choice = 0;
Scanner scanner = new Scanner(System.in);
double price = 0;
int amount = 0;
if (medicineQueue.size() == 0) {
System.out.println("There is no medicine in queue. Try refreshing.");
} else {
for (int i = 0; i < medicineQueue.size(); i++) {
System.out.println((i + 1) + ". Patient ID is " + medicineQueue.get(i).patientID);
System.out.println("Medicine prescribed is " + medicineQueue.get(i).medicinePrescribed);
System.out.println("Amount is " + medicineQueue.get(i).amount);
amount = medicineQueue.get(i).amount;
}
System.out.println("Choose which to prescribe : ");
try {
choice = scanner.nextInt();
choice -= 1;
int count = 0;
for (int i = 0; i < medicineList.size(); i++) {
if (medicineList.get(i).medicineName.equals(medicineQueue.get(choice).medicinePrescribed)) {
count++;
price = medicineList.get(i).price * amount;
break;
}
}
if (count == 0) {
System.out.println("Medicine does not exist in database. Creating new entry..");
try {
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
InputStream input = socket.getInputStream();
DataInputStream dis = new DataInputStream(input);
dos.writeInt(9);
String medicineName;
String medicineUnit;
double medicinePrice;
int medicineID;
medicineName = medicineQueue.get(choice).medicinePrescribed;
System.out.println("Enter medicine unit : ");
medicineUnit = scanner.next();
System.out.println("Enter price per unit : ");
medicinePrice = scanner.nextDouble();
dos.writeUTF(medicineName);
dos.writeUTF(medicineUnit);
dos.writeDouble(medicinePrice);
System.out.println("Adding new medicine..");
medicineID = dis.readInt();
medicineList.add(new Medicine(medicineID, medicineName, medicineUnit, medicinePrice));
System.out.println("Added successfully!\n");
price = medicinePrice * amount;
} catch (IOException e) {
System.out.println("Cannot get IO streams in prescribe medicine.");
}
}
} catch (InputMismatchException e) {
System.out.println("Please enter only integers.");
return;
}
System.out.println("\nTotal price for the medicine is : " + price);
//after prescribing medicine, delete from queue and database
try {
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
dos.writeInt(11);
dos.writeInt(medicineQueue.get(choice).queueID);
}
catch (IOException e) {
System.out.println("Cannot create IO streams in delete medicine");
}
medicineQueue.remove(choice); //remove the medicine queue from the queue after prescription
}
System.out.println("\nEnter any key to continue.");
scanner.next();
}
private static void getMedicineList(Socket socket) {
int medicineID;
String medicineName;
String medicineUnit;
double price;
try {
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
InputStream input = socket.getInputStream();
DataInputStream dis = new DataInputStream(input);
dos.writeInt(8);
int numberOfMedicine = dis.readInt();
for (int i = 0; i < numberOfMedicine; i++) {
medicineID = dis.readInt();
medicineName = dis.readUTF();
medicineUnit = dis.readUTF();
price = dis.readDouble();
medicineList.add(new Medicine(medicineID, medicineName, medicineUnit, price));
}
} catch (IOException e) {
System.out.println("Cannot get IO streams.");
}
}
public static void refreshMedicineQueue(Socket socket) {
try {
InputStream input = socket.getInputStream();
DataInputStream dis = new DataInputStream(input);
OutputStream output = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(output);
dos.writeInt(10);
int found = 0;
int inQueue = dis.readInt();
if (inQueue > 0) {
int queueID;
int patientID;
String medicineName;
int medicineAmount;
for (int i = 0; i < inQueue; i++) {
queueID = dis.readInt();
patientID = dis.readInt();
medicineName = dis.readUTF();
medicineAmount = dis.readInt();
for (i = 0; i < medicineQueue.size(); i++) {
if (medicineQueue.get(i).queueID == queueID) {
found++;
}
}
if (found == 0) {
medicineQueue.add(new MedicineQueue(queueID, patientID, medicineName, medicineAmount));
System.out.println("Updated!");
}
}
} else {
System.out.println("No new queue.\n");
}
} catch (IOException e) {
System.out.println("Cannot get IO streams in refresh medicine queue.");
}
}
private static void searchIllness() {
String userSearch;
Scanner scanner = new Scanner(System.in);
int found = 0;
System.out.println("Enter illness to search for : ");
userSearch = scanner.next();
for (int i = 0; i < illnessList.size(); i++) {
if (illnessList.get(i).medicalCondition.equals(userSearch)) {
System.out.println("\nFound!");
System.out.println("Patient ID is " + illnessList.get(i).patientID);
System.out.println("Date is " + illnessList.get(i).date + " " + illnessList.get(i).time);
found++;
}
}
if (found == 0) {
System.out.println("No such condition found.");
}
}
private static void loadIllnessList(Socket socket) {
int patientSize, patientID;
String date;
String time;
String medicalCondition;
String medicinePrescribed;
int medicineAmount;
OutputStream out = null;
DataOutputStream dos = null;
InputStream in = null;
DataInputStream dis = null;
try {
out = socket.getOutputStream();
dos = new DataOutputStream(out);
in = socket.getInputStream();
dis = new DataInputStream(in);
dos.writeInt(5);
patientSize = dis.readInt();
for (int i = 0; i < patientSize; i++) {
patientID = dis.readInt();
date = dis.readUTF();
time = dis.readUTF();
medicalCondition = dis.readUTF();
medicinePrescribed = dis.readUTF();
medicineAmount = dis.readInt();
illnessList.add(new Illness(patientID, date, time, medicalCondition, medicinePrescribed, medicineAmount));
}
} catch (IOException e) {
System.out.println("Cannot get IO stream in load patient history.");
}
}
public static class Read implements Runnable { //read thread to read messages from chat
DataInputStream dis;
public Read(DataInputStream in) {
dis = in;
}
public void run() {
String input;
String end = "/end";
while (connectedToChat) {
try {
input = dis.readUTF();
if (input.equals(end)) {
connectedToChat = false;
} else {
System.out.println(input);
}
} catch (IOException e) {
System.out.println("Cannot read messages from server.");
}
}
}
}
public static class Write implements Runnable { //write thread to write chat messages
DataOutputStream dos;
public Write(DataOutputStream dos) {
this.dos = dos;
}
public void run() {
Scanner scanner = new Scanner(System.in);
String output;
String end = "/end";
while (connectedToChat == true) {
try {
output = scanner.nextLine();
if (connectedToChat == true) {
if (output.equals(end)) {
dos.writeUTF("/end");
connectedToChat = false;
break;
} else {
dos.writeUTF(output);
}
}
} catch (IOException e) {
System.out.println("Cannot write to server.");
connectedToChat = false;
}
}
}
}
}