Wednesday, February 28, 2007

I am the king of FizzBuzz

With all the latest hooplah about fizzBuzz, I decided to make the most maintainable VB.NET implementation of fizzbuzz ever! Check out the majesty:
Public Class fizzBuzzCalculator

private _count as integer
private readonly _fizzText, _buzzText as string
private readonly _fizzInt, _buzzInt, _ceilingInt, _DoneInt as integer

Public Sub New()
_count = 1
_ceilingInt = 100
_doneInt = Integer.minValue
_fizzText = "fizz"
_buzzText = "buzz"
_fizzInt = 3
_buzzInt = 5
end sub

Public Function PrintNumbers() as String
dim count as integer
count = _GetNextInt()
While count != Integer.MinValue
Console.WriteLine(_CalculateOutputForSingleInt(count)
count = _GetNextInt()
end while
end function

Private Function _CalculateOutputForSingleInt(ByVal int as integer) as String
Dim output as string
if int mod _fizzInt = 0 then
output += _fizzText
end if
if int mod _buzzInt = 0 then
output += _buzzText
end if
if output = String.Empty then
output = int.toString()
end if
return output
end function

Private Function _GetNextInt() as integer
if _count > _ceilingInt then
_GetNextInt = Integer.minValue
else
_GetNextInt = _count
_count += 1
end if
end function

end class
Post a Comment
All rights reserved. Take that!