模拟信用卡登录程序

需求: 实现普通用户和管理员的操作

   普通用户只能对自身进行金额变动,以及帐号信息的修改

   管理员可以发信卡号,冻结卡号,修改指定帐号的额度等等

   指定还款日

   指定出账单日,定期出账单

 

由于当前水平有限,仅依靠文本操作和函数实现需求

流程图如下:

 

主函数:

 1 all_card_bill()
 2     exit_ = False
 3     while not exit_:
 4         a = input('1:信用卡中心;2:信用卡后台管理;3:退出\n>>>')
 5         if a == '3':
 6             exit_ = True
 7             continue
 8         if a == '1':
 9             credit_card()
10         if a == '2':
11             manage()
View Code

 

登录:

def credit_card_login(card_num, password):
    global LOGIN_ED_CARD, LIMIT, BALANCE, AUTHORITY, STATUS
    f = open('credit_card_msg', 'r')
    for line in f:
        if card_num == line.strip().split()[0] and password == line.strip().split()[1]:
            LOGIN_ED_CARD = True
            LIMIT = float(line.strip().split()[2])
            BALANCE = float(line.strip().split()[3])
            AUTHORITY = line.strip().split()[4]
            STATUS = line.strip().split()[5]
            return True
    if not LOGIN_ED_CARD:
        print('卡号或者密码错误')
View Code

 

登录验证:

def wrapper_credit_login(fuc):
    def inner(*args, **kwargs):
        if LOGIN_ED_CARD:
            return fuc(*args, **kwargs)
        else:
            times = 0
            while times < 3:
                times += 1
                dic_['card_num'] = input('请输入信用卡卡号:').strip()
                dic_['password'] = input('请输入密码:').strip()
                if credit_card_login(dic_['card_num'], dic_['password']):
                    return fuc(*args, **kwargs)
            if times >= 3:
                print('错误次数达到3次,bye')

    return inner
View Code

 

权限验证,仅在进行管理员操作时才会执行:

def wrapper_authority(fuc):
    def inner(*args, **kwargs):
        if AUTHORITY == 'admin':
            return fuc(*args, **kwargs)
        else:
            print('权限不够')
    return inner
View Code

 

 

信用卡中心:

 1 @wrapper_credit_login
 2 def credit_card():
 3     if STATUS == 'frozen':
 4         print('您的帐号已被冻结,请联系客服')
 5         return
 6     print('尊敬的', dic_['card_num'])
 7     exit_flag = False
 8     while not exit_flag:
 9         choose = input('1:我的信用卡,2:还款,3:转账,4:提现,5返回\n>>>').strip()
10         if choose == '5':
11             exit_flag = True
12             continue
13         if choose == '1':
14             my_card()
15         if choose == '2':
16             pay_back()
17         if choose == '3':
18             transfer_accounts()
19         if choose == '4':
20             get_cash()
View Code

 

mycard:

def my_card():
    exit_flag = False
    while not exit_flag:
        choose = input('1:查看信用额度:,2:查看可用额度,3:查看流水明细,4:查询最近的账单,5:修改密码,6:返回\n>>>')
        if choose == '6':
            exit_flag = True
            continue
        if choose == '1':
            print('您的信用额度为', LIMIT)
        if choose == '2':
            print('您的可用额度为', BALANCE)
        if choose == '3':
            check_transaction_detail()
        if choose == '4':
            check_bill()

        if choose == '5':
            change_password()
View Code

 

信用额度、可用额度是作为全局变量的,用户登录之后被赋值,有变动就与文件同步更新

 

查看流水明细:

def check_transaction_detail():
    if os.path.exists(dic_['card_num'] + '_transaction_detail'):
        list_ = []
        f = open(dic_['card_num'] + '_transaction_detail', 'r')
        for line in f:
            list_.append(line)
        f.close()
        if len(list_) < 2:
            print('您还没有交易进出记录')
        else:
            for line in list_:
                if line.strip().startswith('cred'):
                    print('存入', '支出', '交易日期')
                    continue
                print(line)
    else:
        f = open(dic_['card_num'] + '_transaction_detail', 'x')
        line = 'cred' + ' ' + 'debt' + ' ' + 'tran_date' + '\n'
        f.write(line)
        f.close()
        print('您还没有交易进出记录')
View Code

 

查看最近一期的账单

def check_bill():
    if os.path.exists(dic_['card_num']+'_bill'):
        f = open(dic_['card_num']+'_bill', 'r')
        lines = f.readlines()
        target_line = lines[-1]
        print('尊敬的', dic_['card_num'], '您最近的一期账单如下')
        print('信用额度' + ' ' + '可用余额' + ' ' + '账单日期' + ' ' + '到期还款日' + ' ' + '上期账单金额'
              + ' ' + '支出总计' + ' ' + '存入总计' + ' ' + '本期应还金额' + ' ' + '最低还款额')
        print(target_line.strip())

    else:
        print('尊敬的', dic_['card_num'], '您还没有生成的账单')
View Code

 

修改密码:

def change_password():
    dic_['card_num_again'] = input('请输入信用卡号:').strip()
    dic_['password_again'] = input('请输入密码:').strip()
    if dic_['card_num'] == dic_['card_num_again'] and dic_['password'] == dic_['password_again']:
        dic_['new_password'] = input('请输入新密码:')
        dic_['new_password_again'] = input('请再次输入新密码:')
        if dic_['new_password_again'] == dic_['new_password']:
            dic_['password'] = dic_['new_password']
            list_ = []
            f = open('credit_card_msg', 'r')
            for line in f:
                if line.strip().split()[0] == dic_['card_num']:
                    card_num = line.strip().split()[0]
                    password = dic_['password']
                    limit = line.strip().split()[2]
                    authority = line.strip().split()[3]
                    status = line.strip().split()[4]
                    new_line = (card_num + ' ' + password + ' ' + limit + ' '
                                + authority + ' ' + status + '\n')
                    list_.append(new_line)
                    continue
                list_.append(line)
            f.close()

            f = open('credit_card_msg', 'w')
            f.writelines(list_)
            f.close()

            print('您的密码已重新设置:请牢记', dic_['password'])
        else:
            print('您的两次密码输入不一样')
    else:
        print('您输入的卡号或密码不正确')
View Code

 

存款:

def pay_back():
    global BALANCE
    exit_flag = False
    while not exit_flag:
        choose = input('1:查看最近账单,2:存款,3:返回\n>>>').strip()
        if choose == '3':
            exit_flag = True
            continue
        if choose == '1':
            check_bill()
        if choose == '2':
            last_step = False
            while not last_step:
                money_payback = input('[b=back]请输入存款金额\n>>>').strip()
                if money_payback == 'b' or money_payback == 'back':
                    last_step = True
                    continue
                if money_payback.isdigit():
                    money_payback = float(money_payback)
                    BALANCE += money_payback
                    list_ = []
                    f = open('credit_card_msg', 'r')
                    for line in f:
                        if line.strip():
                            if line.strip().split()[0] == dic_['card_num']:
                                balance = float(line.strip().split()[3]) + money_payback
                                new_line = (line.strip().split()[0] + ' ' + line.strip().split()[1] + ' ' +
                                            line.strip().split()[2] + ' ' + str(balance) + ' ' +
                                            line.strip().split()[4] + ' ' + line.strip().split()[5] + '\n')
                                list_.append(new_line)
                                continue
                        list_.append(line)
                    f.close()
                    f = open('credit_card_msg', 'w')
                    f.writelines(list_)
                    f.close()
                    now_times = TODAY
                    line = str(money_payback) + ' ' + '0.0' + ' ' + now_times + '\n'
                    if os.path.exists(dic_['card_num'] + '_transaction_detail'):
                        f = open(dic_['card_num'] + '_transaction_detail', 'a')
                        f.write(line)
                        f.close()
                    else:
                        f = open(dic_['card_num'] + '_transaction_detail', 'w')
                        line_ = 'cred' + ' ' + 'debt' + ' ' + 'datetime' + '\n'
                        f.write(line_)
                        f.write(line)
                        f.close()
                    print('您已于', now_times, '存入金额:', money_payback, '\n输入金额继续存款,输入b或者back退出')
