📚 Dan's personal Library Manager

🔍 Search

➕ Add Book

🗑️ Remove Book

📖 All Books

TitleAuthorISBNRatingAvailableActions
The Eye of The World Robert Jordan 123456 ⭐ 4.0 ✅ Yes
The Great Hunt Robert Jordan 234561 ⭐ 4.0 ✅ Yes
The Dragon Reborn Robert Jordan 345612 ⭐ 4.0 ✅ Yes
The Shadow Rising Robert Jordan 456123 ⭐ 5.0 ✅ Yes
The Fires of Heaven Robert Jordan 561234 ⭐ 5.0 ✅ Yes
Lords of Chaos Robert Jordan 612345 ⭐ 5.0 ✅ Yes
A Crown of Swords Robert Jordan 567890 ⭐ 4.0 ✅ Yes
The Path of Daggers Robert Jordan 678905 ⭐ 4.0 ✅ Yes
Winters Heart Robert Jordan 789056 ⭐ 4.0 ✅ Yes
Crossroads of Twilight Robert Jordan 890567 ⭐ 4.0 ✅ Yes
Knife of Dreams Robert Jordan 905678 ⭐ 4.0 ✅ Yes
The Gathering Storm Robert Jordan 056789 ⭐ 5.0 ✅ Yes
The Towers of Midnight Robert Jordan 456789 ⭐ 5.0 ✅ Yes
A Memory of Light Robert Jordan 387645 ⭐ 5.0 ✅ Yes
Malice John Gwynne 387202 ⭐ 5.0 ✅ Yes
Valour John Gwynne 387203 ⭐ 5.0 ✅ Yes
Ruin John Gwynne 387204 ⭐ 5.0 ✅ Yes
Wraith John Gwynne 387205 ⭐ 5.0 ✅ Yes

PHP code being used for the Library


declare(strict_types=1);

// ---------------------
// Book Class
// ---------------------
class Book {
    public string $title;
    public string $author;
    public string $isbn;
    public bool $isAvailable;
    public float $rating;

    public function __construct(string $title, string $author, string $isbn,float $rating = 0.0) {
        $this->title = $title;
        $this->author = $author;
        $this->isbn = $isbn;
        $this->isAvailable = true;
        $this->rating = $rating;
    }

    public function toArray(): array {
        return [
            'title' => $this->title,
            'author' => $this->author,
            'isbn' => $this->isbn,
            'isAvailable' => $this->isAvailable,
            'rating' => $this->rating,
        ];
    }

    public static function fromArray(array $data): Book {
        $book = new Book($data['title'], $data['author'], $data['isbn'], isset($data['rating']) ? (float)$data['rating'] : 0.0);
        $book->isAvailable = $data['isAvailable'];
        return $book;
    }
}

// ---------------------
// Library Class
// ---------------------
class Library {
    /** @var Book[] */
    private array $books = [];

    public function addBook(Book $book): void {
        $this->books[] = $book;
    }

    public function removeBook(string $isbn): bool {
        foreach ($this->books as $index => $book) {
            if ($book->isbn === $isbn) {
                array_splice($this->books, $index, 1);
                return true;
            }
        }
        return false;
    }

    public function searchBooks(string $query): array {
        $results = [];
        foreach ($this->books as $book) {
            if (stripos($book->title, $query) !== false || stripos($book->author, $query) !== false) {
                $results[] = $book;
            }
        }
        return $results;
    }

    public function borrowBook(string $isbn): bool {
        foreach ($this->books as $book) {
            if ($book->isbn === $isbn) {
                if ($book->isAvailable) {
                    $book->isAvailable = false;
                    return true;
                } else {
                    echo "Error: Book is already borrowed.\n";
                    return false;
                }
            }
        }
        echo "Error: Book not found.\n";
        return false;
    }

    public function returnBook(string $isbn): bool {
        foreach ($this->books as $book) {
            if ($book->isbn === $isbn) {
                if (!$book->isAvailable) {
                    $book->isAvailable = true;
                    return true;
                } else {
                    echo "Error: Book is already available.\n";
                    return false;
                }
            }
        }
        echo "Error: Book not found.\n";
        return false;
    }

    public function getBooks(): array {
        return $this->books;
    }

    public function saveToFile(string $filename): void {
        $data = array_map(fn($book) => $book->toArray(), $this->books);
        file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT));
    }

    public function loadFromFile(string $filename): void {
        if (!file_exists($filename)) {
            echo "No saved library found.\n";
            return;
        }

        $json = file_get_contents($filename);
        $data = json_decode($json, true);

        $this->books = array_map(fn($bookData) => Book::fromArray($bookData), $data);
    }
}

// ---------------------
// Helper Functions
// ---------------------
function displayBook(Book $book): void {
    echo "Title: {$book->title}, Author: {$book->author}, ISBN: {$book->isbn}, Rating: {$book->rating}, Available: " . ($book->isAvailable ? 'Yes' : 'No') . "\n";
}

function displayAllBooks(array $books): void {
    if (empty($books)) {
        echo "No books found.\n";
        return;
    }
    foreach ($books as $book) {
        displayBook($book);
    }
}



require_once 'library_management.php';

$library = new Library();
$saveFile = 'library_data.json';
$library->loadFromFile($saveFile);

$message = "";

// Handle form actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add'])) {
        $title = trim($_POST['title'] ?? '');
        $author = trim($_POST['author'] ?? '');
        $isbn = trim($_POST['isbn'] ?? '');
         $rating = isset($_POST['rating']) ? (float)trim($_POST['rating']) : 0.0;
        if ($title && $author && $isbn) {
            $library->addBook(new Book($title, $author, $isbn, $rating));
            $library->saveToFile($saveFile);
            $message = "✅ Book added successfully!";
        } else {
            $message = "⚠️ All fields are required to add a book.";
        }
    }

    if (isset($_POST['remove'])) {
        $isbn = trim($_POST['isbn'] ?? '');
        if ($library->removeBook($isbn)) {
            $library->saveToFile($saveFile);
            $message = "🗑️ Book removed successfully.";
        } else {
            $message = "⚠️ Book with that ISBN not found.";
        }
    }

    if (isset($_POST['borrow'])) {
        $isbn = trim($_POST['isbn'] ?? '');
        if ($library->borrowBook($isbn)) {
            $library->saveToFile($saveFile);
            $message = "📚 Book borrowed.";
        } else {
            $message = "⚠️ Cannot borrow: Already borrowed or not found.";
        }
    }

    if (isset($_POST['return'])) {
        $isbn = trim($_POST['isbn'] ?? '');
        if ($library->returnBook($isbn)) {
            $library->saveToFile($saveFile);
            $message = "🔄 Book returned.";
        } else {
            $message = "⚠️ Cannot return: Book is already available or not found.";
        }
    }
}