Menu
Coddy logo textTech

Admin Interface

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

challenge icon

Challenge

Easy

Let's extend our Library Management System by adding administrative capabilities. You'll create an Admin class that inherits from User but has special powers to manage the library's inventory — adding new books and removing existing ones.

You'll organize your code across five files:

  • Book.php — Your Book class 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. Your Library class with the existing functionality including addBook(), findBook(), borrowBook(), returnBook(), and the search methods. Add a new method:
    • removeBook(string $isbn): void — removes a book from the collection using unset()
  • Admin.php — Include the Library file. Create an Admin class that extends User. Admins inherit all user capabilities but gain two administrative methods:
    • addBookToLibrary(Library $library, Book $book): string — adds the book to the library and returns "Added: [title]"
    • removeBookFromLibrary(Library $library, string $isbn): string — attempts to remove a book with these checks:
      • If the book doesn't exist, return "Book not found"
      • If the book is currently borrowed, return "Cannot remove: book is currently borrowed"
      • Otherwise, remove it and return "Removed: [title]"
  • main.php — Include the Admin file. You'll receive two inputs: an ISBN and a book title.

    Create a Library and an Admin with ID 1 and name "AdminUser". Also create a regular User with ID 2 and name "RegularUser".

    Create a Book with the provided ISBN, title, and author "Test Author". Perform these operations and print each result on a new line:

    1. Have the admin add the book to the library
    2. Have the regular user borrow the book
    3. Have the admin try to remove the book (should fail — it's borrowed)
    4. Have the regular user return the book
    5. Have the admin remove the book (should succeed now)
    6. Have the admin try to remove the same book again (should fail — not found)

This challenge demonstrates how inheritance creates a natural hierarchy — admins are users with extra privileges. The safety checks in removeBookFromLibrary ensure data integrity by preventing removal of books that are still in circulation.

Try it yourself

<?php
require_once 'Admin.php';

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

// TODO: Create a Library

// TODO: Create an Admin with ID 1 and name "AdminUser"

// TODO: Create a regular User with ID 2 and name "RegularUser"

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

// TODO: Perform these operations and print each result on a new line:
// 1. Have the admin add the book to the library
// 2. Have the regular user borrow the book
// 3. Have the admin try to remove the book (should fail - it's borrowed)
// 4. Have the regular user return the book
// 5. Have the admin remove the book (should succeed now)
// 6. Have the admin try to remove the same book again (should fail - not found)

?>

All lessons in Object Oriented Programming