用類和對象實現一個銀行賬戶的資金交易管理, 包括存款、取款和打印交易詳情, 交易詳情中包含每次交易的時間、存款或者取款的金額、每次交易后的余額。
如:

下面按照要求定義一個賬戶 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("請選擇正確的編號")
分享題目:Python實現銀行賬戶資金交易管理系統-創新互聯
網站路徑:http://www.yijiale78.com/article48/ddhchp.html
成都網站建設公司_創新互聯,為您提供網站導航、微信小程序、品牌網站制作、電子商務、建站公司、服務器托管
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