For a homework assignment for an online class (pretty much learning everything from the internet anyway) we were asked to build an appointment program. This is just a start, however I feel I am definitely over-complicating things and going about this the complete wrong way.
These were the instructions we were given:
Implement a superclass
Appointmentand sub classesOneTime,DayandMonth. An appointment has a description (for example, "Root Canal"), and dates information (you can useDateobject or Int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments.Write a method
OccursOninside 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 againstOneTime,DayorMonthappointment. Based on what the user selected,OccursOninside each of the sub class should run and display any matching appointment and associated descriptions.
require 'date'
class Appointment
attr_accessor :info, :date, :year, :day, :month
def initialize(info, date)
@info = info
@date = Date.parse(date)
end
def get_info
@info
end
def get_date
@date
end
def get_year
@year = date.year
end
def get_month
@month = date.mon
end
end
class OneTime < Appointment
def OccursOn(year, month, day)
puts "The year you entered is #{year}"
puts "The month you entered is #{month}"
puts "The day you entered is #{day}"
appointments.each do |x|
puts "Month #{x.get_month}"
if x.get_month == month
puts x.get_date
puts x.get_info
end
end
end
end
class Day < Appointment
def OccursOn(day)
puts day
end
end
class Month < Appointment
def OccursOn(month)
puts month
end
end
appointments = []
appointment1 = Appointment.new("Root Canal", "31/10/2017")
appointment2 = Appointment.new("Cavity Filling", "31/12/2017")
appointments << appointment1 << appointment2
puts appointment1.get_date
puts appointment2.get_date
puts appointment1.get_info
onetime1 = OneTime.new("Root Canal", "31-10-2017")
puts "Enter a date to check (for example, 31-10-2017)"
checkdate = gets.chomp
checkdate = Date.parse(checkdate)
puts "Would you like to check against OneTime, Day or Month appointment?"
checkagainst = gets.chomp
if checkagainst == "OneTime" or checkagainst == "onetime"
onetime1.OccursOn(checkdate.year, checkdate.month, checkdate.day)
elsif checkagainst == "Day" or checkagainst == "day"
puts "Day" # run day check function
elsif checkagainst == "Month" or checkagainst == "Month"
puts "Month" # run month check function
else
puts "Invalid option."
end