In: Computer Science
In Ruby
A dentist appointment schedule validation software
Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments.
Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask the user to enter a date to check (for example, 2006 10 5), and ask to user if they want to check against OneTime, Day or Month appointment. Based on what the user selected, OccursOn inside each of the sub class should run and display any matching appointment and associated descriptions.
Hint: For OneTime subclass, OccursOn need three inputs (Year, Month and Day) to validate, for Day subclass, OccursOn needs one input (Day) to validate, and for Month subclass, OccursOn need one input (Month) to validate. OccursOn is different for different subclasses.
Code Image:
Sample Output:
Code to Copy:
main.rb:
#main.rb
#import the appointement classes
require_relative 'Appointment'
#Implementation of genRequiredAppointment method
#which accepts argument
def genRequiredAppointment(argument)
#call Appointment.instance(argument)
resultantObj = Appointment.instance(argument)
#Get random number
rNumber = Random.new()
#Generate random day
resultantObj.day = rNumber.rand(1..10000000) % 30 + 1
#Generate random month
resultantObj.month = rNumber.rand(1..10000000) % 12 + 1
#Generate random year
resultantObj.year = rNumber.rand(2014..2019)
resultantObj.description = "#{resultantObj.class} appointment #{resultantObj.month}/#{resultantObj.day}/#{resultantObj.year}"
resultantObj
end
#Implementation genRequiredArray method which accepts
#arrSize as parameter
def genRequiredArray(arrSize)
#Declare result
result = []
#Generate rNumber
rNumber = Random.new((Time.now.to_f * 100000).round)
#Iterate the loop
for k in 0..arrSize
# 1 for OneTime, 2 for Day, for Month
resutantNumber = rNumber.rand(1..3)
case (resutantNumber)
#check resutantNumber 1 then call
# the below statement
when 1
result[k] = genRequiredAppointment(:OneTime)
#check resutantNumber 2 then call
# the below statement
when 2
result[k] = genRequiredAppointment(:Day)
#check resutantNumber 3 then call
# the below statement
when 3
result[k] = genRequiredAppointment(:Month)
end
end
#call result
result
end
#Implementaion menu method
def menu
#Display statement
puts 'Appointements:'
#Display statement for One time
puts '1: One time'
#Display statement for Day
puts '2: Day'
#Display statement for Month
puts '3: Month'
#Display statement for Show
puts '4: Show'
puts '5: Exit'
puts 'Enter your userInput (1..5):'
end
if __FILE__ == $PROGRAM_NAME
#Declare userInput and assign 0
userInput = 0
#Declare userInput and assign 50
arrSize = 50
#assign return value to genRequiredArray(arrSize)
result = genRequiredArray(arrSize)
#Iterate the loop
while userInput != 5
#call menu method
menu
#convert into integer
userInput = gets.to_i
case userInput
#check userInput 1 then call
# the below statements
when 1
#Display statement
puts 'Enter a date(mm/dd/yyyy) format:'
reqValue = gets.chomp
#get the number
numbers = reqValue.split('/', 3)
#Iterate the loop
for k in 0..arrSize
if result[k].class.to_s == 'OneTime' && result[k].occursOn(numbers[1].to_i, numbers[0].to_i, numbers[2].to_i)
#Display statement
puts result[k].description
end
end
#check userInput 2 then call
# the below statements
when 2
#Display statement
puts 'Enter a day between (1..30):'
reqValue = gets.to_i
#Iterate the loop
for k in 0..arrSize
#Display statement
puts result[k].description if result[k].class.to_s == 'Day' && result[k].occursOn(reqValue, 0, 0)
end
#check userInput 3 then call
# the below statements
when 3
#Display statement
puts 'Enter a month between(1..12):'
reqValue= gets.to_i
#Iterate the loop
for k in 0..arrSize
#Display statement
puts result[k].description if result[k].class.to_s == 'Month' && result[k].occursOn(0,reqValue, 0)
end
#check userInput 4 then call
# the below statements
when 4
#Iterate the loop
for k in 0..arrSize
#Display statement
puts result[k].description
end
#check userInput 5 then call
# the below statements
when 5
#Display statement for Exit
puts 'Exit .'
end
#Display statement
puts
end
end
Appointment.rb:
#Implementation of Appointment class
class Appointment
#Declare day, month, year as attributes
attr_accessor :description, :day, :month, :year
#Implementation of occursOn method
# which checks whether the appointment occurs on
# day,month,year
def occursOn(day, month, year)
#raise message Not implemented
raise 'Not Implemented'
end
@@classregister = {}
#Implementation of register method
def self.register(argument)
#assign self to @@classregister[argument]
@@classregister[argument] = self
end
#Implementation of instance method
def self.instance(argument)
#assign @@classregister[argument] to argument
argument = @@classregister[argument]
#check argument or not
if argument
argument.new
else
#otherwise raise an error message
raise "Error Message: No such type exist"
end
end
end
#Implementation of OneTime
#OneTime is subclass of Appointment
class OneTime < Appointment
#Implementation of occursOn method
# which checks whether the appointment occurs on
# day,month,year
def occursOn(day, month, year)
#check the @day is equal to day and
#@month is equal to month
#@year is equal to year or not
if @day == day && @month == month && @year == year
#return true
return true
end
#otherwise false
false
end
# register OneTime subtype class
register(:OneTime)
end
#Implementation of Day
#Day is subclass of Appointment
class Day < Appointment
#Implementation of occursOn method
# which checks whether the appointment occurs on
# day,month,year
def occursOn(day, month, year)
#check @day is equal to day
#then return true
if @day == day
return true
end
#otherwise false
false
end
# register Day subtype class
register(:Day)
end
#Implementation of Month
#Month is subclass of Appointment
class Month < Appointment
#Implementation of occursOn method
# which checks whether the appointment occurs on
# day,month,year
def occursOn(day, month, year)
#check @month is equal to month
#then return true
if @month == month
return true
end
#otherwise false
false
end
# register Month subtype class
register(:Month)
end