13.Library Management SystemOOPs.py
# Write a "Library" class with no_of_books(int) and books(list) as two instance variables.
# Make a method that checks whether the no_of_books== books(len)
# Write a program to create a library from Library class and show how you can print all books, add a book and get the number of books using different methods.
# Show that your program dosent persist the books after the program is stopped
class Library:
def __init__(self, no_of_books, books):
self.no_of_books = no_of_books
self.books = books
def check(self):
# Check whether the number of books matches the length of the books list
if self.no_of_books == len(self.books):
print("Number of books matches the length of the books list.")
else:
print("Number of books does not match the length of the books list.")
def print_all_books(self):
# Print all books in the library
print("Books in the library:")
for book in self.books:
print(book)
def add_book(self, new_book):
# Add a new book to the library
self.books.append(new_book)
self.no_of_books += 1
print(f"Added '{new_book}' to the library.")
def get_number_of_books(self):
# Return the number of books in the library
print(f"There are now {self.no_of_books} books in the library.")
# Example usage:
# Provide the initial number of books and a list of books when creating an instance
library_instance = Library(3, ['The Psychology of Money', 'The Richest man in Babylon', 'Think and Grow Rich'])
# Call the check method to verify the initial state
library_instance.check()
# Call the print_all_books method to display all books in the library
library_instance.print_all_books()
# Call the add_book method to add a new book to the library
library_instance.add_book('The Power of your Subconcious Mind')
# Call the get_number_of_books method to get the updated number of books
library_instance.get_number_of_books()
Comments
Post a Comment