2
2
3
3
import java .util .HashMap ;
4
4
5
- public class IntegerToEnglish {
6
- private static final HashMap <Integer , String > baseNumbersMap = new HashMap <>() {
5
+ public final class IntegerToEnglish {
6
+ private static final HashMap <Integer , String > BASE_NUMBERS_MAP = new HashMap <>() {
7
7
{
8
8
put (0 , "" );
9
9
put (1 , "One" );
@@ -37,14 +37,17 @@ public class IntegerToEnglish {
37
37
}
38
38
};
39
39
40
- private static final HashMap <Integer , String > thousandPowerMap = new HashMap <>() {
40
+ private static final HashMap <Integer , String > THOUSAND_POWER_MAP = new HashMap <>() {
41
41
{
42
42
put (1 , "Thousand" );
43
43
put (2 , "Million" );
44
44
put (3 , "Billion" );
45
45
}
46
46
};
47
47
48
+ private IntegerToEnglish () {
49
+ }
50
+
48
51
/**
49
52
converts numbers < 1000 to english words
50
53
*/
@@ -54,20 +57,20 @@ private static String convertToWords(int number) {
54
57
String result ;
55
58
56
59
if (remainder <= 20 ) {
57
- result = baseNumbersMap .get (remainder );
58
- } else if (baseNumbersMap .containsKey (remainder )) {
59
- result = baseNumbersMap .get (remainder );
60
+ result = BASE_NUMBERS_MAP .get (remainder );
61
+ } else if (BASE_NUMBERS_MAP .containsKey (remainder )) {
62
+ result = BASE_NUMBERS_MAP .get (remainder );
60
63
} else {
61
64
int tensDigit = remainder / 10 ;
62
65
int onesDigit = remainder % 10 ;
63
66
64
- result = String .format ("%s %s" , baseNumbersMap .get (tensDigit * 10 ), baseNumbersMap .get (onesDigit ));
67
+ result = String .format ("%s %s" , BASE_NUMBERS_MAP .get (tensDigit * 10 ), BASE_NUMBERS_MAP .get (onesDigit ));
65
68
}
66
69
67
70
int hundredsDigit = number / 100 ;
68
71
69
72
if (hundredsDigit > 0 ) {
70
- result = String .format ("%s %s%s" , baseNumbersMap .get (hundredsDigit ), baseNumbersMap .get (100 ), (result .isEmpty () ? "" : " " + result ));
73
+ result = String .format ("%s %s%s" , BASE_NUMBERS_MAP .get (hundredsDigit ), BASE_NUMBERS_MAP .get (100 ), (result .isEmpty () ? "" : " " + result ));
71
74
}
72
75
73
76
return result ;
@@ -77,7 +80,9 @@ private static String convertToWords(int number) {
77
80
Only convert groups of three digit if they are non-zero
78
81
*/
79
82
public static String integerToEnglishWords (int number ) {
80
- if (number == 0 ) return "Zero" ;
83
+ if (number == 0 ) {
84
+ return "Zero" ;
85
+ }
81
86
82
87
StringBuilder result = new StringBuilder ();
83
88
@@ -92,10 +97,10 @@ public static String integerToEnglishWords(int number) {
92
97
93
98
if (!subResult .isEmpty ()) {
94
99
if (!result .isEmpty ()) {
95
- result .insert (0 , subResult + " " + thousandPowerMap .get (index ) + " " );
100
+ result .insert (0 , subResult + " " + THOUSAND_POWER_MAP .get (index ) + " " );
96
101
} else {
97
102
if (index > 0 ) {
98
- result = new StringBuilder (subResult + " " + thousandPowerMap .get (index ));
103
+ result = new StringBuilder (subResult + " " + THOUSAND_POWER_MAP .get (index ));
99
104
} else {
100
105
result = new StringBuilder (subResult );
101
106
}
0 commit comments