Skip to content

Commit

Permalink
Added comments to code
Browse files Browse the repository at this point in the history
  • Loading branch information
dibek committed Oct 4, 2015
1 parent 0c16586 commit c93d204
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
96 changes: 49 additions & 47 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 43 additions & 18 deletions src/main/java/com/worldmightpay/wordsofnumbers/WordsOfNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/
public class WordsOfNumbers {


/**
* Enum to retrieve basic numbers
*/
public enum BaseNumbers {

ZERO(0),
Expand Down Expand Up @@ -54,6 +58,12 @@ public int getNum() {

}

/**
* Find the enum from the value
*
* @param num
* @return
*/
public static BaseNumbers findFromNum(int num) {
BaseNumbers tensNumbers = NOTFOUND;
for (BaseNumbers tensNumbersItem : BaseNumbers.values()) {
Expand All @@ -66,6 +76,12 @@ public static BaseNumbers findFromNum(int num) {
}
}

/**
* A method to transform a number to its string value
*
* @param number
* @return The string representation of the number
*/
public static String transform(Long number) {

String dlm = " and ";
Expand All @@ -74,7 +90,7 @@ public static String transform(Long number) {
}
Integer exp = getExpValue(number);

String wordNum = extractWordNum("",number, exp);
String wordNum = extractWordNum("", number, exp);

String plainNumber = String.valueOf(number);
int i = 1;
Expand All @@ -96,12 +112,9 @@ public static String transform(Long number) {
newNum = Long.valueOf(plainNumber);
newExpValue = getExpValue(newNum);
if (exp - newExpValue > 1) {
//wordNum = wordNum.replaceAll(dlm, " ");
wordNum = extractWordNum(wordNum,newNum, newExpValue);
} else
if (newExpValue > 0) {
//wordNum = wordNum.replaceAll(dlm, " ");
wordNum = extractWordNum(wordNum,newNum, newExpValue);
wordNum = extractWordNum(wordNum, newNum, newExpValue);
} else if (newExpValue > 0) {
wordNum = extractWordNum(wordNum, newNum, newExpValue);
}

exp = getExpValue(newNum);
Expand All @@ -119,7 +132,15 @@ public static String transform(Long number) {
return wordNum;
}

private static String extractWordNum(String wordNum,Long number, int expValue) {
/**
* Convert a number to its string representation adding eventually to an existing string
*
* @param wordNum The original string
* @param number The number to transform
* @param expValue The exp value of the number
* @return The new string representing the number
*/
private static String extractWordNum(String wordNum, Long number, int expValue) {


String dlm = " ";
Expand All @@ -136,7 +157,7 @@ private static String extractWordNum(String wordNum,Long number, int expValue) {
if (baseNumbers.equals(BaseNumbers.NOTFOUND)) {
baseNumbers = BaseNumbers.findFromNum(Integer.valueOf(firstDigit + "0"));
}
if (!wordNum.equals("")){
if (!wordNum.equals("")) {
wordNum += sep;
}
wordNum += baseNumbers.name().toLowerCase();
Expand All @@ -149,20 +170,20 @@ private static String extractWordNum(String wordNum,Long number, int expValue) {
wordNum += dlm + strFirstDigit + dlm + "hundred";
break;
case 3:
if (!wordNum.equals("")){
if (!wordNum.equals("")) {
wordNum += sep;
}
wordNum += strFirstDigit + dlm + "thousand";
break;
case 4:

if (!wordNum.equals("")){
if (!wordNum.equals("")) {
wordNum += sep;
}
calcModule = extractBaseExp(10, expValue - 2);
firstDigit = Long.valueOf(number / calcModule).intValue();
Long newFirstDigitsNum = Long.valueOf(firstDigit);
strFirstDigit = extractWordNum("",newFirstDigitsNum, getExpValue(newFirstDigitsNum));
strFirstDigit = extractWordNum("", newFirstDigitsNum, getExpValue(newFirstDigitsNum));
wordNum += strFirstDigit + dlm + "thousand";
break;
case 5:
Expand All @@ -174,13 +195,13 @@ private static String extractWordNum(String wordNum,Long number, int expValue) {
wordNum += dlm + strFirstDigit + dlm + "million";
break;
case 7:
if (!wordNum.equals("")){
if (!wordNum.equals("")) {
wordNum += sep;
}
calcModule = extractBaseExp(10, expValue - 2);
firstDigit = Long.valueOf(number / calcModule).intValue();
newFirstDigitsNum = Long.valueOf(firstDigit);
strFirstDigit = extractWordNum("",newFirstDigitsNum, getExpValue(newFirstDigitsNum));
strFirstDigit = extractWordNum("", newFirstDigitsNum, getExpValue(newFirstDigitsNum));
wordNum += strFirstDigit + dlm + "million";
break;
case 8:
Expand All @@ -190,7 +211,7 @@ private static String extractWordNum(String wordNum,Long number, int expValue) {
wordNum += strFirstDigit + dlm + "billion";
break;
default:
if (!wordNum.equals("")){
if (!wordNum.equals("")) {
wordNum += sep;
}
baseNumbers = BaseNumbers.findFromNum(number.intValue());
Expand All @@ -208,17 +229,21 @@ private static int extractBaseExp(int num, int expValue) {
return baseExp;
}


/**
* Extract the power in base 10 of the number
*
* @param number
* @return
*/
private static Integer getExpValue(Long number) {
NumberFormat formatter = new DecimalFormat("0E0");

String expNumber = formatter.format(number);
int indexE = expNumber.indexOf("E");

String expFound = expNumber.substring(indexE + 1);
return Integer.valueOf(expFound);
}


public static void main(String[] args) {
System.out.println("Hello words of numbers!");
WordsOfNumbers wordsOfNumbers = new WordsOfNumbers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.Assert.assertTrue;

/**
* Step definition class for wordsofnumbers feature
* Created by dibek on 03/10/15.
*/

Expand Down
Loading

0 comments on commit c93d204

Please sign in to comment.