You have been provided with the skeleton of a simple project: Library management system written in PHP. Your task is to complete the implementation of the classes by fulfilling the requirements specified in the TODO comments. Below is a detailed breakdown of what you need to do for each class:
This abstract class defines the structure and methods for a library system. You do not need to modify this class. Instead, you will implement its methods in the Library class.
- Key Methods:
addAuthor: Adds an author to the library.addBookForAuthor: Associates a book with an author.getBooksForAuthor: Retrieves all books for a specific author.search: Searches for a book by its title and returns its instance.print: Prints the authors and their books in a formatted output.
The Library class extends AbstractLibrary. Your job is to implement all the methods declared in the abstract class.
- Steps:
- Implement the
addAuthormethod to add authors to the$authorsarray and return the newly createdAuthorinstance. - Implement the
addBookForAuthormethod to associate aBookinstance with an existing author and set the book’sauthorproperty. - Implement the
getBooksForAuthormethod to retrieve all books for a given author. - Implement the
searchmethod to find and return a book by its title from the authors' books. - Implement the
printmethod to display the authors and their books in the required format.
- Implement the
The Book class represents a book and has the following attributes:
-
title: The title of the book. -
price: The price of the book. -
author: The author of the book (optional during creation). -
Steps:
- Generate getters and setters for all properties.
- Create a constructor that accepts values for all attributes. The
authorparameter should be optional.
The Author class represents an author and contains the following:
-
name: The name of the author. -
books: An array of books written by the author. -
Steps:
- Generate getters and setters for all properties.
- Create a constructor that initializes the
nameand optionally thebooksarray. - Implement the
addBookmethod to create a new book, add it to the$booksarray, and return theBookinstance.
You are provided with a test script at the end of the file:
require_once "Book.php";
require_once "Library.php";
$library = new Library();
$author = $library->addAuthor('Jack London');
$author->addBook("Martin Eden", 55);
$author->addBook("The Game", 35);
$library->addBookForAuthor('Jack London', new Book("A Son of the Sun", 25));
$author2 = $library->addAuthor('Mark Twain');
$author2->addBook('The Adventures of Tom Sawyer', 65);
$author2->addBook('Luck', 12);
$book = $library->search('Martin Eden'); // This must return instance of the book
$author = $book->getAuthor(); // This must return instance of the Author class
echo $author->getName(); // This must print "Jack London"
$library->print();- Run the Script:
- After completing the implementation, run the test script to ensure all functionalities work as expected.
- The output of the
printmethod should match the format given in the comments.