Download all the code in one zipfile.
Or copy and paste it:
from employee import Manager from employee import Attendant from employee import Cook from employee import Mechanic from reporting import AccountingReport from reporting import StaffingReport from reporting import ScheduleReport from shift import MorningShift from shift import AfternoonShift from shift import NightShift employees = [ Manager("Vera", "Schmidt", 2000, MorningShift()), Attendant("Chuck", "Norris", 1800, MorningShift()), Attendant("Samantha", "Carrington", 1800, AfternoonShift()), Cook("Roberto", "Jacketti", 2100, MorningShift()), Mechanic("Dave", "Dreissig", 2200, MorningShift()), Mechanic("Tina", "Rivers", 2300, MorningShift()), Mechanic("Ringo", "Rama", 1900, AfternoonShift()), Mechanic("Chuck", "Rainey", 1800, NightShift()), ] reports = [ AccountingReport(employees), StaffingReport(employees), ScheduleReport(employees), ] for r in reports: r.print_report() print()
class Employee: def __init__(self, first_name, last_name, salary, shift): self._first_name = first_name self._last_name = last_name self.salary = salary self.shift = shift def get_full_name(self): return f"{self._first_name} {self._last_name}" class Manager(Employee): job_title = "Manager" class Attendant(Employee): job_title = "Station Attendant" class Cook(Employee): job_title = "Cook" class Mechanic(Employee): job_title = "Mechanic"
class Report: def __init__(self, emp_list): self._emp_list = emp_list class AccountingReport(Report): def print_report(self): print("Accounting") print("==========") for e in self._emp_list: print(f"{e.get_full_name()}, ${e.salary}") class StaffingReport(Report): def print_report(self): print("Staffing") print("========") for e in self._emp_list: print(f"{e.get_full_name()}, {e.job_title}") class ScheduleReport(Report): def print_report(self): print("Schedule") print("========") for e in self._emp_list: print(f"{e.get_full_name()}, {e.shift.get_shift_info()}")
import datetime class Shift: def get_shift_info(self): return f"{self.start_time:%H:%M} to {self.end_time:%H:%M}" class MorningShift(Shift): start_time = datetime.time(8, 00) end_time = datetime.time(16, 00) class AfternoonShift(Shift): start_time = datetime.time(12, 00) end_time = datetime.time(20, 00) class NightShift(Shift): start_time = datetime.time(14, 00) end_time = datetime.time(22, 00)
import unittest from employee import Employee class TestEmployee(unittest.TestCase): def test_get_full_name(self): e = Employee("Vera", "Schmidt", 0, None) self.assertEqual(e.get_full_name(), "Vera Schmidt") def test_raise_salary(self): e = Employee("", "", 2000, None) e.raise_salary(1.1) self.assertEqual(e.salary, 2200)