Language C#
| Date: | 04/20/05 |
| Author: | Paul M. Parks |
| URL: | n/a |
| Comments: | 8 |
| Info: | n/a |
| Score: |
/// 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
{
/// <summary>
/// References the method of output.
/// </summary>
public delegate void Writer(string format, params object[] arg);
/// <summary>
/// References the corrective action to take when we run out.
/// </summary>
public delegate int MakeRun();
/// <summary>
/// The act of consuming all those beverages.
/// </summary>
public class Binge
{
/// <summary>
/// What we'll be drinking.
/// </summary>
private string beverage;
/// <summary>
/// The starting count.
/// </summary>
private int count = 0;
/// <summary>
/// The manner in which the lyrics are output.
/// </summary>
private Writer Sing;
/// <summary>
/// What to do when it's all gone.
/// </summary>
private MakeRun RiskDUI;
public event MakeRun OutOfBottles;
/// <summary>
/// Initializes the binge.
/// </summary>
/// <param name="count">How many we're consuming.</param>
/// <param name="disasterWaitingToHappen">
/// Our instructions, should we succeed.
/// </param>
/// <param name="writer">How our drinking song will be heard.</param>
/// <param name="beverage">What to drink during this binge.</param>
public Binge(string beverage, int count, Writer writer)
{
this.beverage = beverage;
this.count = count;
this.Sing = writer;
}
/// <summary>
/// Let's get started.
/// </summary>
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);
}
}
}
/// <summary>
/// The song remains the same.
/// </summary>
class SingTheSong
{
/// <summary>
/// Any other number would be strange.
/// </summary>
const int bottleCount = 99;
/// <summary>
/// The entry point. Sets the parameters of the Binge and starts it.
/// </summary>
/// <param name="args">unused</param>
static void Main(string[] args)
{
Binge binge =
new Binge("beer", bottleCount, new Writer(Console.WriteLine));
binge.OutOfBottles += new MakeRun(SevenEleven);
binge.Start();
}
/// <summary>
/// There's bound to be one nearby.
/// </summary>
/// <returns>Whatever would fit in the trunk.</returns>
static int SevenEleven()
{
Console.WriteLine("Go to the store, get some more...");
return bottleCount;
}
}
}
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Concise, using C# 3.5 features | Jeff Dietrich | 10/26/07 | 9 | |
| 2 | Mark Hurley | 05/31/05 | 0 | |
| Shows new features of C# 2.0. | Bradley Tetzlaff | 11/24/05 | 0 |
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!
Comments
I applied a patch from Tamara Roberson. She said the code had some other problem as well. Unfortunately, I can't test it since I don't have C# available atm. Can anyone give feedback, if the code is correct now?
And yes, Main is in SingTheSong.
still, since it's just a showoff, i'll give you the benefit of doubt...