Skip to content

Commit 0b47db6

Browse files
committed
solve build error, fix checkstyle issues
1 parent 75dfd9e commit 0b47db6

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

src/main/java/com/thealgorithms/conversions/WordsToNumber.java

+61-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.thealgorithms.conversions;
22

33
import java.math.BigDecimal;
4-
import java.util.*;
4+
import java.util.ArrayDeque;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
59

610
/**
711
A Java-based utility for converting English word representations of numbers
@@ -56,12 +60,16 @@ private WordsToNumber() {
5660
}
5761

5862
public static String convert(String numberInWords) {
59-
if (numberInWords == null) return "Null Input";
63+
if (numberInWords == null) {
64+
return "Null Input";
65+
}
6066

6167
String[] wordSplitArray = numberInWords.trim().split("[ ,-]");
6268
ArrayDeque<String> wordDeque = new ArrayDeque<>();
6369
for (String word : wordSplitArray) {
64-
if (word.isEmpty()) continue;
70+
if (word.isEmpty()) {
71+
continue;
72+
}
6573
wordDeque.add(word.toLowerCase());
6674
}
6775

@@ -77,12 +85,18 @@ public static String convert(String numberInWords) {
7785
boolean currentChunkIsZero = currentChunk.equals(BigDecimal.ZERO);
7886

7987
boolean isConjunction = word.equals("and");
80-
if (isConjunction && isValidConjunction(prevNumWasHundred, prevNumWasPowerOfTen, wordDeque)) continue;
88+
if (isConjunction && isValidConjunction(prevNumWasHundred, prevNumWasPowerOfTen, wordDeque)) {
89+
continue;
90+
}
8191

8292
boolean isHundred = word.equals("hundred");
8393
if (isHundred) {
84-
if (currentChunk.compareTo(BigDecimal.TEN) >= 0 || prevNumWasPowerOfTen) return "Invalid Input. Unexpected Word: " + word;
85-
if (currentChunkIsZero) currentChunk = currentChunk.add(BigDecimal.ONE);
94+
if (currentChunk.compareTo(BigDecimal.TEN) >= 0 || prevNumWasPowerOfTen) {
95+
return "Invalid Input. Unexpected Word: " + word;
96+
}
97+
if (currentChunkIsZero) {
98+
currentChunk = currentChunk.add(BigDecimal.ONE);
99+
}
86100
currentChunk = currentChunk.multiply(BigDecimal.valueOf(100));
87101
prevNumWasHundred = true;
88102
continue;
@@ -91,13 +105,17 @@ public static String convert(String numberInWords) {
91105

92106
BigDecimal powerOfTen = POWERS_OF_TEN.getOrDefault(word, null);
93107
if (powerOfTen != null) {
94-
if (currentChunkIsZero || prevNumWasPowerOfTen) return "Invalid Input. Unexpected Word: " + word;
108+
if (currentChunkIsZero || prevNumWasPowerOfTen) {
109+
return "Invalid Input. Unexpected Word: " + word;
110+
}
95111
BigDecimal nextChunk = currentChunk.multiply(powerOfTen);
96112

97-
if (chunks.isEmpty() || isAdditionSafe(chunks.getLast(), nextChunk))
113+
if (chunks.isEmpty() || isAdditionSafe(chunks.getLast(), nextChunk)) {
98114
chunks.add(nextChunk);
99-
else
115+
}
116+
else {
100117
return "Invalid Input. Unexpected Word: " + word;
118+
}
101119
currentChunk = BigDecimal.ZERO;
102120
prevNumWasPowerOfTen = true;
103121
continue;
@@ -106,45 +124,59 @@ public static String convert(String numberInWords) {
106124

107125
Integer number = NUMBER_MAP.getOrDefault(word, null);
108126
if (number != null) {
109-
if (number == 0 && !(currentChunkIsZero && chunks.isEmpty())) return "Invalid Input. Unexpected Word: " + word;
127+
if (number == 0 && !(currentChunkIsZero && chunks.isEmpty())) {
128+
return "Invalid Input. Unexpected Word: " + word;
129+
}
110130
BigDecimal bigDecimalNumber = BigDecimal.valueOf(number);
111131

112-
if (currentChunkIsZero || isAdditionSafe(currentChunk, bigDecimalNumber))
132+
if (currentChunkIsZero || isAdditionSafe(currentChunk, bigDecimalNumber)) {
113133
currentChunk = currentChunk.add(bigDecimalNumber);
114-
else
134+
}
135+
else {
115136
return "Invalid Input. Unexpected Word: " + word;
137+
}
116138
continue;
117139
}
118140

119141
if (word.equals("point")) {
120-
if (!currentChunkIsZero) chunks.add(currentChunk);
142+
if (!currentChunkIsZero) {
143+
chunks.add(currentChunk);
144+
}
121145
currentChunk = BigDecimal.ZERO;
122146

123147
String decimalPart = convertDecimalPart(wordDeque);
124-
if (!decimalPart.startsWith("I"))
148+
if (!decimalPart.startsWith("I")) {
125149
chunks.add(new BigDecimal(decimalPart));
126-
else
150+
}
151+
else {
127152
return decimalPart;
153+
}
128154
break;
129155
}
130156

131157
if (word.equals("negative")) {
132-
if (isNegative) return "Invalid Input. Multiple 'Negative's detected.";
158+
if (isNegative) {
159+
return "Invalid Input. Multiple 'Negative's detected.";
160+
}
133161
isNegative = chunks.isEmpty() && currentChunkIsZero;
134162
continue;
135163
}
136164

137165
return "Invalid Input. " + (isConjunction ? "Unexpected 'and' placement" : "Unknown Word: " + word);
138166
}
139167

140-
if (!currentChunk.equals(BigDecimal.ZERO)) chunks.add(currentChunk);
168+
if (!currentChunk.equals(BigDecimal.ZERO)) {
169+
chunks.add(currentChunk);
170+
}
141171
BigDecimal completeNumber = combineChunks(chunks);
142172

143173
return isNegative ? completeNumber.multiply(BigDecimal.valueOf(-1)).toString() : completeNumber.toString();
144174
}
145175

146176
private static boolean isValidConjunction(boolean prevNumWasHundred, boolean prevNumWasPowerOfTen, ArrayDeque<String> wordDeque) {
147-
if (wordDeque.isEmpty()) return false;
177+
if (wordDeque.isEmpty()) {
178+
return false;
179+
}
148180

149181
String nextWord = wordDeque.pollFirst();
150182
String afterNextWord = wordDeque.peekFirst();
@@ -165,18 +197,22 @@ private static boolean isAdditionSafe(BigDecimal currentChunk, BigDecimal number
165197
return chunkDigitCount > numberDigitCount;
166198
}
167199

168-
private static String convertDecimalPart(Queue<String> wordDeque) {
200+
private static String convertDecimalPart(ArrayDeque<String> wordDeque) {
169201
StringBuilder decimalPart = new StringBuilder(".");
170202
while (!wordDeque.isEmpty()) {
171203
String word = wordDeque.poll();
172204
Integer number = NUMBER_MAP.getOrDefault(word, null);
173-
if (number != null)
205+
if (number != null) {
174206
decimalPart.append(number);
175-
else
207+
}
208+
else {
176209
return "Invalid Input. Unexpected Word (after Point): " + word;
210+
}
177211
}
178212

179-
if (decimalPart.length() == 1) return "Invalid Input. Decimal Part is missing Numbers.";
213+
if (decimalPart.length() == 1) {
214+
return "Invalid Input. Decimal Part is missing Numbers.";
215+
}
180216
return decimalPart.toString();
181217
}
182218

@@ -188,7 +224,9 @@ private static BigDecimal combineChunks(List<BigDecimal> chunks) {
188224

189225
public static BigDecimal convertToBigDecimal(String numberInWords) {
190226
String conversionResult = convert(numberInWords);
191-
if (conversionResult.startsWith("I")) return null;
227+
if (conversionResult.startsWith("I")) {
228+
return null;
229+
}
192230
return new BigDecimal(conversionResult);
193231
}
194232
}

src/test/java/com/thealgorithms/conversions/WordsToNumberTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.conversions;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45

56
import java.math.BigDecimal;
67
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)