图书管理系统--Python基础项目(2)

一、需求:

"""
5.图书管理系统的编写
图书类Book:
​     属性:书名name  作者author  是否借出isborrow  书籍位置index
​     注意:书籍的位置不能重复
图书管理系统BookManager类
​     存放图书的工具使用列表
​     方法:
​           1.添加图书
​           2.借书 (根据图书名字借书)
​              要检验图书是否存在、图书是否已经借出
​           3.还书
​           4.查询书籍 (根据名字查询)
"""

 二、编程

1、框架

class Book():
    __slots__ = ("name","author","isborrow","index")
    def __init__(self,name,author,index):
        pass
    def __str__(self):
        pass
class BookManage():
    booklist = [] #图书库
    def start_book(self): #图书库初始化
        pass
    def menu(self):
    self.start_book()
    while True:
        print("欢迎进入图书管理系统".center(100, "="))
        print("""
                1. 添加图书
                2. 借书
                3. 还书
                4. 查询书籍
                5. 查看所有书籍
                6. 退出""")
        select = int(input("请选着功能:"))
        if select == 1:
            self.add_book()
        elif select == 2:
            self.borrow_book()
        elif select == 3:
            self.return_book()
        ...

    def check_book(self,bookname):
        pass
    ...

设置两个类,分别为Book、与BookManage(遵循大驼峰规则)

BookManage:内嵌图书库初始化函数

menu函数:内嵌各类增、删、改、查函数

2、功能实现

1. Book类实现

class Book():
    __slots__ = ("name","author","isborrow","index")
    def __init__(self,name,author,index):
        self.name = name
        self.author = author
        self.index = index
        self.isborrow = False # 初始状态为未借出
    def __str__(self):
        return f"书名为{self.name},作者为{self.author},索引号为{self.index}"
    __repr__ = __str__ # 当打印一个对象的时候,默认会打印该对象的地址,如果在类中重写了__str__函数,则打印对象的时候会调用__str__,从而打印出和对象相关的属性信息

1. __slots__ =  (" ",):属性限制,若属性为单一属性,必须加","消除歧义

2. __init__():初始化函数(初始化类对象)、__new__():创建类对象(系统自动调用,无需敲出)

3. __str__(self):重写系统自带函数(可通过dir()查看),重写后不再打印改对象的地址

2. 图书库初始化函数

    booklist = [] #图书库
    def start_book(self): #图书库初始化
        book1 = Book("西游记", "wjy", 1)
        book2 = Book("三国演绎", "qs", 2)
        book3 = Book("红楼梦", "cocair", 3)
        self.booklist = [book1,book2,book3]
        print(f"书籍有{self.booklist}")

 创建三个Book对象,初始化图书库(self.booklist = []列表初始化的方法)

 3. 菜单函数

    def menu(self):
        self.start_book()
        while True:
            print("欢迎进入图书管理系统".center(100, "="))
            print("""
                    1. 添加图书
                    2. 借书
                    3. 还书
                    4. 查询书籍
                    5. 查看所有书籍
                    6. 退出""")
            select = int(input("请选着功能:"))
            if select == 1:
                self.add_book()
            elif select == 2:
                self.borrow_book()
                pass
            elif select == 3:
                self.return_book()
            elif select == 4:
                bookname = input("请输入书本名:")
                self.check_book(bookname)
            elif select == 5:
                self.show_all_book()
            elif select == 6:
                print("退出成功,欢迎再次登入~~")
                break
            else:
                print("暂无此功能")

用死循环while True控制 

4. 查询函数

    def check_book(self,bookname):
        for bname in self.booklist:
            if bname.name == bookname:
                print(f"{bname.name}存在")
                return True
        return print(f"您输入的{bookname}不存在嗷~~")

5. 增加函数

    def add_book(self):
        book_name = input("请输入图书名:")
        book_author = input("请输入图书作者:")
        # book_index = input("请输入图书索引号:")
        book_indexs = [book.index for book in self.booklist]
        # for book_self in book_indexs:
        #     if book_self != book_index:
        #         book = Book(book_name,book_author,book_index)
        #         self.booklist.append(book)
        #         print("图书添加成功")
        #         break
        #     else:
        #         print("该图书已经在图书库中")
        #         break #没有考虑到索引存在,而再次输入新书名情况
        while True:
            bindex = input("请输入存放书的位置:")
            # 筛查书籍位置
            if bindex not in book_indexs:
                # 这是一个新的位置
                break
            else:
                print('该位置已有书籍 请重新设置位置')
        book = Book(book_name,book_author,bindex)
        self.booklist.append(book)
        print("添加图书%s成功!" % (book))

