Skip to content

Instantly share code, notes, and snippets.

@tatewake
Created March 22, 2017 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatewake/c371d59988c68d0ecee3951aa9562696 to your computer and use it in GitHub Desktop.
Save tatewake/c371d59988c68d0ecee3951aa9562696 to your computer and use it in GitHub Desktop.
public class fizzbuzz
{
enum StateType
{
kUninitialized,
kDestroyed,
kIdle,
kStart,
};
protected StateType mState = StateType.kUninitialized;
protected int i = 0;
protected void EnterIncrementI()
{
i++;
}
protected boolean EnterIsIDivisibleByFive()
{
return (i % 5) == 0;
}
protected boolean EnterIsIDivisibleByThree()
{
return (i % 3) == 0;
}
protected void EnterPrintBuzz()
{
System.out.println("Buzz");
}
protected void EnterPrintFizz()
{
System.out.println("Fizz");
}
protected void EnterPrintFizzBuzz()
{
System.out.println("Fizz Buzz");
}
protected void EnterPrintI()
{
System.out.println(i);
}
protected void EnterSetIToOne()
{
i = 1;
}
protected void OnConstruct()
{
assert(mState == StateType.kUninitialized);
if (mState == StateType.kUninitialized)
{
mState = StateType.kStart;
}
}
protected void OnDestruct()
{
assert(mState == StateType.kIdle || mState == StateType.kStart);
switch (mState)
{
case kIdle:
case kStart:
mState = StateType.kDestroyed;
break;
default:
break;
}
}
protected void OnIncrement()
{
assert(mState == StateType.kIdle || mState == StateType.kStart);
switch (mState)
{
case kIdle:
EnterIncrementI();
if (EnterIsIDivisibleByThree())
{
if (EnterIsIDivisibleByFive())
{
EnterPrintFizzBuzz();
}
else
{
EnterPrintFizz();
}
}
else
{
if (EnterIsIDivisibleByFive())
{
EnterPrintBuzz();
}
else
{
EnterPrintI();
}
}
break;
case kStart:
EnterSetIToOne();
if (EnterIsIDivisibleByThree())
{
if (EnterIsIDivisibleByFive())
{
mState = StateType.kIdle;
EnterPrintFizzBuzz();
}
else
{
mState = StateType.kIdle;
EnterPrintFizz();
}
}
else
{
if (EnterIsIDivisibleByFive())
{
mState = StateType.kIdle;
EnterPrintBuzz();
}
else
{
mState = StateType.kIdle;
EnterPrintI();
}
}
break;
default:
break;
}
}
public fizzbuzz()
{
mState = StateType.kUninitialized;
OnConstruct();
}
public void finalize() throws Throwable
{
OnDestruct();
super.finalize();
}
public void increment()
{
OnIncrement();
}
public static void main(String[] args)
{
fizzbuzz fb = new fizzbuzz();
for(int y = 0; y < 101; y++)
{
fb.increment();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment