zoukankan      html  css  js  c++  java
  • ATM

    编写ATM程序实现下述功能,数据来源于文件db.txt

    # coding:utf-8
    
    user_info = {
        'username':None
    }
    import os
    BASE_PATH = os.path.dirname(__file__)
    DB_PATH = os.path.join(BASE_PATH, 'db')
    
    
    def save(user_name, user_write):
        '''保存用户'''
        user_path = os.path.join(DB_PATH, f'{user_name}.txt')
        with open(f'{user_path}', 'w', encoding='utf-8')as f:
            f.write(user_write)
    
    def select(user_name):
        '''查看用户'''
        user_path = os.path.join(DB_PATH, f'{user_name}.txt')
        if os.path.exists(user_path):
            with open(f'{user_path}', 'r', encoding='utf-8')as f:
                user_info = f.read().strip().split(':')
                return True, user_info
        else:
            return False, '用户不存在'
    
    
    def login():
        '''用户登录'''
        user_name = input('请输入用户名:').strip()
        flag, msg = select(user_name)
        if flag:
            count = int(msg[3])
            while True:
                if count == 3:
                    res = f'{msg[0]}:{msg[1]}:{msg[2]}:{count}'
                    save(user_name, res)
                    print('该用户已被冻结!')
                    break
                user_pwd = input('请输入密码:').strip()
                if user_pwd == msg[1]:
                    user_info['username'] = user_name
                    print('登录成功!')
                    break
                else:
                    print('密码错误!')
                    count += 1
                    # print(count)
        else:
            print(msg, '请先注册')
    
    
    
    def check_balance():
        '''查看余额'''
        if user_info['username']:
            username = user_info['username']
            flag, msg = select(username)
            print(f'{username}余额为:{msg[2]}')
        else:
            print('用户没有登录,请先登录!')
            login()
    
    def withdraw():
        '''提现功能'''
        if user_info['username']:
            flag, msg = select(user_info['username'])
            balance = msg[2]
            balance = int(balance)
            print(balance,type(balance))
            while True:
                with_money = input('请输入提现金额:').strip()
                with_money = int(with_money)
                if balance >= with_money:
                    balance -= with_money
                    res = f'{msg[0]}:{msg[1]}:{balance}:{msg[3]}'
                    save(user_info['username'], res)
                    print('提现成功')
                    break
                else:
                    print('余额不足')
        else:
            print('用户没有登录,请先登录')
            login()
    
    def transfer():
        '''转账功能'''
        tag = True
        if user_info['username']:
            while tag:
                to_user = input('请输入转账用户:').strip()
                flag, msg = select(to_user)
                if flag:
                    while True:
                        trans_money = input('请输入转账金额:').strip()
                        trans_money = int(trans_money)
                        flag1, msg1 = select(user_info['username'])
                        balance = int(msg1[2])  #转账用户余额
                        to_balance = int(msg[2]) #收账用户余额
                        if balance >= trans_money:
                            balance -= trans_money
                            res1 = f'{msg1[0]}:{msg1[1]}:{balance}:{msg1[3]}'
                            save(user_info['username'], res1)
                            to_balance += trans_money
                            res2 = f'{msg[0]}:{msg[1]}:{to_balance}:{msg[3]}'
                            save(to_user, res2)
                            tag = False
                            break
                        else:
                            print('余额不足')
                else:
                    print('转账用户不存在')
        else:
            print('用户未登录,请先登录!')
            login()
    
    def recharge():
        '''充值功能'''
        if user_info['username']:
            recharge_money = input('请输入充值金额:').strip()
            recharge_money = int(recharge_money)
            flag, msg = select(user_info['username'])
            balance = int(msg[2])
            balance += recharge_money
            res = f'{msg[0]}:{msg[1]}:{balance}:{msg[3]}'
            save(user_info['username'], res)
        else:
            print('用户未登录,请先登录')
            login()
    
    def logout():
        if user_info['username']:
            user_info['username']=None
    
    
    def register():
        while True:
            user_name = input('请输入注册用户名:').strip()
            flag, msg = select(user_name)
            if flag:
                print('该用户已存在!')
                continue
            else:
                user_pwd = input('请输入密码:').strip()
                user_info = f'{user_name}:{user_pwd}:1000:0'
                save(user_name, user_info)
                print('注册成功')
                break
     #功能字典           
    func_dic = {
        '1':('登录',login),
        '2':('查看余额',check_balance),
        '3':('提现', withdraw),
        '4':('转账', transfer),
        '5':('充值', recharge),
        '6':('注册', register),
        '7':('退出登录', logout),
        'q':('退出程序', exit)
    }
    
    def run():
      '''主运行函数'''
        while True:
            print('=====function=======')
            for i in func_dic:
                print(i, func_dic[i][0])
            print('=========end========')
    
            choice = input('请输入功能编号:').strip()
            if choice == 'q':
                break
            elif not choice.isdigit():
                print('请输入数字!')
                continue
            elif choice not in func_dic:
                print('请输入正确的功能编号')
                continue
            else:
                func_dic.get(choice)[1]()
    
    if __name__ == '__main__':
        run()
    

  • 相关阅读:
    HDU 1102 Constructing Roads
    HDU 1285 确定比赛名次。
    最小生成树 HDU 各种畅通工程的题,prim和kru的模板题
    HDU Jungle Roads 1301 最小生成树、
    并查集小结(转)
    HDU hdu 2094 产生冠军 拓扑排序 判定环
    模运算(转)
    拓扑排序(主要是确定环和加法) HDU 2647 Reward
    HDU 1372 Knight Moves 简单BFS
    用计算机模型浅析人与人之间沟通方式 (一)如何谈话
  • 原文地址:https://www.cnblogs.com/xy-han/p/12533507.html
Copyright © 2011-2022 走看看