Untitled
<? namespace Classes; use Classes\Library; use Classes\User; class Book { private int $id; private string $title; private float $price; private array $authors; public function __construct(int $id, string $title, float $price, array $authors) { $this->id = $id; $this->title = $title; $this->price = $price; $this->authors = $authors; } public function getId(): int { return $this->id; } public function getTitle(): string { return $this->title; } public function getPrice(): float { return $this->price; } public function getAuthors(): array { return $this->authors; } } ?> <?php namespace Classes; interface borrowAble{ public function borrowBook(Book $book); public function returnBook(Book $book); } ?> <?php namespace Classes; use Exception; use Classes\Book; use Classes\User; class Library{ private array $books=[]; private array $users=[]; public function addBook(Book $book) { $this->books[]=$book; } public function getUsers() { return $this->users; } public function addUser(User $user) { $this->users[]=$user; } public function searchBooksByAuthor($author) { $sort=function($book) use($author) { $authors=$book->getAuthors(); return in_array($author, $authors); }; return array_filter($this->books, $sort); } public function listBooksAsJson() { $list=[]; foreach($this->books as $book){ $list[]=[ "id"=>$book->getId(), "title"=>$book->getTitle(), "price"=>$book->getPrice(), "authors"=>$book->getAuthors() ]; } return json_encode($list,JSON_PRETTY_PRINT); } public function calculateTotalBookPrice() { $totalPrice = 0; foreach ($this->books as $book) { $totalPrice += $book->getPrice(); } return $totalPrice; } } ?> <?php namespace Classes; use Exception; require_once "Book.php"; require_once "Borrowable.php"; require_once "Library.php"; require_once "User.php"; use Classes\Book; use Classes\Library; use Classes\User; class Test{ public function run() { $book1=new Book(1,"csosztokos",123,["Kocsog Csaba"]); $book2=new Book(2, "josok", 999.9,["Kovacs Szilard"]); $book3=new Book(3,"ass",1221,["nenez00"]); $book4=new Book(1,"szaiamiau",1223,["Tibikevagyokxd "]); $user1=new User(1,"csaba"); $user2=new User(2,"tibike"); $library=new Library(); $library->addBook($book1); $library->addBook($book2); $library->addBook($book3); $library->addBook($book4); $library->addUser($user1); $library->addUser($user2); $user1->borrowBook($book1); try{ $user1->returnBook($book2); }catch(Exception $e) { echo $e->getMessage(); } try{ $user2->borrowBook($book3); $user2->borrowBook($book2); $user2->borrowBook($book4); $user2->borrowBook($book1); }catch(Exception $e) { echo $e->getMessage(); } $userArrays=$library->getUsers(); foreach($userArrays as $user) { echo "User {$user->getName()}"; $books=$user->getBorrowedBooks(); foreach($books as $book) { echo "Borrowedbooks".$book->getTitle(); } } $booksByAuthor = $library->searchBooksByAuthor('Author First'); foreach ($booksByAuthor as $book) { echo "$book->getTitle"; } echo $library->listBooksAsJson(); echo"<br>"; echo $library->calculateTotalBookPrice(); } } $test=new Test(); $test->run(); ?> <?php namespace Classes; use Exception; use Classes\Book; use Classes\Library; class User implements Borrowable { private int $id; private string $name; private array $borrowedBooks=[]; public function __construct(int $id,string $name) { $this->id=$id; $this->name=$name; } public function getBorrowedBooks() { return $this->borrowedBooks; } public function getName() { return $this->name; } public function borrowBook(Book $book) { if (count($this->borrowedBooks)>=3) { throw new Exception("max 3 konyvet kolcsonozhet ki"); } else { echo "{this->name} returned ". $book->getTitle(); $this->borrowedBooks[]=$book; } } public function returnBook (Book $book) { $index=array_search($book, $this->getBorrowedBooks(),TRUE); if ($index === false){ throw new Exception("ez a konyv nincs kikolcsonozve"); }else{ echo "{$this->name}vissza hozta:".$book->getTitle(); unset($this->borrowedBooks[$index]); $this->borrowedBooks=array_values($this->borrowedBooks); } } } ?>
Leave a Comment