-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReception.java
More file actions
215 lines (206 loc) · 7.7 KB
/
Copy pathReception.java
File metadata and controls
215 lines (206 loc) · 7.7 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
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
import java.util.*;
import java.io.*;
/**
* This is a Reception class
* @author Andre Ke
* @author Ryan Su
* @author Wesley Goh
* @author Zilin Weng
* @version Java 17.0.1
*/
class Reception extends Staff implements Sortable{
private static final long serialVersionUID = 6267332186483611803L;
String id = "";
/**
* Explanation - creates instance of Reception
* @param name - a String representing the name
* @param age - a number representing the age
* @param gender - a String representing the gender
* @param dateOfBirth - a String reprsenting the date of birth
* @param phoneNumber - a String representing the phone number
* @param emailAddress - a String that represents the email address
* @param id - a String representing the login ID
* @param password - a String representing the login password
*/
public Reception(String name, int age, String gender, String dateOfBirth, String phoneNumber, String emailAddress, String id, String password){
super(name, age, gender, dateOfBirth, phoneNumber, emailAddress);
super.changeUserUsername(id);
super.changeUserPassword(password);
}
/**
* Explanation - partitions a patient ArrayList and sorts one side
* @param patients - an ArrayList of patient objects
* @param low - the lower restriction of the ArrayList of the that is being partitioned
* @param high - the upper restriction of the part of the ArrayList that is being partitioned
* @return a number is used in the sortPatientName method
*/
@Override
public int sortPatientNamePartition(ArrayList<Patient> patients, int low, int high){
Patient pivot = patients.get(high);
int i = (low - 1);
for (int j = low; j < high; j++){
if ((patients.get(j).getName().compareTo(pivot.getName())) < 0){
i++;
Collections.swap(patients, i, j);
}
}
Collections.swap(patients, i + 1, high);
return (i + 1);
}
/**
* Explanation - sorts an ArrayList of patients by name using an optimized QuickSort algorithm
* @param patients - an ArrayList of patients
* @param low - the lower limit of the ArrayList, typically set initially to 0: first index
* @param high - the upper limit of the ArrayList, typically set to the size of the ArrayList-1
*/
@Override
public void sortPatientName(ArrayList<Patient> patients, int low, int high){
while (low < high){
int pi = sortPatientNamePartition(patients, low, high);
sortPatientName(patients, low, pi - 1);
if (pi - low < high - pi){
sortPatientName(patients, low, pi - 1);
low = pi + 1;
}
else{
sortPatientName(patients, pi + 1, high);
high = pi - 1;
}
}
}
/**
* Explanation - partitions an appointment ArrayList and sorts one side
* @param patients - an ArrayList of appointment objects
* @param low - the lower restriction of the ArrayList of the that is being partitioned
* @param high - the upper restriction of the part of the ArrayList that is being partitioned
* @return a number that is used in the sortAppointTime method
*/
@Override
public int sortAppointTimePartition(ArrayList<Appointment> appoints, int low, int high){
Appointment pivot = appoints.get(high);
int i = (low - 1);
for (int j = low; j < high; j++){
if ((appoints.get(j).getDateAndTime().compareTo(pivot.getDateAndTime())) < 0){
i++;
Collections.swap(appoints, i, j);
}
}
Collections.swap(appoints, i + 1, high);
return (i + 1);
}
/**
* Explanation - sorts the appointment times
* @param appoints - an ArrayList that represents the appointments
* @param low - a number that represents the lower restriction of the ArrayList
* @param high - a number that represents the upper restriction of the ArrayList
*/
@Override
public void sortAppointTime(ArrayList<Appointment> appoints, int low, int high){
while (low < high){
int pi = sortAppointTimePartition(appoints, low, high);
if (pi - low < high - pi){
sortAppointTimePartition(appoints, low, pi - 1);
low = pi + 1;
}
else{
sortAppointTimePartition(appoints, pi + 1, high);
high = pi - 1;
}
}
}
/**
* Explanation - determines if requested appointment is available
* @param request - an Appointment object that represents the requested appointment
* @return a boolean that represents the availability
*/
public static boolean checkAvailability(Appointment request){
Schedule schedule = new Schedule();
try{
schedule.setAppointment(readFromFile("appointments.ser", Appointment.class));
}
catch(EOFException e){
}
catch(Exception e){
e.printStackTrace();
}
ArrayList<Appointment> docSched = schedule.getAllAppointments();
ArrayList<Appointment> onlyDates = new ArrayList<Appointment>();
if(!docSched.isEmpty()){
for (int i = 0; i < docSched.size(); i++){
//Checks if the appointments in the doctors schedule yyyy/mm/dd is the same as requests
if (docSched.get(i).getDateAndTime().substring(0, 9).equals(request.getDateAndTime().substring(0, 9))){
onlyDates.add(docSched.get(i));
}
}
String startTime = request.getDateAndTime().substring(11);
String endTime = request.getAppointmentEndTime();
//Check if appointments overlap
for (int i = 0; i < onlyDates.size(); i++){
if (onlyDates.get(i).getDateAndTime().substring(11).compareTo(startTime) < 0 && !(onlyDates.get(i).getAppointmentEndTime().compareTo(startTime) < 0)){
return false;
}
else if(!(onlyDates.get(i).getDateAndTime().substring(11).compareTo(endTime) > 0)){
return false;
}
}
}
addAppointmentToSchedule(request, schedule);
try{
Main.writeAppointments(schedule.getAllAppointments());
}
catch(Exception e){
e.printStackTrace();
}
return true;
}
/**
* Explanation - adds an appointment to the schedule
* @param appointment - an Appointment object that represents the appointment
* @param schedule - a Schedule object that the appointment is being added to
*/
public static void addAppointmentToSchedule(Appointment appointment, Schedule schedule){
schedule.addAppointment(appointment);
}
/**
* Explanation - searches for a doctor
* @param name - a String that represents the name of the doctor being searched for
* @param doctors - an ArrayList that represents all the doctors
* @return a Doctor object that represents the doctor being searched for
*/
public Doctor searchDoctor(String name, ArrayList<Doctor> doctors){
ArrayList<String> doctorNames = new ArrayList<String>();
int index = -1;
for(int i = 0; i < doctors.size()-1; i++){
doctorNames.add(doctors.get(i).getName());
}
if(doctorNames.contains(name)){
index = doctorNames.indexOf(name);
return doctors.get(index);
}
return null;
}
/**
* Explanation - reads a file and makes an object ArrayList of the file
* @param fileName - a String representing the file name
* @param clazz - a Class of any object
* @return an ArrayList that can hold any object
* @throws IOException if file name is not found
* @throws ClassNotFoundException if the class being converted to is not found
*/
public static <T> ArrayList<T> readFromFile(String fileName, Class<T> clazz) throws IOException, ClassNotFoundException {
ArrayList<T> objects = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis)) {
while (true) {
objects.add(clazz.cast(ois.readObject()));
}
}
catch (EOFException e){
return objects;
}
catch (Exception e){
e.printStackTrace();
}
return objects;
}
}