day5-ATM和购物车

1、流程图

2、目录分类
 start.py 程序入口


 bin
   auth.py 提供统一登录
 
 conf
   operation.py 提供用户包括管理员操作函数,包括:添加账户、锁定账户、
  搜索账户、更新额度、取现、转账、还款、查看
 
 db
   info.txt 保存账户信息
 
 log
   getlog.py 记录用户操作日志的函数
   openlog.py 管理员查看用户操作日志的函数
   getwater.py 记录用户消费流水的函数
   openwater.py 用户获取消费流水的函数
 
 shop
   shopping.py 购物车导航、购物、付款函数

3、功能分类

(1)、管理员

额度15000或自定义
ATM记录操作日志
提供管理接口,包括添加账户、用户额度,冻结账户等。。。

(2)、普通用户

可以提现,手续费5%
支持多账户登录
支持账户间转账
记录每月日常消费流水
提供还款接口

(3)、购物

实现购物商城,买东西加入购物车,调用信用卡接口结账

4、功能演示

1、登录

①系统提供3次输入账户的机会,超过3次将自动退出

------ 欢迎来到XX银行信用卡中心 ------
1.登录
q、退出
--------------------------------------
请键入您要的操作:1
请输入用户名:user1
请输入密码:1
请输入用户名:user1
请输入密码:2
请输入用户名:user1
请输入密码:3

...已尝试了3次均错误,系统将在3秒后退出...

②系统自动根据输入的账户判断是管理员还是普通用户,并列出各自权限

# 管理员登录
------ 欢迎来到XX银行信用卡中心 ------
1.登录
q、退出
--------------------------------------
请键入您要的操作:1
请输入用户名:admin
请输入密码:admin

>>>欢迎admin登录<<<
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|账户数据示例:User(账户名)| Pass(密码)| Card_Num(卡号)| Quota(信用额度)| Balance(可用额度)| Take_Quota(取现额度)| Status(账户状态)| Type(账户类型)|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:

~~~~~~~~~~~~
# 普通用户登录
------ 欢迎来到XX银行信用卡中心 ------
1.登录
q、退出
--------------------------------------
请键入您要的操作:1
请输入用户名:user1
请输入密码:user1

>>>欢迎user1登录<<<
------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:

2、管理员功能

(1)添加账户

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:1
请输入新的用户名:user3
请输入卡号:3333
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
账户已存在!请选择其他账户
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
请输入新的用户名:user4
请输入卡号:4444
请输入密码:user4
请输入额度:10000

user4账户开户成功!当前信用额度10000 当前可用余额10000 当前取现额度5000
**********************************************************************

(2)冻结账户

①冻结的账户存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:2
请输入要冻结的账户:user1

user1账户冻结成功!
*************************

②冻结的账户不存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:2
请输入要冻结的账户:user8

...您要冻结的账户不存在!...

(3)搜索账户

①搜索的账户存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:3
请输入要搜索的账户信息(账户名或卡号):user1

账户:user1,卡号:1111,信用额度:10000,可用余额:10000,取现额度:5000,账户状态:锁定!请联系管理员

②搜索的账户不存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:3
请输入要搜索的账户信息(账户名或卡号):user8

...账户不存在,请确认!...

(4)更新额度

①更新的账户存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:4
请输入要更新额度的账户:user1
请输入要更新的额度:12000

user1账户额度更新成功,当前:信用额度12000,可用额度12000,取现额度6000!
**********************************************************************

①更新的账户不存在

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:4
请输入要更新额度的账户:user8

...账户不存在,请确认!...

(5)日志查看

############################## 管理员功能列表 ##############################
1.添加账户 2.冻结账户 3.搜索账户
4.更新额度 5.日志查看 q.退出

键入您的选择:5
请输入要查询的用户:user1

2017-02-06 21:53:22,021 - 操作日志 - INFO - 进行了'信息查看'操作

2017-02-06 21:53:52,632 - 操作日志 - INFO - 进行了'购物'操作

---日志结果将会停留屏幕5秒钟---

3、普通用户功能

(1)提现

①取现余额充足

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:1
当前取现额度5000
请输入要提取的金额:1000

user1取现成功! 取现金额1000, 取现手续费50, 当前取现额度3950
**********************************************************************

②取现余额不足

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:1
当前取现额度5000
请输入要提取的金额:6000

...取现余额不足!...

(2)转账

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:2
您需要转账给谁:user2
请输入转账金额:500

转账成功!
********************

(3)还款

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:3
当前账单1550
请输入还款金额:1550

user1还款成功! 还款金额1550, 账单剩余0
**********************************************************************

(4)查询

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:4

账户:user1,卡号:1111,信用额度:10000,可用余额:10000,取现额度:5000,账单金额:0,账户状态:正常

(5)购物

①余额充足的情况

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:5

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
* 欢迎user1来到购物商城 *
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
0 - 服装类
1 - 电子类
2 - 家电类
3 - 食品类
-----------------------End------------------------
[选择种类,按q返回用户菜单] 请输入您的选择:1
--> 0 通讯
--> 1 办公
-----------------------End------------------------
[选择分类,按'b'返回上一级,按'q'返回到用户菜单] 请输入您的选择:1
----> 0 笔记本 5000
----> 1 台式机 3000
-----------------------End------------------------
[选择商品,按'b'返回上一级,按'q'返回到用户菜单] 请输入您的选择:1
请输入商品件数:1

