加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

ATM程序规范练习

发布时间:2020-12-14 03:50:38 所属栏目:大数据 来源:网络整理
导读:? ATM购物车程序(练习) ------------README 作者:飞乐版本:示例版本 v0. 1 程序介绍: 实现ATM常用功能 功能全部用python的基础知识实现,用到了timeossysopen函数模块知识,主要帮给大家一个简单的模块化编程的示例 注意:只实现了 " 认证 " , " 注册 "

?

ATM购物车程序(练习)

------------README

作者:飞乐
版本:示例版本 v0.1
程序介绍:
    实现ATM常用功能
    功能全部用python的基础知识实现,用到了timeossysopen函数模块知识,主要帮给大家一个简单的模块化编程的示例

    注意:只实现了"认证""注册""购物""支付"功能

程序结构:
ATM--/
├——─ README
├── atm #ATM主程目录
│?? ├── __init__.py
│?? ├── bin #ATM 执行文件 目录
│?? │?? ├── __init__.py
│?? │?? ├── start.py  #ATM 执行程序
│?? │?? └── manage.py #ATM 管理端,未实现
│?? ├── conf #配置文件
│?? │?? ├── __init__.py
│?? │?? └── settings.py #文本配置
│?? ├── core #主要程序逻辑都 在这个目录 里
│?? │?? ├── __init__.py
│?? │?? ├── auth.py      #用户认证模块
│?? │?? ├── shop.py  #购物商城
│?? │?? ├── register.py       #用户注册模块
│?? │?? ├── src.py         #主逻辑交互程序
│?? ├── db  #用户数据存储的地方
│?? │?? ├── __init__.py
│?? │?? ├── db.py #生成一个临时的账户数据 
│?? │?? └── error_db.py #账户锁定文本 
│?? ├── lib  #公共程序
│?? │?? ├── __init__.py 
│?? │?? ├── common.py     #临时获取的用户信息
│?? └── log #日志目录
│??      ├── __init__.py
└── ── ├── access.log #用户访问和操作的相关日志

ATM

-------bin目录

start.py

1 import sys,os
2 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
3 sys.path.append(BASE_DIR)
4 
5 from core import src
6 src.run()
View Code

?

-------conf目录

settings.py

1 import os
2 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
3 DB_PATH=r%sdbdb.txt%BASE_DIR#用户注册信息路径
4 ER_PATH=r%sdberror_db.txt%BASE_DIR#锁定账户的信息路径
View Code

-------core目录

auth.py

 1 import sys,os
 2 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
 3 sys.path.append(BASE_DIR)
 4 from lib import common  #导入读取到的用户信息
 5 from conf import settings #导入配置模块获取文本路径,用于锁定账户写入文本
 6 
 7 
 8 def outter(func):
 9     def inner(*args,**kwargs):
10         user_info_dic = common.user_info()
11         while True:
12             if len(user_info_dic) == 0:
13                 print(请先注册)
14                 break
15             if common.current_user[username]:
16                 print(用户已登录)
17                 res = func(*args,**kwargs)
18                 return res
19 
20             uname=input(用户名>>:).strip()
21             if uname==q:break
22             if uname not in user_info_dic:
23                 print(用户不存在。。。)
24                 continue
25 
26             if uname in common.user_error_info():
27                 print(此账户已被锁定)
28                 continue
29 
30             if user_info_dic[uname][status]>2:
31                 with open(settings.ER_PATH,a,encoding=utf-8)as f:
32                     f.write(%s|%uname)
33                     user_info_dic[uname][status]=0
34                     print(尝试次数过多,账户已被锁定)
35                     continue
36 
37             pwd=input(密码>>:).strip()
38             if pwd==q:break
39             if pwd==user_info_dic[uname][password]:
40                 print(欢迎 %s 登录成功%uname)
41                 common.current_user[username]=uname#将用户名存入临时字典内
42                 common.current_user[balance]=int(user_info_dic[uname][balance])#将价格信息转成int存入字典内,用于购物运算
43                 res=func(*args,**kwargs)
44                 return res
45             else:
46                 print(密码错误)
47                 user_info_dic[uname][status] +=1
48     return inner
49 
50 
51 @outter
52 def auth1():
53     pass
View Code

?

register.py

 1 import sys,os
 2 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
 3 sys.path.append(BASE_DIR)
 4 from conf import settings
 5 
 6 def get_uname():
 7     while True:
 8         uname=input(请输入用户名>>:).strip()
 9         if not uname.isalpha():
10             print(输入不合法)
11             continue
12         with open(settings.DB_PATH,r,encoding=utf-8)as f:
13             for line in f:#如果break没有运行,则运行else
14                 line=line.strip(n).split(,)
15                 if uname==line[0]:
16                     print(用户已存在)
17                     break
18             else:
19                 return uname
20 def get_pwd():
21     while True:
22         pwd1=input(请输入密码>>:).strip()
23         if not len(pwd1) > 0:
24             print(输入不能为空)
25             continue
26         pwd2=input(请再次输入密码>>:).strip()
27         if pwd1==pwd2:
28             return pwd1
29         else:
30             print(两次输入密码不一致,请重新输入)
31 def get_bal():
32     while True:
33         bal=input(请输入金额>>:).strip()
34         if bal.isdigit():
35             return bal
36         else:
37             print(输入非法)
38 def uinfo_write(uname,pwd,bal):
39     with open(settings.DB_PATH,encoding=utf-8)as f:
40         f.write(%s,%s,%sn%(uname,bal))
41         print(注册成功)
42 
43 #用户注册执行入口
44 def registe():
45     print(33[34;1m========注册=========33[0m)
46     uname=get_uname()
47     pwd=get_pwd()
48     bal=get_bal()
49     uinfo_write(uname,bal)
View Code

?

shop.py

 1 import sys,os
 2 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
 3 sys.path.append(BASE_DIR)
 4 from lib import common
 5 from core import auth
 6 
 7 @auth.outter
 8 def shopping():
 9     #登录成功,开始购物
10     uname_of_db=common.current_user[username]#获取用户名
11     balance_of_db=common.current_user[balance]#获取余额
12     print(33[32;1m欢迎进入购物商城n尊敬的用户 [%s] 您的余额为 [%s],祝您购物愉快33[0m%(
13         uname_of_db,balance_of_db))
14     while True:
15         goods = [
16             {"name":"电脑","price":2000},17             {"name":"鼠标","price":10},18             {"name":"游艇","price":20},19             {"name":"美女","price":100},20         ]
21         print(------商品编号-------商品名称-----商品价格------)
22         for index,item in enumerate(goods):
23             print(         %s            %s            %s%(
24                 index,goods[index][name],goods[index][price]))
25         print(--------------------end------------------------)
26         choice=input(请输入想购买的商品编号,输入"q"退出>>:).strip()
27         if choice.isdigit():
28             choice=int(choice)
29             if choice < 0 or choice >= len(goods):continue#判断购买的商品是否超过可选范围
30 
31             gname=goods[choice][name]#商品名
32             gprice=goods[choice][price]#商品价格
33 
34             if balance_of_db >= gprice:
35                 if gname in common.shopping_cart:#原来已经买过了
36                     common.shopping_cart[gname][count] +=1
37                 else:
38                     common.shopping_cart[gname]={gprice:gprice,count:1}
39 
40                 balance_of_db -=gprice #扣钱
41                 common.current_user[balance]=balance_of_db#更新账户余额
42                 print(成功购买 %s 添加到购物车,价格是 %s%(gname,gprice))
43             else:
44                 print(你个穷逼买不起,产品价格是 {price},你还差 {lack_price}.format(
45                     price=gprice,46                     lack_price=(gprice-balance_of_db)
47                 ))
48             print(common.shopping_cart)
49             print(退出并支付购买的商品请输入:"b")
50         elif choice==b:
51             break
52         elif choice==q:
53             common.shopping_cart={}
54             break
55         else:
56             print(输入非法)
View Code

?

src.py

 1 from core import register#用户注册模块
 2 from core import auth #导入用户注册模块
 3 from core import shop #导入购物模块
 4 from lib import common#导入公共模块
 5 from conf import settings #导入配置模块
 6 import os
 7 
 8 @auth.outter
 9 def pay():
10     print(------------支付---------n)
11     print("""
12     ---------------------------------已购买商品列表---------------------------------
13     id          商品                   数量             单价               总价
14     """)
15     total_cost=0
16     for i,key in enumerate(common.shopping_cart):
17         print(%22s%18s%18s%18s%18s%(
18             i,19             key,20             common.shopping_cart[key][count],21             common.shopping_cart[key][gprice],22             common.shopping_cart[key][count]*common.shopping_cart[key][gprice]
23         ))
24         total_cost +=common.shopping_cart[key][count]*common.shopping_cart[key][gprice]
25     print(‘‘‘
26     您的总花费为:%s
27     您的余额为:%s
28     ---------------------------------end---------------------------------
29     ‘‘‘%(total_cost,common.current_user[balance]))
30     while True:
31         if len(common.shopping_cart) == 0:
32             print(购物车为空,无需支付)
33             break
34 
35         inp=input(确认购买(yes/no?)>>:).strip()
36         if inp not in [Y,N,y,n,yes,no]:continue
37         if inp in [Y,yes]:
38             #将余额写入文件
39 
40             src_file=settings.DB_PATH
41             dst_file=r%s.swp%settings.DB_PATH
42             with open(src_file,encoding=utf-8)as read_f,43                 open(dst_file,w,encoding=utf-8)as write_f:
44                 for line in read_f:
45                     if line.startswith(common.current_user[username]):
46                         l=line.strip(n).split(,)
47                         l[-1]=str(common.current_user[balance])
48                         line=,.join(l)+n
49                         #在内存中修改的数据,写入到硬盘中
50                     write_f.write(line)
51             os.remove(src_file)
52             os.rename(dst_file,src_file)
53             print(购买成功,请耐心等待发货)
54             common.shopping_cart={}
55             common.current_user[username]=None
56             common.current_user[balance]=None
57             break
58         if inp in [N,no]:
59             print(正在退出支付平台。。。。。)
60             break
61 
62 @auth.outter
63 def transter():
64     print(------------转账---------)
65 
66 
67 dic={
68     1:auth.auth1,69     2:register.registe,70     3:shop.shopping,71     4:pay,72     5:transter,73 }
74 
75 def run():
76     while True:
77         print(‘‘‘
78         1 认证
79         2 注册
80         3 购物
81         4 支付
82         5 转账
83         ‘‘‘)
84         choice = input(>>: ).strip()
85         if choice in dic:
86             dic[choice]()
87         elif choice==q:break
88         else:
89             print(输入非法)
View Code

?

-------db目录

db.txt

1 alex,abc,2400

error_db.txt

alex|egon|jack|

?

-------lib目录

common.py

 1 from conf import settings #引入配置模块获取文本信息路径,用于读取用户信息
 2 
 3 #获取用户列表信息
 4 def user_info():
 5     user_info_dic={}
 6     with  open(settings.DB_PATH,encoding=utf-8)as f:
 7         for line in f:
 8             line=line.strip(n).split(,)
 9             user_info_dic[line[0]]={
10                 password:line[1],11                 balance:line[2],12                 status:0,13             }
14     return user_info_dic
15 
16 
17 #获取被锁定的用户信息
18 def user_error_info():
19     with open(settings.ER_PATH,encoding=utf-8)as f:
20         line=f.read()
21         line=line.strip().split(|)
22         return line
23 
24 #将登成功的信息保存到字典中
25 current_user={
26     username:None,27     balance:None,28 }
29 
30 shopping_cart={}
View Code

?

-------log目录

access.log(尚未实现)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读