Guest User

Untitled

a guest
Dec 5th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.  
  4.         int startNum = 1;
  5.         int endNum = 100;
  6.  
  7.          new GameManager(startNum, endNum).handleValue();
  8.     }
  9. }
  10.  
  11. ---
  12.  
  13. public class GameManager {
  14.  
  15.     private int currentNumber;
  16.     private int maxNumber;
  17.     private Command commandChain;
  18.  
  19.     public GameManager(int initialNumber, int maxNumber) {
  20.         this.currentNumber = initialNumber;
  21.         this.maxNumber = maxNumber;
  22.  
  23.         //Create the command chain - FizzBuzz has to the be first
  24.         this.commandChain = new FizzBuzz(new Fizz(new Buzz(
  25.                 new BasicNumber(null, this), this), this), this);
  26.     }
  27.  
  28.  
  29.     public void handleValue() {
  30.         commandChain.startCommandChain(this.currentNumber);
  31.     }
  32.  
  33.     public void valueHandled() {
  34.         //increment and start handling of the new value if max value not yet reached
  35.         this.currentNumber++;
  36.         if(this.currentNumber <= maxNumber) {
  37.             handleValue();
  38.         }
  39.     }
  40. }
  41.  
  42. ---
  43.  
  44. public abstract class Command {
  45.  
  46.     private Command nextCommand;
  47.     private GameManager gameManager;
  48.  
  49.     public Command(Command nextCommand, GameManager gameManager) {
  50.         this.nextCommand = nextCommand;
  51.         this.gameManager = gameManager;
  52.     }
  53.  
  54.     public void startCommandChain(int currentNumber) {
  55.         //if the command is able to handle the currentNumber -> notify the GameManager
  56.         if (this.doCommand(currentNumber)) {
  57.             this.gameManager.valueHandled();
  58.         }
  59.         //Go on with the chain if the currentNumber is not yet handled
  60.         else {
  61.             if (this.nextCommand != null) {
  62.                 this.nextCommand.startCommandChain(currentNumber);
  63.             }
  64.         }
  65.        
  66.     }
  67.  
  68.     abstract boolean doCommand(int currentNumber);
  69.  
  70. }
  71.  
  72. ---
  73.  
  74. public class FizzBuzz extends Command {
  75.  
  76.     private static final int DIVISOR = 15;
  77.  
  78.     public FizzBuzz(Command nextCommand, GameManager gameManager) {
  79.         super(nextCommand, gameManager);
  80.     }
  81.  
  82.     @Override
  83.     public boolean doCommand(int currentNumber) {
  84.         if(currentNumber % DIVISOR == 0) {
  85.             System.out.println("FIZZBUZZ");
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90.  
  91. }
  92.  
  93. ---
  94.  
  95. public class Fizz extends Command {
  96.  
  97.     private static final int DIVISOR = 3;
  98.  
  99.     public Fizz(Command nextCommand, GameManager gameManager) {
  100.         super(nextCommand, gameManager);
  101.     }
  102.  
  103.     @Override
  104.     public boolean doCommand(int currentNumber) {
  105.         if(currentNumber % DIVISOR == 0) {
  106.             System.out.println("FIZZ");
  107.             return true;
  108.         }
  109.         return false;
  110.     }
  111.  
  112. }
  113.  
  114. ---
  115.  
  116. public class Buzz extends Command {
  117.  
  118.     private static final int DIVISOR = 5;
  119.  
  120.     public Buzz(Command nextCommand, GameManager gameManager) {
  121.         super(nextCommand, gameManager);
  122.     }
  123.  
  124.     @Override
  125.     public boolean doCommand(int currentNumber) {
  126.         if(currentNumber % DIVISOR == 0) {
  127.             System.out.println("BUZZ");
  128.             return true;
  129.         }
  130.         return false;
  131.     }
  132.  
  133. }
  134.  
  135. ---
  136.  
  137. public class BasicNumber extends Command {
  138.  
  139.     public BasicNumber(Command nextCommand, GameManager gameManager) {
  140.         super(nextCommand, gameManager);
  141.     }
  142.  
  143.     @Override
  144.     public boolean doCommand(int currentNumber) {
  145.         System.out.println(currentNumber);
  146.         return true;
  147.     }
  148.  
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment