Skip to content

Instantly share code, notes, and snippets.

@Back2Basics
Created April 1, 2024 00:48
Show Gist options
  • Save Back2Basics/151dd9c171947041af0727f0a570f156 to your computer and use it in GitHub Desktop.
Save Back2Basics/151dd9c171947041af0727f0a570f156 to your computer and use it in GitHub Desktop.
# Hire person
# Hire other person
# print all employees
# fire person
# pay person (that wasn't fired)
# print all employees
from dataclasses import dataclass
@dataclass
class Person:
first_name: str
last_name: str
routing_number: str = None
account_number: str = None
def pay(self, amount):
print(f"{self.name} was paid {amount} to {self.account_number = } {self.routing_number = }.")
@property
def name(self):
return f"{self.first_name} {self.last_name}"
@dataclass
class HumanResources:
employees: list
def __init__(self):
self.employees = []
def hire(self, person: Person): # very specific Person type
self.employees.append(person)
print(f"{person.name} was hired.")
def fire(self, person: Person):
self.employees.remove(person)
print(f"{person.name} was fired.")
def pay(self, person: Person, amount: int):
person.pay(amount)
hr = HumanResources()
arial = Person("Arial", "Greentext")
hr.hire(arial)
# add account number and routing number after adding her to HR
arial.account_number = "123456789"
arial.routing_number = "987654321"
felix = Person("Felix", "Furlough")
hr.hire(felix)
# see the account number and routing number are updated in the __str__ of employee list
print(hr)
hr.pay(arial, 1000)
hr.fire(felix)
print(hr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment