/// Implementation of Ninety-Nine Bottles of Beer Song in C#.
/// What's neat is that .NET makes the Binge class a
/// full-fledged component that may be called from any other
/// .NET component.
///
/// Paul M. Parks
/// http://www.parkscomputing.com/
/// February 8, 2002
///
using System;
namespace NinetyNineBottles
{
///
/// References the method of output.
///
public delegate void Writer(string format, params object[] arg);
///
/// References the corrective action to take when we run out.
///
public delegate int MakeRun();
///
/// The act of consuming all those beverages.
///
public class Binge
{
///
/// What we'll be drinking.
///
private string beverage;
///
/// The starting count.
///
private int count = 0;
///
/// The manner in which the lyrics are output.
///
private Writer Sing;
///
/// What to do when it's all gone.
///
private MakeRun RiskDUI;
public event MakeRun OutOfBottles;
///
/// Initializes the binge.
///
/// How many we're consuming.
///
/// Our instructions, should we succeed.
///
/// How our drinking song will be heard.
/// What to drink during this binge.
public Binge(string beverage, int count, Writer writer)
{
this.beverage = beverage;
this.count = count;
this.Sing = writer;
}
///
/// Let's get started.
///
public void Start()
{
while (count > 0)
{
Sing(
@"
{0} bottle{1} of {2} on the wall,
{0} bottle{1} of {2}.
Take one down, pass it around,",
count, (count == 1) ? "" : "s", beverage);
count--;
if (count > 0)
{
Sing("{0} bottle{1} of {2} on the wall.",
count, (count == 1) ? "" : "s", beverage);
}
else
{
Sing("No more bottles of {0} on the wall.", beverage, null);
}
}
Sing(
@"
No more bottles of {0} on the wall,
No more bottles of {0}.", beverage, null);
if (this.OutOfBottles != null)
{
count = this.OutOfBottles();
Sing("{0} bottles of {1} on the wall.", count, beverage);
}
else
{
Sing("First we weep, then we sleep.");
Sing("No more bottles of {0} on the wall.", beverage, null);
}
}
}
///
/// The song remains the same.
///
class SingTheSong
{
///
/// Any other number would be strange.
///
const int bottleCount = 99;
///
/// The entry point. Sets the parameters of the Binge and starts it.
///
/// unused
static void Main(string[] args)
{
Binge binge =
new Binge("beer", bottleCount, new Writer(Console.WriteLine));
binge.OutOfBottles += new MakeRun(SevenEleven);
binge.Start();
}
///
/// There's bound to be one nearby.
///
/// Whatever would fit in the trunk.
static int SevenEleven()
{
Console.WriteLine("Go to the store, get some more...");
return bottleCount;
}
}
}