付款成功!
*********************购物清单********************
台式机 {'总价': 3000, '件数': 1, '单价': 3000} 2017.02.06 22.16.33

----------------------清单END-----------------------

②余额不足的情况

...余额不足以支付此次购物!但同样会显示购物清单...

*********************购物清单********************
台式机 {'总价': 30000, '件数': 10, '单价': 3000} 2017.02.08 15.55.16

----------------------清单END-----------------------

(6)流水

------------------------------------------------ 用户菜单列表 ------------------------------------------------
1.提现 2.转账 3.还款
4.查询 5.购物 6.流水 q.退出

键入您的选择:6

2017-02-06 22:18:38,955 - 消费流水 - INFO - 购物花费了3000

---消费流水将会停留屏幕5秒钟---

5、代码部分
start.py
 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 
 7 from bin import auth
 8 
 9 
10 def main():
11     """
12     程序入口
13     :return:
14     """
15     print('-'.center(6, '-'), "33[1;34;43m欢迎来到XX银行信用卡中心33[0m", '-'.center(6, '-'))
16     print("	1.登录
	q、退出")
17     print('-'.center(38, '-'))
18 
19     while True:
20         inp = input("请键入您要的操作:")
21         if inp == "q":
22             exit()
23         if not inp.isdigit():
24             print("输入无效")
25             continue
26         if inp == "1":
27             auth.login()
28             break
29         else:
30             print("请输入正确的数字")
31             continue
32 
33 main()
View Code

 bin-auth.py

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 
 7 import os
 8 import time
 9 from conf import operation
10 from shop import shopping
11 from log import openlog
12 from log import openwater
13 
14 
15 INFO = os.path.join("db", "info.txt")
16 INFO_TMP = os.path.join("db", "info_tmp.txt")
17 INFO_BAK = os.path.join("db", "info_bak.txt")
18 LOGIN_USER = {"is_login": False}
19 
20 
21 def login():
22     """
23     定义登录功能函数
24     提供三次尝试登陆机会
25     :return:
26     """
27     try_times = 3
28     for i in range(3):
29         try_times -= 1
30         username = input("请输入用户名:")
31         pwd = input("请输入密码:")
32         with open(INFO, 'r') as f:
33             for line in f.readlines():
34                 # 布尔值判断是否为空行,strip()忽略空格和换行符

35                 if line.strip():
36                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
37                 if username == User and pwd == Pass:
38                     LOGIN_USER['is_login'] = True                                  # 字典修改
39                     LOGIN_USER['current_user'] = username                          # 字典添加
40                     LOGIN_USER['Quota'] = Quota                                     # 字典添加
41                     LOGIN_USER['Balance'] = Balance                                 # 字典添加
42                     LOGIN_USER['Take_Quota'] = Take_Quota                          # 字典添加
43                     LOGIN_USER['Status'] = Status                                   # 字典添加
44                     LOGIN_USER['Type'] = Type                                       # 字典添加
45                     # order()      # 函数order执行不可放置在此,下一步调用rename INFO,将会报错文件正在使用
46                     break
47         order()
48     if try_times == 0:
49         print("
33[1;31;47m...已尝试了3次均错误,系统将在3秒后退出...33[0m")
50         time.sleep(3)
51         exit()
52 
53 
54 def order():
55     if LOGIN_USER['is_login']:
56         print("
>>>欢迎%s登录<<<" % (LOGIN_USER['current_user']))
57         while True:
58             if LOGIN_USER['Type'] == 'a':
59                 print("-".center(164,'-'))
60                 print("|账户数据示例:User(账户名)| Pass(密码)| Card_Num(卡号)| Quota(信用额度)| Balance(可用额度)| Take_Quota(取现额度)| Status(账户状态)| Type(账户类型)|")
61                 print("-".center(164, '-'))
62                 print('#'.center(30, '#'), "管理员功能列表", '#'.center(30, '#'))
63                 print("1.添加账户	2.冻结账户	3.搜索账户
4.更新额度	5.日志查看	q.退出
")
64                 select = input("键入您的选择:")
65                 if select == '1':
66                     operation.register()
67                 elif select == '2':
68                     operation.lock()
69                 elif select == '3':
70                     operation.search()
71                 elif select == '4':
72                     operation.update()
73                 elif select == '5':
74                     openlog.openlog()
75                 elif select == 'q':
76                     exit()
77             elif LOGIN_USER['Type'] == 'u':
78                 if LOGIN_USER['Status'] == '1':
79                     print('-'.center(48, '-'), "用户菜单列表", '-'.center(48, '-'))
80                     print("1.提现	2.转账	3.还款
4.查询	5.购物	6.流水	q.退出
")
81                     select = input("键入您的选择:")
82                     if select == '1':
83                         operation.take()
84                     elif select == '2':
85                         operation.transfer()
86                     elif select == '3':
87                         operation.repay()
88                     elif select == '4':
89                         operation.check()
90                     elif select == '5':
91                         shopping.navigation()
92                     elif select == '6':
93                         openwater.openwater()
94                     elif select == 'q':
95                         exit()
96                 else:
97                     print("
但是您的账户已被锁定!系统将在3秒后退出....")
98                     time.sleep(3)
99                     exit()
View Code

conf-operation.py

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # Author: Tony Chiu
  4 # Blog:http://www.cnblogs.com/tonychiu
  5 # Github:https://github.com/qiujichun
  6 
  7 
  8 """
  9 如下from bin import auth从bin目录导入auth.py,以便调用auth中定义的变量
 10 代码调用auth中变量,比如auth.INFO
 11 """
 12 import os
 13 from bin import auth
 14 from log import getlog
 15 
 16 
 17 USER_LIST = []
 18 CARD_LIST = []
 19 
 20 
 21 def outer1(func):
 22     """
 23     定义函数:装饰器
 24     :param func:
 25     :return:
 26     """
 27     def inner():
 28         r = func()
 29         # 这里首先判断是否存在auth.INFO_TMP的原因是:保证用户修改成功完成(例如执行升级额度操作,如果用户输入的不是合法的数字,那么就不存在auth.INFO_TMP)
 30         if os.path.exists(auth.INFO_TMP):
 31             if os.path.exists(auth.INFO_BAK):
 32                 os.remove(auth.INFO_BAK)
 33             os.rename(auth.INFO, auth.INFO_BAK)
 34             os.rename(auth.INFO_TMP, auth.INFO)
 35         return r
 36     return inner
 37 
 38 
 39 def register():
 40     """
 41     定义添加账户功能函数
 42     提供三次尝试机会避免添加已存在的用户
 43     使用列表USER_LIST.append(User)保存已存在的用户
 44     :return:
 45     """
 46     for i in range(3):
 47         username = input("请输入新的用户名:")
 48         cardnum = input("请输入卡号:")
 49         with open(auth.INFO, 'r') as f:
 50             for line in f.readlines():
 51                 if line.strip():
 52                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
 53                     USER_LIST.append(User)
 54                     CARD_LIST.append(Card_Num)
 55             if username in USER_LIST or cardnum in CARD_LIST:
 56                 print('xxxxxxxxxxxxxxxxxxxxxxxxxxxx')
 57                 print("账户已存在!请选择其他账户")
 58                 print('xxxxxxxxxxxxxxxxxxxxxxxxxxxx')
 59                 continue
 60             else:
 61                 pwd = input("请输入密码:")
 62                 quota = input("请输入额度:")
 63                 #提现额度=信用额度/2
 64                 take_quota = int(int(quota) / 2)
 65                 with open(auth.INFO, 'a') as f:
 66                     f.write('
'+str(username)+'|'+str(pwd)+'|'+str(cardnum)+'|'+str(quota)+'|'+str(quota)+'|'+str(take_quota)+'|'+'1'+'|'+str('u'))
 67                 print("
	%s账户开户成功!当前信用额度%s 当前可用余额%s 当前取现额度%s" % (username,quota,quota,take_quota))
 68                 print("*".center(70, '*'))
 69                 break
 70 
 71 
 72 @outer1
 73 def lock():
 74     """
 75     定义函数:冻结指定的账户
 76     :return:
 77     """
 78     Lock_User = input("请输入要冻结的账户:")
 79     with open(auth.INFO, 'r') as old, open(auth.INFO_TMP,'w') as new:
 80         for line in old:
 81             # 布尔值判断是否为空行,strip()忽略空格和换行符

 82             if line.strip():
 83                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
 84                 USER_LIST.append(User)
 85                 if Lock_User == User:
 86                     new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str('0')+'|'+str(Type) + '
')
 87                 # elif User == 'admin':
 88                 #     new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
 89                 else:
 90                     new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
 91         if Lock_User in USER_LIST:
 92             print("
	%s账户冻结成功!" % Lock_User)
 93             print("*".center(25, '*'))
 94         else:
 95             print("
33[1;31;47m...您要冻结的账户不存在!...33[0m
")
 96 
 97 
 98     # if os.path.exists(auth.INFO_BAK):
 99     #     os.remove(auth.INFO_BAK)
100     # os.rename(auth.INFO, auth.INFO_BAK)
101     # os.rename(auth.INFO_TMP, auth.INFO)
102 
103 
104 def search():
105     """
106     定义搜索功能函数,支持精确查找和模糊查找
107     :return:
108     """
109     SEARCH_EXIST = {"is_ok": False}
110     Search_info = input("请输入要搜索的账户信息(账户名或卡号):")
111     with open(auth.INFO,'r') as f:
112         for line in f.readlines():
113             # 布尔值判断是否为空行,strip()忽略空格和换行符

114             if line.strip():
115                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
116                 if Search_info in User:
117                     SEARCH_EXIST = {"is_ok": True}
118                     if Status == '1':
119                         print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账户状态:正常
" % (User, Card_Num,Quota,Balance,Take_Quota))
120                     elif Status == '0':
121                         print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账户状态:锁定!请联系管理员
" % (User, Card_Num,Quota,Balance,Take_Quota))
122                 elif Search_info in Card_Num:
123                     SEARCH_EXIST = {"is_ok": True}
124                     if Status == '1':
125                         print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账户状态:正常
" % (User, Card_Num,Quota,Balance,Take_Quota))
126                     elif Status == '0':
127                         print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账户状态:锁定!请联系管理员
" % (User, Card_Num,Quota,Balance,Take_Quota))
128 
129     if SEARCH_EXIST == {"is_ok": False}:
130         print("
	33[1;31;47m...账户不存在,请确认!...33[0m
")
131 
132 
133 @outer1
134 def update():
135     """
136     定义函数:更新设定指定的账户额度
137     :return:
138     """
139     UPDATE_EXIST = {"is_ok": False}
140     Update_User = input("请输入要更新额度的账户:")
141 
142     with open(auth.INFO, 'r') as f:
143         for line in f:
144             # 布尔值判断是否为空行,strip()忽略空格和换行符

145             if line.strip():
146                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
147                 if Update_User in User:
148                     UPDATE_EXIST = {"is_ok": True}
149     if UPDATE_EXIST == {"is_ok": False}:
150         outp = print("
	33[1;31;47m...账户不存在,请确认!...33[0m
")
151         return outp         # 当前条件如果成立,以下程序就不执行了,执行return
152 
153     Update_Quota = input("请输入要更新的额度:")
154     if Update_Quota.isdigit():
155         with open(auth.INFO, 'r') as old, open(auth.INFO_TMP,'w') as new:
156             for line in old:
157                 # 布尔值判断是否为空行,strip()忽略空格和换行符

158                 if line.strip():
159                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
160                     if Update_User == User:
161                         UPDATE_EXIST = {"is_ok": True}
162                         # 更新后的提现额度 = 更新后的信用额度/2
163                         Update_Take_Quota = int(int(Update_Quota) / 2)
164                         # 更新后的可用额度 = 当前可用额度 + (更新后的信用额度 - 原始信用额度)
165                         Update_Balance = int(Balance) + int(int(Update_Quota) - int(Quota))
166                         new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Update_Quota)+'|'+str(Update_Balance)+'|'+str(Update_Take_Quota)+'|'+str(Status)+'|'+str(Type) + '
')
167                     # elif User == 'admin':
168                     #     new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
169                     else:
170                         UPDATE_EXIST = {"is_ok": True}
171                         new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
172             print("
	%s账户额度更新成功,当前:信用额度%s,可用额度%s,取现额度%s!" % (Update_User,Update_Quota,Update_Balance,Update_Take_Quota))
173             print("*".center(70, '*'))
174 
175         # if os.path.exists(auth.INFO_BAK):
176         #     os.remove(auth.INFO_BAK)
177         # os.rename(auth.INFO, auth.INFO_BAK)
178         # os.rename(auth.INFO_TMP, auth.INFO)
179 
180     else:
181         print("
33[1;31;47mError! 请键入正确的数字33[0m
")
182 
183 
184 @outer1
185 def take():
186     """
187     定义函数:取现
188     :return:
189     """
190     getlog.create_log("进行了'取现'操作")              # 调用日志函数记录操作日志
191     print("当前取现额度%s" % auth.LOGIN_USER['Take_Quota'])
192     Take_Money = input("请输入要提取的金额:")
193     if Take_Money.isdigit():
194         with open(auth.INFO, 'r') as f:
195             for line in f:
196                 # 布尔值判断是否为空行,strip()忽略空格和换行符

197                 if line.strip():
198                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
199                     if auth.LOGIN_USER['current_user'] == User:
200                         if int(Take_Quota) < int(Take_Money):
201                             outp = print("
33[1;31;47m...取现余额不足!...33[0m
")
202                             return outp
203                         else:
204                             # 定义提现手续费5%
205                             Take_Charge = int(int(Take_Money) * 0.05)
206                             with open(auth.INFO, 'r') as old, open(auth.INFO_TMP, 'w') as new:
207                                 for line in old:
208                                     # 布尔值判断是否为空行,strip()忽略空格和换行符

209                                     if line.strip():
210                                         User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
211                                         # 当前提现额度 = 原始取现额度-已取现金额-手续费
212                                         New_Take_Quota = int(Take_Quota) - int(Take_Money) - int(Take_Charge)
213                                         # 当前可用额度 = 原始可用额度-已取现金额-手续费
214                                         New_Balance = int(Balance) - int(Take_Money) - int(Take_Charge)
215                                         if auth.LOGIN_USER['current_user'] == User:
216                                             new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(New_Balance)+'|'+str(New_Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
217                                         # elif User == 'admin':
218                                         #     new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
219                                         else:
220                                             new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota)+'|'+str(Balance)+'|'+str(Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
221                                 # 显示当前提现额度=当前用户的原始额度-提现数-手续费 int(int(auth.LOGIN_USER['Take_Quota'])-int(Take_Money)-int(Take_Charge))
222                                 print("
	%s取现成功! 取现金额%s, 取现手续费%s, 当前取现额度%s" % (auth.LOGIN_USER['current_user'], Take_Money, Take_Charge, int(int(auth.LOGIN_USER['Take_Quota'])-int(Take_Money)-int(Take_Charge)) ))
223                                 print("*".center(70, '*'))
224 
225                             # if os.path.exists(auth.INFO_BAK):
226                             #     os.remove(auth.INFO_BAK)
227                             # os.rename(auth.INFO, auth.INFO_BAK)
228                             # os.rename(auth.INFO_TMP, auth.INFO)
229     else:
230         print("
33[1;31;47mError! 请键入正确的数字33[0m
")
231 
232 
233 @outer1
234 def transfer():
235     """
236     定义函数:转账给指定账户
237     :return:
238     """
239     getlog.create_log("进行了'转账'操作")  # 调用日志函数记录操作日志
240     Transfer_User = input("您需要转账给谁:")
241     Transfer_Money = input("请输入转账金额:")
242     Transfer_Charge = int(int(Transfer_Money) * 0.05)
243     if Transfer_Money.isdigit():
244         with open(auth.INFO, 'r') as old, open(auth.INFO_TMP, 'w') as new:
245             for line in old:
246                 # 布尔值判断是否为空行,strip()忽略空格和换行符

247                 if line.strip():
248                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
249                     if Transfer_User == User:
250                         #接收账户当前余额=原始余额+转账数
251                         New_Balance = int(Balance) + int(Transfer_Money)
252                         #接受账户当前提现额度=原始额度+转账数
253                         New_Take_Quota = int(Take_Quota) + int(Transfer_Money)
254                         new.write(str(User)+'|'+str(Pass)+'|'+str(Card_Num)+'|'+str(Quota) + '|' + str(New_Balance)+'|'+str(New_Take_Quota)+'|'+str(Status)+'|'+str(Type)+'
')
255                     elif auth.LOGIN_USER['current_user'] == User:
256                         #转账账户当前余额=原始余额-转账数
257                         New_Balance = int(Balance) - int(Transfer_Money) - int(Transfer_Charge)
258                         #转账账户当前提现额度=原始额度-转账数
259                         New_Take_Quota = int(Take_Quota) - int(Transfer_Money) - int(Transfer_Charge)
260                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(New_Balance) + '|' + str(New_Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
261                     # elif User == 'admin':
262                     #     new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(Balance) + '|' + str(Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
263                     else:
264                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(Balance) + '|' + str(Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
265             print("
	转账成功!")
266             print("*".center(20, '*'))
267 
268         # if os.path.exists(auth.INFO_BAK):
269         #     os.remove(auth.INFO_BAK)
270         # os.rename(auth.INFO, auth.INFO_BAK)
271         # os.rename(auth.INFO_TMP, auth.INFO)
272 
273     else:
274         print("
Error! 请键入正确的金额
")
275 
276 
277 @outer1
278 def repay():
279     """
280     定义函数:还账单
281     :return:
282     """
283     getlog.create_log("进行了'还款'操作")  # 调用日志函数记录操作日志
284     """
285     重新读取账户相应金额,不可以从默认auth中LOGIN_USER字典读取,那样会引起数据不一致
286     """
287     with open(auth.INFO, 'r') as f:
288         for line in f:
289             if line.strip():
290                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
291                 Bill = int(int(Quota) - int(Balance))
292                 if auth.LOGIN_USER['current_user'] == User:
293                     print("当前账单%s" % int(Bill))
294 
295     Repay_Money = input("请输入还款金额:")
296     if Repay_Money.isdigit():
297         with open(auth.INFO, 'r') as old, open(auth.INFO_TMP, 'w') as new:
298             for line in old:
299                 # 布尔值判断是否为空行,strip()忽略空格和换行符

300                 if line.strip():
301                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
302                     # 新的可用额度 = 原始可用额度 + 还款金额
303                     New_Balance = int(Balance) + int(Repay_Money)
304                     # 新的取现额度 = 原始取现额度 + 还款金额
305                     New_Take_Quota = int(Take_Quota) + int(Repay_Money)
306                     # 新的账单金额 = 原始信用额度 - 新的可用额度
307                     # New_Bill = int(Quota) - int(New_Balance)
308                     if auth.LOGIN_USER['current_user'] == User:
309                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(New_Balance) + '|' + str(New_Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
310                     # elif User == 'admin':
311                     #     new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(
312                     #         Balance) + '|' + str(Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
313                     else:
314                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(Balance) + '|' + str(Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
315             #账单剩余=信用额度-当前余额-还款数
316             print("
	%s还款成功! 还款金额%s, 账单剩余%s" % (auth.LOGIN_USER['current_user'], Repay_Money, int(Bill)))
317             # print("
	%s还款成功! 还款金额%s, 账单剩余%s" % (auth.LOGIN_USER['current_user'], Repay_Money, int(int(auth.LOGIN_USER['Quota']) - int(auth.LOGIN_USER['Balance']) - int(Repay_Money))))
318             print("*".center(70, '*'))
319 
320         # if os.path.exists(auth.INFO_BAK):
321         #     os.remove(auth.INFO_BAK)
322         # os.rename(auth.INFO, auth.INFO_BAK)
323         # os.rename(auth.INFO_TMP, auth.INFO)
324 
325     else:
326         print("
Error! 请键入正确的数字
")
327 
328 
329 def check():
330     """
331     定义查看信息函数:信用额度、可用余额、取现额度
332     :return:
333     """
334     getlog.create_log("进行了'信息查看'操作")  # 调用日志函数记录操作日志
335     with open(auth.INFO,'r') as f:
336         for line in f.readlines():
337             # 布尔值判断是否为空行,strip()忽略空格和换行符

338             if line.strip():
339                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
340                 if auth.LOGIN_USER['current_user'] == User:
341                     if Status == '1':
342                         Bill = int(int(Quota) - int(Balance))
343                         if int(Bill) < 0:
344                             print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账单金额:0,账户状态:正常
" % (User, Card_Num,Quota,Balance,Take_Quota))
345                         else:
346                             print("
	账户:%s,卡号:%s,信用额度:%s,可用余额:%s,取现额度:%s,账单金额:%s,账户状态:正常
" % (User, Card_Num,Quota,Balance,Take_Quota,int(Bill)))
View Code

log-getlog.py

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 
 7 import os
 8 import logging
 9 from bin import auth
10 
11 # from conf import operation
12 
13 
14 def create_log(option):
15     with open(auth.INFO, 'r') as f:
16         for line in f:
17             if line.strip():
18                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
19                 if auth.LOGIN_USER['current_user'] == User:
20                     global LOG_INFO
21                     LOG_INFO = os.path.join("log", auth.LOGIN_USER['current_user']+'_'+"access.log")
22     # LOG_INFO = os.path.join("access.log")
23 
24     # create logger
25     logger = logging.getLogger("操作日志")    # 首先获取日志对象
26     logger.setLevel(logging.INFO)              # 设定全局日志级别(下面局部设定的级别只能比全局高,否则按照全局来)
27 
28     # create console handler and set level to info
29     outstream = logging.StreamHandler()               # 把日志打印到屏幕
30     outstream.setLevel(logging.INFO)                  # 设定输出到屏幕的日志级别
31 
32     # create file handler and set level to info
33     outfile = logging.FileHandler(LOG_INFO)           # 把info日志输出到文件access.log
34     outfile.setLevel(logging.INFO)                    # 设定输出文件的日志级别
35 
36     # create formatter
37     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
38 
39     # add formatter to outstream and outfile
40     outstream.setFormatter(formatter)                 # 设定输出到屏幕的日志格式(可以与输出到文件的不一样)
41     outfile.setFormatter(formatter)                   # 设定输出到access.log文件的日志格式(可以与输出到屏幕的不一样)
42 
43     # add outstream and outfile to logger
44     # logger.addHandler(outstream)                    # 加载到屏幕
45     logger.addHandler(outfile)                        # 加载到文件access.log
46 
47     # 'application' code
48     logger.info(option)
View Code

log-openlog.py

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 
 7 import os
 8 import time
 9 
10 
11 def openlog():
12     checkuser = input("请输入要查询的用户:")
13     log = os.path.join("log", checkuser+'_'+"access.log")
14     if os.path.exists(log):
15         with open(log,'r') as f:
16             for line in f.readlines():
17                 print(line)
18             print("	---日志结果将会停留屏幕5秒钟---
")
19             time.sleep(5)
20     else:
21         print("
	***用户%s未进行过任何操作***
" % checkuser)
22 
23 
24 # openlog()
View Code

log-getwater.py

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 import os
 7 import logging
 8 from bin import auth
 9 
10 # from conf import operation
11 
12 
13 def create_water(option):
14     """
15     定义函数:记录消费流水
16     :param option:
17     :return:
18     """
19     with open(auth.INFO, 'r') as f:
20         for line in f:
21             if line.strip():
22                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
23                 if auth.LOGIN_USER['current_user'] == User:
24                     global LOG_INFO
25                     LOG_INFO = os.path.join("log", auth.LOGIN_USER['current_user']+'_'+"water.log")
26     # LOG_INFO = os.path.join("access.log")
27 
28     # create logger
29     logger = logging.getLogger("消费流水")    # 首先获取日志对象
30     logger.setLevel(logging.INFO)              # 设定全局日志级别(下面局部设定的级别只能比全局高,否则按照全局来)
31 
32     # create console handler and set level to info
33     outstream = logging.StreamHandler()               # 把日志打印到屏幕
34     outstream.setLevel(logging.INFO)                  # 设定输出到屏幕的日志级别
35 
36     # create file handler and set level to info
37     outfile = logging.FileHandler(LOG_INFO)           # 把info日志输出到文件access.log
38     outfile.setLevel(logging.INFO)                    # 设定输出文件的日志级别
39 
40     # create formatter
41     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
42 
43     # add formatter to outstream and outfile
44     outstream.setFormatter(formatter)                 # 设定输出到屏幕的日志格式(可以与输出到文件的不一样)
45     outfile.setFormatter(formatter)                   # 设定输出到access.log文件的日志格式(可以与输出到屏幕的不一样)
46 
47     # add outstream and outfile to logger
48     # logger.addHandler(outstream)                    # 加载到屏幕
49     logger.addHandler(outfile)                        # 加载到文件access.log
50 
51     # 'application' code
52     logger.info(option)
View Code

log-openwater.py

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Tony Chiu
 4 # Blog:http://www.cnblogs.com/tonychiu
 5 # Github:https://github.com/qiujichun
 6 import os
 7 import time
 8 from bin import auth
 9 
10 
11 def openwater():
12     """
13     定义函数:获取消费流水
14     :return:
15     """
16     with open(auth.INFO, 'r') as f:
17         for line in f:
18             if line.strip():
19                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
20                 if auth.LOGIN_USER['current_user'] == User:
21                     global LOG_INFO
22                     LOG_INFO = os.path.join("log", auth.LOGIN_USER['current_user']+'_'+"water.log")
23     # log = os.path.join("log", checkuser+'_'+"water.log")
24     if os.path.exists(LOG_INFO):
25         with open(LOG_INFO,'r') as f:
26             for line in f.readlines():
27                 print(line)
28             print("	---消费流水将会停留屏幕5秒钟---
")
29             time.sleep(5)
30     else:
31         print("
	***用户%s未进行过任何消费***
" % auth.LOGIN_USER['current_user'])
View Code

shop-shopping.py

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # Author: Tony Chiu
  4 # Blog:http://www.cnblogs.com/tonychiu
  5 # Github:https://github.com/qiujichun
  6 
  7 import os
  8 import time
  9 import json
 10 from bin import auth
 11 from log import getlog
 12 from log import getwater
 13 
 14 SHOPPING_LIST = os.path.join("shop", "shopping_list.txt")
 15 
 16 buy_time = time.strftime("%Y.%m.%d %H.%M.%S",time.localtime(time.time()))
 17 
 18 # 定义购物车字典
 19 shopping_cart = {}
 20 
 21 # 定义商品列表字典
 22 Product = {
 23     "家电类":{"厨房用品":[("微波炉",500),("冰箱",1000),("燃气灶",1500)],
 24                "卧室用品":[("空调",3000),("电风扇",200)],
 25                "卫浴用品":[("马桶",1200),("花洒",800)]},
 26     "服装类":{"休闲":[("休闲鞋",300),("休闲裤",200)],
 27                "正装":[("西服",3000),("皮鞋",500)]},
 28     "电子类":{"办公":[("笔记本",5000),("台式机",3000)],
 29                "通讯":[("手机",2000),("座机",100)]},
 30     "食品类":{"生鲜":[("三文鱼",50),("",80)],
 31                "熟食":[("夫妻肺片",30),("烤鸭",40)]}
 32 }
 33 
 34 
 35 def kind1():
 36     """
 37     定义函数:打印一级商品种类
 38     :return:
 39     """
 40     print('''
 41 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 42 *           欢迎%s来到购物商城             *
 43 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **''' %(auth.LOGIN_USER['current_user']))
 44     for kind1_index, kind1_key in enumerate(Product.keys()):
 45         print(kind1_index, '-', kind1_key)
 46     print('End'.center(50, '-'))
 47 
 48 
 49 def kind2():
 50     """
 51     定义函数:打印二级商品种类
 52     :return:
 53     """
 54     for kind2_index,kind2_key in enumerate(Product[kind1_list]):
 55         print('->'.center(3,'-'),kind2_index,kind2_key)
 56     print('End'.center(50,'-'))
 57 
 58 
 59 def kind3():
 60     """
 61     定义函数:打印三级商品种类
 62     :return:
 63     """
 64     for kind3 in enumerate(Product[kind1_list][kind2_list]):
 65         kind3_index = kind3[0]
 66         kind3_key = kind3[1][0]
 67         kind3_price = kind3[1][1]
 68         print('--->'.center(5, '-'), kind3_index, kind3_key, kind3_price)
 69     print('End'.center(50, '-'))
 70 
 71 
 72 def start_shopping():
 73     """
 74     定义函数:生成购物车
 75     :return:
 76     """
 77     select = list(Product[kind1_list][kind2_list])[choice3]  # 生成所选商品的列表信息
 78     select_product = select[0]
 79     select_price = select[1]
 80     select_num = input("请输入商品件数:")
 81     if select_num.isdigit():
 82         select_sum_price = int(select_num) * int(select_price)
 83 
 84         # 判断商品是否已存在于购物车
 85         if select_product in shopping_cart.keys():
 86             exist_num = shopping_cart[select_product]["件数"]         # 取值已存在的件数
 87             exist_sum_price = shopping_cart[select_product]["总价"]   # 取值已存在的总价
 88             current_num = int(exist_num) + int(select_num)             # 当前件数=已存在的加上此次选择的
 89             global current_sum_price                                  # 定义current_sum_price为全局变量,便于payment调用
 90             current_sum_price = int(exist_sum_price) + int(select_sum_price)    # 当前总价=已存在的加上此次选择的
 91             shopping_cart[select_product] = {"件数":int(current_num), "单价": select_price, "总价":int(current_sum_price)}
 92             payment()       # 调用付款函数
 93         else:
 94             current_sum_price = int(select_sum_price)
 95             shopping_cart[select_product] = {"件数":int(select_num),"单价":select_price, "总价":int(current_sum_price)}
 96             payment()       # 调用付款函数
 97 
 98         getwater.create_water("购物花费了%s" %current_sum_price)         # 调用流水函数,记录花费流水
 99         json.dump(shopping_cart, open(SHOPPING_LIST, 'w'))
100         shopping_list = json.load(open(SHOPPING_LIST, 'r'))
101         print("购物清单".center(45,'*'))
102         for key in shopping_list:
103             print(key,shopping_list[key], buy_time.center(5, '-'), '
')
104         print('清单END'.center(50, '-'),'
')
105 
106     else:
107         print("
Error! 请键入正确的数字
")
108 
109 
110 def payment():
111     """
112     定义函数:付款
113     :return:
114     """
115     with open(auth.INFO, 'r') as f:
116         for line in f:
117             # 布尔值判断是否为空行,strip()忽略空格和换行符

118             if line.strip():
119                 User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
120                 if auth.LOGIN_USER['current_user'] == User:
121                     if int(Balance) < int(current_sum_price):
122                         outp = print("
33[1;31;47m...余额不足以支付此次购物!但同样会显示购物清单...33[0m
")
123                         return outp
124                     else:
125                         with open(auth.INFO, 'r') as old, open(auth.INFO_TMP, 'w') as new:
126                             for line in old:
127                                 # 布尔值判断是否为空行,strip()忽略空格和换行符

128                                 if line.strip():
129                                     User, Pass, Card_Num, Quota, Balance, Take_Quota, Status, Type = line.strip().split('|')
130                                     if auth.LOGIN_USER['current_user'] == User:
131                                         # 账户当前可用额度 = 原始可用额度 - 购物总价
132                                         New_Balance = int(Balance) - int(current_sum_price)
133                                         #账户当前取现额度 = 原始取现额度 - 购物总价
134                                         New_Take_Quota = int(Take_Quota) - int(current_sum_price)
135 
136                                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(
137                                             New_Balance) + '|' + str(New_Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
138                                     else:
139                                         new.write(str(User) + '|' + str(Pass) + '|' + str(Card_Num) + '|' + str(Quota) + '|' + str(
140                                             Balance) + '|' + str(Take_Quota) + '|' + str(Status) + '|' + str(Type) + '
')
141 
142                             print("
	付款成功!")
143                             # print("*".center(20, '*'))
144                         if os.path.exists(auth.INFO_TMP):
145                             if os.path.exists(auth.INFO_BAK):
146                                 os.remove(auth.INFO_BAK)
147                             os.rename(auth.INFO, auth.INFO_BAK)
148                             os.rename(auth.INFO_TMP, auth.INFO)
149 
150 
151 def navigation():
152     """
153     定义函数:列出菜单导航
154     :return:
155     """
156     getlog.create_log("进行了'购物'操作")  # 调用日志函数记录操作日志
157     kind1()
158     while True:
159         choice1 = input("[选择种类,按q返回用户菜单] 请输入您的选择:")
160         if choice1.isdigit():
161             choice1 = int(choice1)
162             if choice1 < len(Product):
163                 global kind1_list
164                 kind1_list = list(Product.keys())[choice1]          # 得出选择种类的具体类,比如“电子类”
165                 kind2()
166             else:
167                 print("请输入正确的选择!")
168                 continue
169         elif choice1.lower() == 'q':            # lower转换为小写,无论输入大写还是小写字母都执行返回到用户菜单
170             auth.order()
171         else:
172             print("你输入的不是数字!")
173             continue
174 
175         while True:
176             choice2 = input("[选择分类,按'b'返回上一级,按'q'返回到用户菜单] 请输入您的选择:")
177             if choice2.isdigit():
178                 choice2 = int(choice2)
179                 if choice2 < len(Product[kind1_list]):
180                     global kind2_list
181                     kind2_list = list(Product[kind1_list].keys())[choice2]
182                     kind3()
183                 else:
184                     print("请输入正确的选择!")
185                     continue
186             elif choice2.lower() == 'b':
187                 kind1()
188                 break
189             elif choice2.lower() == 'q':
190                 auth.order()
191             else:
192                 print("你输入的不是数字!")
193                 continue
194 
195             while True:
196                 global choice3
197                 choice3 = input("[选择商品,按'b'返回上一级,按'q'返回到用户菜单] 请输入您的选择:")
198                 if choice3.isdigit():
199                     choice3 = int(choice3)
200                     if choice3 < len(Product[kind1_list][kind2_list]):
201                         start_shopping()
202                         kind3()
203                     else:
204                         print("请输入正确的选择!")
205                         continue
206                 elif choice3.lower() == 'b':
207                     kind2()
208                     break
209                 elif choice3.lower() == 'q':
210                     auth.order()
211                 else:
212                     print("你输入的不是数字!")
213                     continue
View Code


原文地址:https://www.cnblogs.com/tonychiu/p/6378791.html