I tried to make the famous problem FizzBuzz that many programmers go through into an extremely flexible implementation in vain.
--Policy - Java11 --Active use of standard library --Finally it will be like a Builder Pattern
It will be shorter if you devise various things, but is it like this if the answer appears in a textbook?
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 15 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
}
output (combined in one line for easy viewing)
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz ...
--Save in Map format --Judge each number and combine character strings with Stream
Map<Integer, String> map = new HashMap<>();
map.put(3, "Fizz");
map.put(5, "Buzz");
for (int i = 1; i <= 100; i++) {
final int current = i; //Will disappear after this
String result = map.entrySet().stream()
.filter(v -> current % v.getKey() == 0)
.map(Entry::getValue)
.reduce("", (left, right) -> left + right);
System.out.println(result.isBlank() ? i : result);
}
--Separated into methods that return results as a list ――By the way, you can customize the start and end values.
public static List<String> fizzBuzz(int start, int end, Map<Integer, String> map) {
return IntStream.rangeClosed(start, end).mapToObj(i -> {
String result = map.entrySet().stream()
.filter(v -> i % v.getKey() == 0)
.map(Entry::getValue)
.reduce("", (left, right) -> left + right);
return result.isBlank() ? String.valueOf(i) : result;
}).collect(Collectors.toList());
}
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(3, "Fizz");
map.put(5, "Buzz");
fizzBuzz(1, 100, map).forEach(System.out::println);
}
--In general, the build method in the Builder pattern should return an independent class, but here it returns List \ <String >. ――If you do it properly, maybe you will divide it into two classes, "FizzBuzzBuilder" and "FizzBuzz". --javadoc is omitted
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
public class FizzBuzz {
private Map<Integer, String> map = new HashMap<>();
private int start = 1;
private int end = 100;
public List<String> build() {
return IntStream.rangeClosed(start, end).mapToObj(i -> {
String result = map.entrySet().stream()
.filter(v -> i % v.getKey() == 0)
.map(Entry::getValue)
.reduce("", (left, right) -> left + right);
return result.isBlank() ? String.valueOf(i) : result;
}).collect(Collectors.toList());
}
public FizzBuzz addPair(int value, String text) {
map.put(value, text);
return this;
}
public FizzBuzz start(int start) {
this.start = start;
return this;
}
public FizzBuzz end(int end) {
this.end = end;
return this;
}
public static void main(String[] args) {
new FizzBuzz()
.start(1)
.end(20)
.addPair(3, "Fizz")
.addPair(5, "Buzz")
.build()
.forEach(System.out::println);
}
}
――Because it's a big deal, we tested with disjoint 3, 5, 11, 13 --Since the least common multiple is 2145, output around 2130 to 2150.
new FizzBuzz()
.start(2130)
.end(2150)
.addPair(3, "Fizz")
.addPair(5, "Buzz")
.addPair(11, "Foo")
.addPair(13, "Bar")
.build()
.forEach(System.out::println);
FizzBuzz
2131
Bar
Fizz
Foo
Buzz
Fizz
2137
2138
Fizz
Buzz
2141
Fizz
2143
2144
FizzBuzzFooBar
2146
2147
Fizz
2149
Buzz
This is what happens when someone who is not familiar with the JavaScript paradigm creates it It seems to be different from what the main job makes, so it is just for reference
(() => {
class FizzBuzz {
constructor() {
this._from = 1;
this._to = 100;
this._pair = [];
}
build() {
const convert = i => this._pair
.filter(divisor => i % divisor.value == 0)
.reduce((left, right) => left + right.text, "") || i;
return [...Array(this._to - this._from + 1).keys()]
.map(i => i + this._from)
.map(convert);
}
addPair(value, text) {
this._pair.push({value: value, text: text});
return this;
}
from(from) {
this._from = from;
return this;
}
to(to) {
this._to = to;
return this;
}
};
new FizzBuzz()
.from(2130)
.to(2150)
.addPair(3, "Fizz")
.addPair(5, "Buzz")
.addPair(11, "Foo")
.addPair(13, "Bar")
.build()
.forEach(result => console.log(result));
})();
Recommended Posts