ATM+カート

16831 ワード

目次
  • ATMカート
  • 一、1つのプロジェクトがどのように無から有への
  • であるか
  • 二、プロジェクト需要
  • 三、プロジェクト開発
  • random.txtプロジェクト説明ファイル
  • start.pyプロジェクト起動ファイル
  • conf--------setting.pyシステム環境変数構成
  • core------src.pyビジネスコアロジック
  • db-----db_hander.py実データ層
  • interface-----admin_interface.py管理者インタフェース
  • interface----bank_interface.py銀行インタフェース
  • interface----shoping_interface.pyショッピングインタフェース
  • interface----user_interface.pyユーザインタフェース
  • lib----common.py共通機能

  • ATMカート
    一、一つのプロジェクトがどのように無から有になるか
    1.    
          ,  ,    ,  ,   ,
          ,  ,  ,    ,  ,   ,
             ,       ,     
    
    2.       
            :
                 :          ,        .       ,          .
               :       .
                 :          .
    
    3.     
    4.  
    5.    

    二、プロジェクトの需要
    1.  
    2.  
    3.  
    4.    
    5.  
    6.  
    7.    
    8.  
    9.      
    10.   
    q.   
    
    
             
       :  、    

    三、プロジェクト開発
    random.txtプロジェクト説明ファイル
    ATM +    
    
      :             .
    
          
                 ,         .
    
        -   15000      --->     
    
        -       ,        ,           --->    ,     
    
        -     ,   5%  --->   
    
        -          --->   
    
        -           --->   
    
        -             --->     
    
        -         --->   
    
        - ATM        --->     
    
        -       ,      、    ,     ...  --->      
    
        -         --->     ,     
    
             
             :
                       ,        .
                     .
    
           :
                  .
    
             :
                   .
             , , ,   .
    
               :
            1.      
            2.     .
            3.   ,  .
    
    
           
        - CTO
            -     
                -    
                    -     
                        -     
                            UI:        
                              :      
                              :      ,   
                              :     
                              :     .
    
        
        -     
                     .
    
        -      
                      ,       .
    
        -     :
                     .
    
        -     :
                      ,             .
    
          
                     ,     .

    start.pyプロジェクト起動ファイル
    import os
    import sys
    from core import src
    BASE_PATH = os.path.dirname(__file__)
    #        
    sys.path.append(BASE_PATH)
    
    if __name__ == '__main__':
        src.run()

    conf--------setting.pyシステム環境変数構成
    import os
    
    BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    DB_PATH = os.path.join(BASE_PATH, 'db')
    
    

    core------src.pyビジネスコアロジック
    from interface import user_interface
    from interface import bank_interface
    from interface import shoping_interface
    from lib import common
    from interface import admin_interface
    import datetime
    
    user_info = {
        'user': None
    }
    
    
    def register():
        while True:
            print("---  ---")
            user_name = input('      :').strip()
            passwd = input('     :').strip()
            passwd_d = input('    :').strip()
            #   
            flat = user_interface.check_user_interface(user_name)
            if flat:
                print('     ,    !')
                continue
            elif passwd == passwd_d:
                #   
                user_interface.register_inerface(user_name, passwd)
                print('    !')
                break
    
    
    def login():
        while True:
            print("---  ---")
            user = input('     :').strip()
            passwd = input('    :').strip()
            flag, msg = user_interface.login_interface(user, passwd)
            if flag:
                print(msg)
                user_info['user'] = user
                break
            else:
                print(msg)
                break
    
    
    @common.outter
    def transfer():
        while True:
            print("---  ---")
            to_name = input('        :')
            to_user = user_interface.check_user_interface(to_name)
            if to_user:
                money = input('       :').strip()
                if money.isdigit():
                    money = int(money)
                    flaw, msg = bank_interface.transfer_interface(to_name, money, user_info['user'])
                    if flaw:
                        print(msg)
                        break
                    else:
                        print(msg)
                        break
                else:
                    print('     !!')
                    continue
    
            else:
                print('     ,    !')
                continue
    
    
    @common.outter
    def check_balance():
        print("---    ---")
        bank_interface.select_money(user_info['user'])
    
    
    @common.outter
    def repayment():
        print("---  ---")
        money = input('      :').strip()
        if money.isdigit():
            money = int(money)
            bank_interface.repayment_interface(user_info['user'], money)
        else:
            print('     !')
    
    
    @common.outter
    def withdraw_money():
        print("---  ---")
        money = input('      :').strip()
        if money.isdigit():
            money = int(money)
            if money >= 0:
                bank_interface.withdraw_interface(user_info['user'], money)
            else:
                print('    0')
        else:
            print("     !")
    
    
    @common.outter
    def view_pipelining():
        print("---    ---")
        bank_interface.see_worter_interface(user_info['user'])
    
    
    @common.outter
    def shopping():
    
        #       
        pay_list = []
        num_money = 0
        while True:
            print("---  ---")
            shopping_list = [
                ['QBZ95    ', 999],
                ['M4A1', 999],
                ['  ', 99],
                ['   ', 299],
                ['     ', 199],
                ['  ', 5000000],
                ['    VIP', 1000000]
            ]
    
            #       
            for index, i in enumerate(shopping_list):
                print(index, i)
            print('q.   w.   e.      ')
            choice = input('         :').strip()
            if choice == 'q':
                break
    
            elif choice == 'w':
                yes = input('    ?y/n:')
                if yes == 'y':
                    #       
                    bank_interface.payment(num_money, user_info['user'])
                    #           
                    shoping_interface.save_car(pay_list, user_info['user'])
                    break
                elif yes == 'n':
                    continue
            elif choice == 'e':
                print('---    ---')
                for index, i in enumerate(pay_list):
                    print(index, i)
                continue
    
            # 1.         
            if not choice.isdigit():
                print('     !!!       ')
                continue
            # 2.       ,  int   
            choice = int(choice)
            # 3.          
            if 0 <= choice <= len(shopping_list):
                name, money = shopping_list[choice]
    
                # 4.       
                now_time = datetime.datetime.today()
                now_time = str(now_time)
                #         2019-11-21 18:45:18.803910     2019-11-21 18:45:18
                now_time = now_time[0:19]
                #     
                shopping_list[choice].append(now_time)
                pay_list.append(shopping_list[choice])
                #   
                num_money += money
                print('    ')
                continue
            else:
                print('        !!!')
                continue
    
    
    @common.outter
    def shopping_cat():
        while True:
            print("---      ---")
            shoping_interface.select_car(user_info['user'])
            break
    
    
    def admin():
        while True:
            print('''
            1:    
            2:    
            q:  
            ''')
            dict = {
                '1': lock,
                '2': unlock
            }
    
            choice = input('       :').strip()
            if choice == 'q':
                break
            elif not choice.isdigit():
                print('     !!')
                continue
            elif choice in dict:
                dict[choice]()
            else:
                print("      ,    !!!")
                continue
    
    
    def lock():
        print('---    ---')
        user_name = input('           :').strip()
        yes = input('       ?  y/n:')
        if yes == 'y':
            res = admin_interface.lock_interface(user_name)
            print(res)
        elif yes == 'n':
            print('     !')
        else:
            print('    ,    !')
    
    
    def unlock():
        print('---    ---')
        user_name = input('           :').strip()
        yes = input('       ?  y/n:')
        if yes == 'y':
            res = admin_interface.unlock_interface(user_name)
            print(res)
        elif yes == 'n':
            print('     !')
        else:
            print('    ,    !')
    
    
    
    
    def run():
        while True:
            print('''
            1.  
            2.  
            3.  
            4.    
            5.  
            6.  
            7.    
            8.  
            9.      
            10.   
            q.      
            ''')
            list_dic = {
                '1': register,
                '2': login,
                '3': transfer,
                '4': check_balance,
                '5': repayment,
                '6': withdraw_money,
                '7': view_pipelining,
                '8': shopping,
                '9': shopping_cat,
                '10': admin
            }
            choice = input('       :').strip()
            if choice == 'q':
                break
    
            elif choice in list_dic:
                list_dic.get(choice)()
    
            else:
                print('      ,     :')
                continue
    

    db-----db_hander.pyリアルデータ層
    from conf import setting
    import os
    import json
    
    
    def select_user(user):
        user_path = f'{setting.DB_PATH}/{user}.json'
    
        if os.path.exists(user_path):
            with open(user_path,'r',encoding='utf-8')as f:
                user_dict = json.load(f)
                return user_dict
        else:
            return False
    
    
    def save(user_dict):
        #   
        user_path = f'{setting.DB_PATH}/{user_dict["user"]}.json'
        #   
        with open(user_path, 'w', encoding='utf-8')as f:
            json.dump(user_dict, f, ensure_ascii=False)
            f.flush()
    

    interface-----admin_interface.py管理者インタフェース
    from db import db_hander
    
    
    #     
    def lock_interface(user_name):
            user_dict = db_hander.select_user(user_name)
            if user_dict:
                user_dict['lock'] = False
                #     
                db_hander.save(user_dict)
                return '   !'
            else:
                return '      ,    !'
    
    
    #     
    def unlock_interface(user_name):
        user_dict = db_hander.select_user(user_name)
        if user_dict:
            user_dict['lock'] = True
            #     
            db_hander.save(user_dict)
            return '   !'
        else:
            return '      ,    !'

    interface----bank_interface.py銀行インタフェース
    from db import db_hander
    import datetime
    
    #     
    def transfer_interface(to_name, money, user):
        to_user_dict = db_hander.select_user(to_name)
        user_dict = db_hander.select_user(user)
    
        if to_name != user:
            #             
            if 0 <= money <= user_dict['balance']:
                #       
                user_dict['balance'] -= money
                to_user_dict['balance'] += money
    
                #   
                my = user_dict['balance']
                to = to_user_dict['balance']
                time = datetime.datetime.today()
                msg = f'【{user}】   【{to_name}】    【{money}】 ,    【{my}】     {time}'
                flg = f'【{to_name}】     【{user}】    【{money}】 ,    【{to}】     {time}'
                #        
                user_dict['worter'].append(msg)
                to_user_dict['worter'].append(flg)
    
                #     
                db_hander.save(user_dict)
                db_hander.save(to_user_dict)
    
    
                return True, msg
            else:
                msg = '    '
                return False, msg
        else:
            return False, '          !'
    
    #     
    def select_money(user):
        user_dict = db_hander.select_user(user)
        money = user_dict['balance']
        return print('     【%s】 ' % money)
    
    
    #     
    def repayment_interface(user, money):
        user_dict = db_hander.select_user(user)
        #     
        user_dict['balance'] += money
    
        a = user_dict['balance']
        time = datetime.datetime.today()
        msg = f'【{user}】    【{money}】   ,    【{a}】    {time}'
        #        
        user_dict['worter'].append(msg)
        #     
        db_hander.save(user_dict)
    
    
        return print(msg)
    
    
    #     
    def withdraw_interface(user, money):
        while True:
            user_dict = db_hander.select_user(user)
            #         
            if money <= user_dict['balance']*1.05:
                #     ,   
                money_s = money*0.05
                user_dict['balance'] -= money*1.05
    
                a = user_dict['balance']
                time = datetime.datetime.today()
                msg = f'【{user}】    【{money}】   ,   5%【{money_s}】 ,    【{a}】    {time}'
    
                #        
                user_dict['worter'].append(msg)
    
                #     
                db_hander.save(user_dict)
                return print(msg)
            else:
                print('    !!')
                break
    
    
    #     
    def see_worter_interface(user):
        user_dict = db_hander.select_user(user)
        worter = user_dict['worter']
        for i in worter:
            print(i)
    
    #     
    def payment(num_money,user):
        while True:
            user_dict = db_hander.select_user(user)
            if num_money <= user_dict['balance']:
                #      
                user_dict['balance'] -= num_money
    
                a = user_dict['balance']
                time = datetime.datetime.today()
                msg = f'【{user}】        ,  【{num_money}】 ,    【{a}】     {time}'
                #     
                user_dict['worter'].append(msg)
                #     
                db_hander.save(user_dict)
                print(msg)
                break
            else:
                print('    ,   !!!')
                break
    
    

    interface----shoping_interface.pyショッピングインタフェース
    from db import db_hander
    
    #      
    def save_car(shopping_list, user):
        user_dict = db_hander.select_user(user)
        #      
        user_dict['shop_car'].append(shopping_list)
        #   
        db_hander.save(user_dict)
    
    #      
    def select_car(user):
        user_dict = db_hander.select_user(user)
        car_list = user_dict['shop_car']
        # for index, i in enumerate(car_list):
        #     print(index, i)
        for line in car_list:
            for index, i in enumerate(line):
                print(index, i)
    
    

    interface----user_interface.pyユーザーインタフェース
    from db import db_hander
    from lib import common
    
    
    #           ,    Turn   ,     False
    def check_user_interface(user):
        user_dict = db_hander.select_user(user)
        if user_dict:
            return user_dict
        else:
            return False
    
    
    #     
    def register_inerface(user, passwd):
        #     ,  
        pwd = common.get_md5(passwd)
    
        #              ,        
        user_dict = {
            'user': user,
            'pwd': pwd,
            'balance': 10000,
            'worter': [],
            'shop_car': [],
            'lock': True
        }
    
        db_hander.save(user_dict)
    
        return f'{user_dict["user"]}      !'
    
    
    #     
    def login_interface(user, passwd):
        user_dict = db_hander.select_user(user)
        passwd = common.get_md5(passwd)
        #       
        if user_dict:
            #         
            if user_dict['lock'] == True:
                #       
                if passwd == user_dict['pwd']:
                    return True, '    !'
                else:
                    return False, '         ,    !'
            else:
                return False, '      ,      !'
        else:
            return False, '         ,    !'
    

    lib----common.py共通機能
    import hashlib
    from core import src
    
    def get_md5(pwd):
        val = '        '
        md5 = hashlib.md5()
        md5.update(val.encode('utf-8'))
        md5.update(pwd.encode('utf-8'))
        res = md5.hexdigest()
        return res
    
    def outter(func):
    
        def inner(*args, **kwargs):
            while True:
                if src.user_info['user']:
                    res = func(*args, **kwargs)
                    return res
                else:
                    print('    !')
                    break
        return inner