Repository navigation
Expand file tree
/
Copy pathSay.java
More file actions
127 lines (106 loc) · 3.94 KB
/
Copy pathSay.java
File metadata and controls
127 lines (106 loc) · 3.94 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Say {
private static List<Magnitude> supportedMagnitudes = Arrays.asList(
Magnitude.BILLIONS, Magnitude.MILLIONS,
Magnitude.THOUSANDS, Magnitude.ONES);
private enum Magnitude {
TRILLIONS(1_000_000_000_000L, "trillion"),
BILLIONS(1_000_000_000, "billion"),
MILLIONS(1_000_000, "million"),
THOUSANDS(1_000, "thousand"),
ONES(1, "one");
private long number;
private String inEnglish;
private Magnitude(long number, String inEnglish) {
this.number = number;
this.inEnglish = inEnglish;
}
public long getNumber() {
return number;
}
public String inEnglish() {
return inEnglish;
}
}
public String say(long number) {
if (number < 0 || number >= Magnitude.TRILLIONS.getNumber()) {
throw new IllegalArgumentException("input out of range");
}
List<Part> parts = new ArrayList<Say.Part>();
for (Magnitude magnitude: supportedMagnitudes) {
double quoziente = Math.floor(number / magnitude.getNumber());
Part part = new Part((int) quoziente, magnitude);
if (part.getDigits() > 0 || (part.getMagnitude().equals(Magnitude.ONES) && parts.size() == 0)) {
parts.add(part);
}
number = number % magnitude.getNumber();
}
return String.join(" ", parts.stream().map(part -> part.say()).collect(Collectors.toList()));
}
private static class Part {
private int digits;
private Magnitude magnitude;
public Part(int digits, Magnitude magnitude) {
this.digits = digits;
this.magnitude = magnitude;
}
private static String[] untilTwenty = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eigth",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "senventeen", "eithteen",
"nineteen" };
public int getDigits() {
return digits;
}
public Magnitude getMagnitude() {
return magnitude;
}
private String convertTens(long digit) {
switch ((int) digit) {
case 2:
return "twenty";
case 3:
return "thirty";
case 4:
return "forty";
case 5:
return "fifty";
case 6:
return "sixty";
case 7:
return "seventy";
case 8:
return "eighty";
case 9:
return "ninety";
default:
throw new IllegalArgumentException("not supported value");
}
}
private String say() {
StringBuffer stringBuffer = new StringBuffer();
int hundreds = (int) digits / 100;
int tens = (int) digits % 100;
if (hundreds > 0) {
stringBuffer.append(untilTwenty[hundreds]).append(" hundred");
}
if (stringBuffer.length() > 0) {
stringBuffer.append(" ");
}
if (tens >= 20) {
stringBuffer.append(convertTens(tens / 10));
if (tens % 10 > 0) {
stringBuffer.append("-").append(untilTwenty[tens % 10]);
}
} else {
stringBuffer.append(untilTwenty[tens]);
}
if (!magnitude.equals(Magnitude.ONES)) {
stringBuffer.append(" ").append(magnitude.inEnglish());
}
String message = stringBuffer.toString().replace(" zero", "");
return message;
}
}
}