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 haXe

Date:11/27/06
Author:Stephane Bebrone
URL:http://weblog.shaoken.be
Comments:2
Info:http://haxe.org/
Score: (3.01 in 186 votes)
/// Implementation of Ninety-Nine Bottles of Beer Song in HaXe.
/// This code is inspired by C# implementations.
/// You can use the differents outputs (JS, Flash, Neko) allowed by HaXe compiler.
///
/// Stephane Bebrone
/// http://weblog.shaoken.be
/// November 27, 2006
/// 

package ninetyninebottles;

class SingTheSong
{

	static var _BOTTLECOUNT:Int = 100;
	
	static function main()
	{
        var binge:Binge = 
			new Binge(_BOTTLECOUNT);
		binge.start();
	}	
}

class Binge
{
	private var _beverage:String;
	private var _count:Int;
	
	/**
	 * Initializes the binge.
	 * 
	 * @param	count		How many we're consuming.
	 */
	public function new(count:Int)
	{
		_count = count;
	}
	
	/**
	 * Let's get started.
	 * 
	 */
	public function start():Void
	{
		var currentIndex:Int = 0;
		var sb:StringBuf = new StringBuf();
		var count:Int = _count;
		while(count-- > 0)
		{
			sb.add(singVerseMethod(count));
		}
		trace(sb.toString());
	}
	
	/**
	 * The pluralization method type.
	 * 
	 * @param	bottleIndex	The bottle index.
	 * @return	An "bottles" if plural, otherwise "bottle".
	 */
	private function pluralizer(bottleIndex:Int):String
	{
		if(bottleIndex == 1)
		{
			return "1 bootle";
		}
		else
		{
			return bottleIndex + " bottles";
		}
	}
	
	/**
	 * Sings one verse of the 99 Bottles of Beer song.
	 * 
	 * @param	bottleIndex	The bottle index.
	 * @return	The song verse.
	 */
	private function singVerseMethod(bottleIndex:Int):String
	{
		var sbTmp:StringBuf = new StringBuf();
		if(bottleIndex > 1)
		{
			sbTmp.add(pluralizer(bottleIndex));
			sbTmp.add(" of beer on the wall, ");
			sbTmp.add(pluralizer(bottleIndex));
			sbTmp.add(" of beer.\nTake one down, pass it around,");
			sbTmp.add(pluralizer(bottleIndex - 1));
			sbTmp.add(" of beer on the wall.\r\r");
		}
		else if (bottleIndex == 1)
		{
			sbTmp.add(pluralizer(bottleIndex));
			sbTmp.add(" of beer on the wall, ");
			sbTmp.add(pluralizer(bottleIndex));
			sbTmp.add(" of beer.\nTake one down, pass it around,");
			sbTmp.add(" no more bottles of beer on the wall.\r\r");	
		}
		else if (bottleIndex == 0)  
		{
			sbTmp.add("No more bottles of beer on the wall,");
			sbTmp.add("no more bottles of beer.\r");
			sbTmp.add("Go to the store and buy some more, ");
			sbTmp.add(_count-1);
			sbTmp.add(" bottles of beer on the wall.\r");
		}
		return sbTmp.toString();
	}
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
simple, short, fast and crossplatformtong11/28/090

Comments

>>  Garren said on 04/12/09 19:16:00

Garren That's twice as long as the COBOL version!!!

>>  kasper789 said on 03/05/10 15:01:32

kasper789

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: