码迷,mamicode.com
首页 > 其他好文 > 详细

ATM的流程的实现

时间:2017-11-08 21:25:20      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:else   atm   时间   信息   new   lan   user   返回   sdi   

  1 import os,time
  2 #登录选择
  3 def login():
  4     welcome =‘‘‘
  5 ----欢迎使用自动取款机系统-----
  6 操作指南:
  7     【1】用户登录
  8     【2】管理员登录
  9     【3】退出系统 
 10     ‘‘‘
 11     while True:
 12         print(welcome)
 13         choice_num =input(请输入指令序号:).strip()
 14         if choice_num.isdigit() and choice_num in [1,2,3]:
 15             return choice_num
 16         else:
 17             print(您的输入指令有误,请重新选择!)
 18             continue
 19 #用户登录
 20 def user_login(user_name,user_pwd,count): #传入用户名、密码、和输入次数
 21     with open(user_locked.txt,a+,encoding=utf8) as f1:
 22         if count >=2: #判断用户是否被冻结
 23             f1.write(\n+user_name)
 24         f1.seek(0)
 25         for line in f1:
 26             if user_name in line:
 27                 print(该账户已经被冻结!)
 28                 return 2
 29     with open(user_info.txt,r,encoding=utf8) as f2:
 30         for line in f2:
 31             each_line =line.split(:)
 32             if each_line[1].startswith(user_name) and each_line[2].startswith(user_pwd):#验证用户名和密码
 33                 print(用户登录成功!)
 34                 return 1
 35         else:
 36             print(账户名或密码错误,请重新输入!)
 37             return 0
 38 
 39 #用户登录操作
 40 def user_welcome():
 41     user_welcome =‘‘‘
 42     操作说明:
 43     【取款】:1  【余额查询】:4
 44     【存款】:2  【定期还款】:5
 45     【转账】:3  【显示账单】:6
 46     【退出】:Q
 47     ‘‘‘
 48     print(user_welcome)
 49 
 50 #管理员登录
 51 def root_login():
 52     ‘‘‘
 53     系统默认管理员名字:root  密码:123
 54     :return:
 55     ‘‘‘
 56     while True:
 57         user_name = input(请输入管理员用户名,默认root(或者q退出):\n>>>).strip()
 58         if user_name.lower() == q:
 59             return 0                     #退出返回0
 60         password = input(请输入管理员密码,默认123:\n>>>).strip()
 61         if user_name.lower() == root and password == 123:
 62             print(您已获得管理者权限!)
 63             return 1                     #成功返回1
 64         else:
 65             print(请重新输入!)
 66             continue
 67 #管理员登录
 68 def root_welcome():
 69     root_welcome =‘‘‘
 70     操作说明:
 71     【添加用户】:1
 72     【冻结用户】:2
 73     【设定额度】:3
 74     【退出】:Q
 75     ‘‘‘
 76     print(root_welcome)
 77 
 78 #用户取款
 79 def withdrawal(user_name):
 80     while True:
 81         get_money =input("请输入取款数额【退出Q】【返回R】:").strip()
 82         if get_money.lower() ==r:
 83             return
 84         if get_money.lower() ==q:
 85             exit()
 86         elif get_money.isdigit():
 87                 f1 = open(user_info.txt, r, encoding=utf8)
 88                 f2 = open(new_user_info.txt, w, encoding=utf8)
 89                 f1.seek(0)
 90                 for line in f1:
 91                     if user_name in line:
 92                         if int(line.split(:)[3][:-5]) >int(get_money):
 93                             balance =int(line.split(:)[3][:-5])-int(get_money)
 94                             line =line.replace(str(line.split(:)[3][:-5]),str(balance))
 95                             print(取款成功!余额为%s%balance)
 96                         else:
 97                             print(余额不足!)
 98                     f2.write(line)
 99                 f1.close()
