作业需求:
模拟实现一个ATM + 购物商城程序
- 额度 15000或自定义
- 实现购物商城,买东西加入 购物车,调用信用卡接口结账
- 可以提现,手续费5%
- 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
- 支持多账户登录
- 支持账户间转账
- 记录每月日常消费流水
- 提供还款接口
- ATM记录操作日志
- 提供管理接口,包括添加账户、用户额度,冻结账户等。。。
1 import sys 2 sys.path.append("..") 3 from config import main 4 5 main.main()
1 import os 2 import pickle 3 import datetime 4 A = datetime.datetime.now() 5 6 def login(): 7 # 已有管理员 nikita; 密码 123 8 ''' 9 管理员登录 10 :return: 11 ''' 12 while True: 13 name = input('Please enter name:') 14 password = input('Please enter password:') 15 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 16 line = pickle.load(open(os.path.join(a,'db','admin'), 'rb')) 17 if line[name]== password: 18 print ('登录成功!') 19 with open('record', 'a') as f: 20 f.write("%s %s 登陆 " % (name, A)) 21 break 22 else: 23 print('用户名或密码错误,请重新输入') 24 25 26 def ls(): 27 while True: 28 choice = input('请选择 1 增加管理员; 2 查看管理员; 3 修改用户信息; 4 删除用户信息; q = 退出') 29 if choice == '1': 30 add_admin() 31 elif choice == '2': 32 check() 33 elif choice == '3': 34 ck_ls() 35 amend_user() 36 elif choice == '4': 37 remove_user() 38 elif choice == 'q': 39 exit() 40 else: 41 print('输入有误,请重新输入') 42 43 44 45 def add_admin(): 46 ''' 47 添加管理员 48 49 :return: 50 ''' 51 while True: 52 name = input('请输入姓名') 53 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 54 line = pickle.load(open(os.path.join(a,'db','admin'), 'rb')) 55 if name in line: 56 name1 = input('此用户已存在,y = 重新输入,其他键 = 退出') 57 if name1 == 'y': 58 continue 59 else: 60 break 61 else: 62 password = input('请输入密码') 63 line[name]= password 64 with open('record', 'a') as f: 65 f.write(" %s 添加 %s " % (A, name)) 66 pickle.dump(line, open(os.path.join(a, 'db', 'admin'), 'wb')) 67 break 68 69 70 def remove_user(): 71 ''' 72 删除用户 73 :return: 74 ''' 75 while True: 76 name = input('请输入需要删除的用户') 77 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 78 if os.path.exists(os.path.join(a,'db',name)) is True: 79 rm = input('请确认删除,q = 退出, y = yes, n = 其他键') 80 if rm == 'y': 81 os.remove(os.path.join(a,'db',name)) 82 print('删除成功') 83 with open('record', 'a') as f: 84 f.write(" %s 删除 %s " % (A, name)) 85 break 86 else: 87 break 88 else: 89 print('用户不存在,请重新输入') 90 91 92 def amend_user(): 93 ''' 94 修改用户信息 95 :return: 96 ''' 97 98 while True: 99 name = input('请输入要修改的用户, q = 退出') 100 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 101 if name == 'q': 102 break 103 elif os.path.exists(os.path.join(a,'db',name)) is True: 104 line1 = pickle.load(open(os.path.join(a,'db',name), 'rb')) 105 print (line1) 106 key = input('请输入要修改的选项') 107 item = input('请输入要修改的内容') 108 line1[key] = item 109 print(line1) 110 ok = input('请确认是否正确,y = yes, 其他键 = no, q =退出') 111 if ok =='q': 112 break 113 elif ok == 'y': 114 with open('record', 'a') as f: 115 f.write(" %s 修改 %s %s 的 %s " % (A, name, key, item)) 116 pickle.dump(line1, open(os.path.join(a,'db',name), 'wb')) 117 break 118 else: 119 continue 120 else: 121 print('用户不存在,请重新输入') 122 123 def check(): 124 ''' 125 查看管理员 126 :return: 127 ''' 128 129 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 130 line = pickle.load(open(os.path.join(a,'db','admin'), 'rb')) 131 for key in line: 132 print (key, line[key]) 133 134 def ck_ls(): 135 ''' 136 查看用户名单 137 :return: 138 ''' 139 a = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 140 file = os.listdir(os.path.join(a,'db')) 141 file.remove('admin') 142 print ('用户名单:') 143 for i in file: 144 print (i)
1 import sys 2 sys.path.append("..") 3 from config import admin 4 from config import user 5 from config import shop 6 import datetime 7 8 B = datetime.datetime.now() 9 def main(): 10 T = '0' 11 while True: 12 login = input('请选择 1 普通用户;2 管理员; 3 注册; 4 退出') 13 if login == '1': 14 if T == '0': 15 name = user.login() 16 choice = input('请选择 1 购物; 2 进入ATM; 其他键 = 退出 ') 17 if choice == '1': 18 while True: 19 mk = shop.ls() 20 T = user.charge(name, mk[-1]) 21 with open('record', 'a') as f: 22 f.write('%s %s 购物名单 %s' % (name, B, mk)) 23 if T == '1': 24 continue 25 elif T == '2': 26 break 27 elif T == '3': 28 exit() 29 30 31 elif choice == '2': 32 user.ls(name) 33 break 34 else: 35 exit() 36 elif login == '2': 37 admin.login() 38 admin.ls() 39 break 40 elif login == '3': 41 user.add_user() 42 elif login == '4': 43 exit() 44 else: 45 print('输入有误,请重新输入')
1 import datetime 2 B =datetime.datetime.now() 3 4 # 创造三级菜单 5 shoppingmall = {'sport': {'clothes':{'coat':{20: 80}, 'shirt': {30: 100}, 'skirt': {20: 120}}, 6 'tools': {'net': {20: 70}, 'bat': {40: 100}, 'fish pole': {20: 90}}, 7 'ball': {'basketball': {90: 200}, 'pingpong': {10: 20}, 'football': {50: 170}}}, 8 'electrical equipment': {'phone': {'Apple': {10: 6000}, 'Xiaomi': {15: 2000}, 'Huawei': {5: 3000}}, 9 'computer': {'HP': {2: 4000}, 'Lenovo': {6: 2200}, 'Dell': {10: 3000}}, 10 'TV': {'Sony': {60: 5000}, 'LG': {30: 3000}, 'TCL': {40: 2200}}}, 11 'book': {'baby': {'Story': {50: 37},'Picture': {30: 64},'Music': {60: 93}}, 12 'child': {'Exercise': {30: 36},'Earth': {20: 55},'Weather': {30: 64}}, 13 'adult': {'Leaning': {40: 45},'Cookbook': {50: 74},'Tourism': {50: 43}}}} 14 15 def ls(): 16 list1=[] 17 list2=[] 18 list3=[] 19 shopping_list=[] 20 item_price={} 21 while True: 22 for key in shoppingmall: 23 print(key) 24 list1.append(key) 25 # 选择一项到下一级菜单或者退出 26 choose = input('Please choose thing or q = quit:') 27 if choose == 'q': 28 exit('Bye!') 29 # 进入下一级菜单 30 elif choose in list1: 31 a = shoppingmall.get(choose) 32 while True: 33 for key1 in a: 34 print(key1) 35 list2.append(key1) 36 # 选择一项到下一级菜单或者退出或返回 37 choose1 = input('Please chose that you want or q = quit or b = back:') 38 if choose1 == 'q': 39 exit('Bye!') 40 elif choose1 == 'b': 41 break 42 elif choose1 in list2: 43 # 进入下一级菜单 44 b = a.get(choose1) 45 while True: 46 for key2 in b: 47 c = b.get(key2) 48 list3.append(key2) 49 for number in c: 50 price = c.get(number) #number剩余数量,price 商品价钱 51 item_price[key2] = price#商品和对应的价钱放入字典中 52 print ('%s is $%s left %s' % (key2, item_price[key2], number)) 53 # 选择退出,返回上一级菜单 或者第一个菜单 54 choose2 = input('What do you want to buy? n = quit,b = back.') 55 if choose2 == 'n': 56 exit('Bye!') 57 elif choose2 == 'b': 58 break 59 elif choose2 in list3: 60 quantity = int(input('Please choose quantity')) 61 if quantity <=number: #检查购买数量是否大于存货量 62 if choose2 in shopping_list: 63 local = shopping_list.index(choose2) 64 quantity = int(quantity) + int(shopping_list[local+2]) 65 goods_price = (quantity * item_price[choose2]) 66 shopping_list.pop(local+1) 67 shopping_list.pop(local+1) 68 shopping_list.insert(local+1, goods_price) 69 shopping_list.insert(local+2, quantity) #物品数量放入购物名单中 70 else: 71 shopping_list.append(choose2) 72 goods_price = (quantity * item_price[choose2]) 73 shopping_list.append(goods_price) 74 shopping_list.append(quantity) # 物品数量放入购物名单中 75 shop_thing = shopping_list[::3] 76 shop_num = shopping_list[2::3] 77 print('Shopping list has:') 78 ii = len(shop_thing) 79 80 for item in range(ii): # 输出购买名单 81 print ('%s has %s'% (shop_thing[item], shop_num[item])) 82 last = input('Goods have been put in shopping_list, what do you want to do? ' 83 'c = check out, q = quit,other keys = other thing I want to buy.') 84 if last == 'c': 85 #计算总数 86 total_price = 0 87 for h in range(len(shop_thing)): 88 total_price += goods_price 89 print('Total price is %s' % (total_price)) 90 shopping_list.append(total_price) 91 #shopping_list[购买物品,这个物品总价,购买数量,总价] 92 return shopping_list 93 elif last == 'q': 94 exit() 95 else: 96 continue 97 else: 98 print ('Quantity is over, please choose again') 99 else: 100 print ('Yout choose is over the choose, please choose again') 101 else: 102 print('Sorry, your choose not in list! Please choose again') 103 else: 104 print('Sorry, your choose not in list! Please choose again')
1 import os 2 import pickle 3 import datetime 4 5 6 A = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 7 B = datetime.datetime.now() 8 9 10 11 def login(): 12 ''' 13 普通用户登录 14 :return: 15 ''' 16 T = True 17 while T: 18 name = input('Please enter name:') 19 password = input('Please enter password:') 20 if os.path.exists(os.path.join(A, 'db', name)) is True: 21 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 22 if line['status'] == '0': 23 if line['name'] == name and line['password'] == password: 24 print ('欢迎 %s!' % name) 25 with open('record', 'a') as f: 26 f.write("%s %s 成功登陆 " % (name, B)) 27 T = False 28 return name 29 else: 30 print('用户名或密码错误,请重新输入') 31 else: 32 print ('你的账号已被冻结,请联系管理员') 33 break 34 else: 35 print ('没有该用户信息,请先注册') 36 add_user() 37 38 39 def ls(name): 40 ''' 41 42 :return: 43 ''' 44 while True: 45 choice = input('请输入需要的选项:1 取款;2 查看账单,还款还款;3 转账, 4 修改还款日;5 退出') 46 if choice == '1': 47 cash(name) 48 elif choice == '2': 49 pay (name) 50 elif choice == '3': 51 bring_fd(name) 52 elif choice == '4': 53 change_pay(name) 54 elif choice == '5': 55 exit() 56 else: 57 print('输入有误,请重新输入') 58 59 60 def cash(name): 61 ''' 62 提取现金 63 :param name: 64 :return: 65 ''' 66 while True: 67 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 68 mk = input('余额为 %.2f, 可以透支 %.2f, 请输入提取多少现金,或者 q = 退出' % (line['balance'], line['credit'])) 69 if mk == 'q': 70 break 71 elif float(mk) > 0: 72 mk1 = float(line['balance']) 73 mk2 = float(line['credit']) 74 if mk1>float(mk): 75 mk1 -= float(mk) 76 line['balance'] = mk1 77 print('取款成功') 78 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 79 break 80 elif mk1+mk2>=float(mk): 81 mk1-=float(mk) 82 mk2-=abs(float(mk1)) 83 line['balance'] = 0 84 line['credit'] = mk2 85 line['debt'].append(B) 86 line['debt'].append(abs(float(mk1))*0.05+abs(float(mk1))) 87 print('取款成功,手续为5%') 88 with open('record', 'a') as f: 89 f.write("%s %s 提取 %s " % (name, B, mk)) 90 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 91 break 92 else: 93 print('卡的余额不足,请重新输入') 94 else: 95 print('输入有误,请重新输入') 96 97 98 99 100 def add_user(): 101 ''' 102 添加新用户 103 :return: 104 ''' 105 106 while True: 107 name = input('请输入姓名') 108 if os.path.exists(os.path.join(A, 'db', name)) is True: 109 name1 = input('此用户已存在,y = 重新输入,其他键 = 退出') 110 if name1 == 'y': 111 continue 112 else: 113 break 114 else: 115 password = input('请输入密码') 116 print('注册成功') 117 with open('record', 'a') as f: 118 f.write("%s %s 成功注册 " % (name, B)) 119 info = {'name': name, 120 'password': password, 121 'credit': 15000,# 透支额度 122 'balance': 0, # 余额 123 'status': '0', # 0 = 正常,1 = 冻结 124 'debt': [], #欠款 125 'debt1': [],#上期欠款没有还玩 126 'pay_date':[15] #还款日期,默认15号 127 } 128 pickle.dump(info, open(os.path.join(A,'db',name), 'wb')) 129 break 130 131 132 def pay (name): 133 ''' 134 查看账单,还款还款,22号出账单,指定日期还款,过期不还,收万分之5利息 135 :param name: 136 :return: 137 ''' 138 total1 = 0 139 total2 = 0 140 ls=[] #不用计利息 141 ls1=[] #计利息 142 pay_ls=[] 143 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 144 credit = line['credit'] 145 for a in line['debt'][0::2]: 146 #当月和上月10号之后消费的,不用计利息的放入ls 147 if a.month - B.month == 0 or B.month - a.month == 1 and (a.day >=22 or B.day<=line['pay_date']): 148 c = line['debt'].index(a)+1 149 ls.append(line['debt'][c]) 150 #上个月22号前计利息的放入ls1 151 else: 152 c = line['debt'].index(a)+1 153 day = (B - a).days 154 ls1.append(day) 155 ls1.append(line['debt'][c]) 156 if line['debt1']==[]: 157 total3 = 0 158 else: 159 #欠钱的总和利息加欠款 160 total3 = total2*(B-line['debt1'][0]).days * 0.0005+line['debt1'][1] 161 #不计利息欠钱的总数 162 for i in ls: 163 total1 += i 164 #把每个利息和本金的和放入pay_ls 165 for i1 in ls1[::2]: 166 d = ls1.index(i1)+1 167 e = ls1[d] 168 h = e * i1 * 0.0005 + e 169 pay_ls.append(h) 170 #计算这期账单 171 for i3 in pay_ls: 172 total2 += i3 173 total2+=total3 174 #一共欠款 175 total = total1+total2 176 print('你需要还%.2f, 还可以透支 %.2f,余额为%s' % (total2, credit, line['balance'])) 177 if total2 > 0 or credit < 15000: 178 mk = input('请输入还款金额, q = 退出,') 179 if mk == 'q': 180 with open('record', 'a') as f: 181 f.write("%s %s 查看账单 " % (name, B)) 182 exit() 183 elif float(mk)>0 or float(mk)>0: 184 #还款金额大于欠款 185 if total2 <= float(mk): 186 #还款金额大于欠款加透支 187 if total <float(mk): 188 pay = total - float(mk) 189 line['balance']=abs(pay)+line['balance'] 190 line['debt']=[] 191 line['debt1']=[] 192 line['credit']= 15000 193 print('款项已还清') 194 else: 195 pay = total - float(mk) 196 if pay == 0: 197 line['credit']= 15000 198 line['debt']=[] 199 line['debt1'] =[] 200 print('款项已还清') 201 else: 202 credit = 15000-pay 203 line['credit']= credit 204 line['debt']=[] 205 line['debt'].append(B) 206 line['debt'].append(pay) 207 print('你需要还%.2f, 还可以透支 %.2f,余额为%s' % (total2, credit, line['balance'])) 208 else: 209 pay = total2 - float(mk) 210 print('还欠%.2f,请尽快归还' % pay) 211 line['debt']=[] 212 line['debt1']=[] 213 line['debt1'].append(B) 214 line['debt1'].append(pay) 215 line['debt1'].append(total2) 216 line['debt'].append(B) 217 line['debt'].append(total1) 218 219 else: 220 print('输入有误') 221 with open('record', 'a') as f: 222 f.write("%s %s 还款 %s " % (name, B, mk)) 223 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 224 225 else: 226 chooic = input('存多少钱, q = 退出') 227 if chooic == 'q': 228 with open('record', 'a') as f: 229 f.write("%s %s 查看账单 " % (name, B)) 230 exit() 231 elif float(chooic)>0: 232 balance = line['balance'] + float(chooic) 233 line['balance'] = balance 234 line['debt']=[] 235 line['debt1']=[] 236 print ('你的余额为%.2f' % balance) 237 else: 238 print('输入有误') 239 with open('record', 'a') as f: 240 f.write("%s %s 存款 %s " % (name, B, chooic)) 241 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 242 243 244 def bring_fd(name): 245 ''' 246 用户之间转账 247 :param name: 248 :return: 249 ''' 250 while True: 251 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 252 print('你账号余额为%s' % line['balance']) 253 pay_name= input('请输入要转账的账号名, q = 退出') 254 if pay_name == 'q': 255 exit() 256 if pay_name == name: 257 print('不能转账给相同账户') 258 continue 259 if os.path.exists(os.path.join(A, 'db', pay_name)) is True: 260 pay_line = pickle.load(open(os.path.join(A,'db',pay_name), 'rb')) 261 mk = input('请输入转账数目') 262 if line['balance'] > float(mk): 263 line['balance'] = float(line['balance']) - float(mk) 264 pay_line['balance'] = float(pay_line['balance']) + float(mk) 265 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 266 pickle.dump(pay_line, open(os.path.join(A,'db',pay_name), 'wb')) 267 print('转账成功') 268 with open('record', 'a') as f: 269 f.write("%s %s 转了 %s元 给 %s " % (name, B, mk, pay_name)) 270 break 271 else: 272 print ('余额不足,请重新输入') 273 else: 274 print('该账号不存在,请重新输入') 275 276 277 def charge(name, mk): 278 ''' 279 付款 280 :param name: 281 :param mk: 282 :return: 283 ''' 284 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 285 a = float(line['credit']) 286 b = float(line['balance']) 287 c = float(mk) 288 yes = input('请输入是否付款?y = yes,其他键退出') 289 if yes == 'y': 290 if b > c: 291 line['balance']= a-c 292 cont = input('付款成功, 1 继续购物 2 返回上级菜单 3 退出') 293 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 294 with open('record', 'a') as f: 295 f.write("%s %s 支付了 %s " % (name, B, mk)) 296 return cont 297 elif a+b >c: 298 line['0']=0 299 line['balance']= 0 300 line['credit']= a-c 301 line['debt'].append(B) 302 line['debt'].append(c) 303 cont = input('付款成功, 1 继续购物 2 返回上级菜单; 其他键 = 退出') 304 pickle.dump(line, open(os.path.join(A,'db',name), 'wb')) 305 with open('record', 'a') as f: 306 f.write("%s %s 支付了 %s " % (name, B, mk)) 307 return cont 308 else: 309 print('余额不足') 310 else: 311 exit() 312 313 def change_pay(name): 314 ''' 315 设置还款日期 316 :param name: 317 :return: 318 ''' 319 line = pickle.load(open(os.path.join(A,'db',name), 'rb')) 320 while True: 321 date = input('请设置还款日,q=退出') 322 if date == 'q': 323 break 324 elif date.isdigit(): 325 line['pay_date']=int(date) 326 print('你的还款日期改为%s' % (date)) 327 with open('record', 'a') as f: 328 f.write("%s %s 还款日改成 %s " % (name, B, date)) 329 pickle.dump(line,open(os.path.join(A,'db',name), 'wb')) 330 break 331 else: 332 print('输入有误,请重新输入')