View Code

 

转账的话,涉及到两个账户的操作,因此两个账户的数据都得同步

转账:

def transfer_accounts():
    global BALANCE
    exit_flag = False
    while not exit_flag:
        choose = input('1:查看剩余额度,2:转账,3:返回\n>>>').strip()
        if choose == '3':
            exit_flag = True
            continue
        if choose == '1':
            print('您当前剩余额度为:', BALANCE)
        if choose == '2':
            last_step = False
            while not last_step:
                transfer_card_num = input('[b=back]请输入对方卡号:\n>>>').strip()
                if transfer_card_num == 'b' or transfer_card_num == 'back':
                    last_step = True
                    continue

                else:
                    find = False
                    f = open('credit_card_msg', 'r')
                    for line in f:
                        if line.strip():
                            if line.strip().split()[0] == transfer_card_num:
                                find = True
                                break
                    if not find:
                        print('卡号不存在,请核对再输入')
                        continue
                    f.close()
                    transfer_money = input('[b=back]请输入转账金额\n>>>').strip()
                    if transfer_money == 'b' or transfer_money == 'back':
                        last_step = True
                        continue
                    if transfer_money.isdigit():
                        transfer_money = float(transfer_money)
                        if transfer_money < BALANCE:
                            BALANCE -= transfer_money
                            list1 = []
                            f = open('credit_card_msg', 'r')
                            for line1 in f:
                                if line1.strip():
                                    if line1.strip().split()[0] == dic_['card_num']:
                                        newline = (line1.strip().split()[0] + ' ' + line1.strip().split()[1]
                                                   + ' ' + line1.strip().split()[2] + ' ' + str(BALANCE) +
                                                   ' ' + line1.strip().split()[4] + ' ' +
                                                   line1.strip().split()[5] + '\n')
                                        list1.append(newline)
                                        continue
                                list1.append(line1)
                            f.close()
                            f = open('credit_card_msg', 'w')
                            f.writelines(list1)
                            f.close()
                            list2 = []
                            f = open('credit_card_msg', 'r')
                            for line2 in f:
                                if line2.strip():
                                    if line2.strip().split()[0] == transfer_card_num:
                                        balance = float(line2.strip().split()[3])
                                        balance += transfer_money
                                        newline1 = (line2.strip().split()[0] + ' ' +
                                                    line2.strip().split()[1] + ' '
                                                    + line2.strip().split()[2] + ' '
                                                    + str(balance) + ' ' +
                                                    line2.strip().split()[4] + ' '
                                                    + line2.strip().split()[5] + '\n')
                                        list2.append(newline1)
                                        continue
                                list2.append(line2)
                            f.close()
                            f = open('credit_card_msg', 'w')
                            f.writelines(list2)
                            f.close()
                            now_times = TODAY
                            line_add = '0.0' + ' ' + str(transfer_money) + ' ' + now_times + '\n'
                            if os.path.exists(dic_['card_num'] + '_transaction_detail'):
                                f = open(dic_['card_num'] + '_transaction_detail', 'a')
                                f.write(line_add)
                                f.close()
                            else:
                                f = open(dic_['card_num'] + '_transaction_detail', 'w')
                                line_add_2 = 'cred' + ' ' + 'debt' + ' ' + 'datetime' + '\n'
                                f.write(line_add_2)
                                f.write(line_add)
                                f.close()
                            line_add_transfer_card_num = str(transfer_money) + ' ' + '0.0' + ' ' + now_times + '\n'
                            if os.path.exists(transfer_card_num + '_transaction_detail'):
                                f = open(transfer_card_num + '_transaction_detail', 'a')
                                f.write(line_add_transfer_card_num)
                                f.close()
                            else:
                                f = open(transfer_card_num + '_transaction_detail', 'w')
                                line_add_transfer_card_num_2 = 'cred' + ' ' + 'debt' + ' ' + 'datetime' + '\n'
                                f.write(line_add_transfer_card_num_2)
                                f.write(line_add)
                                f.close()
                            print('您已于', now_times, '向账户', transfer_card_num, '转入金额', transfer_money)
                            print('输入卡号继续转账,输入b或者back返回')
                        else:
                            print('转账金额超过可用余额上限')
                            continue
