day13作业

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

 

import os
def change_file(file_path,src_data,des_data):
    """文件内容修改功能"""
    if os.path.exists(file_path):
        swap_path=file_path+".swap"
        with open(file_path,"r",encoding="utf-8") as src_file,\
            open(swap_path,"w",encoding="utf-8") as des_file:
            for line in src_file:
                if src_data in line:
                    line=line.replace(src_data,des_data)
                des_file.write(line)
        os.remove(file_path)
        os.rename(swap_path,file_path)
    else:
        print("文件{}不存在。".format(file_path))

 

# 2、编写tail工具

import time,os

def use_tail(file_path):
    """tail功能"""
    if os.path.exists(file_path):
        with open(file_path,"rb") as f:
            f.seek(0,2)
            while True:
                line=f.readline()
                if len(line) == 0:
                    time.sleep(1)
                else:
                    print(line.decode("utf-8"), end="")
    else:
        print("{}文件不存在".format(file_path))

 

# 3、编写登录功能

def u_login(username,passwd):
    """用户登录验证"""
    with open("u_db.txt","r",encoding="utf-8") as f:
        for line in f:
            name,pwd,identity=line.strip("\n").split(":")
            if username == name and passwd == pwd:
                print("用户{}登录成功,身份为{}。".format(username,identity))
                break
        else:
            print("用户名或密码错误。")

 

# 4、编写注册功能

def  register(username,passwd,identity):
    """注册用户"""
    with open("u_db.txt", "r", encoding="utf-8") as f:
        for line in f:
            if username in line:
                print("用户{}已被注册".format(username))
                break
    with open("u_db.txt","a",encoding="utf-8") as fa:
        fa.write("{}:{}:{}\n".format(username,passwd,identity))
        print("用户{}注册成功。".format(username))

 

# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额

# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作

目录

 

 db.txt内容:

zhangsan:123:200
lisi:123:10
egon:123:455365
alex:123:152222
jc:123:0
lxx:123:12280

atm程序.py代码:

 

# 开发人员: alias
# 开发时间: 2020/3/17 16:06
# 文件名称: atm程序.py
# 开发工具: PyCharm
import os
from prettytable import PrettyTable


def func_menu_login():
    """打印登录之前功能菜单"""
    x = PrettyTable(field_names=["功能id", "功能"])
    x.add_row(["0", "退出"])
    x.add_row(["1", "登录"])
    x.add_row(["2", "注册"])
    print(x)


def func_menu_use():
    """打印登录成功的功能菜单"""
    x = PrettyTable(field_names=["功能id", "功能"])
    x.add_row(["0", "充值"])
    x.add_row(["1", "转账"])
    x.add_row(["2", "提现"])
    x.add_row(["3", "显示余额"])
    x.add_row(["4", "退出"])
    print(x)


def user_login(user_name, passwd):
    """用户登录验证"""
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            name, pwd, money = line.strip("\n").split(":")
            if user_name == name and passwd == pwd:
                print("用户{}登录成功。".format(user_name))
                return 1
        else:
            print("用户名或密码错误。")


def user_register(user_name, passwd):
    """注册用户"""
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            if user_name in line:
                print("用户{}已被注册".format(user_name))
                return
    with open("db.txt", "a", encoding="utf-8") as fa:
        fa.write("{}:{}:{}\n".format(user_name, passwd, "0"))
        print("用户{}注册成功。".format(user_name))


def deposit(user_name, money):
    """用户充值"""
    with open("db.txt", "r", encoding="utf-8") as f, \
            open(".db.txt.swap", "w", encoding="utf-8") as fs:
        for line in f:
            name, pwd, mon = line.strip("\n").split(":")
            if user_name in line:
                mon = money
            fs.write("{}:{}:{}\n".format(name, pwd, mon))
        print("用户{}成功充值{}元。".format(user_name, money))
    os.remove("db.txt")
    os.rename(".db.txt.swap", "db.txt")


def transfer(transfer_out_user, transfer_in_user, money):
    """用户转账"""
    user_exist = False
    money=int(money)
    money_flag=True
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            if transfer_in_user in line:
                user_exist = True
                break
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            name, pwd, mon = line.strip("\n").split(":")
            mon = int(mon)
            if transfer_out_user in line:
                if mon < money:
                    money_flag=False
    if user_exist:
        if money_flag:
            with open("db.txt", "r", encoding="utf-8") as f, \
                    open(".db.txt.swap", "w", encoding="utf-8") as fs:
                for line in f:
                    name, pwd, mon = line.strip("\n").split(":")
                    mon = int(mon)
                    if transfer_out_user in line:
                        mon -= money
                    if transfer_in_user in line:
                        mon += money
                    fs.write("{}:{}:{}\n".format(name, pwd, mon))
                else:
                    print("用户{}成功转账{}元到{}用户。".format(transfer_out_user, money, transfer_in_user))
            os.remove("db.txt")
            os.rename(".db.txt.swap", "db.txt")
        else:
            print("尊敬的用户{}您的余额不足,无法进行转账操作。".format(transfer_out_user))

    else:
        print("{}用户不存在".format(transfer_in_user))


def withdraw(username, money):
    """用户提现"""
    money = int(money)
    money_flag = True
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            name, pwd, mon = line.strip("\n").split(":")
            mon = int(mon)
            if username in line:
                if mon < money:
                    money_flag=False
    if money_flag:
        with open("db.txt", "r", encoding="utf-8") as f, \
                open(".db.txt.swap", "w", encoding="utf-8") as fs:
            for line in f:
                name, pwd, mon = line.strip("\n").split(":")
                mon = int(mon)
                if username in line:
                    mon -= money
                fs.write("{}:{}:{}\n".format(name, pwd, mon))
            else:
                print("用户{}成功提现{}元。".format(username, money))
        os.remove("db.txt")
        os.rename(".db.txt.swap", "db.txt")
    else:
        print("账户余额不足。无法进行提现操作。")


def show_money(username):
    """显示余额"""
    with open("db.txt", "r", encoding="utf-8") as f:
        for line in f:
            name, pwd, mon = line.strip("\n").split(":")
            if username in line:
                print("{}用户账户余额为{}".format(username, mon))
                # return mon


flag = True
while flag:
    func_menu_login()
    func_id = input("请输入功能id:>>").strip()
    if func_id.isdigit():
        if func_id == "0":
            break
        elif func_id == "1":
            inp_name = input("请输入用户名:>>").strip()
            inp_pwd = input("请输入密码:>>").strip()
            result=user_login(inp_name, inp_pwd)
            if result== 1:
                while flag:
                    func_menu_use()
                    opt_id = input("请输入功能id:>>").strip()
                    if opt_id == "0":
                        inp_money = input("请输入充值金额:>>").strip()
                        deposit(inp_name, inp_money)
                    elif opt_id == "1":
                        transfer_in_user = input("请输入转入账号名:>>").strip()
                        transfer_money = input("请输入转入金额:>>").strip()
                        transfer(inp_name, transfer_in_user, transfer_money)
                    elif opt_id == "2":
                        withdraw_money = input("请输入提现金额:>>").strip()
                        withdraw(inp_name, withdraw_money)
                    elif opt_id == "3":
                        show_money(inp_name)
                    elif opt_id == "4":
                        flag=False
                        break
                    else:
                        print("输入的功能id不存在。")
        elif func_id == "2":
            add_name = input("请输入注册的用户名:>>").strip()
            add_pwd = input("请输入注册用户的密码:>>").strip()
            user_register(add_name, add_pwd)
        else:
            print("输入的功能id不存在,请重新输入。")
    else:
        print("请输入数字。")

 

posted @ 2020-03-17 15:38  耗油炒白菜  阅读(277)  评论(1编辑  收藏  举报