-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcis131_lab_abstract_class_and_methods.py
More file actions
131 lines (122 loc) · 3.46 KB
/
cis131_lab_abstract_class_and_methods.py
File metadata and controls
131 lines (122 loc) · 3.46 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
script: cis131_lab_abstract_class_and_methods.py
action: A class hierarchy example
Author: Declan Juliano
Date: 10/14/2025
'''
from abc import ABC, abstractmethod
# Abstract employee class
class Employee(ABC):
def __init__(self, first, last, ssn):
self._first = first
self._last = last
self._ssn = ssn
# Get first name
@property
def first_name(self):
return
# Get last name
@property
def last_name(self):
return self._last
# Get name (first + last)
@property
def name(self):
return f"{self._first} {self._last}"
# Get ssn
@property
def ssn(self):
return self._ssn
# Get earnings
@abstractmethod
def earnings(self):
raise NotImplementedError("Subclasses must implement earnings method")
# Print
def __repr__(self):
return f"{self._first} {self._last}, SSN: {self.ssn}"
# salaried employee
class SalariedEmployee(Employee):
def __init__(self, first, last, ssn, weekly_salary):
super().__init__(first, last, ssn)
self.weekly_salary = weekly_salary
# get weekly salary
@property
def weekly_salary(self):
return self._weekly_salary
# Change weekly salary
@weekly_salary.setter
def weekly_salary(self, value):
if value < 0:
raise ValueError("Weekly salary must be non-negative")
self._weekly_salary = value
# Get earnings
def earnings(self):
return self.weekly_salary
# Print
def __repr__(self):
return f"SalariedEmployee: {super().__repr__()}, Weekly Salary: ${self.weekly_salary:.2f}"
# Hourly employee
class HourlyEmployee(Employee):
def __init__(self, first, last, ssn, hours, wage):
super().__init__(first, last, ssn)
self.hours = hours
self.wage = wage
# Hours worked
@property
def hours(self):
return self._hours
# change hours
@hours.setter
def hours(self, value):
if not (0 <= value <= 168):
raise ValueError("Hours must be between 0 and 168")
self._hours = value
# get Wage
@property
def wage(self):
return self._wage
# Change wage
@wage.setter
def wage(self, value):
if value < 0:
raise ValueError("Wage must be non-negative")
self._wage = value
# Get earnings
def earnings(self):
if self.hours <= 40:
return self.hours * self.wage
else:
overtime = self.hours - 40
return 40 * self.wage + overtime * self.wage * 1.5
# Print
def __repr__(self):
return (f"HourlyEmployee: {super().__repr__()}, Hours: {self.hours}, "
f"Wage: ${self.wage:.2f}")
# Try to create a class object of an abstract class
try:
bob = Employee("Bob","Jones",123123)
print(bob)
except Exception as e:
print("You can't do that.",e)
# Create employees
print('')
Octavia = SalariedEmployee("Octavia","Melody",734235274,1832.62)
Aurry = HourlyEmployee("Aurelia","Celune",412575349,45,21.72)
Aura = HourlyEmployee("Lunar","Aura",745783123,42,1523.01)
Lyra = SalariedEmployee("Lyra","Heartstrings",47329250,2475.23)
# Print employees and their earnings sepreately
print(Octavia)
print(Octavia.earnings())
print(Aurry)
print(Aurry.earnings())
print(Aura)
print(Aura.earnings())
print(Lyra)
print(Lyra.earnings())
print('')
# List of employees
employees = [Octavia,Aurry,Aura,Lyra]
# Itterate through the list and print the employees and their earnings
for i in employees:
print(i)
print(i.earnings())