-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.java
38 lines (29 loc) · 1.3 KB
/
Application.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
package readability;
import java.util.Scanner;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
public class Application {
private final TextStatistics textStatistics;
Application(TextStatistics textStatistics) {
this.textStatistics = textStatistics;
}
void run() {
System.out.println(textStatistics);
System.out.printf("Enter the score you want to calculate (%s, all):%n",
Stream.of(ReadabilityScores.values()).map(Enum::toString).collect(joining(", ")));
final var rsName = new Scanner(System.in).next().toUpperCase();
final var isAll = rsName.equals(ReadabilityScores.ALL);
Stream.of(ReadabilityScores.values())
.filter(rs -> isAll || rs.name().equals(rsName))
.peek(rs -> System.out.println(rs.getScoreAndAge(textStatistics)))
.mapToInt(rs -> rs.getAge(textStatistics))
.average()
.ifPresentOrElse(this::printAverage, this::printErrorMessage);
}
private void printErrorMessage() {
System.out.println("Wrong name of Readability Score!");
}
private void printAverage(double averageAge) {
System.out.printf("This text should be understood in average by %.2f year olds.", averageAge);
}
}