View Code

 

提现:

def get_cash():
    global BALANCE
    exit_flag = False
    while not exit_flag:
        choose = input('1:查看可用额度,2:提现,3:返回\n>>>')
        if choose == '3':
            exit_flag = True
            continue
        if choose == '1':
            print('您目前可用额度为:', BALANCE)
        if choose == '2':
            cash = input('[b=back]请输入提现金额:')
            if cash == 'b' or cash == 'back':
                continue
            if cash.isdigit():
                cash = float(cash)
                if cash*1.05 <= BALANCE:
                    poundage = cash*0.05
                    money = cash + poundage
                    BALANCE -= money
                    list_ = []
                    f = open('credit_card_msg', 'r')
                    for line in f:
                        if line.strip():
                            if line.strip().split()[0] == dic_['card_num']:
                                newline = (dic_['card_num'] + ' ' + line.strip().split()[1] + ' '
                                           + line.strip().split()[2] + ' ' + str(BALANCE) + ' '
                                           + line.strip().split()[4] + ' ' + line.strip().split()[5] + '\n')
                                list_.append(newline)
                                continue
                        list_.append(line)
                    f.close()
                    f = open('credit_card_msg', 'w')
                    f.writelines(list_)
                    f.close()
                    now_times = TODAY
                    line_add = '0.0' + ' ' + str(money) + ' ' + now_times + '\n'
                    if os.path.exists(dic_['card_num'] + '_transaction_detail'):
                        f = open(dic_['card_num'] + '_transaction_detail', 'a')
                        f.write(line_add)
                        f.close()
                    else:
                        f = open(dic_['card_num'] + '_transaction_detail', 'w')
                        line_add_2 = 'cred' + ' ' + 'debt' + ' ' + 'datetime' + '\n'
                        f.write(line_add_2)
                        f.write(line_add)
                        f.close()
                    print('您已成功提现金额:', cash)
                    print('扣除手续费:', poundage)
                else:
                    print('可用额度不足')
View Code

以上为普通用户的操作

 

后台管理:

@wrapper_credit_login
@wrapper_authority
def manage():
    print('尊敬的admin')
    exit_flag = False
    while not exit_flag:
        choose = input('1:发卡,2:查看用户信息,3:冻结信用卡,4:修改用户密码,5:修改用户额度,6:返回\n>>>')
        if choose == '6':
            exit_flag = True
            continue
        if choose == '1':
            card_add()
        if choose == '2':
            card_user_msg()
        if choose == '3':
            card_frozen()
        if choose == '4':
            user_card_password_change()
        if choose == '5':
            user_card_limit_change()
View Code

 

发卡:

def card_add():
    exit_flag = False
    while not exit_flag:
        choose = input('1:发行普通用户(额度:15000.0),2:发行高级用户(额度:30000.0),'
                       '3:特殊用户(额度:自定义),4:返回\n>>>')
        if choose == '4':
            exit_flag = True
            continue
        if choose == '1':
            card_add_choose('normal')
        if choose == '2':
            card_add_choose('high')
        if choose == '3':
            card_add_choose('special')
View Code

 

冻结信用卡:

def card_frozen():
    exit_flag = False
    while not exit_flag:
        card_num = input('[b=back]请输入要冻结的卡号:\n>>>').strip()
        if card_num == 'b' or card_num == 'back':
            exit_flag = True
            continue
        list_ = []
        find = False
        f = open('credit_card_msg', 'r')
        for line in f:
            if line.strip().split()[0] == card_num:
                card_user_password = line.strip().split()[1]
                limit = line.strip().split()[2]
                balance = line.strip().split()[3]
                authority = line.strip().split()[4]
                status = 'frozen'
                new_line = (card_num + ' ' + card_user_password + ' ' + limit + ' ' + balance
                            + ' ' + authority + ' ' + status + '\n')
                list_.append(new_line)
                find = True
                continue
            list_.append(line)
        f.close()
        if find:
            f = open('credit_card_msg', 'w')
            f.writelines(list_)
            f.close()
            print('卡号', card_num, '已被冻结')
        else:
            print('卡号', card_num, '不存在')
View Code

 

修改用户密码:

def user_card_password_change():
    exit_flag = False
    while not exit_flag:
        card_num = input('[b=back]请输入要修改密码的卡号:\n>>>').strip()
        if card_num == 'b' or card_num == 'back':
            exit_flag = True
            continue
        find = False
        list_ = []
        card_user_password = None
        f = open('credit_card_msg', 'r')
        for line in f:
            if line.strip().split()[0] == card_num:
                flag = True
                find = True
                while flag:
                    card_user_password = input('请输入新密码:').strip()
                    card_user_password_again = input('请再次输入:').strip()
                    if card_user_password == card_user_password_again:
                        limit = line.strip().split()[2]
                        balance = line.strip().split()[3]
                        authority = line.strip().split()[4]
                        status = line.strip().split()[5]
                        new_line = (card_num + ' ' + card_user_password + ' ' + limit
                                    + ' ' + balance + ' ' + authority + ' ' + status + '\n')

                        list_.append(new_line)
                        flag = False
                        continue
                    else:
                        print('两次密码输入不一样')
                        continue
                if not flag:
                    continue

            list_.append(line)
        f.close()
        if find:
            f = open('credit_card_msg', 'w')
            f.writelines(list_)
            f.close()
            print('密码已被修改,请牢记', card_user_password)
        else:
            print('卡号不存在')
View Code

 

修改用户额度:

def user_card_limit_change():
    exit_flag = False
    while not exit_flag:
        card_num = input('[b=back]请输入用户卡号:\n>>>').strip()
        if card_num == 'b' or card_num == 'back':
            exit_flag = True
            continue
        limit = None
        balance = None
        authority = None
        status = None
        find = False
        list_ = []
        f = open('credit_card_msg', 'r')
        for line in f:
            if line.strip().split()[0] == card_num:
                flag = True
                while flag:
                    limit = input('请设置额度:\n>>>')
                    if limit.isdigit():
                        limit = float(limit)
                        limit_before = float(line.strip().split()[2])
                        num = limit - limit_before
                        password = line.strip().split()[1]
                        balance = float(line.strip().split()[3])
                        balance += num
                        authority = line.strip().split()[4]
                        status = line.strip().split()[5]
                        new_line = (card_num + ' ' + password + ' ' + str(limit)
                                    + ' ' + str(balance) + ' ' + authority + ' ' + status + '\n')
                        list_.append(new_line)
                        find = True
                        flag = False
                        continue
                    else:
                        print('额度输入不合法')
                if not flag:
                    continue
            list_.append(line)
        f.close()
        if find:
            f = open('credit_card_msg', 'w')
            f.writelines(list_)
            f.close()
            print('用户额度已重新设置:')
            print('卡号:', card_num, '信用额度:', limit, '可用额度:', balance, '权限:', authority, '信用卡状态:', status)
        else:
            print('卡号不存在')
View Code

以上为管理员操作部分

 

其实这个作业最难处理的应该是出账单,涉及到日期、公式,而且涉及的文件操作也非常繁琐

在指定日期,迭代文件中的用户,就会生成对应的账单

账单函数:

