Building real-world projects using OOP principles
Imagine you have a big box of LEGO bricks. Each brick is a small part, but when you put it together, you can build something amazing like a castle or a spaceship. In programming, we use something called Object-Oriented Programming (OOP) to build big projects by combining small, manageable parts. Let’s learn how to do this using Python!
Real-World Project: A Simple Library System
Let’s build a small library system where you can add books, lend them to people, and return them.
Step 1: Define the Classes
We’ll need three main classes: Library
, Book
, and Member
.
- Library: Manages the collection of books and the members.
- Book: Represents a book with a title and author.
- Member: Represents a library member who can borrow books.
Code Example
Class Definitions
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.is_borrowed = False
class Member:
def __init__(self, name):
self.name = name
self.borrowed_books = []
class Library:
def __init__(self):
self.books = []
self.members = []
def add_book(self, book):
self.books.append(book)
def add_member(self, member):
self.members.append(member)
def lend_book(self, book_title, member_name):
for book in self.books:
if book.title == book_title and not book.is_borrowed:
for member in self.members:
if member.name == member_name:
book.is_borrowed = True
member.borrowed_books.append(book)
return f"Book '{book_title}' has been lent to {member_name}"
return f"Book '{book_title}' is not available"
def return_book(self, book_title, member_name):
for member in self.members:
if member.name == member_name:
for book in member.borrowed_books:
if book.title == book_title:
book.is_borrowed = False
member.borrowed_books.remove(book)
return f"Book '{book_title}' has been returned by {member_name}"
return f"Book '{book_title}' was not borrowed by {member_name}"
Step 2: Create Objects and Use Them
Now, let’s create some books, members, and a library. We’ll add books and members to the library and perform lending and return actions.
# Create some books
book1 = Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling")
book2 = Book("The Hobbit", "J.R.R. Tolkien")
# Create some members
member1 = Member("Alice")
member2 = Member("Bob")
# Create a library
library = Library()
# Add books to the library
library.add_book(book1)
library.add_book(book2)
# Add members to the library
library.add_member(member1)
library.add_member(member2)
# Lend a book to a member
print(library.lend_book("Harry Potter and the Philosopher's Stone", "Alice"))
# Return a book from a member
print(library.return_book("Harry Potter and the Philosopher's Stone", "Alice"))
Practice Questions
- Add More Books: Create two more books and add them to the library. Write the code to do this.
- More Members: Create one more member and add them to the library. Write the code to do this.
- Lend and Return: Lend a book to the new member and then return it. Write the code to do this.
Practice Solutions
- Add More Books:
# Create more books
book3 = Book("1984", "George Orwell")
book4 = Book("To Kill a Mockingbird", "Harper Lee")
# Add books to the library
library.add_book(book3)
library.add_book(book4)
- More Members:
# Create another member
member3 = Member("Charlie")
# Add the member to the library
library.add_member(member3)
- Lend and Return:
# Lend a book to the new member
print(library.lend_book("1984", "Charlie"))
# Return the book from the new member
print(library.return_book("1984", "Charlie"))
By using classes and objects, we can create complex systems in a simple and organized way. In this example, we built a small library system that can manage books and members, and handle lending and returning books. This is just a start! With OOP, you can build even more complex and interesting projects. Keep practicing, and you’ll become a great programmer!