Books

Python Classes Tutorial: Building a Simple Library Management System

In this tutorial, we’ll explore a practical use case for Python classes by building a simple library management system. The system will have basic functionalities like adding books, removing books, and displaying available books.

Table of Contents

  1. Getting Started
  2. Book Class
  3. Library Class
  4. User Interface
  5. Putting It All Together

1. Getting Started

Before diving in, it’s important to know why we use classes. Classes provide a means of bundling data and functionality together, making it a powerful tool for creating more complex data structures or objects.

2. Book Class

We start by defining a simple Book class which has attributes like title, author, and ISBN number.

class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn

Example:

book1 = Book("1984", "George Orwell", "978-0451524935")

3. Library Class

Next, we define a Library class that will manage multiple Book objects.

class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.append(book)
        print(f"Added {book.title}")

    def remove_book(self, isbn):
        for book in self.books:
            if book.isbn == isbn:
                self.books.remove(book)
                print(f"Removed {book.title}")
                return
        print("Book not found")

    def display_books(self):
        for book in self.books:
            print(f"{book.title} by {book.author} - {book.isbn}")

Example:

library = Library()
library.add_book(Book("1984", "George Orwell", "978-0451524935"))

4. User Interface

Now, we’ll create a simple user interface to interact with the Library class.

def library_interface():
    library = Library()

    while True:
        print("\nLibrary Management")
        print("1: Add Book")
        print("2: Remove Book")
        print("3: Display Books")
        print("4: Exit")

        choice = input("Choose an option (1/2/3/4): ")

        if choice == '1':
            title = input("Enter book title: ")
            author = input("Enter book author: ")
            isbn = input("Enter book ISBN: ")
            library.add_book(Book(title, author, isbn))

        elif choice == '2':
            isbn = input("Enter book ISBN to remove: ")
            library.remove_book(isbn)

        elif choice == '3':
            print("Available Books:")
            library.display_books()

        elif choice == '4':
            break

Example:

library_interface()

5. Putting It All Together

Here’s how you can combine all the pieces to build the complete library management system.

# Book Class
class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn

# Library Class
class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.append(book)
        print(f"Added {book.title}")

    def remove_book(self, isbn):
        for book in self.books:
            if book.isbn == isbn:
                self.books.remove(book)
                print(f"Removed {book.title}")
                return
        print("Book not found")

    def display_books(self):
        for book in self.books:
            print(f"{book.title} by {book.author} - {book.isbn}")

# User Interface
def library_interface():
    library = Library()

    while True:
        print("\nLibrary Management")
        print("1: Add Book")
        print("2: Remove Book")
        print("3: Display Books")
        print("4: Exit")

        choice = input("Choose an option (1/2/3/4): ")

        if choice == '1':
            title = input("Enter book title: ")
            author = input("Enter book author: ")
            isbn = input("Enter book ISBN: ")
            library.add_book(Book(title, author, isbn))

        elif choice == '2':
            isbn = input("Enter book ISBN to remove: ")
            library.remove_book(isbn)

        elif choice == '3':
            print("Available Books:")
            library.display_books()

        elif choice == '4':
            break

# Run the interface
library_interface()

Congratulations, you’ve built a simple library management system using Python classes. This tutorial was designed to help you understand how Python classes can be used to encapsulate related data and functions into objects, making it easier to build complex applications. Feel free to expand upon this by adding features like user accounts, book check-out and return functionalities, etc.