-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleInterface.java
More file actions
158 lines (136 loc) · 4.73 KB
/
ConsoleInterface.java
File metadata and controls
158 lines (136 loc) · 4.73 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
package BloodMatch.src.bloodmatch;
import static BloodMatch.src.bloodmatch.Patient.*;
import static BloodMatch.src.bloodmatch.Donor.*;
import java.util.Scanner;
// a class, which manages the console input data.
public class ConsoleInterface {
String id, name, email, city, bloodType, donorId;
int priority, option;
Scanner console = new Scanner(System.in);
// method, which launches the application.
public void launch() {
display();
option = askOption();
doOption(option);
}
// method, which interracts with application, based on the choosen option.
private void doOption(int what) {
if (what == -1) {
System.out.println("Invalid input type!Write an integer.");
return;
}
System.out.println();
// do specific task.
switch (what) {
case 1:
id = askId();
name = askName();
email = askEmail();
city = askCity();
bloodType = askBloodType();
addDonor(id, name, email, city, bloodType);
break;
case 2:
id = askId();
name = askName();
city = askCity();
bloodType = askBloodType();
priority = askPriority();
addPatient(id, name, city, bloodType, priority);
break;
case 3:
DataManager.showDonors();
break;
case 4:
DataManager.showPatients();
break;
case 5:
System.out.println("To quit the buffer, enter 'exit'");
do {
id = askId();
if (id.equalsIgnoreCase("exit"))
break;
// if patient's criteria matches with donor's, create a queue.
if (matchWithDonor(id))
getAndCreate(id);
} while (!(id.equalsIgnoreCase(bloodType)));
Scheduler.manageFLow();
removePatient(id);
break;
case 6:
System.out.println("Terminating the program.");
break;
default:
System.out.println("We only have options from 1 to 6.");
break;
}
}
// method, which dispkays the interface.
private void display() {
System.out.println("===BLOOD MATCH SYSTEM===");
System.out.println("1. Add donor");
System.out.println("2. Add patient");
System.out.println("3. Show all donors");
System.out.println("4. Show all requests");
System.out.println("5. Match donors with requests");
System.out.println("6. Exit");
}
// method, which asks for option.
private int askOption() {
System.out.print("Choose: ");
if (console.hasNextInt()) {
option = console.nextInt();
console.nextLine();
return option;
}
return -1;
}
// method, which asks for id.
private String askId() {
System.out.print("Enter id: ");
return console.nextLine();
}
// method, which asks for name.
private String askName() {
System.out.print("Enter name: ");
return console.nextLine();
}
// method, which asks for email.
private String askEmail() {
System.out.print("Enter email: ");
return console.nextLine();
}
// method, which asks for city.
private String askCity() {
System.out.print("Enter city: ");
return console.nextLine();
}
// method, which asks for priority.
private int askPriority() {
System.out.print("Enter urgency(1-10): ");
if (console.hasNextInt()) {
priority = console.nextInt();
console.nextLine();
return priority;
}
return -1;
}
// method, which asks for blood typE.
private String askBloodType() {
System.out.print("Enter blood type: ");
bloodType = console.nextLine();
return BloodType.parseType(bloodType);
}
// method, which gets priority and creates a thread for patient.
private void getAndCreate(String id) {
priority = Patient.getUrgency(id);
donorId = Patient.matchForId(id);
// create if thread's priority is valid.
if (priority != -1) {
PatientThread.createThread(id, donorId, priority);
return;
}
System.out.println("Priority isn't valid.");
return;
}
}