Last active
February 19, 2021 15:44
-
-
Save press0/4ac61a8afb90f388674c5086cf264e65 to your computer and use it in GitHub Desktop.
ConvertMoneyToEnglish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.junit.jupiter.api.Test; | |
public class ConvertMoneyToEnglish { | |
public static String convertMoneyToEnglish(int i) { | |
Map<Integer, String> firstDigit = new HashMap<>(); | |
firstDigit.put(0, "zero "); | |
firstDigit.put(1, "one "); | |
firstDigit.put(2, "two "); | |
firstDigit.put(3, "three "); | |
firstDigit.put(4, "four "); | |
firstDigit.put(5, "five "); | |
firstDigit.put(6, "six "); | |
firstDigit.put(7, "seven "); | |
firstDigit.put(8, "eight "); | |
firstDigit.put(9, "nine "); | |
// | |
Map<Integer, String> tensDigit = new HashMap<>(); | |
tensDigit.put(0, ""); | |
tensDigit.put(1, "ten "); | |
tensDigit.put(2, "twenty "); | |
tensDigit.put(3, "thirty "); | |
tensDigit.put(4, "forty "); | |
tensDigit.put(5, "fifty "); | |
tensDigit.put(6, "sixty "); | |
tensDigit.put(7, "seventy "); | |
tensDigit.put(8, "eighty "); | |
tensDigit.put(9, "ninety "); | |
// | |
Map<Integer, String> teens = new HashMap<>(); | |
teens.put(10, "ten "); | |
teens.put(11, "eleven "); | |
teens.put(12, "twelve "); | |
teens.put(13, "thirteen "); | |
teens.put(14, "fourteen"); | |
teens.put(15, "fifteen "); | |
teens.put(16, "sixteen "); | |
teens.put(17, "seventeen "); | |
teens.put(18, "eighteen "); | |
teens.put(19, "nineteen "); | |
// | |
int ones; | |
int tens; | |
int hundreds; | |
if (i < 10) { | |
return firstDigit.get(i) + (i == 1 ? "dollar" : "dollars"); | |
} else if (i < 19) { | |
return teens.get(i) + "dollars"; | |
} else if (i < 99) { | |
ones = i % 10; | |
tens = i / 10; | |
return tensDigit.get(tens) + firstDigit.get(ones) + "dollars"; | |
} else if (i > 100) { | |
ones = i % 10; | |
tens = (i / 10) % 10; | |
hundreds = i / 100; | |
return firstDigit.get(hundreds) + "hundred and " + tensDigit.get(tens) + firstDigit.get(ones) + "dollars"; | |
} | |
return null; | |
} | |
@Test | |
public void test789() { | |
String string = convertMoneyToEnglish(789); | |
assertEquals(string, "seven hundred and eighty nine dollars"); | |
} | |
@Test | |
public void test101() { | |
String string = convertMoneyToEnglish(101); | |
assertEquals(string, "one hundred and one dollars"); | |
} | |
@Test | |
public void test21() { | |
String string = convertMoneyToEnglish(21); | |
assertEquals(string, "twenty one dollars"); | |
} | |
@Test | |
public void test16() { | |
String string = convertMoneyToEnglish(16); | |
assertEquals(string, "sixteen dollars"); | |
} | |
@Test | |
public void test9() { | |
String string = convertMoneyToEnglish(9); | |
assertEquals(string, "nine dollars"); | |
} | |
@Test | |
public void test1() { | |
String string = convertMoneyToEnglish(1); | |
assertEquals(string, "one dollar"); | |
} | |
@Test | |
public void test0() { | |
String string = convertMoneyToEnglish(0); | |
assertEquals(string, "zero dollars"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment