Python 7 Levels, L5, Unit 53, Library Management System, II

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)

class User():
    @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 borrow_book(book_list):
        name = input("Please input book name to be borrowed:")
        index = User.seek_book(book_list,name)
        if index != -1:
            if book_list[index].stock == 0:
                print("The book is out of stock. Please try in a few days.")
            else:
                print("The book is borrowed successfully.")
                book_list[index].stock -= 1

    @staticmethod
    def return_book(book_list):
        name = input("Please input the book to be returned.")
        index = User.seek_book(book_list, name)
        if index != -1:
            if book_list[index].stock == Book.stock:
                print("The book is never borrowed,Please check and return.")
            else:
                print("The book is returned successfully.")
                book_list[index].stock += 1

class Menu():
    @staticmethod
    def admin():
        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
    @staticmethod
    def user():
        while True:
            num = input("Please pick an operation(1.View All Books 2.Borrow Book 3.Return Book 99.Quit)")
            if num == "1":
                User.show_book(book_list)
            elif num == "2":
                User.borrow_book(book_list)
            elif num == "3":
                User.return_book(book_list)
            elif num == "99":
                break

class Login():
    stu_list = []#student list
    @staticmethod
    def admin():
        password = input("Please input admin password:")
        if password == "123456":
            Menu.admin()
        else:
            print("Sorry, user/password mismatch.")

    @classmethod
    def user(cls):
        name = input("Please input user name:")
        password = int(input("Please input user password:"))
        for stu in Login.stu_list:
            if stu.name == name and stu.password == password:
                Menu.user()
                break
        else:
            print("Sorry, user/password mismatch.")

    @classmethod
    def add_user(cls):
        print("New User Registration.")
        name = input("Please input user name:")
        password = int(input("Please input user password:"))
        stu = Student(name, password)
        Login.stu_list.append(stu)  # add new user to uer list
        print("Registration succeeds.")

stu_list = []#student list
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)

while True:
    num = input("Please pick your operation(1.Administration 2.User Login 3.New User Registration 99.Quit)")
    if num == "1":
        Login.admin()
    elif num == "2":
        Login.user()
    elif num == "3":
        Login.add_user()
        Login.user()
    elif num == "99":
        break

dahan1999

1 Comment

Leave a Reply

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

Related Posts