Implement a superclass Appointment and subclasses Onetime, Daily, Monthly. An ap
ID: 3556139 • Letter: I
Question
Implement a superclass Appointment and subclasses Onetime, Daily, Monthly. An appointment has a description(for example, "see the dentist") and a date. Write a method occursOn(year, month, day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a list of Appointment(class) objects with a mixture of appointments. Have the user enter a date and print out all appointments that occur on that date.
Test code is provided:
from appointment import Daily, Monthly, Onetime
# Create a list of appointments.
appList = []
appList.append(Daily(1, 1, 2013, "Do pushups"))
appList.append(Daily(15, 1, 2013, "Floss teeth"))
appList.append(Monthly(15, 12, 2012, "Backup data"))
appList.append(Onetime(21, 12, 2012, "Computer Science Final Exam"))
appList.append(Monthly(4, 2, 2013, "Call grandma"))
appList.append(Onetime(12, 4, 2013, "See dentist"))
# Read a date from the user and display all of its appointments until the user
# decides to quit.
day = int(input("Enter the day (0 to quit): "))
while day != 0 :
month = int(input("Enter the month: "))
year = int(input("Enter the year: "))
# Find all of the appointments on the entered date.
for app in appList :
if app.occursOn(day, month, year) :
print(app)
day = int(input("Enter the day (0 to quit): "))
Explanation / Answer
from datetime import date
class Appointment(object):
def __init__(self,day,month,year,description):
self.description=description
self.date=date(year,month,day)
def __unicode__(self):
return self.description
def occursOn(self,day,month,year):
if self.date == date(year,month,day):
return True
else:
return False
class Onetime(Appointment):
def __init__(self,day,month,year,description):
super(Onetime,self).__init__(day,month,year,description)
def __unicode__(self):
return self.description
class Daily(Appointment):
def __init__(self,day,month,year,description):
super(Daily,self).__init__(day,month,year,description)
def __unicode__(self):
return self.description
#function over written
def occursOn(self,day,month,year):
return True
# because daily appointment is true for all days
class Monthly(Appointment):
def __init__(self,day,month,year,description):
super(Monthly,self).__init__(day,month,year,description)
def __unicode__(self):
return self.description
#function over written
def occursOn(self,day,month,year):
if self.date.day == day:
return True
else:
return False
appList = []
appList.append(Daily(1, 1, 2013, "Do pushups"))
appList.append(Daily(15, 1, 2013, "Floss teeth"))
appList.append(Monthly(15, 12, 2012, "Backup data"))
appList.append(Onetime(21, 12, 2012, "Computer Science Final Exam"))
appList.append(Monthly(4, 2, 2013, "Call grandma"))
appList.append(Onetime(12, 4, 2013, "See dentist"))
day = int(input("Enter the day (0 to quit): "))
while day != 0 :
month = int(input("Enter the month: "))
year = int(input("Enter the year: "))
for app in appList :
if app.occursOn(day,month,year) :
print(app.description)
day = int(input("Enter the day (0 to quit): "))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.