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

下面按照要求定義一個賬戶 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,'轉(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("請輸入您的選擇: "))
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("請選擇正確的編號")
測試結(jié)果如下:
====================銀行賬戶資金交易管理==================== 0: 退出 1:存款 2: 取款 3: 打印交易詳情 =========================================================== 請輸入您的選擇: 1 存入金額:300 是否繼續(xù)存款(Y|N): N 請輸入您的選擇: 2 取出金額:300 是否繼續(xù)取款(Y|N): Y 取出金額:100 余額不足 是否繼續(xù)取款(Y|N): N 請輸入您的選擇: 3 +---------------------+------+--------+--------+-------+ | 交易日期 | 摘要 | 金額 | 幣種 | 余額 | +---------------------+------+--------+--------+-------+ | 2020-01-02 19:53:54 | 轉(zhuǎn)入 | +300.0 | 人民幣 | 300.0 | +---------------------+------+--------+--------+-------+ +---------------------+------+--------+--------+-------+ | 交易日期 | 摘要 | 金額 | 幣種 | 余額 | +---------------------+------+--------+--------+-------+ | 2020-01-02 19:53:54 | 轉(zhuǎn)入 | +300.0 | 人民幣 | 300.0 | | 2020-01-02 19:54:02 | 取出 | -300.0 | 人民幣 | 0.0 | +---------------------+------+--------+--------+-------+ 請輸入您的選擇: 5 請選擇正確的編號 請輸入您的選擇: 0 Process finished with exit code 0
如果可以每次存入和取出錢之后都有余額提示就更友好了!
總結(jié)
以上所述是小編給大家介紹的Python實現(xiàn)銀行賬戶資金交易管理系統(tǒng),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
VS2022安裝Python開發(fā)環(huán)境的詳細過程
這篇文章主要介紹了VS2022安裝Python開發(fā)環(huán)境,文中用Python實現(xiàn)裴波那契數(shù)列,來感受一下Python的魅力,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-08-08
關(guān)于Python 多重繼承時metaclass conflict問題解決與原理探究
這篇文章主要介紹了Python 多重繼承時metaclass conflict問題解決與原理探究 ,需要的朋友可以參考下2022-10-10
Python EOL while scanning string literal問題解決方法
這篇文章主要介紹了Python EOL while scanning string literal問題解決方法,本文總結(jié)出是數(shù)據(jù)庫數(shù)據(jù)出現(xiàn)問題導(dǎo)致這個問題,需要的朋友可以參考下2015-04-04
django 文件上傳功能的相關(guān)實例代碼(簡單易懂)
這篇文章主要介紹了django 文件上傳功能的相關(guān)實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
接口自動化多層嵌套json數(shù)據(jù)處理代碼實例
這篇文章主要介紹了接口自動化多層嵌套json數(shù)據(jù)處理代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
Python批量實現(xiàn)Word/EXCEL/PPT轉(zhuǎn)PDF
在日常辦公和文檔處理中,有時我們需要將多個Word文檔、Excel表格或PPT演示文稿轉(zhuǎn)換為PDF文件,本文將介紹如何使用Python編程語言批量實現(xiàn)將多個Word、Excel和PPT文件轉(zhuǎn)換為PDF文件,需要的可以參考下2023-09-09

