Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.HashMap;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Iterator;
- class FizzBuzz {
- public enum Divisible {
- CanDiv3,
- CanDiv5,
- CanDiv3And5,
- CantDiv,
- }
- private List<Integer> values = new ArrayList<Integer>();
- private Map<Divisible, String> messages = new HashMap<Divisible, String>();
- public FizzBuzz(Integer start, Integer end) {
- for (Integer i = start; i <= end; i += 1) {
- this.values.add(i);
- }
- this.messages.put(Divisible.CanDiv3, "Fizz");
- this.messages.put(Divisible.CanDiv5, "Buzz");
- this.messages.put(Divisible.CanDiv3And5, "FizzBuzz");
- }
- public void exec() {
- for (Integer value : this.values) {
- System.out.printf("%s ", getFizzBuzzMessage(value));
- }
- System.out.println();
- }
- public Divisible detectDivisible(Integer value) {
- if (value % 15 == 0) {
- return Divisible.CanDiv3And5;
- }
- if (value % 3 == 0) {
- return Divisible.CanDiv3;
- }
- if (value % 5 == 0) {
- return Divisible.CanDiv5;
- }
- return Divisible.CantDiv;
- }
- private String getFizzBuzzMessage(Integer value) {
- Divisible divisible = this.detectDivisible(value);
- return divisible == Divisible.CantDiv ? value.toString() : this.messages.get(divisible);
- }
- public static void main(String[] args) {
- FizzBuzz fb = new FizzBuzz(1, 30);
- fb.exec();
- }
- }
Add Comment
Please, Sign In to add comment