从零学习python - 09图书管理系统-本地文件持久化小实例

# -*- coding: utf-8 -*-
"""
@Time : 2023/4/6 10:09
@Auth : [email protected]
@File : books_system.py
"""
import os

# 图书管理系统
books_path = r'E:\a1\books_system\books.txt'
users_path = r'E:\a1\books_system\users.txt'
user_book_path = r'E:\a1\books_system\user_book.txt'


def dict_user(src):
    """
    传入一个用户路径 将文件内容以列表中的字典形式返回
    :param src: 用户文件路径
    :return: list
    """
    with open(src, encoding='utf-8') as rStream:
        user_list = []
        # 以列表形式拿到文件内容
        src_list = rStream.readlines()
        for user in src_list:
            one_user = user.split(' ')
            # print(one_user)
            user_list.append({
                'username': one_user[0],
                'password': one_user[1]
            })
        return user_list


# 注册功能
def register():
    print('--------开始会员注册--------')
    username = input('请输入会员名:')
    password = input('请输入密码:')
    re_password = input('请确认密码:')

    if password == re_password:
        # 先判断用户名是否存在 如果存在则注册失败
        user_list = dict_user(users_path)
        for user in user_list:  # user = {'username': 'daming', 'password': '123456\n'},
            if username == user.get('username'):
                print('用户名已被注册,注册失败!')
                break
        else:
            # 两次密码相同 用户名不存在则写入文件
            with open(users_path, 'a', encoding='utf-8') as wStream:
                wStream.write(f'{username} {password}\n')
            print(f'用户<{username}>注册成功!')
    else:
        print('两次输入密码不一致,注册失败!')


# 登录功能
def login():
    print('--------开始会员登录--------')
    user_name = input('请输入会员名:')
    pass_word = input('请输入密码:')

    user_list = dict_user(users_path)
    # 开始判断用户名与密码是否匹配文件
    for user in user_list:
        if user_name == user.get('username'):  # user = {'username': 'daming', 'password': '123456\n'},
            if pass_word == user.get('password').strip('\n'):
                print('登录成功!')
                return user_name, pass_word
                break
            else:
                print('密码错误,请重新输入密码!')
                break
    else:
        print('会员账号未注册,请先注册!')


# 查询所有图书
def select_books():
    with open(books_path, encoding='utf-8') as rStream:
        books_list = rStream.readlines()
        print('--------图书馆藏书有--------')
        for book in books_list:
            print(book, end='')


# 添加书本
def add_book(book_name):
    add_book_name = f'{book_name} \n'
    with open(books_path, 'a', encoding='utf-8') as aStream:
        aStream.write(add_book_name)
    print(f'<{book_name}>添加完毕!')


# 添加用户借书记录

user_login_info = []
username = ''
password = ''
user_borrow_books = []
borrow_list = []
while True:
    print('--------欢迎来到图书管理系统--------')
    choice1 = input('1-会员注册 2-会员登录 3-借书功能 '
                    '4-查询藏书 5-借书情况 6-退出系统:')
    if choice1 == '1':
        register()

    elif choice1 == '2':
        # 如果完成登录则将登录信息保存
        username, password = login()
        user_login_info.append(username)
        user_login_info.append(password)
        # print(user_login_info[0], user_login_info[1])

    elif choice1 == '3':
        if not user_login_info:
            print('您还未登录,请登录后再进行操作!')
        else:
            borrow_book = input(f'用户<{username}>,请选择您要借阅的书:')
            with open(user_book_path, 'a', encoding='utf-8') as aStream:
                aStream.write(f'{user_login_info[0]}:{borrow_book}\n')
            print('借书成功!')

    elif choice1 == '4':
        select_books()

    elif choice1 == '5':
        print(f'用户<{username}>当前借书详情如下:')
        with open(user_book_path, encoding='utf-8') as rStream:
            user_borrow_list = rStream.readlines()

        for user_borrow in user_borrow_list:
            new_borrow = user_borrow.split(':')
            user_borrow_books.append({
                'username': new_borrow[0],
                'bookname': new_borrow[1]
            })
        # 根据当前登录username判断所借的书
        for borrow_info in user_borrow_books:
            if username == borrow_info.get('username'):
                borrow_list.append(borrow_info.get('bookname'))

        print(borrow_list)

    elif choice1 == '6':
        print('正在退出系统,请稍后...')
        break

    else:
        print('指令输入错误,请重新输入!')









你可能感兴趣的:(python学习,python,学习,开发语言)