I made this FizzBuzz game for my college class and it's kind of ridiculous, but is suppose to demonstrate chain of command so writing it in a little forloop was out of the question. In case you don't know the game, this program counts numbers 1-100 printing them to the console but replace any multiple of 3 with the word Fizz, multiples of 5 with the word Buzz, and multiples of 3 and 5 with the word FizzBuzz. There's actually more I'd like to do to it, but I've already been "spoken to" about adding extra features that he didn't ask for so I'm trying to keep this simple. I'll take any critique though because I tried new things like declaring string as constants and not using curly braces when my if statements only had one line. I have another version with user inputs and the ability to restart, and I'm thinking about making it go backwards, use letters, and add options for other multiples.
public class Main {
public static void main(String[] args) {
int startNum = 1;
int endNum = 100;
GameMgr.Start(startNum, endNum);
}
}
public abstract class GameMgr {
// THESE ARE THE MESSAGES THAT WILL BE PRINTED
// IF THE CURRENT NUMBER IS DIVISIBLE WITH THE
// CURRENT OBJECTS DIVISOR
final String FIFTEEN = "FizzBuzz";
final String THREE = "Fizz";
final String FIVE = "Buzz";
// THESE HOLD THE CURRENT NUMBER BEING
// EVALUATED AS WELL AS THE NUMBER THAT
// INDICATES THE END
static int currNum;
static int endNum;
//DEFAULT CONSTRUCTOR
public GameMgr(){}
// FILLS THE VALUES TO BE EVALUATED AND
// STARTS THE CHAIN OF COMMAND
public static void Start(int a, int b){
currNum = a;
endNum = b;
new BuzzFizz();
}
// CALCULATES THE CURRENT VALUE WITH THE
// CURRENT OBJECTS DIVISOR
protected boolean isDivisible(int divisor){
return currNum % divisor == 0;
}
// CHECKS IF THE SYSTEM HAS EXCEEDED THE
// ENDPOINT. IF NOT, VALUE IS INCREASED
// AND CHAIN OF COMMAND IS RESTARTED
protected void ProcessNext(){
currNum++;
if (currNum <= endNum)
GameMgr.Start(currNum, endNum);
else
System.exit(0);
}
// METHOD IS IMPLEMENTED IN THE CHILD CLASSES
// TO EVALUATE THE CURRENT WITH WITH THEIR
// RESPECTIVE DIVISOR
public abstract String Eval();
}
public class BuzzFizz extends GameMgr {
// SPECIFIC DIVISOR FOR THIS CHILD
int divisor = 15;
// CALLS THE EVALUATION METHOD DETERMINES
// WHETHER TO CONTINUE CHAIN OR RESTART
// THE CHAIN
public BuzzFizz(){
System.out.println(Eval());
if (Eval() != null)
ProcessNext();
}
// CHECKS IF THE CURRENT NUMBER IS DIVISIBLE
// WITH THIS OBJECTS DIVISOR RETURNS MESSAGE
// TO PRINT TO CONSOLE IF TRUE, CREATES
// NEXT OBJECT IN THE CHAIN IF FALSE
@Override
public String Eval(){
if (isDivisible(divisor))
return FIFTEEN;
new Fizz();
return "";
}
}
public class Fizz extends GameMgr {
// SPECIFIC DIVISOR FOR THIS CHILD
int divisor = 3;
// CALLS THE EVALUATION METHOD DETERMINES
// WHETHER TO CONTINUE CHAIN OR RESTART
// THE CHAIN
public Fizz(){
System.out.println(Eval());
if (Eval() != null)
ProcessNext();
}
// CHECKS IF THE CURRENT NUMBER IS DIVISIBLE
// WITH THIS OBJECTS DIVISOR RETURNS MESSAGE
// TO PRINT TO CONSOLE IF TRUE, CREATES
// NEXT OBJECT IN THE CHAIN IF FALSE
@Override
public String Eval(){
if (isDivisible(divisor))
return THREE;
new Buzz();
return "";
}
}
public class Buzz extends GameMgr {
// SPECIFIC DIVISOR FOR THIS CHILD
int divisor = 5;
// CALLS THE EVALUATION METHOD DETERMINES
// WHETHER TO CONTINUE CHAIN OR RESTART
// THE CHAIN
public Buzz(){
System.out.println(Eval());
if (Eval() != null)
ProcessNext();
}
// CHECKS IF THE CURRENT NUMBER IS DIVISIBLE
// WITH THIS OBJECTS DIVISOR RETURNS MESSAGE
// TO PRINT TO CONSOLE IF TRUE, CREATES
// NEXT OBJECT IN THE CHAIN IF FALSE
@Override
public String Eval(){
if (isDivisible(divisor))
return FIVE;
new BasicNum();
return "";
}
}
public class BasicNum extends GameMgr {
// PRINTS THE CURRENT NUMBER AND
// RESTARTS THE CHAIN
public BasicNum(){
System.out.println(currNum);
ProcessNext();
}
// SINCE THIS IS THE END OF THE
// CHAIN, THIS METHOD DOESN'T NEED
// TO DO ANYTHING
@Override
public String Eval(){
return null;
}
}