def bill_get(card_num, card_limit, card_balance):
    if os.path.exists(card_num+'_bill'):
        f = open(card_num+'_bill', 'r')
        lines = f.readlines()
        f.close()
        target_line = lines[-1]
        limit = float(target_line.strip().split()[0])  # 额度上限 ,通常不变
        datetime_ = YEAR + '-' + MONTH + '-' + '1'                      # 本期账单日期,固定
        due_date = YEAR+'-' + MONTH + '-' + '15'      # 本期还款日, 固定
        balance_bef = float(target_line.strip().split()[7])  # 上期账单应还金额, 固定
        payments = 0.0                              # 本期存入,可能变化
        new_charge = 0.0                            # 本期支出, 可能变化
        current_balance = 0.0   # 本期应还(实时账单余额),此处可能会变动,balance参数受影响
        min_payment = float(target_line.strip().split()[8])  # 此处为上期账单最小还款额,用完覆盖放入本期
        payments_1 = 0.0    # 还款日之前存入金额
        payments_2 = 0.0    # 还款日之后存入金额
        datetime_last = target_line.strip().split()[2]  # 上个账单日期
        list_cost = []      # 从临时文件中找出上期账单的支出记录分别对应的日期 ,利息的计算= 未还金额 * 天数
        if os.path.exists(card_num + '_temporary'):
            f = open(card_num+'_temporary', 'r')
            for line in f:
                if line.strip():
                    list_cost.append(line)
            f.close()
        if os.path.exists(card_num+'_transaction_detail'):
            min_payment_enough_date = ''  # 找出还够最小还款额的日期 ,以便计算利息天数
            list_temp = []  # 本期交易记录,找出来放入临时文件
            list_ = []
            f = open(card_num+'_transaction_detail', 'r')
            find = False    # 是否找到达到最小还款额的日期
            for line in f:
                if line.strip().startswith('cred'):
                    list_.append(line)
                    continue
                if line.strip().endswith('reckoned'):
                    list_.append(line)
                    continue
                if line.strip():
                    if MONTH == '1':
                        if (line.strip().split()[2]).strip().split('-')[1] == '12' and \
                                        int((line.strip().split()[2]).strip().split('-')[2]) <= 15:
                            new_line = line.strip().split()[0] + ' ' + line.strip().split()[1] + ' ' + \
                                       line.strip().split()[
                                           2] + ' ' + 'reckoned' + '\n'
                            list_.append(new_line)
                            list_temp.append(line)
                            payments_1 += float(line.strip().split()[0])    # 还款日之前存入
                            if not find and payments_1 > min_payment:
                                min_payment_enough_date = line.strip().split()[2]
                                find = True
                            new_charge += float(line.strip().split()[1])
                            continue
                    if (line.strip().split()[2]).strip().split('-')[1] == str(int(MONTH)-1) and \
                                    int((line.strip().split()[2]).strip().split('-')[2]) <= 15:
                        list_temp.append(line)
                        new_line = line.strip().split()[0] + ' ' + line.strip().split()[1] + ' ' + line.strip().split()[
                            2] + ' ' + 'reckoned' + '\n'
                        list_.append(new_line)
                        payments_1 += float(line.strip().split()[0])    # 还款日之前存入
                        if not find and payments_1 > min_payment:
                            min_payment_enough_date = line.strip().split()[2]
                            find = True
                        new_charge += float(line.strip().split()[1])
                        continue
                    new_line = line.strip().split()[0] + ' ' + line.strip().split()[1] + ' ' + line.strip().split()[
                            2] + ' ' + 'reckoned' + '\n'
                    payments_2 += float(line.strip().split()[0])    # 还款日之后存入
                    list_temp.append(line)
                    list_.append(new_line)
                    payments += float(line.strip().split()[0])
                    new_charge += float(line.strip().split()[1])
                    continue
                list_.append(line)
            f.close()
            f = open(card_num+'_temporary', 'w')        # 将本期交易记录放入临时文件
            f.writelines(list_temp)
            f.close()
            f = open(card_num+'_transaction_detail', 'w')
            f.writelines(list_)
            f.close()

            def interest(day_num, money):           # 利息计算公式
                ret = money * day_num * 0.0005
                ret = round(ret, ndigits=2)
                return ret

            if payments_1 > balance_bef:    # 全额还款,计算支出new_charge, 得到应还金额
                current_balance = balance_bef + new_charge - payments_1 - payments_2
            elif payments_1 > min_payment:  # 达到最小还款额,计算(上期应还-payments_1-payments_2+利息+new_charge)得出本期应还
                num = 0.00  # 最低还款日之前利息
                for line in list_cost:
                    cost_money = float(line.strip().split()[1])
                    date_cost = line.strip.split()[2]
                    year = date_cost.strip().split('-')[0]
                    month = date_cost.strip().split('-')[1]
                    day_cost = date_cost.strip().split('-')[2]
                    day1 = datetime.datetime(year, month, day_cost)   # 每一个消费日
                    year_1 = datetime_last.strip().split('-')[0]
                    month_1 = datetime_last.strip().split('-')[1]
                    day_1 = datetime_last.strip().split('-')[2]
                    day2 = datetime.datetime(year_1, month_1, day_1)  # 上一个账单日
                    day_dec = int(day2) - int(day1)
                    num += interest(day_dec, cost_money)   # 每一个消费日到上一个账单日那一天的利息累加  , 如果不存在支出,num 依然等于 0
                year = datetime_last.strip().split('-')[0]
                month = datetime_last.strip().split('-')[1]
                day = datetime_last.strip().split('-')[0]
                day1 = datetime.datetime(year, month, day)  # 上一个账单日
                day2 = datetime.datetime(min_payment_enough_date.split('-')[0],  # 还款日
                                         min_payment_enough_date.split('-')[1],
                                         min_payment_enough_date.split('-')[2])
                day_dec = int(day2) - int(day1)
                num_1 = interest(day_dec, balance_bef)  # 上一个账单日到最低还款额那一天的利息累加
                num += num_1       # 最低还款额那一天之前的总利息

                day3 = datetime.datetime(min_payment_enough_date.split('-')[0],
                                         min_payment_enough_date.split('-')[1],
                                         min_payment_enough_date.split('-')[2])
                year_datetime_ = datetime_.strip().split()[0]
                month_datetime_ = datetime_.strip().split()[1]
                day_datetime_ = datetime_.strip().split()[2]
                day4 = datetime.datetime(year_datetime_, month_datetime_, day_datetime_)
                day_dec = int(day4) - int(day3)
                num2 = interest(day_dec, balance_bef-payments_1)    # 最低还款额日期之后的利息
                interest_all = num + num2           # 总共的利息
                current_balance = balance_bef - payments_1 - payments_2 + interest_all + new_charge   # 本期应还金额
            else:    # 未达到最小还款额,全额罚息,上期应还-payments_1-payments_2+全额利息+(min_payment-payments_1) 的滞纳金 + new_charge,得出本期应还
                num = 0.0
                for line in list_cost:
                    cost_money = float(line.strip().split()[1])
                    date_cost = line.strip.split()[2]
                    year = date_cost.strip().split('-')[0]
                    month = date_cost.strip().split('-')[1]
                    day_cost = date_cost.strip().split('-')[2]
                    day1 = datetime.datetime(year, month, day_cost)   # 每一笔消费的日期
                    year_1 = datetime_last.strip().split('-')[0]
                    month_1 = datetime_last.strip().split('-')[1]
                    day_1 = datetime_last.strip().split('-')[2]
                    day2 = datetime.datetime(year_1, month_1, day_1)  # 上一个账单日
                    day_dec = int(day2) - int(day1)
                    num += interest(day_dec, cost_money)   # 每一笔消费到上一个账单日的利息累加 , 如果不存在支出,num依然等于 0
                year_2 = datetime_last.split('-')[0]
                month_2 = datetime_last.split('-')[1]
                day_2 = datetime_last.split('-')[2]
                day1 = datetime.datetime(year_2, month_2, day_2)      # 上个账单日
                day2 = datetime.datetime(datetime_.split('-')[0],
                                         datetime_.split('-')[1],
                                         datetime_.split('-')[2])       # 这个账单日
                day_dec = int(day2) - int(day1)
                num2 = interest(day_dec, balance_bef)     # 上个账单日到这个账单日的利息
                interest_all = num + num2               # 总利息
                fee_late = (min_payment - payments_1)*0.05  # 滞纳金
                current_balance = balance_bef + interest_all + fee_late + new_charge - payments_1 - payments_2  # 本期应还金额
        if current_balance > 0:
            min_payment = round(current_balance/10, ndigits=2)
        else:
            min_payment = 0.0
        payments = payments_1 + payments_2
        balance = limit - current_balance
        if balance <= 0:
            balance = 0.0
        list_ = []
        f = open('credit_card_msg', 'r')        # 更新balance
        for line in f:
            if line.strip():
                if line.strip().split()[0] == card_num:
                    new_line = (card_num + ' ' + line.strip().split()[1] + ' ' + card_limit + ' ' + str(balance) +
                                ' ' + line.strip().split()[4] + ' ' + line.strip().split()[5] + '\n')
                    list_.append(new_line)
                    continue
            list_.append(line)
        f.close()
        f = open('credit_card_msg', 'w')
        f.writelines(list_)
        print_card_num_bill = card_num+'的本期账单如下'
        print(print_card_num_bill.center(60, '-'))
        print('信用额度' + ' ' + '可用余额' + ' ' + '账单日期' + ' ' + '到期还款日' + ' ' + '上期账单金额'
              + ' ' + '支出总计' + ' ' + '存入总计' + ' ' + '本期应还金额' + ' ' + '最低还款额')
        f.close()
        line_add = (card_limit + ' ' + str(balance) + ' ' + datetime_ + ' ' + due_date + ' '
                    + str(balance_bef) + ' ' + str(new_charge) + ' ' + str(payments) + ' '
                    + str(current_balance) + ' ' + str(min_payment) + '\n')
        print(line_add)
        f = open(card_num+'_bill', 'a')
        f.write(line_add)
        f.close()
    else:
        limit = card_limit
        balance = card_balance
        datetime_ = YEAR+'-'+MONTH+'-'+'1'
        due_date = YEAR+'-'+MONTH+'-'+'15'
        balance_bef = 0.0
        payments = 0.0
        new_charge = 0.0
        current_balance = 0.0
        min_payment = 0.0
        if os.path.exists(card_num+'_transaction_detail'):
            f = open(card_num+'_transaction_detail', 'r')
            list_ = []
            list_1 = []
            for line in f:
                if line.strip().startswith('cred'):
                    list_.append(line)
                    continue
                if line.strip():
                    list_1.append(line)
                    payments += float(line.strip().split()[0])
                    new_charge += float(line.strip().split()[1])
                    new_line = (line.strip().split()[0] + ' ' + line.strip().split()[1] + ' '
                                + line.strip().split()[2] + ' ' + 'reckoned'+'\n')
                    list_.append(new_line)
                    continue
                list_.append(line)
                list_1.append(line)
            f.close()
            with open(card_num+'_transaction_detail', 'w') as f, open(card_num+'_temporary', 'w') as f1:
                f.writelines(list_)
                f1.writelines(list_1)
        if new_charge > payments:
            current_balance = new_charge - payments
            min_payment = round(current_balance/10)
            min_payment = float(min_payment)
        f = open(card_num+'_bill', 'w')
        line = ('limit' + ' ' + 'balance' + ' ' + 'datetime' + ' ' + 'due_date' + ' ' +
                'balance_b/f' + ' ' + 'new_charge' + ' ' + 'payments' + ' ' + 'current_balance'
                + ' ' + ' min_payment' + '\n')
        line_1 = (limit + ' ' + balance + ' ' + datetime_ + ' ' + due_date + ' ' + str(balance_bef) + ' '
                  + str(new_charge) + ' ' + str(payments) + ' ' + str(current_balance) + ' ' + str(min_payment) + '\n')
        f.write(line)
        f.write(line_1)
        f.close()
        print_card_num_bill = card_num + '的本期账单如下'
        print(print_card_num_bill.center(60, '-'))
        print('信用额度' + ' ' + '可用余额' + ' ' + '账单日期' + ' ' + '到期还款日' + ' ' + '上期账单金额'
              + ' ' + '支出总计' + ' ' + '存入总计' + ' ' + '本期应还金额' + ' ' + '最低还款额')
        print(line_1)
        print('------------------------------------------------')
        if os.path.exists(card_num + '_temporary'):
            f = open(card_num+'_temporary', 'r')
            list_2 = []
            for line in f:
                if line.strip():
                    list_2.append(line)
            f.close()
            if list_2:
                print('存入', '支出', '交易日期')
                for line in list_2:
                    print(line.strip())
        else:
            print(card_num, '还没有产生任何交易')
View Code

 

转载于:https://www.cnblogs.com/mountian-lion/p/6081335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值