|
1 | | -package xdvrx1ProjectSwing; |
2 | | - |
3 | | -/** |
4 | | - * This class first filter the input |
5 | | - * of a user, then compute when the |
6 | | - * input is correct. It returns |
7 | | - * a string. This can be easily unit tested. |
8 | | - */ |
9 | | - |
10 | | -class ComputeValue { |
11 | | - |
12 | | - String compute(String cmFormatted) { |
13 | | - |
14 | | - //the input should not contain letters |
15 | | - if (cmFormatted.contains("d") | |
16 | | - cmFormatted.contains("e") | |
17 | | - cmFormatted.contains("f")) { |
18 | | - return "malformed"; |
19 | | - } else { |
20 | | - try { |
21 | | - double feet, inches, remainder; |
22 | | - |
23 | | - final double CM_PER_INCH = 2.54; |
24 | | - final double IN_PER_FOOT = 12.00; |
25 | | - //creating a variable that has the type double |
26 | | - //it will parse the string data |
27 | | - //from 'cm' textfield |
28 | | - //and this throws NumberFormatException and |
29 | | - //IndexOutOfBoundsException |
30 | | - //so its encapsulated in a try-catch block |
31 | | - double cmC = Double.parseDouble(cmFormatted); |
32 | | - |
33 | | - //I decided to limit the lowest |
34 | | - //possible number to 1, |
35 | | - //because those .001 and the likes |
36 | | - //they are returning very large numbers |
37 | | - if (cmC < 1) { |
38 | | - return "too small"; |
39 | | - } else { |
40 | | - |
41 | | - //we'll do some math here :) |
42 | | - inches = cmC / CM_PER_INCH; |
43 | | - feet = inches / IN_PER_FOOT; |
44 | | - remainder = inches % IN_PER_FOOT; |
45 | | - |
46 | | - //get the integer |
47 | | - String stringVal = String.valueOf(feet); |
48 | | - int indexOfDecimal = stringVal.indexOf("."); |
49 | | - String integerPart = stringVal.substring(0, indexOfDecimal); |
50 | | - //get the decimal part |
51 | | - String remainderFormatted = String.format("%.5g", remainder); |
52 | | - |
53 | | - //`convertedResult` will be the computed value |
54 | | - String convertedResult = |
55 | | - cmFormatted + " cm" + " = " + integerPart + |
56 | | - "\'" + " " + remainderFormatted + "\""; |
57 | | - |
58 | | - return convertedResult; |
59 | | - } |
60 | | - } catch (NumberFormatException nf2) { |
61 | | - return "malformed"; |
62 | | - } catch (IndexOutOfBoundsException outB) { |
63 | | - return "outB"; |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - } |
68 | | -} |
| 1 | +package xdvrx1ProjectSwing; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class first filter the input |
| 5 | + * of a user, then compute when the |
| 6 | + * input is correct. It returns |
| 7 | + * a string. This can be easily unit tested. |
| 8 | + */ |
| 9 | + |
| 10 | +class ComputeValue { |
| 11 | + |
| 12 | + String compute(String cmFormatted) { |
| 13 | + |
| 14 | + //the input should not contain letters |
| 15 | + if (cmFormatted.contains("d") | |
| 16 | + cmFormatted.contains("e") | |
| 17 | + cmFormatted.contains("f")) { |
| 18 | + return "malformed"; |
| 19 | + } else { |
| 20 | + try { |
| 21 | + double feet, inches, remainder; |
| 22 | + |
| 23 | + final double CM_PER_INCH = 2.54; |
| 24 | + final double IN_PER_FOOT = 12.00; |
| 25 | + //creating a variable that has the type double |
| 26 | + //it will parse the string data |
| 27 | + //from 'cm' textfield |
| 28 | + //and this throws NumberFormatException and |
| 29 | + //IndexOutOfBoundsException |
| 30 | + //so its encapsulated in a try-catch block |
| 31 | + double cmC = Double.parseDouble(cmFormatted); |
| 32 | + |
| 33 | + //I decided to limit the lowest |
| 34 | + //possible number to 1, |
| 35 | + //because those .001 and the likes |
| 36 | + //they are returning very large numbers |
| 37 | + if (cmC < 1) { |
| 38 | + return "too small"; |
| 39 | + } else { |
| 40 | + |
| 41 | + //we'll do some math here :) |
| 42 | + inches = cmC / CM_PER_INCH; |
| 43 | + feet = inches / IN_PER_FOOT; |
| 44 | + remainder = inches % IN_PER_FOOT; |
| 45 | + |
| 46 | + //get the integer |
| 47 | + String stringVal = String.valueOf(feet); |
| 48 | + int indexOfDecimal = stringVal.indexOf("."); |
| 49 | + String integerPart = stringVal.substring(0, indexOfDecimal); |
| 50 | + //get the decimal part |
| 51 | + String remainderFormatted = String.format("%.5g", remainder); |
| 52 | + |
| 53 | + //`convertedResult` will be the computed value |
| 54 | + String convertedResult = |
| 55 | + cmFormatted + " cm" + " = " + integerPart + |
| 56 | + "\'" + " " + remainderFormatted + "\""; |
| 57 | + |
| 58 | + return convertedResult; |
| 59 | + } |
| 60 | + } catch (NumberFormatException nf2) { |
| 61 | + return "malformed"; |
| 62 | + } catch (IndexOutOfBoundsException outB) { |
| 63 | + return "outB"; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | +} |
0 commit comments