Menu
Coddy logo textTech

Borrowing System

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 84 of 91.

challenge icon

Challenge

Easy

Let's bring our Library Management System to life by creating the Library class that orchestrates all borrowing operations. This central coordinator will manage the book collection and enforce the rules that keep our library running smoothly.

You'll organize your code across four files:

  • Book.php — Your Book class from the previous lesson with readonly properties (isbn, title, author), a private $isAvailable property starting as true, and methods: isAvailable(), markAsBorrowed(), and markAsReturned().
  • User.php — Include the Book file. Your User class with readonly properties (id, name), a private $borrowedBooks array, a MAX_BOOKS constant of 3, and methods: getBorrowedBooks(), canBorrow(), addBook(Book $book), and removeBook(string $isbn).
  • Library.php — Include the User file. Create a Library class that manages the book collection. Your library should have:
    • A private $books array to store books indexed by ISBN
    • addBook(Book $book): void — adds a book to the collection
    • findBook(string $isbn): ?Book — returns the book or null if not found
    • borrowBook(User $user, string $isbn): string — validates and processes borrowing, returning appropriate messages
    • returnBook(User $user, string $isbn): string — processes returns and updates both book and user state

    The borrowBook method should check three conditions in order and return these exact messages:

    • "Book not found" — if the book doesn't exist
    • "Book is not available" — if already borrowed
    • "Borrowing limit reached" — if user has 3 books
    • "Successfully borrowed: [title]" — on success

    The returnBook method should return "Book not found" if the book doesn't exist, or "Successfully returned: [title]" on success.

  • main.php — Include the Library file. You'll receive two inputs: an ISBN and a book title.

    Create a Library, a User with ID 1 and name "Alice", and a Book with the provided ISBN, title, and author "Unknown". Add the book to the library, then perform these operations and print each result on a new line:

    1. Borrow the book
    2. Try to borrow the same book again
    3. Return the book
    4. Borrow the book once more

This challenge demonstrates how the Library class centralizes business logic — it validates conditions, coordinates state changes across Book and User objects, and returns meaningful feedback for each operation.

Try it yourself

<?php
require_once 'Library.php';

// Read input
$isbn = trim(fgets(STDIN));
$title = trim(fgets(STDIN));

// TODO: Create a Library instance

// TODO: Create a User with ID 1 and name "Alice"

// TODO: Create a Book with the provided ISBN, title, and author "Unknown"

// TODO: Add the book to the library

// TODO: Perform these operations and print each result on a new line:
// 1. Borrow the book
// 2. Try to borrow the same book again
// 3. Return the book
// 4. Borrow the book once more

?>

All lessons in Object Oriented Programming