At the school library, there is a need for a new library management system. One of the basic requirements of the system is to keep track of which books are in the library and which ones have been issued / checked-out by students.
Using the above management system, you are required to help the school answer the below questions:
1. Which three books have been issued the greatest number of times?
2. Which books do not have any more copies left in the library (all copies have been issued)?
3. Find out if a particular book is available in the library based on the book id.
4. Out of the entire set of books in the library, list the books that have never been checked out by students?
5. Print a list of all the books and available copies.
class Book:
def __init__(self, bookID, totalNumberBooks):
self.bookID = bookID
self.totalNumberBooks = totalNumberBooks
self.numberBooksCheckedOut = 0
def displayBook(self):
print(f"Book ID: {self.bookID}")
print(f"Total number of books left: {self.totalNumberBooks}")
print(f"Number of books checked out: {self.numberBooksCheckedOut}\n")
def checkedOut(self):
self.totalNumberBooks -=1
self.numberBooksCheckedOut +=1
def main():
books=[]
books.append(Book(10000,10))
books.append(Book(10001,5))
books.append(Book(10002,8))
books.append(Book(10003,7))
books.append(Book(10004,9))
books.append(Book(10005,8))
books.append(Book(10006,6))
books.append(Book(10007,7))
books.append(Book(10008,8))
books.append(Book(10009,6))
books.append(Book(10010,3))
for i in range(0,2):
books[2].checkedOut()
books[1].checkedOut()
books[9].checkedOut()
books[8].checkedOut()
for i in range(0,5):
books[2].checkedOut()
books[5].checkedOut()
for i in range(0,4):
books[7].checkedOut()
books[4].checkedOut()
books[9].checkedOut()
for i in range(0,3):
books[5].checkedOut()
books.sort(key = lambda c: c.numberBooksCheckedOut, reverse=True)
#1. Which three books have been issued the greatest number of times?
print("Three books have been issued the greatest number of times:")
for i in range(0,3):
books[i].displayBook()
#2. Which books do not have any more copies left in the library (all copies have been issued)?
print("\nBooks that have not any more copies left in the library (all copies have been issued):")
for book in books:
if(book.totalNumberBooks==0):
book.displayBook()
#3. Find out if a particular book is available in the library based on the book id.
print("\nA particular book is available in the library based on the book id = 10003")
for book in books:
if(book.bookID==10003):
book.displayBook()
#4. Out of the entire set of books in the library, list the books that have never been checked out by students?
print("\nList the books that have never been checked out by students:")
for book in books:
if(book.numberBooksCheckedOut==0):
book.displayBook()
#5. Print a list of all the books and available copies.
print("\nA list of all the books and available copies")
for book in books:
book.displayBook()
main()
Example:
Three books have been issued the greatest number of times:
Book ID: 10005
Total number of books left: 0
Number of books checked out: 8
Book ID: 10002
Total number of books left: 1
Number of books checked out: 7
Book ID: 10009
Total number of books left: 0
Number of books checked out: 6
Books that have not any more copies left in the library (all copies have been issued):
Book ID: 10005
Total number of books left: 0
Number of books checked out: 8
Book ID: 10009
Total number of books left: 0
Number of books checked out: 6
A particular book is available in the library based on the book id = 10003
Book ID: 10003
Total number of books left: 7
Number of books checked out: 0
List the books that have never been checked out by students:
Book ID: 10000
Total number of books left: 10
Number of books checked out: 0
Book ID: 10003
Total number of books left: 7
Number of books checked out: 0
Book ID: 10006
Total number of books left: 6
Number of books checked out: 0
Book ID: 10010
Total number of books left: 3
Number of books checked out: 0
A list of all the books and available copies
Book ID: 10005
Total number of books left: 0
Number of books checked out: 8
Book ID: 10002
Total number of books left: 1
Number of books checked out: 7
Book ID: 10009
Total number of books left: 0
Number of books checked out: 6
Book ID: 10004
Total number of books left: 5
Number of books checked out: 4
Book ID: 10007
Total number of books left: 3
Number of books checked out: 4
Book ID: 10001
Total number of books left: 3
Number of books checked out: 2
Book ID: 10008
Total number of books left: 6
Number of books checked out: 2
Book ID: 10000
Total number of books left: 10
Number of books checked out: 0
Book ID: 10003
Total number of books left: 7
Number of books checked out: 0
Book ID: 10006
Total number of books left: 6
Number of books checked out: 0
Book ID: 10010
Total number of books left: 3
Number of books checked out: 0
Comments
Leave a comment