<?php
/**
* BookStore has single coopies of books
*/
class BookStore {
private $books = array();
/**
* @param Book book, book to be added
* @return boolean true if book was added, false if book was in store...
*/
public function addBook(Book $book) {
if ($this->isBookInStore($book))
return false;
//Add book to the end of the array
$this->books[] = $book;
return true;
}
/**
* @param Book book, book to be compared to Store
* @return boolean true if book exists in store
*/
private function isBookInStore(Book $book) {
//isBookin store
foreach ($this->books as $inStoreBook) {
if ($inStoreBook->isSame($book)) {
return true;
}
}
return false;
}
}
//Testcase
$store = new BookStore();
$bookUnlimited = new Book("Daniel", "PHP Unlimited", "123456");
$bookWasInStore = $store->AddBook($bookUnlimited);
assert($bookWasInStore);
//Add the same book again
$bookWasInStore = $store->AddBook($bookUnlimited);
assert(!$bookWasInStore);