-
Notifications
You must be signed in to change notification settings - Fork 41
/
with_inheritance.py
106 lines (71 loc) · 2.63 KB
/
with_inheritance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Very advanced Employee management system.
"""
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class Employee(ABC):
"""Basic representation of an employee at the company."""
name: str
id: int
@abstractmethod
def compute_pay(self) -> float:
"""Compute how much the employee should be paid."""
@dataclass
class HourlyEmployee(Employee):
"""Employee that's paid based on number of worked hours."""
pay_rate: float
hours_worked: int = 0
employer_cost: float = 1000
def compute_pay(self) -> float:
return self.pay_rate * self.hours_worked + self.employer_cost
@dataclass
class SalariedEmployee(Employee):
"""Employee that's paid based on a fixed monthly salary."""
monthly_salary: float
percentage: float = 1
def compute_pay(self) -> float:
return self.monthly_salary * self.percentage
@dataclass
class Freelancer(Employee):
"""Freelancer that's paid based on number of worked hours."""
pay_rate: float
hours_worked: int = 0
vat_number: str = ""
def compute_pay(self) -> float:
return self.pay_rate * self.hours_worked
@dataclass
class SalariedEmployeeWithCommission(SalariedEmployee):
"""Employee that's paid based on a fixed monthly salary and that gets a commission."""
commission: float = 100
contracts_landed: float = 0
def compute_pay(self) -> float:
return super().compute_pay() + self.commission * self.contracts_landed
@dataclass
class HourlyEmployeeWithCommission(HourlyEmployee):
"""Employee that's paid based on number of worked hours and that gets a commission."""
commission: float = 100
contracts_landed: float = 0
def compute_pay(self) -> float:
return super().compute_pay() + self.commission * self.contracts_landed
@dataclass
class FreelancerWithCommission(Freelancer):
"""Freelancer that's paid based on number of worked hours and that gets a commission."""
commission: float = 100
contracts_landed: float = 0
def compute_pay(self) -> float:
return super().compute_pay() + self.commission * self.contracts_landed
def main() -> None:
"""Main function."""
henry = HourlyEmployee(name="Henry", id=12346, pay_rate=50, hours_worked=100)
print(
f"{henry.name} worked for {henry.hours_worked} hours and earned ${henry.compute_pay()}."
)
sarah = SalariedEmployeeWithCommission(
name="Sarah", id=47832, monthly_salary=5000, contracts_landed=10
)
print(
f"{sarah.name} landed {sarah.contracts_landed} contracts and earned ${sarah.compute_pay()}."
)
if __name__ == "__main__":
main()