Project Level 2: Intermediate
This project is designed for intermediate learners who know Python fundamentals and are practicing building complete programs.
Project Description
For today’s project, the task is to build a console-based book lending system for a small library or a personal book collection. This app allows users to browse available books, borrow a book, return a book, and view all borrowed books.
The user can choose option 1 to view available books for borrowing:
The user can choose 2 and then enter the number of the book they want to borrow:
The user can choose 3 to return a book so the book becomes available again for borrowing:
👉 We challenge you to use classes (i.e., OOP) instead of functions this time. Here is how the code would look like. You can complete the rest yourself.
class BookLendingSystem:
def __init__(self):
self.available_books = {
1: "The Great Gatsby",
2: "To Kill a Mockingbird",
3: "1984",
4: "Pride and Prejudice"
}
self.borrowed_books = {}
def display_menu(self):
print("\nWelcome to the Book Lending System!")
...
...
def view_available_books(self):
...
...
def borrow_book(self):
if not self.available_books:
...
...
def return_book(self):
if not self.borrowed_books:
...
...
def view_borrowed_books(self):
if not self.borrowed_books:
...
...
def run(self):
while True:
self.display_menu()
choice = input("\nChoose an option: ").strip()
if choice == "1":
...
...
system = BookLendingSystem()
system.run()
Learning Benefits
You will practice managing lists and dictionaries, handling user input, and implementing logical workflows for an interactive application. In addition, you will practice working with classes.
Prerequisites
Required Libraries: No libraries are needed for this project.
Required Files: No files are needed for this project.
IDE: You can use any IDE on your computer to code the project.
Danger Zone
Once you code the project, compare it with our two solutions given in the button below.
Hi Ardit - Thanks for the Project. I am currently following your tutorials on oops in Udemy.
Please find the code below let me know how to optimize/improvise the code further. I would like to become a hands on OOPS developer in python. Your feedback helps me alot.
import sys
class BookLendingSystem:
def __init__(self):
self.available_books = {
1: "The Great Gatsby",
2: "To Kill a Mockingbird",
3: "1984",
4: "Pride and Prejudice"
}
self.borrowed_books = {}
def display_menu(self):
print("\nWelcome to the Book Lending System!")
menu = {
1 : "View Available Books" ,
2 : "Borrow a Book",
3 : "Return a Book",
4 : "View Borrowed Books",
5 : "Exit"
}
for key, value in menu.items():
print(f"{key}. {value}")
def view_available_books(self):
print("--- The Available Books ---")
for key, value in self.available_books.items():
print(f"{key}. {value}")
def borrow_book(self):
self.view_available_books()
book_num = int(input("\nEnter the book number to borrow: ").strip())
self.name = input("\nEnter your name: ").strip()
if not self.available_books:
pass
else:
for key, value in self.available_books.items():
if key == book_num:
print(f'You have borrowed "{value}". Please return it on time.')
self.borrowed_books[key] = value
def view_borrowed_books(self):
if not self.borrowed_books:
pass
else:
print("\n--- Borrowed Books ---")
for key, value in self.borrowed_books.items():
print(f"{key}. {value} - Borrowed by {self.name}.")
def return_book(self):
if not self.borrowed_books:
pass
else:
self.view_borrowed_books()
book_num = int(input("\nEnter the book number to return: ").strip())
for key, value in self.borrowed_books.items():
print(f'Thank you, {self.name}, for returning "{value}".')
def run(self):
while True:
self.display_menu()
choice = input("\nChoose an option: ").strip()
if choice == "1":
self.view_available_books()
if choice == "2":
self.borrow_book()
if choice == "3":
self.return_book()
if choice == "4":
self.view_borrowed_books()
if choice == "5":
sys.exit()
system = BookLendingSystem()
system.run()