Python 7 Levels, L5, Unit 52, Library Management System, I

Powered By EmbedPress

Exercise:

class Book():
    stock = 2#stock defaults to 2
    def __init__(self,name,author,location):
        self.name = name
        self.author = author
        self.stock = Book.stock
        self.location = location

    def __str__(self):
        msg = "Book name:{0},Author:{1},Stock:{2},Place:{3}".format(self.name,self.author,self.stock,self.location)
        return msg

class Student():

    def __init__(self,name,password):
        self.name = name
        self.password = password

    def __str__(self):
        msg = "User name: {0},Password: {1}".format(self.name,self.password)
        return msg

class Admin():

    @staticmethod
    def show_book(book_list):
        for book in book_list:
            print(book)
    @staticmethod
    def seek_book(book_list,name):
        for index in range(len(book_list)):
            if book_list[index].name == name:
                print(book_list[index])
                return index
        else:
            print("The book does not exist.")
            return -1

    @staticmethod
    def add_book(book_list):
        name = input("Please input book name:")
        author = input("Please input author:")
        place = input("Please input storage location:")
        book = Book(name,author,place)
        book_list.append(book)
        print("Book added successfully.")

    @staticmethod
    def delet_book(book_list,name):
        result = Admin.seek_book(book_list,name)
        if result != -1:
            del book_list[result]
            print("Deleted.")

    @staticmethod
    def show_user(stu_list):
        for stu in stu_list:
            print(stu)


book_list = []#book list
book1 = Book("Nineteen Eighty-our","George Orwell","001")
book2 = Book("Animal Farm","George Orwell","002")
book_list.append(book1)
book_list.append(book2)

stu_list = []#sdudent list
stu1 = Student("David Smith",123)
stu2 = Student("John Doe",345)
stu_list.append(stu1)
stu_list.append(stu2)

while True:
    num = input("Please pick an operation(1.Display all Books 2.Add Book 3.Search Book 4.Delete Book 5.View All Users 99.Quit)")
    if num == "1":
        Admin.show_book(book_list)
    elif num == "2":
        Admin.add_book(book_list)
    elif num == "3":
        name = input("Please input the book you look for:")
        Admin.seek_book(book_list,name)
    elif num == "4":
        name = input("Please input the book to be deleted:")
        Admin.delet_book(book_list,name)
    elif num == "5":
        Admin.show_user(stu_list)
    elif num == "99":
        break

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts