Arrayer

<?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);

Welcome to CoursePress

en utav Linnéuniversitets lärplattformar. Som inloggad student kan du kommunicera, hålla koll på dina kurser och mycket mer. Du som är gäst kan nå de flesta kurser och dess innehåll utan att logga in.

Läs mer lärplattformar vid Linnéuniversitetet

Student account

To log in you need a student account at Linnaeus University.

Read more about collecting your account

Log in LNU