python类的使用,函数的搭建

题目:
针对图书馆的一个简单的软件系统:
1、读者只能够最多借三本书。每本图书有一个等待借阅的读者的列表。每本图书有一个标题、作者、已经借阅/没借阅的一位读者,以及等待这本书还回后再继续借阅读者列表。编写一个类来模拟这些对象。
2、编写一个类,他能管理1中的图书和读者。这个类应该包含添加、删除和查找图书和读者的方法,还应该有借阅和归还图书的方法。
编写并测试上述程序。

程序:

class library(object):
    book_1 = {"A(a)":"","B(a)":"","C(a)":"","D(a)":"","E(a)":"","F(a)":"","G(a)":"","H(a)":"","I(a)":"","J(a)":"","K(a)":"","L(a)":"","M(a)":"","N(a)":"","O(a)":"","P(a)":"","Q(a)":"","R(a)":"","S(a)":"","T(a)":"","U(a)":"","V(a)":"","W(a)":"","X(a)":"","Y(a)":"","Z(a)":""}
     # Initialize
    def __init__(self,books = book_1):
        self.reset(books)

    def reset(self,books = book_1,readers = [],lists = {}):
        self.readers = readers
        self.books = books
        self.lists = lists
        return self.books
        
    # "books_borrow" use for borrow a book.
    # "name" should include the name of book and the authors of this book in "()"
    def books_borrow(self,name,reader_borrow):
        self.new = {name:reader_borrow}
        self.books.update(self.new)
        return self.books
        
    # "books_return" is used to return a book
    def books_return(self,name):
        self.new = {name:""}
        self.books.update(self.new)
        return self.books
        
	# "reader_wait"is used for creating a list include the readers want to borrow a book.
    def reader_wait(self,new_wait,name):
        for m, n in self.books.items():
            if m == name:
                try:
                    wait = self.lists[name]
                    self.lists[name] = wait + new_wait
                except KeyError:
                    self.lists[name] = new_wait
        return self.lists[name]

	# "check_book" is used to find out whether a book has been checked out or returned. if the library has a reader, the result return True. 
    def check_book(self,book):
        for m,n in self.books.items():
            if m == book:
                if self.books[book] == "":
                    return True
                else:
                    return False
	# "check_reader" is used to check whether a reader borrows books from the library.
    def check_reader(self,name):
        k = 0
        for m,n in self.books.items():
            if n == name:
                k += 1
                print(k)
                if k == 3:
                    print("You have borrowed more than three books")
                    return False
                else:
                    print("You can borrow books")
        return True

lib = library()
class Lend_book(object):

	# "Add" is used to add books to a library.
    def add(self,name):
        lib.books.update({name:""})
        return lib.books

	# "Remove" is used to remove books from a library.
    def remove(self,name):
        lib.books.pop(name)
        return lib.books
	
	# "find_book" is used to find out if there is a book in the library.
    def find_book(self,name):
        for m,n in lib.books.items():
            if m == name:
                print("This library have this book")

	# "find_reader" is used to find out if a reader borrows a book from the library.
    def find_reader(self,reader):
        k = 0
        for m,n in lib.books.items():
            if n == reader:
                print("This reader has borrowed book:",m)
                k += 1
        if k == 0:
            print("No reader in record.")

	# "borrow_books" is used to borrow a book. 
	# Borrowing a book requires first determining whether the reader has borrowed more than three books. Second, check to see if the book is available in the library and if it is on loan. If the book has not been checked out, then the book has been checked out. If the book has been checked out, you need to add the reader to the list of people who want to check it out.
    def borrow_books(self,name,reader):
        temp = lib.check_reader(reader)
        if temp == True:
            check = lib.check_book(name)
            if check == True:
                lib.books_borrow(name,reader)
                print("Success!!!")
            else:
                lib.reader_wait(reader,name)
                print("This book has been checker out.")

	# "return_books" is used to return a book
    def return_books(self,name):
        lib.books_return(name)
        return lib.books_return(name)
	
	# "wait_list" is used to view a list of books waiting to be borrowed
    def wait_list(self,name):
        try:
            lib.lists[name]
            return lib.lists[name]
        except KeyError:
            print("No user wait for this book.")

测试代码:

f = Lend_book()
f1 = f.add("A(b)")
print("f1 is :",f1)
f2 = f.remove("A(a)")
print("f2 is :",f2)
f3 = f.find_book("G(a)")
print("f3 is :",f3)
f4 = f.borrow_books("C(a)","Li")
print("f4 is :",f4)
f5 = f.find_reader("Li")
print("f5 is :",f5)
f6 = f.find_reader("Huang")
print("f6 is :",f6)
f7 = f.return_books("C(a)")
print("f7 is :",f7)
f8 = f.find_reader("Li")
print("f8 is :",f8)
f9 = f.wait_list("C(a)")
print("f9 is :",f9)
f10 = f.borrow_books("C(a)"," Li")
f11 = f.borrow_books("C(a)"," Huang")
f13 = f.borrow_books("C(a)"," Wang")
f12 = f.wait_list("C(a)")
print(f10,f11,f12)

测试结果:

f1 is : {'A(a)': '', 'B(a)': '', 'C(a)': '', 'D(a)': '', 'E(a)': '', 'F(a)': '', 'G(a)': '', 'H(a)': '', 'I(a)': '', 'J(a)': '', 'K(a)': '', 'L(a)': '', 'M(a)': '', 'N(a)': '', 'O(a)': '', 'P(a)': '', 'Q(a)': '', 'R(a)': '', 'S(a)': '', 'T(a)': '', 'U(a)': '', 'V(a)': '', 'W(a)': '', 'X(a)': '', 'Y(a)': '', 'Z(a)': '', 'A(b)': ''}
f2 is : {'B(a)': '', 'C(a)': '', 'D(a)': '', 'E(a)': '', 'F(a)': '', 'G(a)': '', 'H(a)': '', 'I(a)': '', 'J(a)': '', 'K(a)': '', 'L(a)': '', 'M(a)': '', 'N(a)': '', 'O(a)': '', 'P(a)': '', 'Q(a)': '', 'R(a)': '', 'S(a)': '', 'T(a)': '', 'U(a)': '', 'V(a)': '', 'W(a)': '', 'X(a)': '', 'Y(a)': '', 'Z(a)': '', 'A(b)': ''}
This library have this book
f3 is : None
f4 is : None
This reader has borrowed book: C(a)
f5 is : None
No reader in record.
f6 is : None
f7 is : {'B(a)': '', 'C(a)': '', 'D(a)': '', 'E(a)': '', 'F(a)': '', 'G(a)': '', 'H(a)': '', 'I(a)': '', 'J(a)': '', 'K(a)': '', 'L(a)': '', 'M(a)': '', 'N(a)': '', 'O(a)': '', 'P(a)': '', 'Q(a)': '', 'R(a)': '', 'S(a)': '', 'T(a)': '', 'U(a)': '', 'V(a)': '', 'W(a)': '', 'X(a)': '', 'Y(a)': '', 'Z(a)': '', 'A(b)': ''}
No reader in record.
f8 is : None
No user wait for this book.
f9 is : None
Success!!!
This book has been checker out.
This book has been checker out.
None None  Wang Huang

基本可以完美的复现借书还书以及查看等待借书的人的列表。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值