-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
96 lines (74 loc) · 2.7 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package io.github.jkomoroski.declarativejupiter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Main {
public static void main(String... args) {
final DummyResource1 dummyResource1 = new DummyResource1();
final DummyResource2 dummyResource2 = new DummyResource2();
final int exitCode = new Main(dummyResource1, dummyResource2, args).run();
System.exit(exitCode);
}
private final String[] args;
private final DummyResource1 resource1;
private final DummyResource2 resource2;
private boolean isRunning = true;
public Main(DummyResource1 resource1, DummyResource2 resource2, String... args) {
this.resource1 = resource1;
this.resource2 = resource2;
this.args = args;
final String requiredProperty = System.getProperty("key");
if (requiredProperty == null || requiredProperty.isEmpty() || requiredProperty.isBlank()) {
throw new IllegalStateException("Required property 'key' not set!");
}
}
public int run() {
try {
var integers = args.length == 0
? defaultIntegerList()
: convertArgumentsToIntegerList(args);
integers.stream()
.map(Main::toFizzBuzz)
.forEach(System.out::println);
return 0;
} catch (NumberFormatException e) {
log.trace("Error: {}. Cannot parse number formatting for this argument.", e.getMessage());
return 1;
}
}
public void exit() {
isRunning = false;
}
public boolean isRunning() {
return isRunning;
}
public static List<Integer> convertArgumentsToIntegerList(String... args) throws NumberFormatException {
return Arrays.stream(args)
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
}
public static List<Integer> defaultIntegerList() {
return IntStream.range(1, 101)
.sequential()
.boxed()
.collect(Collectors.toList());
}
public static String toFizzBuzz(Integer integer) {
String fizz = isFizz(integer) ? "Fizz" : "";
String buzz = isBuzz(integer) ? "Buzz" : "";
String output = fizz + buzz;
return output.isEmpty()
? String.valueOf(integer.intValue())
: output;
}
public static boolean isFizz(Integer integer) {
return integer % 3 == 0;
}
public static boolean isBuzz(Integer integer) {
return integer % 5 == 0;
}
}