Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Practice Paper

1. The following classes model the the courses, instructors and course schedule offered by a
training provider.

The Course class models a course with code, title and development cost. The Instructor class models
an instructor with email, name, and how much the instructor charges per day.

The CourseSchedule class models a schedule of the course by an instructor, with a start date and
duration in days.

Assume that the python code for Course class is already provided.

a) Write the Instructor class according to the class diagram given, with the 3 attributes, a
constructor, getter method for email, and getter and setter method for ratePerDay attribute.
The __str__() method returns a string in the following format:
Instructor email: xxx@yyy.com Name: Joe Wong

b) Write the CourseSchedule class with the following requirements:


- The scheduleId attribute is obtained by concatenating the course code with the class
variable _schId. E.g. if the course code is PY214, then the scheduleId is PY214_1. If there is
another course JA201, then the scheduleId is JA201_2 etc. The class variable _schId should
increment by 1 for every course schedule object created.
- The course attribute is the Course object.
- The instructor attribute is the Instructor object.
- The datetime attribute is the starting date of the course. It is a datetime object.
- The duration is an integer value representing the duration in days.
- Besides the constructor, there is a getter method to return the scheduleId.
- The class method changeSchId( anotherId ) modifies the class variable _schId to the
anotherId in the parameter.
- The getCourseCode() method returns the course code.
- The getInstructorEmail() method returns the email of the instructor.
- The courseFee() method computes and returns the course fee by taking the development
cost of the course, plus the duration multiply by the instructor rate per day.
- The __str__() method returns a string in the following format:
Schedule Id: PY214_1 Start Date: 1/6/2020 Duration: 3 days
Course Code: PY214 Course Name: Introduction to Python
Instructor email: xxx@yyy.com Name: Joe Wong

(the second line of the string comes from the __str__() method of the Course Class)

2. The following classes model customers who make reservations from the training provider.

The Reservation is an abstract class, with the following attributes and methods:

- It has a class variable _id =1 which is used as a counter for the reservationId attribute.
- It has other attributes, custEmail custName, courseSchedule and reservationDate.
- Besides the constructor, it has an abstract method courseFee() with no implementation.
- The __str__() method returns information on the reservation in the following format:
Reservation Id: 1 Email : a@b.com Name: John Reservation Date: 10/4/2020
Schedule Id: PY214_1 Start Date: 1/6/2020 Duration: 3 days
Course Code: PY214 Course Name: Introduction to Python
Instructor email: xxx@yyy.com Name: Joe Wong
Course fee: $999.99

The IndividualReservation is a subclass of Reservation. It has an additional yearBorn attribute. The


course fee for individual has a discount given by the class variable _discount of 30% for seniors who
are 55 years and older. All others pay the normal course fee.

Write the Reservation and the IndividualReservation class.


3. The Training Provider class models the Training Provider.

The TrainingProvider class has a collection of course schedules. It has the provider name as attribute
and a dictionary collection to store CourseSchedule objects. The course schedule id is the key and
the value is the object.

It has the following methods:

- addCourseSchedule(course, instructor, startDate, duration)


The method creates a CourseSchedule object and adds to the dictionary. An Exception
object is raised for the following cases:
o If the courseScheduleId already exists in the dictionary, raise an Exception with the
message “Course Schedule already added!”.
o startDate must be at least 10 days after today. Otherwise raise an Exception with
message “startDate must be a least 10 days later than today”.
o Duration must be at least 1 day. Otherwise raise an Exception with the message
“duration must be at least 1 day”.
- removeCourseSchedule(courseScheduleId)
The method removes a CourseSchedule from the dictionary. An Exception object is raised for
the following cases:
o If the courseScheduleId is not found, raise an Exception with the message “Course
schedule Id not found!”.
o If the start date of the course is less than 3 days from today, raise an Exception with
the message “Course schedule start date too close to remove!”.
a) Write the TrainingProvider class.
b) What must be done when a TrainingProvider object is created so that error messages are
handled and messages displayed without runtime errors.
4. The following GUI is used to add/search/List instructors with a training provider:

The code for the GUI is provided, but the event handlers for the Add/Search/List buttons are not.
You are NOT required to rewrite the whole program again, but indicate only the statement number
which needs modification and also the statement number where code should be inserted.

A dictionary to store instructors is provided in line 6. For Add button, create an Instructor object
based on the 3 text fields and add the object to the dictionary. The key is the email, and the value
the Instructor object. There is no need to consider blank fields, but a message “Instructor already
added!” should be displayed if another instructor of the same email is added. Display “Instructor
added!” if the instructor is added successfully to the dictionary.

For Search button, retrieve the Instructor object based on the email entered in the email text field.
Display the details in the scrolled text using the __str__ of the instructor object. If no instructor is
found, display “No instructor with this emai!”.

The List button simply displays all the instructors in the scrolled text, one instructor per line. If the
dictionary is empty, display “No instructors”.
1. from tkinter import *
2. from tkinter.scrolledtext import ScrolledText
3.
4. class TrainingProviderGUI():
5. def __init__(self):
6. self._instructors = {}
7. self._tk = Tk()
8. self._tk.geometry('270x200')
9. self._tk.title("Training Provider")
10. self.createWidgets()
11. self._tk.mainloop()
12.
13. def createWidgets(self):
14. inputFrame = Frame(self._tk)
15. lblEmail = Label(inputFrame, text='Email: ')
16. self._tbxEmail = Entry(inputFrame, width=15)
17. lblEmail.grid(row=0, column=0, sticky=W)
18. self._tbxEmail.grid(row=0, column=1, sticky=W)
19.
20. lblName = Label(inputFrame, text='Name: ')
21. self._tbxName=Entry(inputFrame, width=25)
22. lblName.grid(row=1, column=0, sticky=W)
23. self._tbxName.grid(row=1, column=1, sticky=W)
24.
25. lblRate = Label(inputFrame, text='Rate per day: ')
26. self._tbxRate=Entry(inputFrame, width=10)
27. lblRate.grid(row=2, column=0, sticky=W)
28. self._tbxRate.grid(row=2, column=1, sticky=W)
29.
30. inputFrame.grid(row=0, column=0)
31. buttonFrame = Frame(self._tk)
32. self._btnSearch = Button(buttonFrame, text='Add')
33. self._btnModify = Button(buttonFrame, text='Search')
34. self._btnList = Button(buttonFrame, text='List')
35. self._btnSearch.grid(row=0, column=1)
36. self._btnModify.grid(row=0, column=2, padx=5)
37. self._btnList.grid(row=0, column=3)
38.
39. scrollFrame = Frame(self._tk)
40. self._sclText = ScrolledText(scrollFrame, width=30, height=5,
wrap=WORD)
41. self._sclText.grid(row=0, column=0)
42. inputFrame.grid(row=0, column=0, pady=5)
43. scrollFrame.grid(row=2, column=0, pady=5)
44. buttonFrame.grid(row=1, column=0 )

~ end ~

You might also like