Last active
January 19, 2019 15:06
-
-
Save fal-works/33e9187eabec1901d3f86e493af5f942 to your computer and use it in GitHub Desktop.
FizzBuzz with Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map.Entry; | |
class WordList { | |
private final StringList words = new StringList(); | |
private final String separator; | |
WordList(String separator) { | |
this.separator = separator; | |
} | |
void add(String word) { | |
words.append(word); | |
} | |
void addIfEmpty(String word) { | |
if (words.size() == 0) add(word); | |
} | |
String toString() { | |
return join(words.array(), separator); | |
} | |
} | |
interface CounterCallback { | |
void run(int num); | |
} | |
class Counter { | |
private int value; | |
private final int initialValue; | |
private final int maxValue; | |
Counter(int initialValue, int maxValue) { | |
this.initialValue = initialValue; | |
this.maxValue = maxValue; | |
reset(); | |
} | |
void iterate(CounterCallback callback) { | |
while(this.hasNext()) { | |
callback.run(this.next()); | |
} | |
reset(); | |
} | |
private int next() { | |
return value++; | |
} | |
private boolean hasNext() { | |
return value <= maxValue; | |
} | |
private void reset() { | |
value = initialValue; | |
} | |
} | |
interface Answerer { | |
/** | |
* Executes answering and returns the next answerer. | |
*/ | |
Answerer run(int num); | |
} | |
interface AnswerAction { | |
void run(int num); | |
} | |
class FizzBuzzRule { | |
private final HashMap<Integer, String> divisorMap = new HashMap<Integer, String>(); | |
private final String wordSeparator; | |
FizzBuzzRule(String wordSeparator) { | |
this.wordSeparator = wordSeparator; | |
} | |
FizzBuzzRule addDivisor(int divisor, String word) { | |
divisorMap.put(divisor, word); | |
return this; | |
} | |
WordList getAnswerWords(int num) { | |
final WordList words = new WordList(wordSeparator); | |
for (Entry<Integer, String> entry : divisorMap.entrySet()) { | |
int divisor = entry.getKey(); | |
String word = entry.getValue(); | |
if (num % divisor == 0) words.add(word); | |
} | |
words.addIfEmpty(String.valueOf(num)); | |
return words; | |
} | |
} | |
class FizzBuzzSystem { | |
private final FizzBuzzRule rule; | |
private final int initialCount; | |
private final int maxCount; | |
private final String answerSeparator; | |
FizzBuzzSystem(FizzBuzzRule rule, int initialCount, int maxCount, String answerSeparator) { | |
this.rule = rule; | |
this.initialCount = initialCount; | |
this.maxCount = maxCount; | |
this.answerSeparator = answerSeparator; | |
} | |
void run() { | |
final AnswerAction commonAnswerAction = new AnswerAction() { | |
public void run(int num) { | |
print(rule.getAnswerWords(num)); | |
} | |
}; | |
final Answerer subsequentAnswerer = new Answerer() { | |
public Answerer run(int num) { | |
print(answerSeparator); | |
commonAnswerAction.run(num); | |
return this; | |
} | |
}; | |
final Answerer firstAnswerer = new Answerer() { | |
public Answerer run(int num) { | |
commonAnswerAction.run(num); | |
return subsequentAnswerer; | |
} | |
}; | |
final CounterCallback callback = new CounterCallback() { | |
Answerer currentAnswerer = firstAnswerer; | |
public void run(int num) { | |
currentAnswerer = currentAnswerer.run(num); | |
} | |
}; | |
new Counter(initialCount, maxCount).iterate(callback); | |
} | |
} | |
void setup() { | |
final FizzBuzzRule rule = new FizzBuzzRule(" ") | |
.addDivisor(3, "Fizz") | |
.addDivisor(5, "Buzz"); | |
new FizzBuzzSystem(rule, 1, 100, ", ").run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment