Skip to content

Commit a5021f0

Browse files
committed
Add "Library & Books" project
1 parent b3b67e8 commit a5021f0

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 4/8/2020
5+
* Time: 8:53 AM
6+
*/
7+
8+
/**
9+
* Class AbstractLibrary
10+
*/
11+
abstract class AbstractLibrary
12+
{
13+
/**
14+
* @var Author[]
15+
*/
16+
private $authors = [];
17+
18+
// TODO Generate getters and setters of properties
19+
20+
/**
21+
* Method accepts the name of the author, creates instance of the Author class,
22+
* adds the instance in $authors array and returns created instance
23+
*
24+
* @param string $authorName
25+
* @return Author
26+
*/
27+
abstract public function addAuthor(string $authorName): Author;
28+
29+
/**
30+
* Method accepts author name and Book. Finds author in $authors array and adds book to this array.
31+
* The method must set $book's $author property with the found author also.
32+
* This method is equivalent of Author::addBook
33+
*
34+
* @param $authorName
35+
* @param Book $book
36+
*/
37+
abstract public function addBookForAuthor($authorName, Book $book);
38+
39+
/**
40+
* Method returns books for given author
41+
*
42+
* @param $authorName
43+
*/
44+
abstract public function getBooksForAuthor($authorName);
45+
46+
/**
47+
* Method searches for book and returns instance of Book
48+
*
49+
* @param string $bookName
50+
* @return Book
51+
*/
52+
abstract public function search(string $bookName): Book;
53+
54+
/**
55+
* This method must print every author and for each author all its books in the following format
56+
* AuthorName
57+
* ----------------------
58+
* BookName - price
59+
* Book2Name - price
60+
* ...
61+
*
62+
* AnotherAuthorName
63+
* ----------------------
64+
* AnotherBookName - price
65+
* ...
66+
*/
67+
abstract public function print();
68+
}

Library & Books/Author.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
class Author
5+
{
6+
public string $name;
7+
public $books = [];
8+
9+
// TODO Generate getters and setters of properties
10+
// TODO Generate constructor for all attributes. $books argument of the constructor can be optional
11+
12+
/**
13+
* @param string $title
14+
* @param float $price
15+
* @return \Book
16+
*/
17+
public function addBook(string $title, float $price): Book
18+
{
19+
// TODO Create instance of the book. Add into $books array and return it
20+
}
21+
}

Library & Books/Book.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
class Book
5+
{
6+
public string $title;
7+
public float $price;
8+
public Author $author;
9+
10+
// TODO Generate getters and setters of properties
11+
// TODO Generate constructor for all attributes. $author argument of the constructor can be optional
12+
}

Library & Books/Library.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once "AbstractLibrary.php";
3+
4+
class Library extends AbstractLibrary
5+
{
6+
// TODO Implement all the methods declared in AbstractLibrary class
7+
}

Library & Books/index.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
require_once "Book.php";
4+
require_once "Library.php";
5+
6+
$library = new Library();
7+
$author = $library->addAuthor('Jack London');
8+
$author->addBook("Martin Eden", 55);
9+
$author->addBook("The Game", 35);
10+
$library->addBookForAuthor('Jack London', new Book("A Son of the Sun", 25));
11+
12+
$author2 = $library->addAuthor('Mark Twain');
13+
$author2->addBook('The Adventures of Tom Sawyer', 65);
14+
$author2->addBook('Luck', 12);
15+
16+
$book = $library->search('Martin Eden'); // This must return instance of the book
17+
$author = $book->getAuthor(); // This must return instance of the Author class
18+
echo $author->getName(); // This must print "Jack London"
19+
20+
$library->print();
21+
/*
22+
Jack London
23+
--------------------
24+
Martin Eden - 55
25+
The Game - 35
26+
A Son of the Sun - 25
27+
28+
Mark Twain
29+
--------------------
30+
The Adventures of Tom Sawyer - 65
31+
Luck - 12
32+
*/

0 commit comments

Comments
 (0)