用列表推导式赋值book_indexs:用于判断图书是否存在于图书库中 

 6. 借出函数

    def borrow_book(self):
        bookname = input("请输入需要借阅的书:")
        if self.check_book(bookname): # 没错,但面向对象不测底
            for book_self in self.booklist:
                if book_self.name == bookname:
                    if book_self.isborrow:
                        print(f"无法借阅,该图书{book_self}已经借出")
                        break
                    else:
                        book_self.isborrow = True
                        print(f"图书{book_self}借阅成功")
                        break
        else:
            print(f"{bookname}不在此图书系统中")

 用book_self.isborrow来标志书本是否借出

7. 归还函数

    def return_book(self):
        bookname = input("请输入待还的书:")
        if self.check_book(bookname):
            for book_self in self.booklist:
                if book_self.name == bookname:
                    if book_self.isborrow:
                        book_self.isborrow = False
                        print(f"该图书{book_self}退还成功")
                        break
                    else:
                        print(f"图书{book_self}无需退还")
                        break
        else:
            print(f"{bookname}不在此图书系统中")

8. 展示函数

    def show_all_book(self):
        for book_ in self.booklist:
            print(book_)

三、运行与实现 

代码:

class Book():
    __slots__ = ("name","author","isborrow","index")
    def __init__(self,name,author,index):
        self.name = name
        self.author = author
        self.index = index
        self.isborrow = False # 初始状态为未借出
    def __str__(self):
        return f"书名为{self.name},作者为{self.author},索引号为{self.index}"
    __repr__ = __str__ # 当打印一个对象的时候,默认会打印该对象的地址,如果在类中重写了__str__函数,则打印对象的时候会调用__str__,从而打印出和对象相关的属性信息

class BookManage():
    booklist = [] #图书库
    def start_book(self): #图书库初始化
        book1 = Book("西游记", "wjy", 1)
        book2 = Book("三国演绎", "qs", 2)
        book3 = Book("红楼梦", "cocair", 3)
        self.booklist = [book1,book2,book3]
        print(f"书籍有{self.booklist}")

    def menu(self):
        self.start_book()
        while True:
            print("欢迎进入图书管理系统".center(100, "="))
            print("""
                    1. 添加图书
                    2. 借书
                    3. 还书
                    4. 查询书籍
                    5. 查看所有书籍
                    6. 退出""")
            select = int(input("请选着功能:"))
            if select == 1:
                self.add_book()
            elif select == 2:
                self.borrow_book()
                pass
            elif select == 3:
                self.return_book()
            elif select == 4:
                bookname = input("请输入书本名:")
                self.check_book(bookname)
            elif select == 5:
                self.show_all_book()
            elif select == 6:
                print("退出成功,欢迎再次登入~~")
                break
            else:
                print("暂无此功能")

    def check_book(self,bookname):
        for bname in self.booklist:
            if bname.name == bookname:
                print(f"{bname.name}存在")
                return True
        return print(f"您输入的{bookname}不存在嗷~~")

    def add_book(self):
        book_name = input("请输入图书名:")
        book_author = input("请输入图书作者:")
        # book_index = input("请输入图书索引号:")
        book_indexs = [book.index for book in self.booklist]
        # for book_self in book_indexs:
        #     if book_self != book_index:
        #         book = Book(book_name,book_author,book_index)
        #         self.booklist.append(book)
        #         print("图书添加成功")
        #         break
        #     else:
        #         print("该图书已经在图书库中")
        #         break #没有考虑到索引存在,而再次输入新书名情况
        while True:
            bindex = input("请输入存放书的位置:")
            # 筛查书籍位置
            if bindex not in book_indexs:
                # 这是一个新的位置
                break
            else:
                print('该位置已有书籍 请重新设置位置')
        book = Book(book_name,book_author,bindex)
        self.booklist.append(book)
        print("添加图书%s成功!" % (book))


    def borrow_book(self):
        bookname = input("请输入需要借阅的书:")
        if self.check_book(bookname): # 没错,但面向对象不测底
            for book_self in self.booklist:
                if book_self.name == bookname:
                    if book_self.isborrow:
                        print(f"无法借阅,该图书{book_self}已经借出")
                        break
                    else:
                        book_self.isborrow = True
                        print(f"图书{book_self}借阅成功")
                        break
        else:
            print(f"{bookname}不在此图书系统中")

    def return_book(self):
        bookname = input("请输入待还的书:")
        if self.check_book(bookname):
            for book_self in self.booklist:
                if book_self.name == bookname:
                    if book_self.isborrow:
                        book_self.isborrow = False
                        print(f"该图书{book_self}退还成功")
                        break
                    else:
                        print(f"图书{book_self}无需退还")
                        break
        else:
            print(f"{bookname}不在此图书系统中")

    def show_all_book(self):
        for book_ in self.booklist:
            print(book_)

bookmanage = BookManage()
bookmanage.menu()

测试:

图书管理系统--Python基础项目(2)_第1张图片

你可能感兴趣的:(Python--全栈开发,Python--基础语法,Python--项目,python,pycharm,开发语言)