基本的每周工资计算器
#初学者 #python #codenewbie #电脑科学
  • 使用Python的非常基本的每周工资计算器。
  • 用户界面是终端。
  • codecademy.com的项目
  • 计算机科学课程
class Employee:

    def __init__(self, name, employee_id="N/A"):
        self.name = name
        self.employee_id = employee_id

    def __repr__(self):
        if self.employee_id != "N/A":
            print("\nThis employee is: {name}. Employee ID number: {id}".format(name = self.name, id = self.employee_id)) 
        else:
            print("\nThis employee is: {name}".format(name = self.name))

    def calc_gross_pay(self, hours, rate):
        h = float(hours)
        r = float(rate)

        if h <= 40:
            pay = h * r
        else:
            ot = (h - 40) * 1.5 * r
            pay = (40 * r) + ot

        return pay

    def calc_net_pay(self, gross_pay, fed_income_tax, social, medicare, deductions=0, state_income_tax=0, wage_garnishments=0, reimbursement=0):
        pay = gross_pay
        pay_2 = pay - int(deductions) + int(reimbursement)

        fed = round((pay_2*(int(fed_income_tax)/100)), 2)
        soc = round((pay_2*((int(social))/100)), 2)
        med = round((pay_2*(int(medicare)/100)), 2)
        state = round((pay_2*(int(state_income_tax)/100)), 2)

        pay_3 = pay_2 - fed - soc - med - state
        pay_4 = pay_3 - int(wage_garnishments)
        net_pay = round(pay_4, 2)

        print("\ndeductions withheld (if any): ", str(deductions))
        print("\nReimbursements added (if any): ", str(reimbursement))
        print("\nFederal Income Taxes witheld: ", str(fed))
        print("\nSocial Security Taxes witheld: ", str(soc))
        print("\nMedicare Taxes witheld: ", str(med))
        print("\nState Income Taxes witheld (if any): ", str(state))
        print("\nWage garnishments applied (if any): ", str(wage_garnishments))
        print("\nNet Pay: ", net_pay)

name = input("\nWhat is your employee's name? ")
employee_id = input("\nIf your employee deos not have an ID, press enter. Otherwise please enter ID number here: ")

if employee_id == '':
    employee_one = Employee(name)
else:
    employee_one = Employee(name, employee_id)

employee_one.__repr__()

input("\nTo calculate pay answer the following questions...")

rate_one = input("\nEnter Rate: ")

hours_one = input("\nEnter Hours worked (for this pay period only): ")

gross_pay_one = employee_one.calc_gross_pay(hours_one, rate_one)

reim_1 = input("\nEnter reimbursements here (If not applicable press enter): ")
if reim_1 == '':
    reim_1 = 0

ded_1 = input("\nEnter deductions here (If not applicable press enter): ")
if ded_1 == '':
    ded_1 = 0

fed_1 = input("\nEnter the current Federal Income Tax Rate: ")

state_1 = input("\nEnter State Income Tax Rate (If not applicable press enter): ")
if state_1 == '':
    state_1 = 0

soc_1 = input("\nEnter the current Social Security Tax Rate: ")

med_1 = input("\nEnter the current Medicare Tax Rate: ")

garn_1 = input("\nEnter wage garnishements here (If not applicable press enter): ")
if garn_1 == '':
    garn_1 = 0


print("\n======================================")
print("\nGross Pay: " + str(gross_pay_one))

employee_one.calc_net_pay(gross_pay_one, fed_1, soc_1, med_1, ded_1, state_1, garn_1, reim_1)
print("\n======================================")