Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created January 21, 2020 07:59
Show Gist options
  • Save anmolj7/cfd42bc9e0fb1a9caf2fcb84dc2f6ca9 to your computer and use it in GitHub Desktop.
Save anmolj7/cfd42bc9e0fb1a9caf2fcb84dc2f6ca9 to your computer and use it in GitHub Desktop.
#Using OOP
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def change_salary(self, change, increment=True):
if increment:
self.salary += change
else:
self.salary -= change
employee_1 = Employee("Anmol", 10**8)
employee_2 = Employee("Jon Snow", 10**7)
employee_3 = Employee("Tyrion", 10**6)
employee_4 = Employee("Joffrey", 10**0)
employee_1.change_salary(1000)
employee_2.change_salary(1000)
employee_3.change_salary(1000)
employee_4.change_salary(0)
#Functional programming :(
Employees = {'Anmol': 10**8, 'Jon Snow': 10**7, 'Tyrion': 10**6, 'Joffrey': 10**0}
def change_salary(Employees: dict, name: str, change: int, increment: bool):
if name in Employees:
if increment:
Employees[name] += change
else:
Employees[name] += change
return Employees
# I don't know if this demostrates completely, but, with OOP
# It creates a serpate oobject for each employee we make
# This is just a small example, but a real life app, may require a lot of
# features instead of just name and salary, they might require to change the address
# as well! It would be become a night mare if you use FUNCTIONAL PROGRAMMING for this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment