python實現圖書管理系統(簡單版)

2020-08-08 13:17:39

簡單實現圖書管理系統

類和麪向物件程式設計

「」"
小鐘圖書管理系統
1. 查詢
2. 增加
3. 借閱
4. 歸還
5. 退出
「」"

class Book(object):
    def __init__(self,name,author,state,book_position):
        self.name = name
        self.author = author
        self.state = state   # 判斷書的狀態0:借出 1:未借出
        self.book_position = book_position
    # 列印物件時自動呼叫str(物件)
    def __str__(self):
        return f"書名:《{self.name}》,作者:<{self.author}>,狀態:<{self.state}>,位置:<{self.book_position}>"
class Book_Manag(object):
    books = []

    def start(self):
        "對圖書資訊進行初始化"
        b1 = Book("三體", "劉慈欣", 1, "SN700")
        b2 = Book("白夜行", "東野圭吾", 1, "SN705")
        b3 = Book("C語言程式設計", "譚浩強", 1, "SN711")
        b4 = Book("放學後", "東野圭吾", 1, "SN706")
        b5 = Book("祕密", "東野圭吾", 1, "SN755")
        b6 = Book("解憂雜貨店", "東野圭吾", 1, "SN752")
        b7 = Book("超新星紀元", "劉慈欣", 1, "SN857")

        self.books.append(b1)
        self.books.append(b2)
        self.books.append(b3)
        self.books.append(b4)
        self.books.append(b5)
        self.books.append(b6)
        self.books.append(b7)
    def directory(self):
        self.start()
        while True:
            print("""小鐘圖書管理系統:
                1. 查詢
                2. 增加
                3. 借閱
                4. 歸還
                5. 退出
            """)

            chioce = int(input("輸入你想要的操作:"))
            if chioce == 1:
                name = input("你想查詢書籍的書名:")
                self.checkBook(name)
            elif chioce == 2:
                self.addBook()
            elif chioce == 3:
                self.borrowBook()
            elif chioce == 4:
                self.return_book()
            elif chioce == 5:
                print("退出系統!")
                break
            else:
                print("您的輸入有誤,請重新輸入!")

    # 新增書籍
    def addBook(self):
        name = input("需要新增書籍的書名:")
        self.books.append(Book(name,input("作者:"),1,input("位置:")))
        print(f"圖書{name}新增成功!")

    # 借書
    def borrowBook(self):
        name = input("借閱書籍名稱:")
        Books = self.checkBook(name)
        # 判斷書的狀態
        if Books !=None:
            if Books.state == 0:
                print(f"書籍《{name}》已經借出!")
            elif Books.state == 1:
                print(f"書籍《{name}》借閱成功!")
                Books.state = 0
        else:
            print(f"書籍《{name}》不存在!")

    # 歸還書籍
    def return_book(self):
        name = input("歸還書籍名稱:")
        Books = self.checkBook(name)
        if Books != None :
            if Books.state == 1:
                print(f"書籍《{name}》已經借出!")
            elif Books.state == 0:
                print(f"書籍《{name}》歸還成功!")
                Books.state = 1
        else:
            print(f"書籍《{name}》與借出的不一致!")

    def checkBook(self,name):
        "查詢書籍是否存在"
        for book in self.books:
            # book:Book類建立的物件
            if book.name == name:
                print(book)
                return book
        else:
            return print(f"你查詢的書籍《{name}》不在系統中!請重新輸入!")

book_Manag = Book_Manag()
book_Manag.directory()

在这里插入图片描述
在这里插入图片描述