import os
import json
import sys
import hashlib
import time as t
sys.path.append('..\core') # 添加到sys.path中
print(sys.path)
import withdraw # 導入withdraw 雖然報錯但是沒關系這只是pycharm的問題
"""
嘗試把上一章的驗證用戶登陸的裝飾器添加到提現和轉賬的功能上。
"""
def login_youhua_hash(func):
"""
登錄裝飾器,文件名:account_hash.json
:return:
"""
def login_decorator(*arg):
count = 1
m = hashlib.md5()
while count < 4: # 循環3次
uesrname = input('請輸入你的用戶名:')
password_ming = (input('請輸入您的密碼:'))
m.update(password_ming.encode('utf-8')) # 將輸入的密碼傳入到md5的對象m中
password = m.hexdigest() # 將16進制形式的密碼賦值給password
mulu_path = os.path.dirname(os.path.dirname(__file__)) # 當前目錄路徑
file_path = os.path.join(mulu_path, uesrname) # 拼接成文件的路徑
try: # 放在try中執行
fr = open(file_path, 'r')
dict_file = json.load(fr)
fr.close()
guoqishijian_tuple = t.strptime(dict_file['expire_date'], '%Y-%m-%d') # 時間 元祖
guoqishijian = t.mktime(guoqishijian_tuple) # 過期時間的時間戳
now_time = t.time() # 當前時間的 時間戳
if dict_file['status'] == 0: # 判斷是否狀態為0
if password == dict_file['password']: # 判斷是否登陸成功
if now_time < guoqishijian: # 判斷是否過期
print('登陸成功')
func(*arg)
break
else:
print('已過期')
break
else:
if count == 3: # 如果輸入了三次將dict_file['status'] = 1寫入文件
print('賬號已鎖定')
with open(file_path, 'w', encoding='utf-8')as f:
dict_file['status'] = 1
json.dump(dict_file, f)
break
else: # 否則提示密碼錯誤
print('密碼錯誤')
else:
print('已鎖定') # 否則提示已過期退出程序
break
except Exception as e:
print('出錯了{}'.format(e.args))
count += 1 # count放在這里,如果文件名輸入錯誤三次也退出
return login_decorator
"""
最近alex買了個Tesla Model S,通過轉賬的形式,並且支付了5%的手續費,tesla價格為95萬。賬戶文件為json,請用程序實現該轉賬行為。
需求如下:
目錄結構為
.
├── account
│ ├── alex.json
│ └── tesla_company.json
└── bin
└── start.py
當執行start.py時,出現交互窗口
———- ICBC Bank ————-.
1. 賬戶信息
2. 轉賬
選擇1 賬戶信息 顯示alex的當前賬戶余額。
選擇2 轉賬 直接扣掉95萬和利息費用並且tesla_company賬戶增加95萬
"""
@login_youhua_hash
def bug_car(alex_yu_e, tesla_monkey):
"""
不同目錄購買特斯拉
:param alex_yu_e: 你的余額
:param tesla_monkey: 購買特斯拉所需要的錢
:return:
"""
select_num = input(
(
"""
———- ICBC Bank ————-.
1. 賬戶信息
2. 轉賬
""")
)
now_mulu_path = os.path.dirname(os.path.dirname(__file__)) # 當前所在目錄
alex_path = os.path.join(now_mulu_path, r'account\alex.json') # 你的余額所在json文件路徑
tesla_path = os.path.join(now_mulu_path, r'account\tesla_company.json') # 特斯拉賬戶所在json文件路徑
with open(alex_path, 'w', encoding='utf-8')as f:
json.dump({"alex_balace": alex_yu_e}, f) # 寫入余額
with open(alex_path, 'r')as f:
yue_dict = json.load(f) # 將余額讀取出來dict
if select_num == str(1):
print('alex當前余額:{}'.format(yue_dict['alex_balace'])) # 選擇1時返回你的余額
elif select_num == str(2): # 選擇2時將從你的余額扣除,特斯拉賬戶相應增加
buy_yue = (int(yue_dict['alex_balace']) - tesla_monkey) - (tesla_monkey * 0.05) # 你購買 特斯拉+稅后 還剩下余額
if buy_yue >= 0 :
with open(alex_path, 'w', encoding='utf-8')as f: # 寫入還剩下的余額
json.dump({"alex_balace": buy_yue}, f)
with open(tesla_path, 'w', encoding='utf-8')as f: # 往特斯拉賬戶寫入余額
json.dump({"tesla_balace": tesla_monkey}, f)
print('購買tesla成功')
else:
print('你的余額不夠買一輛特斯拉,窮鬼!!!')
bug_car(1000000, 950000)
# withdraw.withdraw_start(yue=1000000, line_of_credit=500000, withdraw_monkey=450000)
# print(os.path.dirname(os.path.dirname(__file__)))
import os
import json
import sys
sys.path.append(r'..\bin')# 將bin目錄添加到path中
"""
對上題增加一個需求:提現。
目錄結構如下
.
├── account
│ └── alex.json
│ └── tesla_company.json
├── bin
│ └── start.py
└── core
└── withdraw.py
當執行start.py時,出現交互窗口
———- ICBC Bank ————-
1. 賬戶信息
2. 提現
選擇1 賬戶信息 顯示alex的當前賬戶余額和信用額度。
選擇2 提現 提現金額應小於等於信用額度,利息為5%,提現金額為用戶自定義。
體現代碼的實現要寫在withdraw.py里
"""
def withdraw_start(yue, line_of_credit, withdraw_monkey):
"""
實現一個提現的需求
:param yue: 當前余額
:param line_of_credit: 信用額度
:param withdraw_monkey: 提現金額
:return:
"""
select_num = input(
"""
———- ICBC Bank ————-
1. 賬戶信息
2. 提現
"""
)
yue, line_of_credit, withdraw_monkey = int(yue), int(line_of_credit), int(withdraw_monkey) # 類型轉換
now_on_mulu = os.path.dirname(os.path.dirname(__file__))
alex_path = os.path.join(now_on_mulu, r'account\alex.json')
with open(alex_path, 'w', encoding='utf-8')as f:
json.dump({'alex_balace': yue, 'line_of_credit': line_of_credit}, f) # 寫入余額,信用額度
with open(alex_path, 'r')as f:
alex_dict = json.load(f)
alex_yue = alex_dict['alex_balace'] # 你的當前余額
alex_line_of_credit = alex_dict['line_of_credit'] # 你的信用額度
if select_num == str(1):
print('alex當前余額:{}'.format(alex_yue))
elif select_num == str(2):
if str(withdraw_monkey) > str(alex_line_of_credit): # 判斷提現金額是否小於信用額度
print('你的胃口也太大了吧 ,不知道你的信用額度就這么點么!!!')
else:
alex_withdraw_monkey = alex_yue + withdraw_monkey # 提現后的金額=當前余額+提現金額
alex_line_of_credit = line_of_credit - withdraw_monkey - (withdraw_monkey * 0.05) # 提現后信用額度=信用額度-提現金額-利息
with open(alex_path, 'w', encoding='utf-8')as f:
json.dump({'alex_balace': alex_withdraw_monkey, 'line_of_credit': alex_line_of_credit}, f)
print("""
提現成功:{}
當前賬戶余額:{}
當前信用額度:{}
""".format(withdraw_monkey, alex_withdraw_monkey, alex_line_of_credit))
else:
print('輸入錯誤了哈!,認真讀題~~~')
withdraw_start(yue=1000000, line_of_credit=500000, withdraw_monkey=450000)
