-
Notifications
You must be signed in to change notification settings - Fork 165
Fixedexercise3.1 #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gimenezr
wants to merge
3
commits into
justomiguel:bootcamp_roberta
Choose a base branch
from
gimenezr:fixedexercise3.1
base: bootcamp_roberta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fixedexercise3.1 #46
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * | ||
| * @author roberta | ||
| */ | ||
| public abstract class NumberToLetterConverter { | ||
|
|
||
| private static final String[] UNITS = {"", "One ", "Two ", "Three ", | ||
| "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", | ||
| "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen", | ||
| "Seventeen", "Eighteen", "Nineteen", "Twenty"}; | ||
| private static final String[] TENS = {"Twenty", "Thirty ", "Forty ", | ||
| "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ", | ||
| "One Hundred "}; | ||
| private static final String[] HUNDREDS = {"One Hundred ", "Two Hundred ", | ||
| "Three Hundred ", "Four Hundred ", "Five Hundred ", "Six Hundred ", | ||
| "Seven Hundred ", "Eight Hundred ", "Nine Hundred "}; | ||
|
|
||
| /** | ||
| * El número es válido si está entre 0 y 999.999.999 | ||
| */ | ||
| public static String convertNumberToLetter(double number) | ||
| throws NumberFormatException { | ||
| String converted = new String(); | ||
|
|
||
| // validate if a legal number | ||
| if (number < 0) { | ||
| throw new IllegalArgumentException("Can't convert negative numbers"); | ||
| } | ||
| double doubleNumber = Math.rint(number * 100) / 100; | ||
| if (doubleNumber > 999999999) { | ||
| throw new NumberFormatException( | ||
| "The number is greater than 999.999.999, " | ||
| + "isn't possible to convert"); | ||
| } | ||
|
|
||
| String splitNumber[] = String.valueOf(doubleNumber).replace('.', '#').split("#"); | ||
|
|
||
| // Descompone el trio de millones | ||
| int millon = Integer.parseInt(String.valueOf(getDigitAt(splitNumber[0], | ||
| 8)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 7)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 6))); | ||
| if (millon == 1) { | ||
| converted = "A million "; | ||
| } | ||
| if (millon > 1) { | ||
| converted = convertNumber(String.valueOf(millon)) + "Millions "; | ||
| } | ||
|
|
||
| // Descompone el trio de miles - | ||
| int miles = Integer.parseInt(String.valueOf(getDigitAt(splitNumber[0], | ||
| 5)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 4)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 3))); | ||
| if (miles == 1) { | ||
| converted += "One Thousand "; | ||
| } | ||
| if (miles > 1) { | ||
| converted += convertNumber(String.valueOf(miles)) + "Thousand "; | ||
| } | ||
|
|
||
| // Descompone el ultimo trio de unidades | ||
| int cientos = Integer.parseInt(String.valueOf(getDigitAt( | ||
| splitNumber[0], 2)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 1)) | ||
| + String.valueOf(getDigitAt(splitNumber[0], 0))); | ||
| if (cientos == 1) { | ||
| converted += "One"; | ||
| } | ||
|
|
||
| if (millon + miles + cientos == 0) { | ||
| converted += "Zero dollars"; | ||
| } | ||
| if (cientos > 1) { | ||
| converted += convertNumber(String.valueOf(cientos)); | ||
| } | ||
|
|
||
| // Descompone los centavos | ||
| int centavos = Integer.parseInt(String.valueOf(getDigitAt( | ||
| splitNumber[1], 2)) | ||
| + String.valueOf(getDigitAt(splitNumber[1], 1)) | ||
| + String.valueOf(getDigitAt(splitNumber[1], 0))); | ||
| if (centavos == 1) { | ||
| converted += " With a cent"; | ||
| } | ||
| if (centavos > 1) { | ||
| converted += "and " + centavos | ||
| + "/100 Dollars"; | ||
| } | ||
|
|
||
| return converted; | ||
| } | ||
|
|
||
| /** | ||
| * Convierte los trios de números que componen las unidades, las decenas y | ||
| * las centenas del número. | ||
| */ | ||
| private static String convertNumber(String number) { | ||
| if (number.length() > 3) { | ||
| throw new NumberFormatException( | ||
| "The maximum length must be 3 digits"); | ||
| } | ||
|
|
||
| String output = new String(); | ||
| if (getDigitAt(number, 2) != 0) { | ||
| output = HUNDREDS[getDigitAt(number, 2) - 1]; | ||
| } | ||
|
|
||
| int k = Integer.parseInt(String.valueOf(getDigitAt(number, 1)) | ||
| + String.valueOf(getDigitAt(number, 0))); | ||
|
|
||
| if (k <= 20) { | ||
| output += UNITS[k]; | ||
| } else { | ||
| if (k > 30 && getDigitAt(number, 0) != 0) { | ||
| output += TENS[getDigitAt(number, 1) - 2] + "And " | ||
| + UNITS[getDigitAt(number, 0)]; | ||
| } else { | ||
| output += TENS[getDigitAt(number, 1) - 2] | ||
| + UNITS[getDigitAt(number, 0)]; | ||
| } | ||
| } | ||
|
|
||
| // Caso especial con 100 | ||
| if (getDigitAt(number, 2) == 1 && k == 0) { | ||
| output = "One Hundred"; | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| /** | ||
| * Retorna el dígito numerico en la posición indicada de derecha a izquierda | ||
| */ | ||
| private static int getDigitAt(String origin, int position) { | ||
| if (origin.length() > position && position >= 0) { | ||
| return origin.charAt(origin.length() - position - 1) - 48; | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * | ||
| * @author roberta | ||
| */ | ||
| public class NumberToLetterConverterTest { | ||
|
|
||
| public NumberToLetterConverterTest() { | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setUpClass() { | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDownClass() { | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| } | ||
|
|
||
| /** | ||
| * Test of convertNumberToLetter method, of class NumberToLetterConverter. | ||
| */ | ||
| @Test | ||
| public void testConvertNumberToLetter() { | ||
| double number = 0.0; | ||
| String expResult = "Zero dollars"; | ||
| String result = NumberToLetterConverter.convertNumberToLetter(number); | ||
| assertEquals(expResult, result); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Test of convertNumberToLetter method, of class NumberToLetterConverter. | ||
| */ | ||
| @Test | ||
| public void test1000ConvertNumberToLetter() { | ||
| double number = 1000.15; | ||
| String expResult = "One Thousand and 15/100 Dollars"; | ||
| String result = NumberToLetterConverter.convertNumberToLetter(number); | ||
| assertEquals(expResult, result); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Test of convertNumberToLetter method, of class NumberToLetterConverter. | ||
| */ | ||
| @Test | ||
| public void testNegative() { | ||
| double number = -1; | ||
| //String expResult = "Negative "; | ||
| try { | ||
| String result = NumberToLetterConverter.convertNumberToLetter(number); | ||
| fail("Shouln't convert negative numbers"); | ||
| } catch (IllegalArgumentException e) { | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Para concatenar Strings siempre usemos Stringbuilders
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable - it cannot change its internal state.
On the other hand StringBuilder is mutable. When you call append(..) it alters the internal char array, rather than creating a new string object.
Thus it is more efficient to have:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500; i ++) {
sb.append(i);
}
rather than str += i, which would create 500 new string objects.
Note that in the example I use a loop. As helios notes in the comments, the compiler automatically translates expressions like String d = a + b + c to something like
String d = new StringBuilder(a).append(b).append(c).toString();
Note also that there is StringBuffer in addition to StringBuilder. The difference is that the former has synchronized methods. If you use it as a local variable, use StringBuilder. If it happens that it's possible for it to be accessed by multiple threads, use StringBuffer (that's rarer)