/**
* Solution is in java
* <p>
* "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe"
* Input : 987654
* NineHundredEightySevenThousandSixHundredFiftyFourDollars
* Input : 111111111
* OneHundredElevenMillionOneHundredElevenThousandOneHundredElevenDollars
* Input : 911
* NineHundredElevenDollars
* <p>
* Process finished with exit code 0
*/
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class DollarsToEnglishConverter {
private static final Map<Integer, String> DIGIT_TO_STRING = new HashMap<>();
static {
DIGIT_TO_STRING.put(1, "One");
DIGIT_TO_STRING.put(2, "Two");
DIGIT_TO_STRING.put(3, "Three");
DIGIT_TO_STRING.put(4, "Four");
DIGIT_TO_STRING.put(5, "Five");
DIGIT_TO_STRING.put(6, "Six");
DIGIT_TO_STRING.put(7, "Seven");
DIGIT_TO_STRING.put(8, "Eight");
DIGIT_TO_STRING.put(9, "Nine");
DIGIT_TO_STRING.put(10, "Ten");
DIGIT_TO_STRING.put(11, "Eleven");
DIGIT_TO_STRING.put(12, "Twelve");
DIGIT_TO_STRING.put(13, "Thirteen");
DIGIT_TO_STRING.put(14, "Fourteen");
DIGIT_TO_STRING.put(15, "Fifteen");
DIGIT_TO_STRING.put(16, "Sixteen");
DIGIT_TO_STRING.put(17, "Seventeen");
DIGIT_TO_STRING.put(18, "Eighteen");
DIGIT_TO_STRING.put(19, "Nineteen");
DIGIT_TO_STRING.put(20, "Twenty");
DIGIT_TO_STRING.put(30, "Thirty");
DIGIT_TO_STRING.put(40, "Forty");
DIGIT_TO_STRING.put(50, "Fifty");
DIGIT_TO_STRING.put(60, "Sixty");
DIGIT_TO_STRING.put(70, "Seventy");
DIGIT_TO_STRING.put(80, "Eighty");
DIGIT_TO_STRING.put(90, "Ninety");
DIGIT_TO_STRING.put(100, "Hundred");
DIGIT_TO_STRING.put(1000, "Thousand");
DIGIT_TO_STRING.put(10000, "TenThousand");
DIGIT_TO_STRING.put(100000, "HundredThousand");
DIGIT_TO_STRING.put(1000000, "Million");
DIGIT_TO_STRING.put(10000000, "TenMillion");
DIGIT_TO_STRING.put(100000000, "HundredMillion");
DIGIT_TO_STRING.put(1000000000, "Billion");
}
public static void main(String[] args) {
DollarsToEnglishConverter converter = new DollarsToEnglishConverter();
List<Integer> candidates = converter.validateInputAndGetValue();
candidates.forEach(candidate -> {
System.out.format("Input : %d%n", candidate);
List<Integer> digits = converter.convertNumberToComponentDigitsInReverse(candidate);
List<String> text = converter.convertToEnglish(digits);
text.forEach(System.out::print);
System.out.println("Dollars");
});
}
private List<String> convertToEnglish(List<Integer> digits) {
List<String> text = new ArrayList<>();
List<Integer> subList = new ArrayList<>();
for (int counter = 0; digits.size() > 0; counter++) {
do {
subList.add(digits.remove(0));
if (subList.size() == 3) break;
} while (!digits.isEmpty());
if (counter > 0) {
int power = (int) Math.pow(1000, counter);
text.add(0, DIGIT_TO_STRING.get(power));
}
text.addAll(0, computeInHundreds(subList));
subList.clear();
}
if (text.isEmpty()) {
text.add("Zero");
}
return text;
}
private List<String> computeInHundreds(List<Integer> digits) {
if (digits.size() > 3) {
throw new RuntimeException("digits should be sent only in hundreds!");
}
List<String> text = new ArrayList<>();
for (int i = 0; i < digits.size(); i++) {
int power = (int) Math.pow(10, i);
int digit = power * digits.get(i);
if (digit == 0) {
continue;
}
if (i == 1 && digits.get(i) == 1) {
if (digits.get(i - 1) != 0) {
text.remove(i - 1);
}
digit = 10 + digits.get(i - 1);
}
if (i == 2) {
text.add(0, DIGIT_TO_STRING.get(power));
text.add(0, DIGIT_TO_STRING.get(digits.get(i)));
} else {
text.add(0, DIGIT_TO_STRING.get(digit));
}
}
return text;
}
private List<Integer> convertNumberToComponentDigitsInReverse(int candidate) {
if (candidate < 0) {
throw new RuntimeException("Value cannot be Negative!");
}
List<Integer> digits = new LinkedList<>();
while (candidate > 0) {
digits.add(candidate % 10);
candidate = candidate / 10;
}
return digits;
}
private List<Integer> validateInputAndGetValue() {
List<Integer> data = new ArrayList<>();
try {
data.add(1);
data.add(717);
data.add(1111);
data.add(1000);
data.add(0);
for (int i = 0; i < 100; i++) {
data.add(ThreadLocalRandom.current().nextInt(1, 199999999));
}
} catch (final Exception e) {
throw new RuntimeException("Not a valid String ", e);
}
return data;
}
}
Comments
Post a Comment