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

模拟实现ATM+购物商城的程序

时间:2018-10-13 18:15:18      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:phone   ==   action   +=   NPU   deposit   break   HERE   closed   

技术分享图片

技术分享图片

技术分享图片
 1 1、函数从ATM_Program--core下的main文件运行,其中的ATM功能只能有三次登录机会。登陆成功失败与否均不允许再次登陆。
 2 2、文件Bin下的atm.py是信用卡操作主程序:例如账户间转账、存钱、提现功能
 3 3、文件夹Bin下的是manager.py是信用卡的管理端:例如冻结账户、改变信用卡额度、添加账户。
 4 4、manager.py的主程序在accounts.py里。
 5 5、logger.py日志记录模块。记录账户登陆和,购物的流水。
 6 6、transaction.py与settings.py与db_handler.py与transaction.py均暂时没有内容
 7 7、db文件是用户数据存储的地方,account_sample.py能生成初始账户数据。
 8 8、accounts文件夹下面存放各个账户,一个账户一个文件
 9 9、log文件夹下面存放日志:用户访问和交易日志存放在同一个文件夹。
10 10、shooping_mall是电子商城程序。
Readme
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import os,sys
 5 import getpass
 6 site_core = os.path.dirname(os.path.abspath(__file__))
 7 site_atm= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 8 sys.path.append(site_atm)
 9 sys.path.append(site_core)
10 from db import account_sample
11 from core import auth,small_function
12 for i in range(3):
13     @auth.auth
14     def log_in():
15         account = input(Please input your account.)
16         password = input(Please enter your password.)
17         # if password.isdigit():
18         #     password = int(password)
19         return (account,password)
20     log_in()
21     consequence = auth.return_result()#consequence中记载的是登录账户,每次只会记录一个
22     if consequence !=False:
23         while True:
24             operation_choose = input("Please enter your next action is:\nExample:\033[32;1m‘Transfer‘\033[0m"
25                                      ",\033[32;1m‘To save money‘\033[0m,\033[32;1m‘Cash‘\033[0m")
26             if operation_choose == Transfer:#转账
27                 transfer_account = input(Please enter the target account to be transferred:)
28                 judge = auth.auth_account(transfer_account)
29                 if judge == True :
30                     transfer_money = input(Please enter the transfer amount:)
31                     auth.operation_money(consequence,transfer_account,transfer_money)
32                     small_function.progress_bar()
33                     print("\033[23;1m\nOperation succeed.\033[0m")
34                 else:
35                     pass
36             elif operation_choose == To save money:#存钱功能
37                 save_money = input("Please enter the deposit amount:")
38                 auth.save_money(consequence,save_money)
39             elif operation_choose == Cash:#取现功能
40                 cash_money = input("Please enter the withdrawal amount:")
41                 auth.cash(consequence,cash_money)
42             elif operation_choose == q:
43                 break
44             else:
45                 print(\033[31;1mPlease enter the currect opeartion.\033[0m)
46     else:
47         pass
48     #print(consequence)#test
atm.py
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import os,sys
 5 site_core = os.path.dirname(os.path.abspath(__file__))
 6 site_atm= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 7 sys.path.append(site_atm)
 8 sys.path.append(site_core)
 9 from db import account_sample
10 while True:
11     choose = input("please input you want to do function.such as: \033[32;1m‘add account‘\033[0m,\033[32;1m"
12                    "‘change user quota‘\033[0m,\033[32;1m‘frozen account‘\033[0m.".capitalize())
13     if choose ==add account:
14         add_account = input(Please enter the account you want to add.)
15         add_passworld = input(Please enter the password you want to add.)
16         print(\033[33;1mRepeat for safety reasons.\033[0m)
17         add_again_account = input(Please enter the account you want to add.)
18         add_again_passworld = input(Please enter the password you want to add.)
19         if add_account == add_again_account and add_passworld == add_again_passworld:
20             add_money_choose = input(Do you want to initialize the amount?)
21             if add_money_choose != N:
22                 while True:
23                     add_money = input(Please enter amount. )
24                     if add_money.isdigit():
25                         add_money = int(add_money)
26                         account_sample.account_initial(add_account,add_passworld,add_money)
27                         break
28                     else:
29                         print(\033[31;1mPlease enter number.\033[0m)
30             else:
31                 account_sample.account_initial(add_account,add_passworld)
32         else:
33             print(\033[36;1mAccount or password error!\033[0m)
34     elif choose ==change user quota:
35         from core import accounts
36         change_account = input("Please enter you want to change account .")
37         change_money = input("Please enter the amount you want to change.")
38         accounts.quota(change_account,change_money)
39     elif choose == frozen account:
40         from core import accounts
41         frozen_account =input("Please enter you want to frozen account.")
42         accounts.frozen_account(frozen_account)
43     elif choose == qor choose ==Q:
44         break
45     else:
46         print(\033[37;1mplease enter legal character.\033[0m)
manager.py
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import json,sys,os
 5 site_core = os.path.dirname(os.path.abspath(__file__))
 6 sys.path.append(site_core)
 7 import small_function
 8 def account():
 9     打印账户名
10     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_total.json,r)as f:
11         print_account = json.load(f)
12         print("These are the account names:",print_account)
13     return print_account
14 def quota(acco,money):
15     改变账户额度
16     account_sum = account()
17     if acco in account_sum:
18         with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%acco,r) as f1:
19             money_befor = json.load(f1)
20             if money.isdigit:
21                 money = int(money)
22                 money_befor[2][0] = money
23                 with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%acco,w)as f2:
24                     json.dump(money_befor,f2)
25                     small_function.progress_bar()#进度条
26                     print("\n\033[38;1mAleady change quoto\033[0m")
27             else:
28                 print("\033[31;1mPlease enter number.\033[0m")
29     else:
30         print("\033[31;1mAccount have error.\033[0m")
31 #quota(‘admin‘,‘5200‘)#测试
32 def frozen_account(f_account):
33     冻结账户,没有这个账户以及已经存在该账户均会报警
34     account_sum = account()
35     if f_account in account_sum:
36         with open("C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/"
37                   "account_frozen.json",r)as f3:
38             account_ice=json.load(f3)
39             if f_account in account_ice:
40                 print("\033[21;1mThe account have been frozen.\033[0m")
41             else:
42                 with open("C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/"
43                           "account_frozen.json",w)as f4:
44                     account_ice.append(f_account)
45                     json.dump(account_ice,f4)
46                     small_function.progress_bar()#进度条
47                     print("\n\033[39;1mAleady frozen account\033[0m")
48     else:
49         print("\033[39;1mThe account you entered does not exist.\033[0m")
50 #frozen_account(‘test‘)#测试
accounts.py
技术分享图片
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # Author:Source
  4 import os,sys,json
  5 site_atm= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  6 sys.path.append(site_atm)
  7 #print(site_atm)
  8 from db import account_sample
  9 from core import logger,small_function
 10 def auth(func):
 11     验证登录的装饰器
 12     def account_call():
 13         输入指定参数可以调用账户
 14         login=func()
 15         account = login[0]
 16         password = login[1]
 17         with open("C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/"
 18                   "account_frozen.json",r)as f3:
 19             ice_account=json.load(f3)
 20         if account in ice_account:
 21             login_false()
 22             print("\033[22;1mThe account have been frozen.\033[0m")
 23         else:
 24             with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_total.json,r)as f2:
 25                 list_account=json.load(f2)
 26                 if account in list_account:
 27                     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%(account),r)as f_call:
 28                         test = json.load(f_call)
 29                         if test[1]==password :
 30                             info =‘‘‘
 31 ---------------Welcome \033[22;1m{account}\33[0m to login---------------
 32 Account = \033[22;1m{account}\33[0m
 33 Password = \033[22;1m{password}\33[0m
 34 --------------------------------------------------------
 35     ‘‘‘.format(account=login[0],password=login[1])
 36                             login_succeed(login[0])
 37                             small_function.progress_bar()
 38                             print(info)
 39                             logger.record_log(login[0])
 40                         else:
 41                             login_false()
 42                             print(\033[35;1mPassword error!\033[0m)
 43                 else:
 44                     login_false()
 45                     print(\033[34;1mAccount error!\033[0m)
 46     return account_call
 47 def login_succeed(enter):
 48     向verify文件写入一个True,以供接下来的判断
 49     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/verify.json,w)as f:
 50         json.dump(enter,f)
 51 def login_false():
 52     向verify文件写入一个False,以供接下来的判断
 53     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/verify.json,w)as f1:
 54         json.dump(False,f1)
 55 def return_result():
 56     读取verify文件,以供接下来的判断
 57     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/verify.json,r)as f_2:
 58         result=json.load(f_2)
 59     return result
 60 def operation_money(func1,func2,func3):
 61     将账户func1的钱func3转给账户func2,自动判断func3是否是合法字符
 62     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func1,r)as f3:
 63         old_money = json.load(f3)
 64         #print(type(old_money[2][0]))#测试
 65         if func3.isdigit():#是数字才可以进行交易:
 66             func3 = int(func3)
 67             surplus = old_money[2][0]-func3
 68             if surplus>=0:#钱有够才可以进行转账
 69                 with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func2,r)as f4:
 70                     target_account_money = json.load(f4)
 71                     target_account_money[2][0] = target_account_money[2][0]+func3
 72                     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func2,w)as f5:
 73                         json.dump(target_account_money,f5)
 74                         with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func1,w)as f6:
 75                             old_money[2][0]=surplus
 76                             json.dump(old_money,f6)
 77             else:
 78                 print("\033[33;1mTrading money operates account amonunts.\033[0m")
 79         else:
 80             print("\033[32;1m‘func3‘Not numbers.\033[0m")
 81 #operation_money(‘source‘,‘shui‘,‘500‘)#test
 82 def auth_account(exist):
 83     验证账户是否存在以及是否在黑名单中
 84     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_total.json,r)as f7:
 85         total_account = json.load(f7)
 86         if exist in total_account:
 87             with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_frozen.json,r)as f8:
 88                 frozen_account = json.load(f8)
 89                 if exist in frozen_account:
 90                     print("\033[22;1mThe account have been frozened.\033[0m")
 91                 else:
 92                     return True
 93         else:
 94             print("\033[24;1mAcoount does not exist.\033[0m")
 95 #test = auth_account(‘source‘)#测试
 96 def save_money(func,how):
 97     存钱功能
 98     judge = auth_account(func)#只要在登录程序就可以了
 99     if judge == True:
100         if how.isdigit():
101             how = int(how)
102             with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func,r)as f9:
103                 func_total = json.load(f9)
104                 func_total[2][0] = func_total[2][0]+how
105                 with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%func,w)as f10:
106                     json.dump(func_total,f10)
107                     small_function.progress_bar()
108                     print("\nOperation is successful.")
109         else:
110             print("\033[32;1m‘func3‘Not numbers.\033[0m")
111     else:
112         pass
113 #test_save_money = save_money(‘source‘,‘3000‘)#测试
114 def cash(account,money):
115     "‘account‘卡里的钱‘money’取出"
116     if money.isdigit():
117         money = int(money)
118         with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%account,r)as f11:
119             account_total = json.load(f11)
120             if account_total[2][0]>(money+money*0.05):
121                 account_total[2][0] = account_total[2][0]-money*(1+0.05)
122                 with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%account,w)as f12:
123                     json.dump(account_total,f12)
124                     small_function.progress_bar()
125                     print("\nThe balance of the credit card is \033[26;1m[%s]\033[0m"%account_total[2][0])
126             else:
127                 print("\033[25;1mExcess deposit amount.\033[0m")
128     else:
129         print("\033[32;1m‘func3‘Not numbers.\033[0m")
130 #test_cash = cash(‘source‘,‘1000‘)#测试
131 #print(__file__)
auth
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 #coding utf-8
 4 import logging
 5 logging.basicConfig(level=logging.DEBUG,format=Asctime:%(asctime)s,levelname:%(levelname)s,%(message)s,filename: %(filename)s,
 6 filename=C:/Users/ys106/PycharmProjects/Source/day4/log/access.log,datefmt=[%d/%b/%Y %H:%M:%S],filemode=a
 7 )
 8 def record_log(*args):
 9     logging.info(%s Log in time.%args)
10 def Goods_flowing_water(*args):
11     logging.info(Sell [%s] goods.%args)
logger.py
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import os,sys
 5 site_core = os.path.dirname(os.path.abspath(__file__))
 6 site_atm= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 7 sys.path.append(site_atm)
 8 sys.path.append(site_core)
 9 sys.path.append("C:\\Users\\ys106\\PycharmProjects\\Source\\day4\\homework\\ATM_Program")
10 while True:
11     function_choose = input(Do you want to use \‘\033[32;1mATM\033[0m\‘ function or \‘\033[32;1mShopping\033[0m\‘or 
12                             \‘\033[32;1mother\033[0m\‘ function? Just say it once ,press \‘q\‘to quit.)
13     if function_choose ==ATM:
14         from bin import atm
15     elif function_choose ==Shopping:
16         from shopping_mall import go_shopping
17     elif function_choose == other:
18         from bin import manage
19     elif function_choose ==q:
20         break
21     else:
22         print(\033[31;1mPlease enter legal characters.\033[0m)
main.py
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import sys,time
 5 def progress_bar():
 6     进度条
 7     for i in range(50):
 8         sys.stdout.write("#")
 9         sys.stdout.flush()
10         time.sleep(0.1)
small_function.py
技术分享图片
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Source
 4 import json
 5 def account_initial(account_new,password_new,*args):
 6     调用函数输入指定参数可以创建一个账户
 7     account = account_new
 8     password = password_new
 9     if args != ‘‘:
10         amount = args
11     else:
12         amount = 15000
13     test_list = [account,password,amount]
14     with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json%(account),w)as f:
15         json.dump(test_list,f)
16         print(Creat account succeed!)
17         f1 = open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_total.json,r)
18         test = json.load(f1)
19         #print(test)
20         f1.closed
21         if account in test:
22             pass
23         else:
24             test.append(account)
25             #print(test)
26             with open(C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/account_total.json,w)as f2:
27                 json.dump(test,f2)
28 #account_initial(‘source‘,‘159753‘,12000)#测试
account_sample.py
技术分享图片
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Source
import json,os,sys,getpass
#site_homework = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
#sys.path.append(site_homework)
site_ATM_Program= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(site_ATM_Program)
sys.path.insert(0,"C:\\Users\\ys106\\PycharmProjects\\Source\\day4\\homework\\ATM_Program\\atm")
from core import auth
from core import logger
commodity=[
("iphoneX".title(),8388),
("ipad mini4".title(),2400),
("dragon fruit".title(),6),
("alkaline mineral water".title(),2),
("toothpaste".title(),12),
]
"""
‘json文件读写测试‘
with open("test.json",‘w‘)as f_write:
    json.dump(commodity,f_write)
with open("test.json","r")as f_read:
    test_total=json.load(f_read)
    print(len(test_total))"""
shopping_cart=[]
#print(commodity)
while True:
    for item in commodity:
        print(commodity.index(item),item)
    #for i,item in  enumerate(commodity):#python 的for in
     #   print(i,item)
    number=input("Please enter the item number you want to buy:")
    if number.isdigit():
        number=int(number)
        #print(len(commodity))#显示商品列表的长度
        if number<len(commodity) and number>=0:
            list_price=commodity[number]
            shopping_cart.append(list_price)
            print("[%s] have been added to the shopping cart. "%(list_price[0]))
        else:
            print("\033[25;1mThe number does not exist.\033[0m")
    elif number == qor number ==Q:
        print("Shopping cart aleady exist goods.")
        for goods in shopping_cart:
            print("\033[31;1m",shopping_cart.index(goods),goods,"\033[0m")
        choose_shopping_cart = input("Confirm the purchase? if there is an item you do not want to buy,just select its number:")
        if choose_shopping_cart.isdigit():
            choose_shopping_cart = int(choose_shopping_cart)
            del shopping_cart[choose_shopping_cart]
        elif choose_shopping_cart == ‘‘:
            transition =0
            for goods in shopping_cart:
                goods_money = goods[1]
                transition += goods_money
            print("The total value of goods is:\033[27;1m%s.\033[0m"%transition)
            @auth.auth
            def log_in():
                account = input(Please input your account.)
                password = input(Please enter your password.)
                return (account,password)
            log_in()
            consequence = auth.return_result()#consequence中记载的是登录账户,每次只会记录一个
            if consequence !=False:
                with open("C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json"%consequence,r)as f_account_begin:
                     account_total = json.load(f_account_begin)
                     if account_total[2][0]<transition:
                         print("\033[27;1mNot enough money.\033[0m")
                     else:
                         account_total[2][0] = account_total[2][0]-transition
                         with open("C:/Users/ys106/PycharmProjects/Source/day4/homework/ATM_Program/atm/db/accounts/%s.json"%consequence,w)as f_account_finish:
                             json.dump(account_total,f_account_finish)
                             logger.Goods_flowing_water(shopping_cart)
                             print("Operation is successful.")
        else:
            print("\033[25;1mPlease enter legal characters.\033[0m")
    else:
        print("\033[25;1mPlease enter legal characters.\033[0m")
go_shopping.py

 

模拟实现ATM+购物商城的程序

标签:phone   ==   action   +=   NPU   deposit   break   HERE   closed   

原文地址:https://www.cnblogs.com/source12/p/9783526.html

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