Guest User

Untitled

a guest
Apr 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6.  
  7. class FizzBuzz {
  8. public enum Divisible {
  9. CanDiv3,
  10. CanDiv5,
  11. CanDiv3And5,
  12. CantDiv,
  13. }
  14. private List<Integer> values = new ArrayList<Integer>();
  15. private Map<Divisible, String> messages = new HashMap<Divisible, String>();
  16. public FizzBuzz(Integer start, Integer end) {
  17. for (Integer i = start; i <= end; i += 1) {
  18. this.values.add(i);
  19. }
  20. this.messages.put(Divisible.CanDiv3, "Fizz");
  21. this.messages.put(Divisible.CanDiv5, "Buzz");
  22. this.messages.put(Divisible.CanDiv3And5, "FizzBuzz");
  23. }
  24. public void exec() {
  25. for (Integer value : this.values) {
  26. System.out.printf("%s ", getFizzBuzzMessage(value));
  27. }
  28. System.out.println();
  29. }
  30. public Divisible detectDivisible(Integer value) {
  31. if (value % 15 == 0) {
  32. return Divisible.CanDiv3And5;
  33. }
  34. if (value % 3 == 0) {
  35. return Divisible.CanDiv3;
  36. }
  37. if (value % 5 == 0) {
  38. return Divisible.CanDiv5;
  39. }
  40. return Divisible.CantDiv;
  41. }
  42. private String getFizzBuzzMessage(Integer value) {
  43. Divisible divisible = this.detectDivisible(value);
  44. return divisible == Divisible.CantDiv ? value.toString() : this.messages.get(divisible);
  45. }
  46. public static void main(String[] args) {
  47. FizzBuzz fb = new FizzBuzz(1, 30);
  48. fb.exec();
  49. }
  50. }
Add Comment
Please, Sign In to add comment