Question
Using Python 3.8 to complete the task asked below. Please give a screenshot of the answers. Thank you very much. Please complete the task below:
Using Python 3.8 to complete the task asked below. Please give a screenshot of the answers. Thank you very much.
Please complete the task below:
=== Module Description === This module contains an illustration of *inheritance* through an abstract Employee class that defines a common interface for all of its subclasses. """ from datetime import date from typing import List
class Employee: """An employee of a company.
This is an abstract class. Only child classes should be instantiated.
=== Public attributes === id_: This employee's ID number. name: This employee's name. """ id_: int name: str
def __init__(self, id_: int, name: str) -> None: """Initialize this employee.
Note: This initializer is meant for internal use only; Employee is an abstract class and should not be instantiated directly. """ self.id_ = id_ self.name = name
def get_monthly_payment(self) -> float: """Return the amount that this Employee should be paid in one month.
Round the amount to the nearest cent. """ raise NotImplementedError
def pay(self, pay_date: date) -> None: """Pay this Employee on the given date and record the payment.
(Assume this is called once per month.) """ payment = self.get_monthly_payment() print(f'An employee was paid {payment} on {pay_date}.')
def total_pay(self) -> float: """Return the total amount of pay this Employee has received.
>>> e = SalariedEmployee(14, 'Gilbert the cat', 1200.0) >>> e.pay(date(2018, 6, 28)) An employee was paid 100.0 on 2018-06-28. >>> e.pay(date(2018, 7, 28)) An employee was paid 100.0 on 2018-07-28. >>> e.pay(date(2018, 8, 28)) An employee was paid 100.0 on 2018-08-28. >>> e.total_pay() 300.0 """ # TODO: implement this method! pass
class SalariedEmployee(Employee): """An employee whose pay is computed based on an annual salary.
=== Public attributes === salary: This employee's annual salary
=== Representation invariants === - salary >= 0 """ id_: int name: str salary: float
def __init__(self, id_: int, name: str, salary: float) -> None: """Initialize this salaried Employee.
Precondition: salary >= 0
>>> e = SalariedEmployee(14, 'Fred Flintstone', 5200.0) >>> e.salary 5200.0 """ # Note that to call the superclass' initializer, we need to use the # full name '__init__'. This is the only time we write '__init__' # explicitly. Employee.__init__(self, id_, name) self.salary = salary
def get_monthly_payment(self) -> float: """Return the amount that this Employee should be paid in one month.
Round the amount to the nearest cent.
>>> e = SalariedEmployee(99, 'Mr Slate', 120000.0) >>> e.get_monthly_payment() 10000.0 """ return round(self.salary / 12, 2)
class HourlyEmployee(Employee): """An employee whose pay is computed based on an hourly rate.
=== Public attributes === hourly_wage: This employee's hourly rate of pay. hours_per_month: The number of hours this employee works each month.
=== Representation invariants === - hourly_wage >= 0 - hours_per_month >= 0 """ id_: int name: str hourly_wage: float hours_per_month: float
def __init__(self, id_: int, name: str, hourly_wage: float, hours_per_month: float) -> None: """Initialize this HourlyEmployee. Precondition: hourly_wage >= 0 and hours_per_month >= 0
>>> barney = HourlyEmployee(23, 'Barney Rubble', 1.25, 50.0) >>> barney.hourly_wage 1.25 >>> barney.hours_per_month 50.0 """ Employee.__init__(self, id_, name) self.hourly_wage = hourly_wage self.hours_per_month = hours_per_month
def get_monthly_payment(self) -> float: """Return the amount that this Employee should be paid in one month.
Round the amount to the nearest cent.
>>> e = HourlyEmployee(23, 'Barney Rubble', 1.25, 50.0) >>> e.get_monthly_payment() 62.5 """ return round(self.hours_per_month * self.hourly_wage, 2)
class Company: """A company with employees.
We use this class mainly as a client for the various Employee classes we defined in employee.
=== Attributes === employees: the employees in the company. """ employees: List[Employee]
def __init__(self, employees: List[Employee]) -> None: self.employees = employees
def pay_all(self, pay_date: date) -> None: """Pay all employees at this company.""" for employee in self.employees: employee.pay(pay_date)
def total_payroll(self) -> float: """Return the total of all payments ever made to all employees.
>>> my_corp = Company([\ SalariedEmployee(24, 'Gilbert the cat', 1200.0), \ HourlyEmployee(25, 'Chairman Meow', 500.25, 1.0)]) >>> my_corp.pay_all(date(2018, 6, 28)) An employee was paid 100.0 on 2018-06-28. An employee was paid 500.25 on 2018-06-28. >>> my_corp.total_payroll() 600.25 """ # TODO: implement this method! pass
if __name__ == '__main__': import doctest doctest.testmod()
# Remember: you'll need to *run this file* to actually get the lines below # to run. This is different than just running doctests. # To run this file in PyCharm, right-click in the file and select # "Run...", and then select "prep3" from the menu that appears. # DON'T select "Doctests in prep3", as that command will not actually # run this file, but instead only run its doctests. import python_ta python_ta.check_all(config={ 'extra-imports': ['datetime'], 'allowed-io': ['pay'] })
Please also finish the starter test below:
=== Module description === This module contains sample tests for Prep 3. Complete the TODO in this file.
When writing a test case, make sure you create a new function, with its name starting with "test_". For example:
def test_my_test_case(): # Your test here """ from datetime import date from hypothesis import given from hypothesis.strategies import integers, floats from prep3 import SalariedEmployee, HourlyEmployee, Company
# TODO: Implement *at least* 2 more test cases to test your code. # These test cases must be in their own functions, and their # names must start with "test_". # These test cases must pass on a working version of # the prep3 code (SalariedEmployee, HourlyEmployee, Company). # There are no other requirements for the test cases.
# === Sample test cases below === # Use the below test cases as an example for writing your own test cases, # and as a start to testing your prep3.py code.
# WARNING: THIS IS CURRENTLY AN EXTREMELY INCOMPLETE SET OF TESTS! # We will test your code on a much more thorough set of tests! def test_total_pay_basic() -> None: e = SalariedEmployee(14, 'Gilbert the cat', 1200.0) e.pay(date(2018, 6, 28)) e.pay(date(2018, 7, 28)) e.pay(date(2018, 8, 28)) assert e.total_pay() == 300.0
def test_total_payroll_mixed() -> None: my_corp = Company([SalariedEmployee(24, 'Gilbert the cat', 1200.0), HourlyEmployee(25, 'Chairman Meow', 500.25, 1.0)]) my_corp.pay_all(date(2018, 6, 28)) assert my_corp.total_payroll() == 600.25
if __name__ == '__main__': import pytest pytest.main(['prep3_starter_tests.py'])
Synthesize (submit on MarkUs) In this prep's readings, we developed a basic trio of classes for tracking employees and their pay. In this programming exercise, you will extend it to keep track of each employee's pay history: the dates on which they were paid, and the amount they were paid in each case. starter code starter tests Because this task's instructions are a bit longer, we've written them here rather than on the starter code directly. 1. Implement Employee. total_pay. Read the header and docstring for the new method. Decide if it should be implemented in the base class, or made abstract and implemented in the subclasses. (Even though the doctest for this method is in the base class, we can still make it an abstract method.) You may add new instance attribute(s) to implement the required functionality; just make sure to document them in the class docstring, add type annotations for them, and initialize them properly. You may also modify the provided methods outside of the areas marked TODO (but you cannot modify the method headers). You may assume as a precondition that the pay method is not called on the same employee twice in the same month. (Remember: "precondition" means here that your implementation can assume that it's true, and does not need to check for it.) 2. Implement Company.total_payroll. Read the header and docstring for this method carefully. You should not need to add any additional attributes to the class in order to be able to write this method. 3. Implement at least 2 more test cases in prep3_starter_tests.py. For prep3, you must simply write two additional cases to test your code. These tests should pass when run on a correct version of prep3's code. We will not test these for rigour, but simply to make sure you have had practice in writing tests. In future preps, we may evaluate the test cases you writeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started