-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organization.rb
518 lines (426 loc) · 14.8 KB
/
Organization.rb
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
require 'tty-prompt'
require_relative 'Separate'
class Organization
@name
@services
@service_providers
def initialize(prompt)
@services = Array.new
@service_providers = Array.new
@name = prompt.ask("Name of the organization: ")
puts "\nThe #{@name} organization has been created!\n"
line_break()
end
def add_service()
prompt = TTY::Prompt.new
service_name = prompt.ask("Name of the service: ")
while true
if service_name != nil
break
else
puts "Error: Not a valid service name. Please enter a valid service below. "
service_name = prompt.ask("Name of the service: ")
end
end
if (!service_name)
puts "Error: Service Must Have Name"
line_break()
return
end
if(service_already_exists?(service_name))
puts "Error: Service Already Exists"
line_break()
return
end
service_price = prompt.ask("Price of the service (ex. '3.00'): ")
while true
if service_price != nil
break
else
puts "Error: Not a valid service price. Please enter a valid service price below. "
service_price = prompt.ask("Price of the service (ex. '3.00'): ")
end
end
service_duration = prompt.ask("Duration of the service (in hours): ")
while true
if service_duration != nil
break
else
puts "Error: Not a valid service duration. Please enter a valid service duration below. "
service_duration = prompt.ask("Duration of the service (in hours): ")
end
end
@services.push(Service.new(service_name, service_price, service_duration))
puts "The #{service_name} service has been created!"
line_break()
end
def remove_service()
prompt = TTY::Prompt.new
service_names = []
@services.each do |service|
service_names.push(service.name)
end
service_to_delete = prompt.select("Choose the service to delete...", service_names)
@services.each do |existing_service|
if(existing_service.name == service_to_delete)
@services.delete(existing_service)
puts "The #{service_to_delete} service has been deleted"
line_break()
return
end
end
puts "The #{@service_to_delete} service does not exist!"
line_break()
end
def list_services()
if @services.length < 1
puts "\nCurrently no services are offered"
else
puts "\nThe offered services are:\n"
i = 0
while i < @services.length
service_name = @services[i].name
puts "\t#{service_name}"
i += 1
end
end
end
def add_service_provider()
prompt = TTY::Prompt.new
service_provider_name = prompt.ask("Name of the service provider: ")
while true
if service_provider_name != nil
break
else
puts "Error: Not a valid service provider name. Please enter a valid service provider name below. "
service_provider_name = prompt.ask("Name of the service provider: ")
end
end
if (!service_provider_name)
puts "Error: Service Provider Must Have Name"
line_break()
return
end
if(service_provider_already_exists?(service_provider_name))
puts "Service Provider already exists"
line_break()
return
end
service_provider_number = prompt.ask("Service provider phone number: ")
while true
if service_provider_number != nil
break
else
puts "Error: Not a valid service provider number. Please enter a valid service provider number below. "
service_provider_number = prompt.ask("Service provider phone number: ")
end
end
available_service_names = prompt.multi_select("Which services can #{service_provider_name} provide?:", get_service_choices())
available_service_names_array = available_service_names
available_services = Array.new
available_service_names_array.each do |service_name|
@services.each do |service|
if(service.name == service_name)
available_services.push(service)
break
end
end
end
if (available_services.length > 0)
@service_providers.push(ServiceProvider.new(service_provider_name, service_provider_number, available_services))
puts "\nThe #{service_provider_name} service provider has been created!"
line_break()
else
puts "\nError: A service provider must provide at least one service."
line_break()
end
end
def remove_service_provider()
prompt = TTY::Prompt.new
providers = []
@service_providers.each do |provider|
providers.push(provider.name)
end
provider_to_delete = prompt.select("Choose which service provider to delete:", providers)
@service_providers.each do |provider|
if(provider.name == provider_to_delete)
@service_providers.delete(provider)
puts "Service Provider #{provider_to_delete} has been deleted"
line_break()
return
end
end
puts "Error: Service provider does not exist"
line_break()
end
def list_service_providers()
if @service_providers.length < 1
puts "\nCurrently no service providers"
else
puts "\nThe service providers are:\n"
i = 0
while i < @service_providers.length
service_provider_name = @service_providers[i].name
puts "\t#{service_provider_name}"
i += 1
end
end
end
def schedule_appointment()
prompt = TTY::Prompt.new
client = prompt.ask("Client name: ")
service_provider_name = prompt.select("Choose a service provider:", get_service_providers_array())
service_provider = get_service_provider_by_name(service_provider_name)
service_name = prompt.select("Choose a service:", get_service_choices())
service = get_service_by_name(service_name)
if(!service_provider_provides_service?(service_provider_name, service_name))
puts "Error: #{service_provider_name} does not provide #{service_name}"
line_break()
return
end
day, month, year = get_date_response(true)
if (!day || !month || !year)
return
end
start_hour, start_minute = get_time_response(true)
if(start_hour == nil || start_minute == nil)
puts "Error: Invalid Time"
line_break()
return
end
appointment_time = Time.new(year, month, day, start_hour, start_minute)
is_recurring = get_recurring_response()
# client = prompt.ask("Client name: ")
# while true
# if client != nil
# break
# else
# puts "Error: Not a valid name. Please enter a valid name below. "
# client = prompt.ask("Client name: ")
# end
# end
if(service_provider.timeslot_is_available?(appointment_time, service.duration, is_recurring))
appt = Appointment.new(appointment_time, service, client, is_recurring)
service_provider.add_appt(appt)
puts "\nAppointment added successfully"
line_break()
else
puts "\nError: Can't add a new appointment to that time"
line_break()
end
end
def list_appointments()
prompt = TTY::Prompt.new
service_provider_name = prompt.select("Choose a service provider:", get_service_providers_array())
service_provider = get_service_provider_by_name(service_provider_name)
if(service_provider == nil)
puts "Error: Service provider does not exist"
line_break()
return
end
service_provider.appointments.each do |appointment|
appointment.output_appointment
end
end
def view_schedule()
prompt = TTY::Prompt.new
service_provider_name = prompt.select("Choose a service provider:", get_service_providers_array)
service_provider = get_service_provider_by_name(service_provider_name)
if(service_provider == nil)
puts "Service provider does not exist"
line_break()
return
end
day, month, year = get_date_response(false)
if (!day || !month || !year)
return
end
time = Time.new(year, month, day)
if (service_provider.appointments.length == 0)
puts "#{service_provider_name} doesn't have any appointments on that day."
end
service_provider.appointments.each do |appointment|
if(appointment.is_recurring)
if(time.wday == appointment.time.wday)
appointment.output_appointment
end
else
if(is_same_date?(time, appointment.time))
appointment.output_appointment
end
end
end
end
def schedule_availability_block()
prompt = TTY::Prompt.new
service_provider_name = prompt.select("Choose a service provider:", get_service_providers_array)
service_provider = get_service_provider_by_name(service_provider_name)
if(service_provider == nil)
puts "Error: Service provider does not exist"
line_break()
return
end
day, month, year = get_date_response(true)
if (!day || !month || !year)
return
end
start_hour, start_minute = get_time_response(false)
if(start_hour == nil || start_minute == nil)
puts "Error: Invalid Time"
line_break()
return
end
block_time = Time.new(year, month, day, start_hour, start_minute)
block_duration = prompt.ask("Duration of the block (in hours): ")
while true
if block_duration != nil
break
else
puts "Error: Not a valid duration. Please enter a valid duration below. "
block_duration = prompt.ask("Duration of the block (in hours): ")
end
end
is_recurring = get_recurring_response()
new_block = AvailabilityBlock.new(block_time, block_duration, is_recurring)
service_provider.add_availability_block(new_block)
end
def list_commands()
return ((Organization.public_instance_methods - Object.public_instance_methods).sort)
end
private
def get_service_choices()
service_choices = Array.new
i = 0
while i < @services.length
service_choices.push(@services[i].name)
i += 1
end
return service_choices
end
def get_service_providers_array()
available_service_providers = Array.new
i = 0
while i < @service_providers.length
available_service_providers.push(@service_providers[i].name)
i += 1
end
return available_service_providers
end
def service_already_exists?(new_service_name)
@services.each do |existing_service|
if(existing_service.name == new_service_name)
return true
end
end
false
end
def service_provider_already_exists?(new_service_provider_name)
@service_providers.each do |existing_service_provider|
if(existing_service_provider.name == new_service_provider_name)
return true
end
end
return false
end
def service_provider_provides_service?(service_provider_name, service_name)
@service_providers.each do |service_provider|
if(service_provider.name == service_provider_name)
service_provider.services.each do |service|
if(service.name == service_name)
return true
end
end
return false
end
end
return false
end
def get_service_by_name(service_name)
@services.each do |service|
if(service.name == service_name)
return service
end
end
return nil
end
def get_service_provider_by_name(service_provider_name)
@service_providers.each do |service_provider|
if(service_provider.name == service_provider_name)
return service_provider
end
end
return nil
end
def get_recurring_response
prompt = TTY::Prompt.new
is_recurring_response = prompt.ask("Appointment recurs weekly? (y/n): ")
while(is_recurring_response != 'y' && is_recurring_response != 'n')
puts "Error: Invalid response (must enter 'y' or 'n')"
is_recurring_response = prompt.ask("Appointment recurs weekly? (y/n): ")
end
return is_recurring_response == 'y'
end
def get_date_response(view_sched)
prompt = TTY::Prompt.new
month = prompt.ask("Month (ex. '4'): ")
while (month.to_i < 1 || month.to_i > 12 || !month)
puts "Error: Invalid Month"
line_break()
month = prompt.ask("Month of appointment (ex. '4'): ")
end
day = prompt.ask("Date of appointment: ")
while (day.to_i < 1 || day.to_i > 31 || !day)
puts "Error: Invalid Day"
line_break()
day = prompt.ask("Date of appointment: ")
end
year = prompt.ask("Year of appointment : ")
while (year.to_i < 2019 || !year)
puts "Error: Invalid Year"
line_break()
year = prompt.ask("Year of appointment : ")
end
return day, month, year
end
def get_time_response(if_appt)
prompt = TTY::Prompt.new
if (if_appt)
time_string = prompt.ask("Time of appointment (ex. '14:45'): ")
while true
if time_string != nil
break
else
puts "Error: Not a valid time. Please enter a valid time below. "
time_string = prompt.ask("Time of appointment (ex. '14:45'): ")
end
end
else
# I don't understand why it is somewhat asked twice.
time_string = prompt.ask("Time off start time (ex. '14:45'): ")
while true
if time_string != nil
break
else
puts "Error: Not a valid time. Please enter a valid time below. "
time_string = prompt.ask("Time off start time (ex. '14:45'): ")
end
end
end
time_string_array = time_string.split(':')
if(time_string_array.length != 2)
return nil, nil
end
hour = time_string_array[0].to_i
minute = time_string_array[1].to_i
if(hour < 0 || hour > 23)
hour = nil
end
if(minute < 0 || minute > 59)
minute = nil
end
return hour, minute
end
def is_same_date?(time_1, time_2)
return (time_1.day == time_2.day && time_1.month == time_2.month && time_1.year == time_2.year)
end
end