Commit f00b1a23 authored by Milica Zivkov's avatar Milica Zivkov

number in words kata after session 1

parents
# See http://help.github.com/ignore-files/ for more about ignoring files.
# compiled output
/dist
/dist-server
/tmp
/out-tsc
/target
# dependencies
/node_modules
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
*.iws
*.iml
*.ipr
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings
# e2e
/e2e/*.js
/e2e/*.map
# System Files
.DS_Store
Thumbs.db
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric.kata</groupId>
<artifactId>tdd-kata-numbers-words</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package de.codecentric.kata;
import java.util.HashMap;
import java.util.Map;
public class NumberToWords {
public static final String DELIMITER = " ";
private static Map<Integer, String> units = new HashMap<Integer, String>() {
{
put(1, "one");
put(2, "two");
put(3, "three");
put(4, "four");
put(5, "five");
put(6, "six");
put(7, "seven");
put(8, "eight");
put(9, "nine");
put(10, "ten");
put(11, "eleven");
put(12, "twelve");
put(13, "thirteen");
put(14, "fourteen");
put(15, "fifteen");
put(16, "sixteen");
put(17, "seventeen");
put(18, "eighteen");
put(19, "nineteen");
}
};
private static Map<Integer, String> decades = new HashMap<Integer, String>() {
{
put(2, "twenty");
put(3, "thirty");
put(4, "forty");
put(5, "fifty");
put(6, "sixty");
put(7, "seventy");
put(8, "eighty");
put(9, "ninety");
}
};
public static String convert(int number) {
if (number >= 1000) {
String description = units.get(number / 1000) + DELIMITER + "thousand";
if (number % 1000 == 0) {
return description;
} else if (number % 1000 < 100) {
return description + " and " + convert(number % 1000);
}
return description + DELIMITER + convert(number % 1000);
}
if (number >= 100) {
String description = units.get(number / 100) + DELIMITER + "hundred";
if (number % 100 == 0) {
return description;
}
return description + " and " + convert(number % 100);
}
if (number >= 20) {
String description = decades.get(number / 10);
if (number % 10 == 0) {
return description;
}
return description + DELIMITER + convert(number % 10);
}
return units.get(number);
}
}
package de.codecentric.kata;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class NumberToWordsShould {
@Test
public void convertNumber() {
assertThat(NumberToWords.convert(1), is("one"));
assertThat(NumberToWords.convert(2), is("two"));
assertThat(NumberToWords.convert(8), is("eight"));
assertThat(NumberToWords.convert(10), is("ten"));
assertThat(NumberToWords.convert(20), is("twenty"));
assertThat(NumberToWords.convert(31), is("thirty one"));
assertThat(NumberToWords.convert(48), is("forty eight"));
assertThat(NumberToWords.convert(55), is("fifty five"));
assertThat(NumberToWords.convert(64), is("sixty four"));
assertThat(NumberToWords.convert(73), is("seventy three"));
assertThat(NumberToWords.convert(89), is("eighty nine"));
assertThat(NumberToWords.convert(99), is("ninety nine"));
assertThat(NumberToWords.convert(40), is("forty"));
assertThat(NumberToWords.convert(100), is("one hundred"));
assertThat(NumberToWords.convert(101), is("one hundred and one"));
assertThat(NumberToWords.convert(121), is("one hundred and twenty one"));
assertThat(NumberToWords.convert(999), is("nine hundred and ninety nine"));
assertThat(NumberToWords.convert(1000), is("one thousand"));
assertThat(NumberToWords.convert(1001), is("one thousand and one"));
assertThat(NumberToWords.convert(1101), is("one thousand one hundred and one"));
assertThat(NumberToWords.convert(9999), is("nine thousand nine hundred and ninety nine"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment