简单,又有点小复杂的图书管理系统(python)

注意:本文章涉及到的应用仅供学习交流使用,禁止将本文技术或者本文所关联的Github项目源码用于任何目的。

写入时间:2022.5.1(大一纯自己练习)

工具:pycharm  python(idle)都可以平时用的就可

内容简单 ,帮助掌握 i/o 主要内容文件的读写,虽然有点多400左右行,但都分为了函数块

每一个函数都有联系,可以一个一个的慢慢看写但是总体思路的不变的   不要的块可以自己删掉,也可以自己改简单点

可交流

以下就是每一个区块


文件的读入命名 ,方便后续的写入(一共需要三个文件)

# 图书登记
path_book = r'book.txt'
# 用户信息
path_users = r'users.txt'
# 借书登记
path_user_book = r'user_book.txt'

检查是否有该民用户

# 注册系统
def register():
    print("--------------欢迎来到霁月的图书管理系统注册界面-------------")

    readUsers = open(path_users, 'r', encoding='utf-8')

    # 输入注册账号
    register_name = input("用户名:")
    while True:
        # 检查是否有该用户
        id = readUsers.readline()
        if register_name in id:
            print("用户以存在!")
            register_name = input("请重新输入用户名:")
        else:
            # 是否加上找回密码
            break

邮箱的写入

可以做密码的找回和修改

    # 输入注册邮箱
    email = input("请输入邮箱:")
    while True:
        if not "@" in email:
            print("邮箱地址有错误")
            email = input("请重新输入:")
        else:
            break


注册密码的写入

    # 注册密码
    register_key = input("密码(只能由6--12位的数字或字母组成):")
    while True:
        # 检测密码正确性
        if (len(register_key) < 8) or (len(register_key)>12):
            print("名字格式错误")
            register_key = input("请重新输入密码:")

        register_password = input("请再次确定密码:")
        # 检查密码输入是否一致
        if register_key == register_password:
            with open('Users.txt', 'a', encoding='utf-8') as f:
                f.write('{}   {}   {} \n'.format(register_name, register_key, email))
                f.close()
            print('用户注册成功!')
            break
        else:
            print('密码不一致!')

    # 保存信息

登陆系统

# 登录系统
def login():
    print("--------------欢迎来到霁月的图书管理系统登录界面-------------")
    # 打开文件检查 用户是否存在
    readUsers = open(path_users, 'r', encoding='utf-8')
    a = "1"

    # 输入账号密码
    username = input("账号:")
    password = input("密码:")

    # 检查 登录
    infos = readUsers.readlines()
    for info in infos:
        if info:
            if(username in info) and (password in info):
                print("--------------------登录成功-----------------------\n登录用户{}".format(username))
                operate_H(username)

            elif (username in info) or (password in info):
                print("账号或者密码错误,请重新输入!!!!!!")
                login()
                break
            else:
                a = "1"
                continue
    if a == "1":
        print("没有此账号请注册")
        register()

    # 返回用户名 方便之后的编写
    return username

登录界面 ,用于选择


# 登录界面
def operate():
    print("-----------欢迎来到霁月的图书管理系统----------\n")
    print("1.登录\n2.注册\n3.退出")
    features = int(input("请输入你的选择:"))
    if features == 1:
        login()
    elif features == 2:
        register()
    elif features == 3:
        print("正在退出 。。。。。\n退出成功!")
    else:
        print("请输出正确的指令!")
        operate()


功能1:图书的查询

# 图书查询
def search_book(username):
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        books_inf = f.readlines()
    print('编号'.center(5, ' '), '书名'.center(15, ' '), '作者'.center(15, ' '), '种类'.center(15, ' '), '状态'.center(15, ' '))
    format_data = '{:^5}\t{:^15}\t{:^15}\t{:^14}\t{:^15}'
    for temp in books_inf:
        b = eval(temp)
        print(format_data.format(b.get('编号'), b.get('书名'), b.get('作者'), b.get('种类'), b.get('状态')))
        operate_H(username)

功能2 :借书模块 分为两个方便看


# 借书模块
def borrow_book(username):
    borrow_book1 = input("请输入你要借阅的图书:")
    a = "0"
    if borrow_book1 == "111":
        operate_H(username)
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        # 遍历图书
        books_inf = f.readlines()
        for book_inf in books_inf:
            book = eval(book_inf)
            book = book.get('书名')

            if book == borrow_book1:
                a = "0"
                borrow_book2(username, borrow_book1)
                break
            else:
                a = "1"
        if a == "1":
            print("没有你要找的书")
            borrow_book(username)

def borrow_book2(username, borrow_book1):

    with open(path_user_book, "r", encoding="utf-8") as f:
        uses = f.readlines()
        a = "1"
        for use in uses:
            # 借过这本书
            if (username in use) and (borrow_book1 in use):
                a = "0"
                print("你以借过此书")
                borrow_book(username)
                break
            # 书被借走了
            elif(borrow_book1 in use) and (username not in use):
                a = "0"
                print("书已被别人借走")
                borrow_book(username)
                break
            else:
                a = "1"
                continue

功能3:归还图书

# 还书
def return_book(username):
    return_book1 = input("请输入你要归还的图书:")
    a = "0"
    if return_book1 == "111":
        operate_H(username)
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        # 遍历图书
        books_inf = f.readlines()
        for book_inf in books_inf:
            book = eval(book_inf)
            book = book.get('书名')

            if book == return_book1:
                a = "0"
                return_book2(username, return_book1)
                break
            else:
                a = "1"
        if a == "1":
            print("图书馆没有此书")
            return_book(username)

def return_book2(username, return_book1):
    with open(path_user_book, "r", encoding="utf-8") as f:
        uses = f.readlines()
        a = "1"
        # 除去文件中书的记录
        with open(path_user_book, "w", encoding="utf-8") as w:
            for use in uses:
                if (username in use) and (return_book1 in use):
                    continue
                else:
                    w.writelines(use)
                    w.close()
        f.close()

        print("--------------{}已将{}归还--------------------".format(username, return_book1))
        print("归还成功")
        # 遍历还未归还的书籍
        with open(path_user_book, "r", encoding="utf-8") as g:
            uses = g.readlines()
            for use in uses:
                if username in use:
                    print("未归还"+use)
                    g.close()
        operate_H(username)


功能4:修改用户信息

# 修改个人信息
def update_password(username):
    tips = input("\n 1.修改邮箱\n 2.修改密码\n请选择操作:")
    if tips == "111":
        operate_H(username)

    # 修改邮箱
    if tips == '1':
        new_email = ''
        line = []
        try:
            with open(path_users, encoding='utf-8') as rstream:
                while True:
                    line = rstream.readline()
                    if not line:
                        break
                    line = line.split('   ')
                    # 去掉密码后面的换行符
                    line[1] = line[1].rstrip('\n')
                    if username == line[0]:
                        new_email = input("请输入新邮箱:")
                        line[2] = new_email
                        break
        except Exception as err:
            print(err)

        else:
            # 将新修改邮箱后的用户的所有信息追加到文件夹
            with open(path_users, 'a', encoding='utf-8') as wstream:
                for i in range(len(line)):
                    if i == 0:
                        # 遍历列表,第一个列表元素需要前面需要加换行,后面需要加空格与其他元素分割
                        line[i] = '\n' + line[i] + '   '
                    else:
                        line[i] = line[i] + '   '
                wstream.writelines(line)
                print("修改成功")
            # 删除修改邮箱之前用户的信息
            with open(path_users, encoding='utf-8') as rstream:
                # 读取多行
                lines = rstream.readlines()
                i = 0
                l = len(lines)
                while i < l:
                    # 当前用户名在用户信息行且新的邮箱不在时就删除之前的用户信息,不会删除其他用户的信息
                    if username in lines[i] and new_email not in lines[i]:
                        lines.remove(lines[i])
                    i += 1
                    l -= 1
                # 删除旧邮箱对应的当前用户信息后,再将新邮箱对应的用户信息以及其他用户的信息从新写入到文件
                with open(path_users, 'w', encoding='utf-8') as wstream:
                    wstream.writelines(lines)
    # 修改密码
    elif tips == '2':
        new_password = ''
        line = []
        try:
            with open(path_users, encoding='utf-8') as rstream:
                while True:
                    line = rstream.readline()
                    if not line:
                        break
                    line = line.split('   ')
                    # 去掉密码后面的换行符
                    line[1] = line[1].rstrip('\n')
                    if username == line[0]:
                        new_password = input("请输入新密码:")
                        # 判断新密码与旧密码是否一致
                        if new_password == line[1]:
                            # 抛出异常
                            raise Exception("新密码不能与旧密码相同哦~")
                        else:
                            line[1] = new_password
                            break
        # 可以捕获到前面raise抛出的异常
        except Exception as err:
            print(err)

        else:
            # 将新修改密码后的用户的所有信息追加到文件夹
            with open(path_users, 'a', encoding='utf-8') as wstream:
                for i in range(len(line)):
                    if i == 0:
                        # 遍历列表,第一个列表元素需要前面需要加换行,后面需要加空格与其他元素分割
                        line[i] = '\n' + line[i] + '   '
                    else:
                        line[i] = line[i] + '   '
                wstream.writelines(line)
                print("修改成功")
            # 删除修改密码之前用户的信息
            with open(path_users, encoding='utf-8') as rstream:
                # 读取多行
                lines = rstream.readlines()
                i = 0
                l = len(lines)
                while i < l:
                    # 当 当前用户名在用户信息行且新的密码不在时就删除之前的用户信息,不会删除其他用户的信息
                    if username in lines[i] and new_password not in lines[i]:
                        lines.remove(lines[i])
                    i += 1
                    l -= 1
                # 删除旧密码对应的当前用户信息后,再将新密码对应的用户信息以及其他用户的信息从新写入到文件
                with open(path_users, 'w', encoding='utf-8') as wstream:
                    wstream.writelines(lines)
                    operate_H(username)

查看个人信息

# 查看个人信息
def look_person_info(username):
    with open(path_users, encoding="utf-8") as rstream:
        lines = rstream.readlines()
        for info in lines:
            # 分割成一个列表
            info = info.split('   ')
            # print(info)
            if username in info:
                print("----个人信息----")
                print("用户名:", info[0])
                print("密码:", info[1])
                print("邮箱:", info[2].rstrip(' '))
                person_information(username)

功能4的界面

# 个人信息的查询和修改
def person_information(username):
    # 信息选择
    tips = input(" 1.查看个人信息\n 2.修改个人信息\n请选择操作:")
    if tips == '1':
        look_person_info(username)
    elif tips == '2':
        update_password(username)

功能5: 添加图书(本来想加管理员)但是太多了有点懒得写

自己可以完善下 :可以发给我 用用

# 添加图书 (管理员)
def add_book():
    id_book = int(input('请输入编号:'))
    name_book = input('请输入书名:')
    author_book = input('请输入作者:')
    type_book = input('请输入种类:')
    state_book = input('请输入图书状态:')
    book_information = {'编号': id_book, '书名': name_book, '作者': author_book, '种类': type_book, '状态':state_book}
    with open(path_book, 'a', encoding='utf-8') as f:
        f.write(str(book_information))
        f.write('\n')

选择功能的大界面

# 主功能页面
def operate_H(username):
    features = int(input("选择你要的功能(0 可打开菜单):"))
    if features == 0:
        menu(username)
    elif features == 1:
        borrow_book(username)
    elif features == 2:
        return_book(username)
        # 归还图书
    elif features == 3:
        search_book(username)
    elif features == 4:
        person_information(username)
    elif features == 5:
        pass
    elif features == 22:
        add_book()
    else:
        print("请不要输入无效信息")
        operate_H(username)
        # 查询个人信息

启动函数(必须有)

# 启动
if __name__ == '__main__':
    operate()


# 有机会加上 图书状态 函数跳跃
# 添加管理员查询借书信息
# 添加管理员可视化借书浮动与数据比较
# 加上可视化界面
# 连接笔趣阁 利用爬虫真正获取图书信息


完整代码


# 大纲 全部内容
# 界面 ;登录 注册 选项
# 普通人 : 借书 还书 查询书籍 账号管理 退出系统
# 管理员; 查询 添加书本 删除 个人信息
# 储存文件;借书登记 图书库 用户信息


# 文件存放位置

# 书本

# 地址

path_book = r'book.txt'
# 用户信息
path_users = r'users.txt'
# 借书登记
path_user_book = r'user_book.txt'



# 注册系统
def register():
    print("--------------欢迎来到霁月的图书管理系统注册界面-------------")

    readUsers = open(path_users, 'r', encoding='utf-8')

    # 输入注册账号
    register_name = input("用户名:")
    while True:
        # 检查是否有该用户
        id = readUsers.readline()
        if register_name in id:
            print("用户以存在!")
            register_name = input("请重新输入用户名:")
        else:
            # 是否加上找回密码
            break


    # 输入注册邮箱
    email = input("请输入邮箱:")
    while True:
        if not "@" in email:
            print("邮箱地址有错误")
            email = input("请重新输入:")
        else:
            break


    # 注册密码
    register_key = input("密码(只能由6--12位的数字或字母组成):")
    while True:
        # 检测密码正确性
        if (len(register_key) < 8) or (len(register_key)>12):
            print("名字格式错误")
            register_key = input("请重新输入密码:")

        register_password = input("请再次确定密码:")
        # 检查密码输入是否一致
        if register_key == register_password:
            with open('Users.txt', 'a', encoding='utf-8') as f:
                f.write('{}   {}   {} \n'.format(register_name, register_key, email))
                f.close()
            print('用户注册成功!')
            break
        else:
            print('密码不一致!')

    # 保存信息



# 登录系统
def login():
    print("--------------欢迎来到霁月的图书管理系统登录界面-------------")
    # 打开文件检查 用户是否存在
    readUsers = open(path_users, 'r', encoding='utf-8')
    a = "1"

    # 输入账号密码
    username = input("账号:")
    password = input("密码:")

    # 检查 登录
    infos = readUsers.readlines()
    for info in infos:
        if info:
            if(username in info) and (password in info):
                print("--------------------登录成功-----------------------\n登录用户{}".format(username))
                operate_H(username)

            elif (username in info) or (password in info):
                print("账号或者密码错误,请重新输入!!!!!!")
                login()
                break
            else:
                a = "1"
                continue
    if a == "1":
        print("没有此账号请注册")
        register()

    # 返回用户名 方便之后的编写
    return username


# 登录界面
def operate():
    print("-----------欢迎来到霁月的图书管理系统----------\n")
    print("1.登录\n2.注册\n3.退出")
    features = int(input("请输入你的选择:"))
    if features == 1:
        login()
    elif features == 2:
        register()
    elif features == 3:
        print("正在退出 。。。。。\n退出成功!")
    else:
        print("请输出正确的指令!")
        operate()




# 图书查询
def search_book(username):
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        books_inf = f.readlines()
    print('编号'.center(5, ' '), '书名'.center(15, ' '), '作者'.center(15, ' '), '种类'.center(15, ' '), '状态'.center(15, ' '))
    format_data = '{:^5}\t{:^15}\t{:^15}\t{:^14}\t{:^15}'
    for temp in books_inf:
        b = eval(temp)
        print(format_data.format(b.get('编号'), b.get('书名'), b.get('作者'), b.get('种类'), b.get('状态')))
        operate_H(username)



# 借书模块
def borrow_book(username):
    borrow_book1 = input("请输入你要借阅的图书:")
    a = "0"
    if borrow_book1 == "111":
        operate_H(username)
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        # 遍历图书
        books_inf = f.readlines()
        for book_inf in books_inf:
            book = eval(book_inf)
            book = book.get('书名')

            if book == borrow_book1:
                a = "0"
                borrow_book2(username, borrow_book1)
                break
            else:
                a = "1"
        if a == "1":
            print("没有你要找的书")
            borrow_book(username)

def borrow_book2(username, borrow_book1):

    with open(path_user_book, "r", encoding="utf-8") as f:
        uses = f.readlines()
        a = "1"
        for use in uses:
            # 借过这本书
            if (username in use) and (borrow_book1 in use):
                a = "0"
                print("你以借过此书")
                borrow_book(username)
                break
            # 书被借走了
            elif(borrow_book1 in use) and (username not in use):
                a = "0"
                print("书已被别人借走")
                borrow_book(username)
                break
            else:
                a = "1"
                continue

        # 借书成功
        if a == "1":
            with open(path_user_book, "a", encoding="utf-8") as g:
                g.writelines("{} {}\n".format(username, borrow_book1))
                print("借书成功")
                g.close()
                operate_H(username)



# 还书
def return_book(username):
    return_book1 = input("请输入你要归还的图书:")
    a = "0"
    if return_book1 == "111":
        operate_H(username)
    with open(path_book, 'r', encoding='utf-8') as f:  # 取所有图书信息
        # 遍历图书
        books_inf = f.readlines()
        for book_inf in books_inf:
            book = eval(book_inf)
            book = book.get('书名')

            if book == return_book1:
                a = "0"
                return_book2(username, return_book1)
                break
            else:
                a = "1"
        if a == "1":
            print("图书馆没有此书")
            return_book(username)

def return_book2(username, return_book1):
    with open(path_user_book, "r", encoding="utf-8") as f:
        uses = f.readlines()
        a = "1"
        # 除去文件中书的记录
        with open(path_user_book, "w", encoding="utf-8") as w:
            for use in uses:
                if (username in use) and (return_book1 in use):
                    continue
                else:
                    w.writelines(use)
                    w.close()
        f.close()

        print("--------------{}已将{}归还--------------------".format(username, return_book1))
        print("归还成功")
        # 遍历还未归还的书籍
        with open(path_user_book, "r", encoding="utf-8") as g:
            uses = g.readlines()
            for use in uses:
                if username in use:
                    print("未归还"+use)
                    g.close()
        operate_H(username)



# 修改个人信息
def update_password(username):
    tips = input("\n 1.修改邮箱\n 2.修改密码\n请选择操作:")
    if tips == "111":
        operate_H(username)

    # 修改邮箱
    if tips == '1':
        new_email = ''
        line = []
        try:
            with open(path_users, encoding='utf-8') as rstream:
                while True:
                    line = rstream.readline()
                    if not line:
                        break
                    line = line.split('   ')
                    # 去掉密码后面的换行符
                    line[1] = line[1].rstrip('\n')
                    if username == line[0]:
                        new_email = input("请输入新邮箱:")
                        line[2] = new_email
                        break
        except Exception as err:
            print(err)

        else:
            # 将新修改邮箱后的用户的所有信息追加到文件夹
            with open(path_users, 'a', encoding='utf-8') as wstream:
                for i in range(len(line)):
                    if i == 0:
                        # 遍历列表,第一个列表元素需要前面需要加换行,后面需要加空格与其他元素分割
                        line[i] = '\n' + line[i] + '   '
                    else:
                        line[i] = line[i] + '   '
                wstream.writelines(line)
                print("修改成功")
            # 删除修改邮箱之前用户的信息
            with open(path_users, encoding='utf-8') as rstream:
                # 读取多行
                lines = rstream.readlines()
                i = 0
                l = len(lines)
                while i < l:
                    # 当前用户名在用户信息行且新的邮箱不在时就删除之前的用户信息,不会删除其他用户的信息
                    if username in lines[i] and new_email not in lines[i]:
                        lines.remove(lines[i])
                    i += 1
                    l -= 1
                # 删除旧邮箱对应的当前用户信息后,再将新邮箱对应的用户信息以及其他用户的信息从新写入到文件
                with open(path_users, 'w', encoding='utf-8') as wstream:
                    wstream.writelines(lines)
    # 修改密码
    elif tips == '2':
        new_password = ''
        line = []
        try:
            with open(path_users, encoding='utf-8') as rstream:
                while True:
                    line = rstream.readline()
                    if not line:
                        break
                    line = line.split('   ')
                    # 去掉密码后面的换行符
                    line[1] = line[1].rstrip('\n')
                    if username == line[0]:
                        new_password = input("请输入新密码:")
                        # 判断新密码与旧密码是否一致
                        if new_password == line[1]:
                            # 抛出异常
                            raise Exception("新密码不能与旧密码相同哦~")
                        else:
                            line[1] = new_password
                            break
        # 可以捕获到前面raise抛出的异常
        except Exception as err:
            print(err)

        else:
            # 将新修改密码后的用户的所有信息追加到文件夹
            with open(path_users, 'a', encoding='utf-8') as wstream:
                for i in range(len(line)):
                    if i == 0:
                        # 遍历列表,第一个列表元素需要前面需要加换行,后面需要加空格与其他元素分割
                        line[i] = '\n' + line[i] + '   '
                    else:
                        line[i] = line[i] + '   '
                wstream.writelines(line)
                print("修改成功")
            # 删除修改密码之前用户的信息
            with open(path_users, encoding='utf-8') as rstream:
                # 读取多行
                lines = rstream.readlines()
                i = 0
                l = len(lines)
                while i < l:
                    # 当 当前用户名在用户信息行且新的密码不在时就删除之前的用户信息,不会删除其他用户的信息
                    if username in lines[i] and new_password not in lines[i]:
                        lines.remove(lines[i])
                    i += 1
                    l -= 1
                # 删除旧密码对应的当前用户信息后,再将新密码对应的用户信息以及其他用户的信息从新写入到文件
                with open(path_users, 'w', encoding='utf-8') as wstream:
                    wstream.writelines(lines)
                    operate_H(username)

# 查看个人信息
def look_person_info(username):
    with open(path_users, encoding="utf-8") as rstream:
        lines = rstream.readlines()
        for info in lines:
            # 分割成一个列表
            info = info.split('   ')
            # print(info)
            if username in info:
                print("----个人信息----")
                print("用户名:", info[0])
                print("密码:", info[1])
                print("邮箱:", info[2].rstrip(' '))
                person_information(username)

# 个人信息的查询和修改
def person_information(username):
    # 信息选择
    tips = input(" 1.查看个人信息\n 2.修改个人信息\n请选择操作:")
    if tips == '1':
        look_person_info(username)
    elif tips == '2':
        update_password(username)



# 菜单
def menu(username):
    print("----------------- 菜单 ------------------------")
    print("1.借书\n2.还书\n3.查看全部书籍\n4.个人信息\n5.退出")
    operate_H(username)



# 添加图书 (管理员)
def add_book():
    id_book = int(input('请输入编号:'))
    name_book = input('请输入书名:')
    author_book = input('请输入作者:')
    type_book = input('请输入种类:')
    state_book = input('请输入图书状态:')
    book_information = {'编号': id_book, '书名': name_book, '作者': author_book, '种类': type_book, '状态':state_book}
    with open(path_book, 'a', encoding='utf-8') as f:
        f.write(str(book_information))
        f.write('\n')


# 主功能页面
def operate_H(username):
    features = int(input("选择你要的功能(0 可打开菜单):"))
    if features == 0:
        menu(username)
    elif features == 1:
        borrow_book(username)
    elif features == 2:
        return_book(username)
        # 归还图书
    elif features == 3:
        search_book(username)
    elif features == 4:
        person_information(username)
    elif features == 5:
        pass
    elif features == 22:
        add_book()
    else:
        print("请不要输入无效信息")
        operate_H(username)
        # 查询个人信息



# 启动
if __name__ == '__main__':
    operate()


# 有机会加上 图书状态 函数跳跃
# 添加管理员查询借书信息
# 添加管理员可视化借书浮动与数据比较
# 加上可视化界面
# 连接笔趣阁 利用爬虫真正获取图书信息

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值