python通过银行卡号_python面向对象编程实例---银行账号
轉載自python面向對象編程示例---銀行賬號 | 酷python?www.coolpython.net
在最初接觸面向對象編程時,你會感到有些不習慣,但這種編程范式卻有助于我們思考問題,前提是你準確的理解面向對象這四個字的含義。今天,我以銀行賬戶為例向你展示如何利用面向對象的編程方法來編寫一個銀行賬戶,這個過程中,你將感受到面向對象編程與面向過程編程的不同。
1. 屬性與方法
我需要一個銀行賬戶的類,我將其命名為BankAccount, 它應該有如下的屬性:用戶姓名(username)
賬號(card_no)
余額(balance)
這個類還應該有幾個方法:存錢(deposit)
取錢(withdrawal)
轉賬(transfer)
查看操作記錄(history)
2. 業務分析
在取錢時,如果賬戶余額小于所取金額,那么要提示用戶余額不足,在轉賬的時候同樣如此。在存錢,取錢,轉賬時,都必須將業務操作記錄保存到一個列表中,查看歷史記錄時,遍歷這個列表,這里我不考慮查詢歷史記錄的時間范圍。
3. 代碼實現
3.1 定義類
class BankAccount(object):
def __init__(self, username, card_no, balance):
self.username = username # 用戶姓名
self.card_no = card_no # 賬號
self.balance = balance # 余額
self.history_lst = [] # 歷史操作記錄
def deposit(self, amount):
'''存錢:param amount::return:'''
pass
def withdrawal(self, amount):
'''取錢:param amount::return:'''
pass
def transfer(self, another, amount):
'''轉賬:param another::param amount::return:'''
pass
def history(self):
'''歷史操作記錄:return:'''
pass
如果業務分析已經很透徹,你可以像我這樣一次性將類的全部屬性和方法都寫好,之后逐步完善,這樣做事井井有條,不至于丟三落四。不過你很難一次性想清楚所有細節,因此在編碼過程中,還會逐漸補充。
3.2 實現存錢方法
不論進行何種操作,金額都必須是大于0的,因此,我需要一個判斷金額是否合法的方法
@classmethod
def is_amount_legitimate(amount):
'''判斷金額是否合法:return:'''
if not isinstance(amount, (float, int)):
return False
if amount <= 0:
return False
return True
由于這個方法無需訪問實例屬性,因此我把它定義為類方法,每一次操作,都需要記錄,記錄里必然包括操作發生的時間,因此還需要一個獲取當前時間的方法
@classmethod
def _get_current_time(cls):
now = datetime.now()
current_time = now.strftime('%Y-%m-%d%H:%M:%S')
return current_time
現在,來實現存錢方法
def deposit(self, amount):
'''存錢:param amount::return:'''
if not self.is_amount_legitimate(amount):
print('金額不合法')
return
self.balance += amount
3.3 實現取錢
轉賬,都需要判斷操作的金額是否小于等于賬戶余額,看來,我需要一個方法來完成這件事情
def withdrawal(self, amount):
'''取錢:param amount::return:'''
if not self.is_amount_legitimate(amount):
print('金額不合法')
return
self.balance -= amount
log = "{operate_time}取出金額{amount}".format(operate_time=self._get_current_time(), amount=amount)
self.history_lst.append(log)
3.4 實現轉賬
此前,我只定義了轉賬函數,還缺少一個接收轉賬的函數,這兩個函數一起實現
def transfer(self, another, amount):
'''轉賬:param another::param amount::return:'''
self.balance -= amount
another.accept_transfer(self, amount)
log = '{operate_time}向{username}轉賬{amount}'.format(operate_time=self._get_current_time(),
username=another.username,
amount=amount)
self.history_lst.append(log)
def accept_transfer(self, another, amount):
'''接收轉賬:param another::param amount::return:'''
self.balance += amount
log = '{operate_time}收到{username}轉來的{amount}'.format(operate_time=self._get_current_time(),
username=another.username,
amount=amount)
self.history_lst.append(log)
3.5 查看歷史操作記錄
def history(self):
'''歷史操作記錄:return:'''
for log in self.history_lst:
print(log)
3.6 測試
def test():
account_1 = BankAccount('小明', '248252543', 4932.13)
account_2 = BankAccount('小紅', '429845363', 3211.9)
account_1.deposit(400) # 存錢
account_1.withdrawal(300) # 取錢
account_1.transfer(account_2, 1024) # 轉賬
account_1.history() # 查詢歷史操作記錄
account_2.history()
if __name__ == '__main__':
test()
測試輸出結果
2020-01-09 19:48:01 存入金額400
2020-01-09 19:48:01取出金額300
2020-01-09 19:48:01向小紅轉賬1024
2020-01-09 19:48:01收到小明轉來的1024
總結
以上是生活随笔為你收集整理的python通过银行卡号_python面向对象编程实例---银行账号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hexo-Butterfly音乐播放器的
- 下一篇: 牛学长智能AI图片处理工具,轻松提高工作