Python实现银行账户资金交易管理系统
用类和对象实现一个银行账户的资金交易管理, 包括存款、取款和打印交易详情, 交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。
如:
下面按照要求定义一个账户 Account 类。账户 Account 类的属性:
1. 当前账户金额 money
2. 当前账户交易日志 account_logs
账户 Account 类的方法:
1. 存钱 deposit()无返回值
2. 取钱 withdrawl()无返回值
3. 打印交易详情 transaction_log()无返回值
案例代码如下:
#coding: utf-8 import time import prettytable as pt money = 0 acount_logs = [] class Account: def __init__(self): global money self.money = money self.acount_logs = acount_logs def deposit(self): amount = float(input('存入金额:')) self.money += amount self.write_log(amount,'转入') def withdrawl(self): amount = float(input('取出金额:')) if amount > self.money: print('余额不足') else: self.money -= amount self.write_log(amount,'取出') def transaction_log(self): tb = pt.PrettyTable() tb.field_names = ["交易日期","摘要","金额","币种","余额"] for info in self.acount_logs: if info[1] =='转入': amount = '+{}'.format(info[2]) else: amount = '-{}'.format(info[2]) tb.add_row([info[0],info[1],amount,'人民币',info[3]]) print(tb) def write_log(self,amout,handle): create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) data =[create_time,handle,amout,self.money] self.acount_logs.append(data) def show_menu(): """ 显示菜单栏 """ menu = """ ====================银行账户资金交易管理==================== 0: 退出 1:存款 2: 取款 3: 打印交易详情 =========================================================== """ print(menu) if __name__ == '__main__': show_menu() account = Account() while True: choice = int(input("请输入您的选择: ")) if choice == 0: exit(0) print("退出系统") elif choice == 1: flag = True while flag: account.deposit() flag = True if input("是否继续存款(Y|N): ").lower()== 'y' else False elif choice == 2: flag = True while flag: account.withdrawl() flag = True if input("是否继续取款(Y|N): ").lower()== 'y' else False elif choice == 3: account.transaction_log() else: print("请选择正确的编号")
测试结果如下:
====================银行账户资金交易管理==================== 0: 退出 1:存款 2: 取款 3: 打印交易详情 =========================================================== 请输入您的选择: 1 存入金额:300 是否继续存款(Y|N): N 请输入您的选择: 2 取出金额:300 是否继续取款(Y|N): Y 取出金额:100 余额不足 是否继续取款(Y|N): N 请输入您的选择: 3 +---------------------+------+--------+--------+-------+ | 交易日期 | 摘要 | 金额 | 币种 | 余额 | +---------------------+------+--------+--------+-------+ | 2020-01-02 19:53:54 | 转入 | +300.0 | 人民币 | 300.0 | +---------------------+------+--------+--------+-------+ +---------------------+------+--------+--------+-------+ | 交易日期 | 摘要 | 金额 | 币种 | 余额 | +---------------------+------+--------+--------+-------+ | 2020-01-02 19:53:54 | 转入 | +300.0 | 人民币 | 300.0 | | 2020-01-02 19:54:02 | 取出 | -300.0 | 人民币 | 0.0 | +---------------------+------+--------+--------+-------+ 请输入您的选择: 5 请选择正确的编号 请输入您的选择: 0 Process finished with exit code 0
如果可以每次存入和取出钱之后都有余额提示就更友好了!
总结
以上所述是小编给大家介绍的Python实现银行账户资金交易管理系统,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!