-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.php
More file actions
47 lines (41 loc) · 1.18 KB
/
Library.php
File metadata and controls
47 lines (41 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once "AbstractLibrary.php";
require_once "Author.php";
class Library extends AbstractLibrary {
public function addAuthor(string $authorName): Author
{
$author = new Author($authorName);
$this->authors[] = $author;
return $author;
}
public function addBooksForAuthor(string $authorName, Book $book)
{
$author = $this->findAuthor($authorName);
if ($author) {
$author->addBook($book->getTitle(), $book->getPrice());
}
}
/**
* @param string $bookName
*/
public function search($bookName): ?Book
{
foreach ($this->getAuthors() as $author){
foreach ($author->getBooks() as $book){
if( $book->getTitle() === $bookName){
return $book;
}
}
}
return null;
}
public function print(){
foreach ($this->getAuthors() as $author){
echo $author->getName().PHP_EOL;
echo "_____________________".PHP_EOL;
foreach($author->getBooks() as $book){
echo $book->getTitle(). " ---". $book->getPrice().PHP_EOL;
}
}
}
}