作业!ATM功能

好几天没更新博客了,清明节的时候玩了几天,节后就开始写这个作业!

作业要求

配置程序

 1 #这个脚本用来生成account_reserve文件,该文件存储了3个账号信息。
 2 import json
 3 
 4 account_bao={
 5     '7654321':{
 6         'accuntID':'qiangql',
 7         'accpassword':'1234aaa',
 8         'phone':18660293125,
 9         'balance':15000,
10         'debt':500,
11         'interests':0,
12         'expire_date':'2020-01-01',
13         'start_date':'2010-01-01',
14         'islocked':0,
15         'login status':0
16 
17     },
18     '1234567':{
19         'accuntID': 'warpat',
20         'accpassword': '1234',
21         'phone':15866841111,
22         'balance': 212,
23         'debt':10033,
24         'interests': 00,
25         'expire_date': '2020-01-01',
26         'start_date': '2010-01-01',
27         'islocked':0,
28         'login status':0
29     },
30     '1234568':{
31         'accuntID':'egbert',
32         'accpassword':'1234aaa',
33         'phone':15000003013,
34         'balance':10000,
35         'debt':20,
36         'interests':0,
37         'expire_date':'2020-01-01',
38         'start_date':'2010-01-01',
39         'islocked':0,
40         'login status':0
41 
42     },
43 
44 }
45 f=open('accont_reserve.text','w')
46 f.write(json.dumps(account_bao))
47 
48 f.close()
49 
50 # f_account_1=open('7654321.text','w')
51 # f_account_1.write(json.dumps())
52 #
53 # f_account_2=open('1234567.text','w')
54 # f_account_2.write(json.dumps())
55 #
56 # f_account_3=open('1234568.text','w')
57 # f_account_3.write(json.dumps())
这个脚本用来生成account_reserve文件,该文件存储了3个账号信息
 1 #这个脚本用来生成account_log文件,该文件用来写入账号流逝,也存储了3个账号信息。
 2 import json
 3 
 4 a={
 5     '7654321':  {
 6         'log'}
 7 
 8 
 9     ,
10 '1234567':{
11         'log':''}
12 
13     ,
14  '1234568':{
15         'log':''}
16 
17 
18 
19 }
20 
21 f=open('account_log.text','w')
22 f.write(json.dumps(a))
23 
24 f.close()
用来生成流水文档,用来保存流水

主程序

  1 #这是我自学一个月的成果,ATM信用卡系统
  2 #原来目标是应该做成规范的软件目录结构,但是不太熟练,我就都写在一起了,后期学的多了,我会再重新做一遍
  3 import json
  4 import sys
  5 import time
  6 
  7 def login():
  8     '''
  9     这个函数的目的是用来登录,运行后立刻执行,
     验证的内容有1,是否是数据库的卡号,2,账号是否被锁,3,三次登录锁定功能
10 这里边我声明了3个全局变量,这里做的不好,我以后会把他们做到类里,这里先这样吧。 11 :return: 12 ''' 13 global dic_account #声明了全局变量,让登录后的账号可以再整个程序识别 14 global input_account #同理 15 f = open('accont_reserve.text', 'r') 16 dic_account = json.load(f) 17 log_account = 0 #统计账号登录次数的,我们要小于3次 18 log_password =0 #统计密码登录次数的,也是小于3次 19 global log_success_flage #这里又声明了全局变量,为了后边的登录后调用
                     global dic_account,dic_account 用的。
20 log_success_flage = False #登录是否成功的flage 21 input_account_flage = False #这里没用到,好像是用来检测登录状态的,但是这里我没做 22 while log_account<3 and log_password<3: 23 input_account =input('请输入账号\n') 24 input_accpassword = input('请输入密码') 25 for i in dic_account : 26 # print (i) 27 break 28 if input_account in dic_account: 29 input_account_flage =True 30 log_account +=1 31 32 if dic_account[input_account]['islocked']==0: #检测账号是否被锁 33 34 if input_accpassword == dic_account[input_account]['accpassword'] : 35 print('登录成功') 36 dic_account[input_account]['login status'] =1 #登录成功,状态为1,
                                           为装饰器做准备,但是我还没做…
37 f = open('accont_reserve.text', 'w+') 38 f.write(json.dumps(dic_account)) 39 40 # name=dic_account[input_account]['accuntID'] 41 # balance=dic_account[input_account]['balance'] 42 # interests=dic_account[input_account]['interests'] 43 # phone=dic_account[input_account]['phone'] 44 log_success_flage=True 45 # return log_success_flage 46 break 47 else: 48 print('登录密码不对') 49 log_password +=1 50 51 else: 52 print('账号为被锁号') 53 break 54 elif input_account not in dic_account: 55 print('账号不存在') 56 57 else: 58 print('请注意你的账号将要被锁') 59 time.sleep(2) #为了表示严肃性,我加了2秒延迟 60 dic_account[input_account]['islocked']=1 61 f = open('accont_reserve.text', 'w+') 62 f.write(json.dumps(dic_account)) 63 print('登录3次账号被锁') 64 return log_success_flage,dic_account 65 # 66 # f = open('accont_reserve.text', 'r') 67 # dic_account = json.load(f) 68 69 70 def account_menu(): #用户信息 71 ''' 72 这个函数是登录成功后,显示对应的用户信息,
     log函数声明的全局变量就在这里用到了
