BAEL-1498 Money into words (#3797)
* Add tradukisto library * Implement MoneyIntoWords * Update the version of tradukisto library * Refactor MoneyIntoWords * Refactor * Refactor pom.xml * Refactor pom.xml * Refactor NumberWordConverter * BAEL-1498 Small refactoring * Test edge cases * BAEL-1498 Additional tests
This commit is contained in:
parent
c5fadb5191
commit
89f1c2721e
@ -9,6 +9,7 @@
|
|||||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
<lombok.version>1.16.12</lombok.version>
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
|
<tradukisto.version>1.0.1</tradukisto.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
@ -39,6 +40,11 @@
|
|||||||
<artifactId>jgrapht-core</artifactId>
|
<artifactId>jgrapht-core</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.allegro.finance</groupId>
|
||||||
|
<artifactId>tradukisto</artifactId>
|
||||||
|
<version>${tradukisto.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
@ -46,7 +52,6 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<pluginManagement>
|
<pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.baeldung.algorithms.numberwordconverter;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import pl.allegro.finance.tradukisto.MoneyConverters;
|
||||||
|
|
||||||
|
public class NumberWordConverter {
|
||||||
|
|
||||||
|
public static final String INVALID_INPUT_GIVEN = "Invalid input given";
|
||||||
|
|
||||||
|
public static final String[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
|
||||||
|
|
||||||
|
public static final String[] tens = {
|
||||||
|
"", // 0
|
||||||
|
"", // 1
|
||||||
|
"twenty", // 2
|
||||||
|
"thirty", // 3
|
||||||
|
"forty", // 4
|
||||||
|
"fifty", // 5
|
||||||
|
"sixty", // 6
|
||||||
|
"seventy", // 7
|
||||||
|
"eighty", // 8
|
||||||
|
"ninety" // 9
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String getMoneyIntoWords(String input) {
|
||||||
|
MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE;
|
||||||
|
return converter.asWords(new BigDecimal(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getMoneyIntoWords(final double money) {
|
||||||
|
long dollar = (long) money;
|
||||||
|
long cents = Math.round((money - dollar) * 100);
|
||||||
|
if (money == 0D) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (money < 0) {
|
||||||
|
return INVALID_INPUT_GIVEN;
|
||||||
|
}
|
||||||
|
String dollarPart = "";
|
||||||
|
if (dollar > 0) {
|
||||||
|
dollarPart = convert(dollar) + " dollar" + (dollar == 1 ? "" : "s");
|
||||||
|
}
|
||||||
|
String centsPart = "";
|
||||||
|
if (cents > 0) {
|
||||||
|
if (dollarPart.length() > 0) {
|
||||||
|
centsPart = " and ";
|
||||||
|
}
|
||||||
|
centsPart += convert(cents) + " cent" + (cents == 1 ? "" : "s");
|
||||||
|
}
|
||||||
|
return dollarPart + centsPart;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String convert(final long n) {
|
||||||
|
if (n < 0) {
|
||||||
|
return INVALID_INPUT_GIVEN;
|
||||||
|
}
|
||||||
|
if (n < 20) {
|
||||||
|
return ones[(int) n];
|
||||||
|
}
|
||||||
|
if (n < 100) {
|
||||||
|
return tens[(int) n / 10] + ((n % 10 != 0) ? " " : "") + ones[(int) n % 10];
|
||||||
|
}
|
||||||
|
if (n < 1000) {
|
||||||
|
return ones[(int) n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
|
||||||
|
}
|
||||||
|
if (n < 1_000_000) {
|
||||||
|
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
|
||||||
|
}
|
||||||
|
if (n < 1_000_000_000) {
|
||||||
|
return convert(n / 1_000_000) + " million" + ((n % 1_000_000 != 0) ? " " : "") + convert(n % 1_000_000);
|
||||||
|
}
|
||||||
|
return convert(n / 1_000_000_000) + " billion" + ((n % 1_000_000_000 != 0) ? " " : "") + convert(n % 1_000_000_000);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.baeldung.algorithms.moneywords;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.algorithms.numberwordconverter.NumberWordConverter;
|
||||||
|
|
||||||
|
public class NumberWordConverterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMoneyNegative_thenReturnInvalidInput() {
|
||||||
|
assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenZeroDollarsGiven_thenReturnEmptyString() {
|
||||||
|
assertEquals("", NumberWordConverter.getMoneyIntoWords(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOnlyDollarsGiven_thenReturnWords() {
|
||||||
|
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOnlyCentsGiven_thenReturnWords() {
|
||||||
|
assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAlmostAMillioDollarsGiven_thenReturnWords() {
|
||||||
|
String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars";
|
||||||
|
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenThirtyMillionDollarsGiven_thenReturnWords() {
|
||||||
|
String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars";
|
||||||
|
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTwoBillionDollarsGiven_thenReturnWords() {
|
||||||
|
String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars";
|
||||||
|
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGivenDollarsAndCents_thenReturnWords() {
|
||||||
|
String expectedResult = "nine hundred twenty four dollars and sixty cents";
|
||||||
|
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOneDollarAndNoCents_thenReturnDollarSingular() {
|
||||||
|
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNoDollarsAndOneCent_thenReturnCentSingular() {
|
||||||
|
assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
|
||||||
|
assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
|
||||||
|
assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
|
||||||
|
assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
|
||||||
|
assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user