Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- int startNum = 1;
- int endNum = 100;
- new GameManager(startNum, endNum).handleValue();
- }
- }
- ---
- public class GameManager {
- private int currentNumber;
- private int maxNumber;
- private Command commandChain;
- public GameManager(int initialNumber, int maxNumber) {
- this.currentNumber = initialNumber;
- this.maxNumber = maxNumber;
- //Create the command chain - FizzBuzz has to the be first
- this.commandChain = new FizzBuzz(new Fizz(new Buzz(
- new BasicNumber(null, this), this), this), this);
- }
- public void handleValue() {
- commandChain.startCommandChain(this.currentNumber);
- }
- public void valueHandled() {
- //increment and start handling of the new value if max value not yet reached
- this.currentNumber++;
- if(this.currentNumber <= maxNumber) {
- handleValue();
- }
- }
- }
- ---
- public abstract class Command {
- private Command nextCommand;
- private GameManager gameManager;
- public Command(Command nextCommand, GameManager gameManager) {
- this.nextCommand = nextCommand;
- this.gameManager = gameManager;
- }
- public void startCommandChain(int currentNumber) {
- //if the command is able to handle the currentNumber -> notify the GameManager
- if (this.doCommand(currentNumber)) {
- this.gameManager.valueHandled();
- }
- //Go on with the chain if the currentNumber is not yet handled
- else {
- if (this.nextCommand != null) {
- this.nextCommand.startCommandChain(currentNumber);
- }
- }
- }
- abstract boolean doCommand(int currentNumber);
- }
- ---
- public class FizzBuzz extends Command {
- private static final int DIVISOR = 15;
- public FizzBuzz(Command nextCommand, GameManager gameManager) {
- super(nextCommand, gameManager);
- }
- @Override
- public boolean doCommand(int currentNumber) {
- if(currentNumber % DIVISOR == 0) {
- System.out.println("FIZZBUZZ");
- return true;
- }
- return false;
- }
- }
- ---
- public class Fizz extends Command {
- private static final int DIVISOR = 3;
- public Fizz(Command nextCommand, GameManager gameManager) {
- super(nextCommand, gameManager);
- }
- @Override
- public boolean doCommand(int currentNumber) {
- if(currentNumber % DIVISOR == 0) {
- System.out.println("FIZZ");
- return true;
- }
- return false;
- }
- }
- ---
- public class Buzz extends Command {
- private static final int DIVISOR = 5;
- public Buzz(Command nextCommand, GameManager gameManager) {
- super(nextCommand, gameManager);
- }
- @Override
- public boolean doCommand(int currentNumber) {
- if(currentNumber % DIVISOR == 0) {
- System.out.println("BUZZ");
- return true;
- }
- return false;
- }
- }
- ---
- public class BasicNumber extends Command {
- public BasicNumber(Command nextCommand, GameManager gameManager) {
- super(nextCommand, gameManager);
- }
- @Override
- public boolean doCommand(int currentNumber) {
- System.out.println(currentNumber);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment