-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doctor.java
142 lines (122 loc) · 4.61 KB
/
Doctor.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
import java.lang.Math;
public class Doctor extends Server {
// time units are in minutes
private static final int ON_BREAK_FREQUENCY = 300;
private static final int ON_BREAK_DURATION = 15;
private static final int BREAKDOWN_FREQUENCY = 600;
private static final int BREAKDOWN_DURATION = 10;
private static final int JDOC_SERVICE_TIME_C1 = (int) Math.round(395/3);
private static final int JDOC_SERVICE_TIME_C2 = (int) Math.round(403/3);
private static final int JDOC_SERVICE_TIME_C3 = (int) Math.round(200/3);
private static final int JDOC_SERVICE_TIME_C4 = (int) Math.round(66/3);
private static final int JDOC_SERVICE_TIME_C5 = (int) Math.round(37/3);
private long on_break_time_start = 0;
private long breakdown_time_start = 0;
private PatientQueue other_queue;
private PatientQueue exitPatients;
private boolean isSeniorDoctor;
private boolean isBrokenDown = false;
private boolean isOnBreak = false;
public Doctor(String name, long hourly_wage, int max_number_of_users, boolean isSeniorDoctor, PatientQueue exit_patients, PatientQueue other_queue) {
super(name, hourly_wage, max_number_of_users);
this.isSeniorDoctor = isSeniorDoctor;
this.exitPatients = exit_patients;
this.other_queue = other_queue;
// this.equipment = equipment;
}
@Override
public boolean serve_user() {
/**
* New user has been admitted to the waiting room. The doctor may be on
* break, or the machines may be down. Stats for current doctor updated,
* and new patient begins being serviced if available.
*
* @returns: boolean if new user is accepted and valid
*/
if (isUnderMaintenance()) {
return false;
}
User next_usr = my_queue.get_next_user();
if (next_usr == null) {
next_usr = other_queue.get_next_user();
if (next_usr == null) {
return false;
} else {
next_usr.setService_time(calculateServiceTime(next_usr));
}
}
next_usr.setTimeToDoctor(this.curr_time);
next_usr.start_service(this.curr_time);
number_served += 1;
current_user_being_served = next_usr;
remaining_service_time = next_usr.getService_time();
next_usr.setTimeInED(this.curr_time + remaining_service_time);
exitPatients.add_user(next_usr);
return true;
}
private int calculateServiceTime(User usr) {
/**
* This function calculates how long it will take to service the patient, based off
* the expertise of their doctor chosen. Senior doctor works faster, but takes on more
* severe cases
*
* @param usr: Current user being calculated:
* @returns int: service time calculated
*/
int serviceTime;
if (usr.getSeverity() == 1) {
serviceTime = JDOC_SERVICE_TIME_C1;
}
else if (usr.getSeverity() == 2) {
serviceTime = JDOC_SERVICE_TIME_C2;
}
else if (usr.getSeverity() == 3) {
serviceTime = JDOC_SERVICE_TIME_C3;
}
else if (usr.getSeverity() == 4) {
serviceTime = JDOC_SERVICE_TIME_C4;
}
else if (usr.getSeverity() == 5) {
serviceTime = JDOC_SERVICE_TIME_C5;
}
else {
serviceTime = -1;
}
if (isSeniorDoctor) {
serviceTime /= 2;
}
return serviceTime;
}
public boolean isUnderMaintenance() {
/**
* Checks if a break is required, or if the doctor's equipment is scheduled
* for a breakdown.
*
* @return: boolean if either the machine or doctor are broken down
*/
if (isBrokenDown) {
if (curr_time >= breakdown_time_start + BREAKDOWN_DURATION) {
isBrokenDown = false;
breakdown_time_start += BREAKDOWN_DURATION + BREAKDOWN_FREQUENCY;
}
}
else {
if (curr_time >= breakdown_time_start) {
isBrokenDown = true;
}
}
if (isOnBreak) {
if (curr_time >= on_break_time_start + ON_BREAK_DURATION) {
isOnBreak = false;
on_break_time_start += ON_BREAK_DURATION + ON_BREAK_FREQUENCY;
}
}
else {
if (curr_time >= on_break_time_start) {
isOnBreak = true;
}
}
isMaintenance = (isOnBreak || isBrokenDown);
return (isOnBreak || isBrokenDown);
}
}