用類和對(duì)象實(shí)現(xiàn)一個(gè)銀行賬戶的資金交易管理, 包括存款、取款和打印交易詳情, 交易詳情中包含每次交易的時(shí)間、存款或者取款的金額、每次交易后的余額。
如:
下面按照要求定義一個(gè)賬戶 Account 類。賬戶 Account 類的屬性:
1. 當(dāng)前賬戶金額 money
2. 當(dāng)前賬戶交易日志 account_logs
賬戶 Account 類的方法:
1. 存錢(qián) deposit()無(wú)返回值
2. 取錢(qián) withdrawl()無(wú)返回值
3. 打印交易詳情 transaction_log()無(wú)返回值
案例代碼如下:
#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,'轉(zhuǎn)入') 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] =='轉(zhuǎn)入': 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("請(qǐng)輸入您的選擇: ")) if choice == 0: exit(0) print("退出系統(tǒng)") elif choice == 1: flag = True while flag: account.deposit() flag = True if input("是否繼續(xù)存款(Y|N): ").lower()== 'y' else False elif choice == 2: flag = True while flag: account.withdrawl() flag = True if input("是否繼續(xù)取款(Y|N): ").lower()== 'y' else False elif choice == 3: account.transaction_log() else: print("請(qǐng)選擇正確的編號(hào)")
分享題目:Python實(shí)現(xiàn)銀行賬戶資金交易管理系統(tǒng)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article48/ddhchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、微信小程序、品牌網(wǎng)站制作、電子商務(wù)、建站公司、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容