Skip to content

Instantly share code, notes, and snippets.

@wildonion
Last active October 28, 2020 11:58
Show Gist options
  • Save wildonion/d09a82e6eec71473dd941f073be54e20 to your computer and use it in GitHub Desktop.
Save wildonion/d09a82e6eec71473dd941f073be54e20 to your computer and use it in GitHub Desktop.
employees design pattern example
# -------------------------------------------------------------------------------------------
# designing pattern on a simple OOP problem
# employees insured and salary
employees = []
# ======================================
# CLASSES
import os
class Employee():
def __init__(self, pcode):
self.name = ""
self.lname = ""
self.meli_code = 0
self.pcode = pcode
self.fixed_sal = 0.0
self.hours_of_work = 0
self.tax = None
self.insurance = 0.0
self.overtime= 0.0
self.final_sal = 0.0
def status(self):
if self.hours_of_work > 40:
hours_of_overtime = self.hours_of_work - 40
self.overtime = hours_of_overtime * 30000
self.insurance = self.fixed_sal * 0.05
if self.fixed_sal < 4000000:
self.tax = "moaf"
if 4000000 < self.fixed_sal < 5000000:
self.tax = (self.fixed_sal - 4000000) * 0.10
if 5000000 < self.fixed_sal < 7000000:
self.tax = (self.fixed_sal - 4000000) * 0.15
if self.fixed_sal > 7000000:
self.tax = (self.fixed_sal - 4000000) * 0.20
self.final_sal = (self.overtime + self.fixed_sal) - (self.insurance + self.tax)
def addinfo(self, name, lname, meli_code, fixed_sal, how):
self.name = name
self.lname = lname
self.meli_code = meli_code
self.fixed_sal = fixed_sal
self.hours_of_work = how
def showinfo(self):
print("\n==============\nEMPLOYEE INFO\n==============\n")
print("[+] NAME : ", self.name)
print("[+] LAST NAME : ", self.lname)
print("[+] MELI CODE : ", self.meli_code)
print("[+] PERSONAL CODE : ", self.pcode)
print("[+] FIXED SALARY : ", self.fixed_sal)
print("[+] HOURS OF WORK : ", self.hours_of_work)
print("[+] TAX : ", self.tax)
print("[+] INSURANCE : ", self.insurance)
print("[+] OVERTIME : ", self.overtime)
print("[+] FINAL SALARY : ", self.final_sal)
# ==================================
# FUNCTIONS
def showallempinfo():
for i in range(len(employees)):
employees[i].showinfo()
print("\t********")
def findbypcode():
# os.system("cls")
while True:
pcode = input("[+] Enter personal code to find an employee >> ")
if not pcode:
break
for i in range(len(employees)):
if employees[i].pcode == int(pcode):
print("[+] FOUND ONE MATCH ... ")
employees[i].showinfo()
break
else:
print("[+] NOTHING FOUND... ")
break
def savetofile(emps, filename):
with open(filename+".txt", "w") as f:
for i in range(len(emps)):
f.write("\n==============\nEMPLOYEE INFO\n==============\n")
f.write("[+] NAME : ")
f.write(str(employees[i].name)+"\n")
f.write("[+] LAST NAME : ")
f.write(str(employees[i].lname)+"\n")
f.write("[+] MELI CODE : ")
f.write(str(employees[i].meli_code)+"\n")
f.write("[+] PERSONAL CODE : ")
f.write(str(employees[i].pcode)+"\n")
f.write("[+] FIXED SALARY : ")
f.write(str(employees[i].fixed_sal)+"\n")
f.write("[+] HOURS OF WORK : ")
f.write(str(employees[i].hours_of_work)+"\n")
f.write("[+] TAX : ")
f.write(str(employees[i].tax)+"\n")
f.write("[+] INSURANCE : ")
f.write(str(employees[i].insurance)+"\n")
f.write("[+] OVERTIME : ")
f.write(str(employees[i].overtime)+"\n")
f.write("[+] FINAL SALARY : ")
f.write(str(employees[i].final_sal)+"\n")
def readfromfile(path):
with open(path+".txt", "r") as f:
for line in f:
print(line)
def save3emptofile():
data = []
for i in range(3):
code = input(f"\tEnter meli code or personal code of person {i+1}, ba comma joda konid, like : 00198222, 33 >> ")
if "," in code:
meli_code = code.split(",")[0]
pcode = code.split(",")[1]
for j in range(len(employees)):
if employees[j].meli_code == meli_code and employees[j].pcode == pcode:
data.append(employees[j])
else:
for j in range(len(employees)):
if employees[j].meli_code == code or employees[j].pcode == code:
data.append(employees[j])
savetofile(data, "data")
# ==============================
# MAIN
if __name__ == "__main__":
# initalizing....
for i in range(int(input("[+] Employee.N >> "))):
name = str(input("\tEnter name >> "))
lname = str(input("\tEnter last name >> "))
meli_code = int(input("\tEnter meli code >> "))
fixed_sal = float(input("\tEnter fixed salary >> "))
how = int(input("\tEnter hours of work >> "))
e = Employee(i+1)
e.addinfo(name, lname, meli_code, fixed_sal, how)
e.status()
employees.append(e)
if i > 1 :
print("\t@@@@@@@@@@@@@@@@@@@")
showallempinfo()
print("[[[[[[ FINDING EMPLOYEE ]]]]]]")
findbypcode()
print("[[[[[[ SAVING TO FILE .... ]]]]]]")
savetofile(employees, "all_employees")
print("[[[[[[ READING FROM FILE ]]]]]]")
readfromfile("all_employees")
print("[[[[[[ SAVING 3 EMPLOYEE TO FILE ]]]]]]")
save3emptofile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment