个税计算-面向对象的练习

个税计算公式:
分别使用几个类,放置相关社保(-c xxxx)、员工信息(-d xxxx)、个税计算结果(-o xxxx)等信息
# -*- coding: utf-8 -*-
#导入模块
import sys
import csv

#argv获取参数
class Args(object):

    def __init__(self):
        self.args = sys.argv[1:]
#索引路径位置
    def get_mark(self, option):
        index = self.args.index(option)
        return self.args[index + 1]
#社保文件路径
    @property
    def config_path(self):
        return self.get_mark('-c')
#社保员工信息路径
    @property
    def userdata_path(self):
        return self.get_mark('-d')
#社保结果文件路径
    @property
    def export_path(self):
        return self.get_mark('-o')
#实例化       
args = Args()        


#configfile社保

class Config(object):

    def __init__(self):
        self.config = self._read_config()
#将文件内容读取到字典中
    def _read_config(self):
        config_path = args.config_path
        config = {}  
        with open(config_path) as confignews:
            for one_config in confignews.readlines():
               news, nums = one_config.strip().split('=')
               config[news.strip()] = float(nums.strip())
        return config
    def get_config(self, key):
        return self.config[key]

config = Config()

#user data员工信息
class UserData(object):
    
    def __init__(self):
        self.userdata = self._read_users_data()
 #将文件内容读取到列表中   
    def _read_users_data(self):
        userdata_path = args.userdata_path     
        userdata = []
        with open(userdata_path) as datanews:
            for one_data in datanews.readlines():
                idnum, wage = one_data.strip().split(',')
                userdata.append((idnum, int(wage)))
        return userdata

    def __iter__(self):
        return iter(self.userdata)


#tax_income
class IncomeTax(object):
    #comployeedata = []
    def __init__(self, userdata):
        self.userdata = userdata
    #salary after tax
    @staticmethod
    def calc_fordata(wage):
    #social security tax社保
        if wage < config.get_config('JiShuL'):
            return config.get_config('JiShuL')*0.165
        if wage > config.get_config('JiShuH'):
            return config.get_config('JiShuH')*0.165
        else:
            return wage*0.165
       
# tax_oneself个税
    @classmethod
    def tax_for_one(cls, wage):
        social_wage = cls.calc_fordata(wage) 
        taxwage = wage - social_wage       
        if taxwage<=0:
            tax_self = 0
        elif taxwage<=1500:
            tax_self = taxwage*0.03
        elif taxwage<=4500:
            tax_self= taxwage*0.1-105
        elif taxwage<=9000:
            tax_self = taxwage*0.2-555
        elif taxwage<=35000:
            tax_self =  taxwage*0.25-1005
        elif taxwage<=55000:
            tax_self = taxwage*0.3-2755
        elif taxwage<=80000:
            tax_self = taxwage*0.35-5505
        else:
            tax_self = taxwage*0.45-13505
        return tax_self   
    #the money we can get at the end最后收入
    def money(self):
        comployeedata= []
        for idnum, wage in self.userdata:
            comployee = [idnum, wage]
            shebao = self.calc_fordata(wage)
            taxone = self.tax_for_one(wage)
            get_money = wage - shebao - taxone
            comployee  += ['{:.2f}'.format(shebao), '{:.2f}'.format(taxone),'{:.2f}'.format(get_money)]
            comployeedata.append(comployee)
        return comployeedata
    def __iter__(self):
        return iter(self.userdata)

    #output csv file输出结果到文件
    def export(self, default = 'csv'):
        result = self.money()
        with open(args.export_path, 'w', newline = '') as f:
            writer = csv.writer(f)
            writer.writerows(result)

#exec

if __name__=='__main__':   
    last_money = IncomeTax(UserData()) 
    last_money.export()

#本次小项目练习,题目来源于实验楼挑战项目,主要为面向对象的练习,包括类、属性、魔法方法、静态方法等的使用和理解。还包括格式化输出,迭代器等内容。在面向对象这一块内容上,还需进一步练习。

#这是一个好的开始!

猜你喜欢

转载自blog.csdn.net/weixin_41512727/article/details/79285400
今日推荐