100                 f2.close()
101                 os.remove(user_info.txt)
102                 os.rename(new_user_info.txt,user_info.txt)
103         else:
104             print(请输入正确的金额!)
105 
106 #用户存款
107 def deposit(user_name):
108     while True:
109         give_money =input("请输入存款数额【退出Q】【返回R】:").strip()
110         if give_money.lower() ==r:
111             return
112         if give_money.lower() ==q:
113             exit()
114         elif give_money.isdigit():
115                 f1 = open(user_info.txt, r, encoding=utf8)
116                 f2 = open(new_user_info.txt, w, encoding=utf8)
117                 f1.seek(0)
118                 for line in f1:
119                     if user_name in line:
120                         balance =int(line.split(:)[3][:-5])+int(give_money)
121                         line =line.replace(str(line.split(:)[3][:-5]),str(balance))
122                         print(存款成功!余额为%s%balance)
123                     f2.write(line)
124                 f1.close()
125                 f2.close()
126                 os.remove(user_info.txt)
127                 os.rename(new_user_info.txt,user_info.txt)
128         else:
129             print(请输入正确的金额!)
130 
131 #用户转账
132 def trans_accounts(user_name):
133     while True:
134         flag = False
135         trans_name =input(请输入收款人账户名【退出Q】【返回R】:).strip()
136         if trans_name.lower() ==r:
137             return
138         if trans_name.lower() ==q:
139             exit()
140         f1 = open(user_info.txt, r, encoding=utf8)
141         for line in f1:
142             if trans_name in line and trans_name !=user_name:
143                     flag =True
144         f1.close()
145         if flag:
146             while True:
147                 trans_money = input("请输入转账数额【退出Q】【返回R】:").strip()
148                 if trans_money.lower() == q:
149                     exit()
150                 if trans_money.lower() == r:
151                     return
152                 elif trans_money.isdigit():
153                     msg_insure =input(确定Y,取消N,退出Q:)
154                     if msg_insure.lower() ==y:
155                         f1 = open(user_info.txt, r, encoding=utf8)
156                         f2 = open(new_user_info.txt, w, encoding=utf8)
157                         for line in f1:
158                             if user_name in line:
159                                 balance = int(line.split(:)[3][:-5]) - int(trans_money)
160                                 line = line.replace(str(line.split(:)[3][:-5]), str(balance))
161                             if trans_name in line:
162                                 balance = int(line.split(:)[3][:-5]) + int(trans_money)
163                                 line = line.replace(str(line.split(:)[3][:-5]), str(balance))
164                             f2.write(line)
165                         f1.close()
166                         f2.close()
167                         os.remove(user_info.txt)
168                         os.rename(new_user_info.txt, user_info.txt)
169                         print(转账成功!)
170                     elif msg_insure ==q:
171                         exit()
172                     else:
173                         continue
174                 else:
175                     print(请输入正确的金额!)
176         else:
177             print(收款人不存在,请重新输入!)
178 
179 #余额查询
180 def balance_search(user_name):
181     with open(user_info.txt, r, encoding=utf8) as f:
182         for line in f:
183             if user_name in line:
184                 balance = int(line.split(:)[3][:-5])
185                 print(您的余额为%s!%balance)
186         return
187 
188 #定期还款
189 def reg_payment(user_name):
190     while True:
191         with open(user_info.txt, r, encoding=utf8) as f:
192             for line in f:
193                 if user_name in line:
194                     debt = int(line.split(:)[-1])
195                     print(您的欠款为%s! % debt)
196         set_money =input(请输入您的每次分期还款金额【退出Q】【返回R】:).strip()
197         if set_money.lower() ==r:
198             return
199         if set_money.lower() ==q:
200             exit()
201         set_freq =input(请输入您的分期还款次数(分期金额*次数应小于欠款数额,否则后果自负!):).strip()
202         count = 0
203         if set_money.isdigit() and set_freq.isdigit():
204             for i in range(int(set_freq)):
205                 f1 = open(user_info.txt, r, encoding=utf8)
206                 f2 = open(new_user_info.txt, w, encoding=utf8)
207                 for line in f1:
208                     if user_name in line:
209                         debt = int(line.split(:)[-1]) - int(set_money)
210                         line = line.replace(str(line.split(:)[-1]), str(debt)+\n)
211                         balance = int(line.split(:)[3][:-5]) - int(set_money)
212                         line = line.replace(str(line.split(:)[3][:-5]), str(balance))
213                     f2.write(line)
214                 f1.close()
215                 f2.close()
216                 os.remove(user_info.txt)
217                 os.rename(new_user_info.txt, user_info.txt)
218                 count +=1
219                 time.sleep(5)  # 设定分期还款间隔时间,这里默认每隔5秒还款一次
220                 print(已经还款%s期!%count)
221             print(分期还款完成!)
222             with open(user_info.txt, r, encoding=utf8) as f:
223                 for line in f:
224                     if user_name in line:
225                         debt = int(line.split(:)[-1])
226                         print(您剩余欠款为%s! % debt)
227                         return
228         else:
229             print(请重新输入!)
230 
231 #显示账单
232 def account_query(user_name):
233     with open(user_info.txt, r, encoding=utf8) as f:
234         for line in f:
235             if user_name in line:
236                 balance = int(line.split(:)[3][:-5])
237                 debt = int(line.split(:)[-1])
238                 print(您的账户信息为余额:%s,欠款:%s!%(balance,debt))
239         return
240 
241 #添加用户
242 def add_user():
243     while True:
244         add_flag =True
245         new_user_name =input(请输入注册账户名【退出Q】【返回R】:).strip()
246         if new_user_name.lower() == q:
247             exit()
248         if new_user_name.lower() == r:
249             return
250         new_user_pwd =input(请输入密码【退出Q】:).strip()
251         if new_user_pwd.lower() ==q:
252             exit()
253         else:
254             f1 =open(user_info.txt, r+, encoding=utf8)
255             f2 =open(user_locked.txt, r, encoding=utf8)
256             for line in f2:
257                 if new_user_name in line:
258                     print(该账户已经被冻结,请重新输入账户名!)
259                     add_flag =False
260                     break
261             if add_flag:
262                 for line1 in f1:
263                     if new_user_name in line1:
264                         print(该账户已经存在,请重新输入账户名!)
265                         break
266                 else:
267                     f1.write(name:+new_user_name+,password:+new_user_pwd+,balance:0,debt:0\n)
268                     print(新用户注册成功!)
269             f1.close()
270             f2.close()
271 
272 #冻结用户
273 def frozen_user():
274     set_frozen_user =input(请输入要冻结的账户名【退出Q】【返回R】:).strip()
275     if set_frozen_user.lower() == q:
276         exit()
277     if set_frozen_user.lower() == r:
278         return
279     with open(user_info.txt, r, encoding=utf8) as f:
280         for line in f:
281             if set_frozen_user in line:
282                 frozen_flag = True
283                 break
284         else:
285             frozen_flag =False
286             print(对不起,该账户名不存在!)
287         if frozen_flag:
288             with open(user_locked.txt, a, encoding=utf8) as f1:
289                 f1.write(set_frozen_user+\n)
290                 print(该账户已冻结!)
291 
292 #设定用户额度
293 def set_account():
294     print(该模块暂时没太多思路)
295     return
296 
297 user_func_choice ={
298     1:withdrawal,
299     2:deposit,
300     3:trans_accounts,
301     4:balance_search,
302     5:reg_payment,
303     6:account_query,
304     q:quit
305 }
306 root_func_choice ={
307     1:add_user,
308     2:frozen_user,
309     3:set_account,
310     R:quit
311 }
312 
313 if __name__ == __main__:
314     count = 0
315     while True:
316         user_choice_num =login()
317         if user_choice_num == 3:
318             exit()
319         elif user_choice_num == 1:
320             while True:
321                 user_name =input(请输入用户名:).strip()
322                 user_pwd =input(请输入密码:).strip()
323                 res = user_login(user_name,user_pwd,count)
324                 if res == 0:
325                     count += 1
326                     continue
327                 elif res == 2:
328                     exit()
329                 else:
330                     print(进入用户使用界面!)
331                     break
332             while True:
333                 user_welcome()
334                 key = input(请输入要进行的操作序号:).strip()
335                 if key.isdigit() and 0< int(key) <7:
336                     user_func_choice[key](user_name)
337                     continue
338                 elif key.lower() ==q:
339                     exit()
340                 else:
341                     print(请输入正确的序号!)
342                     continue
343         else:
344             res =root_login()
345             if res == 0:
346                 exit()
347             else:
348                 print(进入管理员界面)
349                 while True:
350                     root_welcome()
351                     key = input(请输入要进行的操作序号:).strip()
352                     if key.isdigit() and 0 < int(key) < 4:
353                         root_func_choice[key]()
354                         continue
355                     elif key.lower() == q:
356                         exit()
357                     else:
358                         print(请输入正确的序号!)
359                         continue

 

ATM的流程的实现

标签:else   atm   时间   信息   new   lan   user   返回   sdi   

原文地址:http://www.cnblogs.com/sl-swift/p/7806213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!