forked from ArjanCodes/2021-composition-vs-inheritance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbefore.py
84 lines (65 loc) · 2.02 KB
/
before.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
"""
Very advanced Employee management system.
"""
from dataclasses import dataclass
@dataclass
class HourlyEmployee:
"""Employee that's paid based on number of worked hours."""
name: str
id: int
commission: float = 100
contracts_landed: float = 0
pay_rate: float = 0
hours_worked: int = 0
employer_cost: float = 1000
def compute_pay(self) -> float:
"""Compute how much the employee should be paid."""
return (
self.pay_rate * self.hours_worked
+ self.employer_cost
+ self.commission * self.contracts_landed
)
@dataclass
class SalariedEmployee:
"""Employee that's paid based on a fixed monthly salary."""
name: str
id: int
commission: float = 100
contracts_landed: float = 0
monthly_salary: float = 0
percentage: float = 1
def compute_pay(self) -> float:
"""Compute how much the employee should be paid."""
return (
self.monthly_salary * self.percentage
+ self.commission * self.contracts_landed
)
@dataclass
class Freelancer:
"""Freelancer that's paid based on number of worked hours."""
name: str
id: int
commission: float = 100
contracts_landed: float = 0
pay_rate: float = 0
hours_worked: int = 0
vat_number: str = ""
def compute_pay(self) -> float:
"""Compute how much the employee should be paid."""
return (
self.pay_rate * self.hours_worked + 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 = SalariedEmployee(
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()