73 :return: 74 ''' 75 name=dic_account[input_account]['accuntID'] 76 balance=dic_account[input_account]['balance'] 77 debt=dic_account[input_account]['debt'] 78 interests=dic_account[input_account]['interests'] 79 phone=dic_account[input_account]['phone'] 80 print(name) 81 print('持有人%s'%name) 82 print('额度%s'%balance) 83 print('欠款%s'%debt) 84 # print(type(balance)) 85 print('手续费%s'%interests) 86 print('手机号%s'%phone) 87 # f_account=open(input_account.text,'w') 88 # f_account.write(json.dumps()) 89 # print(type(balance)) 90 91 def withdraw(): #提现 92 ''' 93 这个是用来提款,提款的额度是信用卡额度的二分之一,并且收取5%的手续费, 94 同时打印出提款数额,手续费,总共手续费,剩余额度这四个信息。 95 最后讲上述信息打印到account_log.text文件。 96 :return: 97 ''' 98 balance = dic_account[input_account]['balance'] 99 interests = dic_account[input_account]['interests'] 100 debt = dic_account[input_account]['debt'] 101 102 limit = balance / 2 103 print('提现的额度是%s' % limit) 104 input_withraw=int(input('请输入提现金额')) 105 106 # print(type(input_withraw)) 107 if input_withraw <=limit: 108 interests_this_time = input_withraw*0.05 109 balance = balance - input_withraw - interests_this_time 110 interests += interests_this_time 111 dic_account[input_account]['interests']=interests 112 dic_account[input_account]['balance']=balance 113 debt += input_withraw 114 dic_account[input_account]['debt']=debt 115 f = open('accont_reserve.text', 'w+') 116 f.write(json.dumps(dic_account)) 117 time.sleep(2) #这里也是为了庄重,加了2秒延迟 118 a=('\n你已成功提现%s,本次产生手续费%s,共产生手续费%s,现在的额度还有%s,你的欠款为%s\n' % (input_withraw,interests_this_time,interests,balance,debt)) 119 print(a) #这个a就是用来传参的 120 log_menu(a) #这里调用了log_menu 函数,目的是打印账号明细 121 122 # data_log[input_account]['log'] =time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 123 # account_log[input_account]['log'][0] +=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 124 # f_log.write(json.dumps(account_log)) 125 126 def transfer(): #转账 127 ''' 128 这个用来转账,可判断对方账号是否存在account_reserve数据文件,判断对方账号是否被锁,如果账号正常提示账号的所有人, 129 转账金额小于额度,同时打印出对方账号,转入金额,剩余额度,以及信用卡欠款, 130 并将相关流水储存。 131 :return: 132 ''' 133 balance = dic_account[input_account]['balance'] 134 debt = dic_account[input_account]['debt'] 135 transfer_accountID=input('请输入对方卡号') 136 if transfer_accountID in dic_account: 137 print('对方账号为%s'%dic_account[transfer_accountID]['accuntID']) 138 if dic_account[transfer_accountID]['islocked'] == 0: 139 transfer_money = int(input('请输入转账金额')) 140 if transfer_money < balance: 141 # print('111') 142 balance = balance - transfer_money 143 dic_account[transfer_accountID]['balance'] = dic_account[transfer_accountID]['balance'] + transfer_money 144 dic_account[input_account]['balance'] = balance 145 debt += transfer_money 146 dic_account[input_account]['debt']=debt 147 f = open('accont_reserve.text', 'w+') 148 f.write(json.dumps(dic_account)) 149 150 time.sleep(2) 151 a=('\n你已成功转入%s账户%s金额,你的额度还剩下%s,你的欠款为%s\n' %(transfer_accountID, transfer_money, balance,debt)) 152 print(a) 153 log_menu(a) 154 else: 155 print('您的余额不够') 156 else: 157 print('对方账号异常,无法转账') 158 else: 159 print('该账号不存在') 160 161 def repayment(): #还钱 162 ''' 163 有借有还再借不难,这个用来还钱, 164 先打印欠款金额,还款金额应该小于欠款金额,毕竟这是信用卡 165 如果还款金额大于应该会扣下有效的金额其余的退换,但是没做…… 166 :return: 167 ''' 168 debt = dic_account[input_account]['debt'] 169 print('你的欠款金额是%s'%debt) 170 repay_money=int(input('请输入还款金额')) 171 if repay_money<=debt: 172 debt = debt - repay_money 173 dic_account[input_account]['debt']=debt 174 f = open('accont_reserve.text', 'w+') 175 f.write(json.dumps(dic_account)) 176 time.sleep(1) #为了表示还款的严肃性!加了1秒的延迟 177 a=('\n你已成功还款%s,您还欠款%s\n'%(repay_money,debt)) 178 print(a) 179 log_menu(a) 180 else: 181 print('我们这是信用卡,不是借记卡,不能帮你保管钱') 182 183 def log_menu(a): #流水 184 ''' 185 z这个是保存流水,记录操作时间,操作内容,并且存入流水文件, 186 ***这是我第一个有参数的函数***超吊 187 :param a: 188 :return: 189 ''' 190 log_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) 191 f = open('account_log.text', 'r', encoding='utf-8') 192 dic = eval(f.read()) 193 f.close() 194 b = str(log_time) 195 dic[input_account]['log'] += b 196 dic[input_account]['log'] += a 197 198 with open('account_log.text', 'w', encoding='utf-8')as s: 199 s.write(str(dic)) 200 201 def print_log_menu(): 202 ''' 203 调用该函数会打印流水 204 :return: 205 ''' 206 f = open('account_log.text', 'r', encoding='utf-8') 207 208 dic = eval(f.read()) 209 f.close() 210 211 print(dic[input_account]['log']) 212 213 214 215 login() #启用登录功能,应该做个装饰器,检验登录状态的,还没完成 216 217 menu_while_flage = False 218 while not menu_while_flage: 219 if log_success_flage == True: 220 print('1,我的信用卡') #这里很low,可以做成字典的形式 221 print('2,提现') 222 print('3,转账') 223 print('4,还款') 224 print('5,账单') 225 print('6,返回') 226 function_chose=input('请选择相应的功能') 227 228 if function_chose =='1': 229 account_menu() 230 231 if function_chose == '2': #提现 232 # limit = balance / 2 233 # print('提现的额度是%s' % limit) 234 withdraw() 235 236 if function_chose == '3':#转账 237 transfer() 238 239 if function_chose == '4':#还款 240 repayment() 241 242 if function_chose == '5':#清单 243 print_log_menu() 244 245 246 if function_chose == 'b': 247 break 248 elif function_chose == '6': 249 menu_while_flage =True 250 251 #写在这里的话,这是我学习一个月做的第一个小项目,刚开始总会遇到困难,但是都慢慢的克服了, 252 #近三十岁学编程,有点晚,但是只要在行动,就不算晚。 253 254 # while True: 255 # for i in dic_account: 256 # if input_account == dic_account[input_account]: 257 # while True: 258 # for i2 in dic_account[input_account]: 259 # if input_accpassword == dic_account[input_account[input_accpassword]]: 260 # print('登录成功') 261 # break 262 # else: 263 # print('登录失败') 264 # break 265 # def account_menu(): 266 # if log_success_flage = True: 267 # print(name) 268 # print(balance) 269 # print(interests) 270 # print(phone)

一些测试代码

#这里我遇到了困难,json反序列化老是保存,我英文不好,百度的都看不懂,后来改用了eval!
#今后会常用json,慢慢学习
import json
import time
f=open('account_log.text','r',encoding='utf-8')

dic=eval(f.read())
f.close()
print(dic)
dic['7654321']['log']+='3131\n'
dic['7654321']['log']+='31341\n'
dic['7654321']['log']+='www\n'
print(dic)
print(dic['7654321']['log'])
with open('account_log.text','w',encoding='utf-8')as s:
    s.write(str(dic))

# data_log=json.load(f_log)
# data_log["7654321"]["log"][0] += ",可以用爬虫爬下来33\n"
# # print(data_log["7654321"]["log"])
# # data_log=json.load(f_log['7654321']['log'])
# a=data_log['7654321']['log'].append('333')
# print(a)
# json.dump(data_log,f_log)
# print(a)
# a= print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
# data_log['7654321']['log'] =a
# # f_log.writ(json.dumps(data_log))
# print(data_log['log'])

# def log_menu(a): #写入log日志的方法
#     log_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#     f_log = open('account_log.text', 'r+')
#     data_log = json.load(f_log)
#     data_log[input_account]['log'] = log_time + a
#     f_log.write(json.dumps(data_log))
#
# def print_log_menu():
#     f_log=open('account_log.text','r')
#     data_log=json.load(f_log)
#     print(data_log[input_account]['log'])
#     pass

原本的主程序入口

import os
import sys
import json

BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from conf import settings
from core import main
from core import auth


auth.login()

执行后的程序截图

功能区

 

这个程序看上去很low,后来写完了才明白过来应该做成上边的样子,但是此程序是我肚子泄的,开心。

等我学成归来重做之时,就做成上边的样子,用规范目录。   #都喜欢用倒装句啊山东人

posted @ 2018-04-11 20:18  人无远虑  阅读(360)  评论(0编辑  收藏  举报