TOP

Python编写ATM(初级进阶)

Python3.7

基于面向对象编程

主要功能

  一. 用户功能

    查询余额, 取款, 存款, 转账, 修改个人密码

  二. 管理员功能

    添加新账号, 冻结账号, 解除冻结, 查询用户信息, 查询所有用户信息

  三. 系统平台

    登录, 冻结

  四.其他

    文本存储, 信息格式位: 账号,密码,级别,金额,状态,如: X0001,1234,1,10000,0

代码如下:

  1 import os
  2 
  3 
  4 # 普通用户
  5 class Client:
  6     def __init__(self, data):
  7         print(data)
  8         self.id = data[0]  # id
  9         self.password = data[1]  # 密码
 10         self.level = data[2]  # 级别
 11         self.money = data[3]  # 金额
 12         self.stauts = data[4]  # 状态
 13 
 14     # 查询 金额
 15     def chaxun(self):
 16         print("账号'%s',余额为%.2f$" % (self.id, int(self.money)))
 17         return self.money
 18 
 19     # 取钱
 20     def qu(self):
 21         num = int(input("输入取走金额(以百为单位,最高5000):"))
 22         while num % 100 != 0 or num > int(self.money) or num < 100 or num > 5000:
 23             num = int(input("输入错误,请重试:"))
 24         data_list = []
 25         with open("user_info.txt", 'r', encoding='utf8') as f:
 26             for line in f:
 27                 data_list.append(line)
 28         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
 29             for i in range(len(data_list)):
 30                 detail_list = data_list[i].strip().split(',')
 31                 if detail_list[0] == self.id:
 32                     # 取完后的钱
 33                     self.money = str(int(detail_list[3]) - num)
 34                     detail_list[3] = self.money
 35                     info_str = ','.join(detail_list) + "\n"
 36                     f1.write(info_str)
 37                 else:
 38                     info_str = ','.join(detail_list) + '\n'
 39                     f1.write(info_str)
 40         os.remove('user_info.txt')
 41         os.rename('user_info(副本).txt', 'user_info.txt')
 42         print("账号'%s',取走%.2f$,余额为%.2f$" % (self.id, int(num), int(self.money)))
 43         return self.money
 44      # 存钱
 45     def cun(self):
 46         num = int(input("输入存入金额(以百为单位):"))
 47         while num % 100 != 0 or num < 100:
 48             num = int(input("输入错误,请重试:"))
 49         data_list = []
 50         with open("user_info.txt", 'r', encoding='utf8') as f:
 51             for line in f:
 52                 data_list.append(line)
 53         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
 54             for i in range(len(data_list)):
 55                 detail_list = data_list[i].strip().split(',')
 56                 if detail_list[0] == self.id:
 57                     # 存完之后的钱
 58                     self.money = str(int(detail_list[3]) + num)
 59                     detail_list[3] = self.money
 60                     info_str = ','.join(detail_list) + "\n"
 61                     f1.write(info_str)
 62                 else:
 63                     info_str = ','.join(detail_list) + '\n'
 64                     f1.write(info_str)
 65         os.remove('user_info.txt')
 66         os.rename('user_info(副本).txt', 'user_info.txt')
 67         print("账号'%s',取走%.2f$,余额为%.2f$" % (self.id, int(num), int(self.money)))
 68         return self.money
 69 
 70     # 转账
 71     def zhuan(self):
 72         id_list = []
 73         with open("user_info.txt", 'r', encoding='utf8') as f:
 74             for line in f:
 75                 info_list = line.strip().split(',')
 76                 id_list.append(info_list[0])
 77         print("#ID列表", id_list, )
 78         id = input("输入转账ID:")
 79         while id not in id_list or id == self.id:
 80             id = input("ID输入错误,请重试:")
 81             if id.lower() == 'q':
 82                 return
 83 
 84         num = int(input("输入转账金额(以百为单位,最高5000):"))
 85         while num % 100 != 0 or num > int(self.money) or num < 100 or num > 5000:
 86             num = int(input("金额输入错误,请重试:"))
 87 
 88         data_list = []
 89         with open("user_info.txt", 'r', encoding='utf8') as f:
 90             for line in f:
 91                 data_list.append(line)
 92 
 93         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
 94             for i in range(len(data_list)):
 95                 detail_list = data_list[i].strip().split(',')
 96                 if detail_list[0] == self.id:  # 修改自己的 金额
 97                     self.money = str(int(detail_list[3]) - num)
 98                     detail_list[3] = self.money
 99                     info_str = ','.join(detail_list) + "\n"
100                     f1.write(info_str)
101                 elif detail_list[0] == id:  # 增加 接收人的金额
102                     detail_list[3] = str(int(detail_list[3]) + num)
103                     info_str = ','.join(detail_list) + "\n"
104                     f1.write(info_str)
105                 else:
106                     info_str = ','.join(detail_list) + '\n'
107                     f1.write(info_str)
108         os.remove('user_info.txt')
109         os.rename('user_info(副本).txt', 'user_info.txt')
110         print("转账成功,剩余余额%.2f" % int(self.money))
111         return self.money
112 
113     # 修改密码
114     def xiugai(self, ):
115         old_pwd = input("输入旧密码:")
116         while old_pwd != self.password:
117             old_pwd = input("密码错误,请重试:")
118 
119         new_pwd = input("输入新密码(q返回):")
120         if new_pwd.lower() == 'q':
121             print("取消修改密码")
122             return
123 
124         while len(new_pwd) < 6 or len(set(new_pwd)) == 1:
125             new_pwd = input("新密码不能小于6位或者6位完全相同,请重试:")
126 
127         new_pwd_2 = input("确认新密码:")
128         while len(new_pwd_2) < 6 or len(set(new_pwd_2)) == 1:
129             print("新密码不能小于6位或者6位完全相同,请重试:")
130         if new_pwd == new_pwd_2:
131             data_list = []
132             with open("user_info.txt", 'r', encoding='utf8') as f:
133                 for line in f:
134                     data_list.append(line)
135 
136             with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
137                 for i in range(len(data_list)):
138                     detail_list = data_list[i].strip().split(',')
139                     if detail_list[0] == self.id:  # 修改自己密码
140                         self.password = new_pwd
141                         detail_list[1] = self.password
142                         info_str = ','.join(detail_list) + "\n"
143                         f1.write(info_str)
144                     else:
145                         info_str = ','.join(detail_list) + '\n'
146                         f1.write(info_str)
147             os.remove('user_info.txt')
148             os.rename('user_info(副本).txt', 'user_info.txt')
149             print("密码修改成功,新密码为%s" % self.password)
150             return self.password
151 
152         else:
153             print("2次密码不一样,请重试")
154             return
155 
156 # 管理员
157 class Admin:
158     def __init__(self):
159         self.id = 'a0001'
160         self.password = '112233'
161 
162     # 添加 用户
163     def tianjia(self):
164         id_list = []
165         with open("user_info.txt", 'r', encoding='utf8') as f:
166             for line in f:
167                 info_list = line.strip().split(',')
168                 id_list.append(info_list[0])
169 
170         new_id = input("输入新账号ID:")
171         while new_id in id_list:
172             new_id = input("ID已存在,请重试:")
173 
174         with open("user_info.txt", 'a', encoding='utf8') as f:
175             f.write(new_id + ',' + '123456' + ',' + '1' + ',' + '10000' + ',' + '0' + '\n')
176         print(new_id, '添加成功')
177 
178     # 冻结
179     def dongjie(self):
180         id_list = []
181         with open("user_info.txt", 'r', encoding='utf8') as f:
182             for line in f:
183                 info_list = line.strip().split(',')
184                 id_list.append(info_list[0])
185 
186         dongjie_id = input("输入冻结账号ID:")
187         while dongjie_id not in id_list:
188             dongjie_id = input("ID不存在,请重试:")
189 
190         data_list = []
191         with open("user_info.txt", 'r', encoding='utf8') as f:
192             for line in f:
193                 data_list.append(line)
194 
195         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
196             for i in range(len(data_list)):
197                 detail_list = data_list[i].strip().split(',')
198                 if detail_list[0] == dongjie_id:  # 修改自己密码
199                     detail_list[-1] = '1'
200                     info_str = ','.join(detail_list) + "\n"
201                     f1.write(info_str)
202                 else:
203                     info_str = ','.join(detail_list) + '\n'
204                     f1.write(info_str)
205         os.remove('user_info.txt')
206         os.rename('user_info(副本).txt', 'user_info.txt')
207         print(dongjie_id, '以冻结')
208 
209     # 解冻
210     def jiedong(self):
211         id_list = []
212         with open("user_info.txt", 'r', encoding='utf8') as f:
213             for line in f:
214                 info_list = line.strip().split(',')
215                 id_list.append(info_list[0])
216 
217         jiedong_id = input("输入解冻账号ID:")
218         while jiedong_id not in id_list:
219             jiedong_id = input("ID不存在,请重试:")
220 
221         data_list = []
222         with open("user_info.txt", 'r', encoding='utf8') as f:
223             for line in f:
224                 data_list.append(line)
225 
226         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
227             for i in range(len(data_list)):
228                 detail_list = data_list[i].strip().split(',')
229                 if detail_list[0] == jiedong_id:  # 修改自己密码
230                     detail_list[-1] = '0'
231                     info_str = ','.join(detail_list) + "\n"
232                     f1.write(info_str)
233                 else:
234                     info_str = ','.join(detail_list) + '\n'
235                     f1.write(info_str)
236         os.remove('user_info.txt')
237         os.rename('user_info(副本).txt', 'user_info.txt')
238         print(jiedong_id, '已解冻')
239 
240     # 查询用户信息
241     def chaxun(self):
242         id_list = []
243         with open("user_info.txt", 'r', encoding='utf8') as f:
244             for line in f:
245                 info_list = line.strip().split(',')
246                 id_list.append(info_list[0])
247         print('ID列表', id_list)
248         chaxun_id = input("查询ID:")
249 
250         while chaxun_id not in id_list:
251             print('ID列表', id_list)
252             chaxun_id = input("ID不存在,请重试:")
253 
254         l = ['账号', '密码', '级别(0管理员,1普通)', '金额', '状态(0正常,1冻结)']
255 
256         with open("user_info.txt", 'r', encoding='utf8') as f:
257             for line in f:
258                 info_list = line.strip().split(',')
259                 if chaxun_id == info_list[0]:
260                     for i in range(len(info_list)):
261                         print(l[i], ">>>", info_list[i])
262                     return
263             else:
264                 print("ID不存在,请重试:")
265 
266 
267 # ATM 系统
268 class ATM:
269 
270     def __init__(self):
271         with open("user_info.txt", 'w', encoding='utf8') as f:
272             f.write("a0001,112233,0,10000,0" + '\n')  # 管理员
273             f.write("p0001,123456,0,10000,0" + '\n')  # 普通账号1
274             f.write("p0002,123456,0,10000,0" + '\n')  # 普通账号2
275 
276     # 密码错三次冻结
277     def dongjie(self, ID):
278         data_list = []
279         with open("user_info.txt", 'r', encoding='utf8') as f:
280             for line in f:
281                 data_list.append(line)
282 
283         with open("user_info(副本).txt", 'w', encoding='utf8') as f1:
284             for i in range(len(data_list)):
285                 detail_list = data_list[i].strip().split(',')
286                 if detail_list[0] == ID:  # 修改自己密码
287                     detail_list[-1] = '1'
288                     info_str = ','.join(detail_list) + "\n"
289                     f1.write(info_str)
290                 else:
291                     info_str = ','.join(detail_list) + '\n'
292                     f1.write(info_str)
293         os.remove('user_info.txt')
294         os.rename('user_info(副本).txt', 'user_info.txt')
295 
296     # z主程序运行
297     def run(self):
298         ID = input("请输入卡号:")
299         password = input("请输入密码:")
300         client = None
301         admin = None
302 
303         # 管理员登录
304         if ID == 'a0001':
305             while password != '112233':
306                 password = input("密码错误,联系管理员(q退出系统):")
307                 if password.lower() == 'q':
308                     print('退出系统...')
309                     return
310             print("管理员登录成功")
311             admin = Admin()
312 
313         if admin != None:
314             task = {'1': admin.tianjia, '2': admin.dongjie, '3': admin.jiedong, '4': admin.chaxun}
315             menu = {'1': "添加账号", '2': "冻结账号", '3': "解冻", '4': "查询信息", '5': '退出系统'}
316 
317             while 1:
318                 for k, v in menu.items():
319                     print(k, v)
320 
321                 choice = input("请输入你要执行的任务序号:")
322                 if choice == '5':
323                     print('退出系统...')
324                     return
325                 try:
326                     task[choice]()
327                 except Exception as e:
328                     print("出错,请重试....")
329                 print('\n')
330 
331         with open("user_info.txt", 'r', encoding='utf8') as f:
332             data_list = []
333             for line in f:
334                 data_list.append(line)
335 
336         for line in data_list:
337             num = 1
338 
339             # 用户信息列表
340             data = line.strip().split(",")
341             if ID == data[0]:  # 判断 账号是否存在
342                 if data[-1] == "1":
343                     print('该账号已冻结,请联系管理员解冻')
344                     return
345 
346                 # 密码错三次 账号冻结
347                 while password != data[1] and num < 3:
348                     num += 1
349                     password = input("密码错误,请重试(第%s次尝试):" % num)
350 
351                 if ID == data[0] and password == data[1]:
352 
353                     print('"%s",登录成功\n' % ID)
354                     client = Client(data)  # 实例化一个客户类 把用户信息传进去
355                     break  # 跳出 for 循环
356 
357                 else:
358                     self.dongjie(ID)
359                     print("该账号'%s'已冻结,联系管理员解冻" % ID)
360                     break  # 跳出 for 循环
361         else:
362             print("账号'%s'不存在,请联系管理员" % ID)
363 
364         if client != None:
365             task = {'1': client.chaxun, '2': client.qu, '3': client.cun, '4': client.zhuan, '5': client.xiugai}
366             menu = {'1': "查询余额", '2': "取款", '3': "存款", '4': "转账", '5': "修改个人密码", '6': '退出系统'}
367             while 1:
368                 for k, v in menu.items():
369                     print(k, v)
370 
371                 choice = input("请输入你要执行的任务序号(默认为1):")
372                 if choice == '6':
373                     print('退出系统...')
374                     return
375                 try:
376                     task[choice]()
377                 except Exception as e:
378                     print("出错,请重试...")
379                 print('\n')
380 
381 
382 if __name__ == '__main__':
383     ATM().run()  # 程序的入口

 

posted on 2019-07-11 10:21  hui_T  阅读(787)  评论(0编辑  收藏  举报