Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language PHP5

(Objects, phpdoc comments, exceptions)

Date:09/22/05
Author:Dave Marshall
URL:n/a
Comments:2
Info:http://www.php.net
Score: (3.08 in 717 votes)
<?php

/**
 * One bottle left exception
 *
 * @author Dave Marshal
 * @since 22/09/2005
 * @package Bottles
 */
class LastBottleException extends Exception {}

/**
 * Out of bottles exception
 *
 * @author Dave Marshal
 * @since 22/09/2005
 * @package Bottles
 */
class OutOfBottlesException extends Exception {}

/**
 * Wall Class
 *
 * @author Dave Marshal
 * @since 22/09/2005
 * @package Bottles
 */
class Wall {

	/**
	 * Collection of Bottles
	 */
	private $bottle = array();

	/**
	 * Wall Constructor
	 *
	 * @param int $numberOfBottles The number of bottles on this wall
	 */
	public function __construct($numberOfBottles=99)
	{
		$this->addBottles($numberOfBottles);
	}

	/**
	 * Wall Desctructor
	 */
	public function __Destruct() {}

	/**
	 * Get next bottle
	 *
	 * @param boolean $force Force the function to return the last bottle
	 * @returns Bottle
	 * @throws OutOfBeerException
	 */
	public function getNextBottle($force = FALSE)
	{
		if (count($this->bottle) > 1) return array_pop($this->bottle);
		else if (count($this->bottle)==1) 
		{
			if ($force) return array_pop($this->bottle);
			else throw new LastBottleException();
		}
		else throw new OutOfBottlesException();
	}

	/** 
	 * Get number of beers left on the wall
	 *
	 * @return int
	 */
	public function getNumberOfBottles() 
	{
		return count($this->bottle);
	}

	/**
	 * Add more bottles
	 *
	 * @param int $numberOfBottles
	 */
	public function addBottles($numberOfBottles)
	{
		for ($i=0;$i<$numberOfBottles;$i++)
		{
			$this->bottle[] = new Bottle();
		}
	}
}

/**
 * Bottle Class
 *
 * @author Dave Marshal
 * @since 22/09/2005
 * @package Bottles
 */
class Bottle {
	
	/** 
	 * Bottle Constructor
	 */
	public function __construct() {}

	/**
	 * Bottle Destructor
	 */
	public function __destruct() {}

}

$wall = new Wall(99);

$continue = TRUE;

while ($continue)
{
	try {
		$bottlesLeftBefore = $wall->getNumberOfBottles();
		$bottle = $wall->getNextBottle();
		$bottlesLeftAfter = $wall->getNumberOfBottles();

		echo $bottlesLeftBefore." bottles of beer on the wall, ".$bottlesLeftBefore." bottles of
beer.\n";
		echo "Take one down and pass it around, ".$bottlesLeftAfter." bottles of beer on the wall.\n\n";
	}
	catch (LastBottleException $oneLeft)
	{
		$bottle = $wall->getNextBottle(TRUE);
		echo "1 bottle of beer on the wall, 1 bottle of beer.\n";
		echo "Take one down and pass it around, no more bottles of beer on the wall.\n\n";
	}
	catch (OutOfBottlesException $out)
	{
		echo "No more bottles of beer on the wall, no more bottles of beer.\n";
		echo "Go to the store and buy some more, ";
		$wall->addBottles(99);
		echo $wall->getNumberOfBottles()." bottles of beer on the wall.\n\n";
		$continue=FALSE;
	}
}

?>

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
PHP5 code using classesTim M09/02/059
another versionKenneth Clark05/17/054

Comments

>>  Eric J K said on 02/14/07 14:08:57

Eric J K Beautifull

>>  gurubob said on 02/10/08 08:02:34

gurubob Ok, so this is nice - it demonstrates advanced PHP features, but why oh why can't someone do a *simple* version like some of the other languages?

<?php
for ($c=99; $c>1; $c--)
echo "$c bottles of beer on the wall, $c bottles of beer, take one down and pass it around, ".($c-1)." bottles of beer on the wall!\n";

echo "1 bottle of beer on the wall, one bottle of beer, take one down and pass it around, no more bottles of beer on the wall!\n";
?>

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: