Skip to content

Commit 4139a9d

Browse files
committed
Corrected decimal alignment display bug
- Adding number without decimal would not align - Added a test for the new queryDecimalIndex() method - Updated pom.xml to ignore windows error on `chmod` for exec-maven-plugin - Updated maven exec plugin from 3.3.0 -> 3.4.0 - Updated JUnit from 5.11.0-M2 -> 5.11.0-RC1
1 parent ee67656 commit 4139a9d

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

pom.xml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>org.fross</groupId>
55
<artifactId>rpncalc</artifactId>
6-
<version>5.2.9</version>
6+
<version>5.2.10</version>
77
<packaging>jar</packaging>
88

99
<name>rpncalc</name>
@@ -208,7 +208,7 @@
208208
<plugin>
209209
<groupId>org.codehaus.mojo</groupId>
210210
<artifactId>exec-maven-plugin</artifactId>
211-
<version>3.3.0</version>
211+
<version>3.4.0</version>
212212
<executions>
213213
<execution>
214214
<id>chmod</id>
@@ -220,9 +220,12 @@
220220
<executable>chmod</executable>
221221
<arguments>
222222
<argument>+x</argument>
223-
<argument>
224-
${project.build.directory}/${project.name}.jar</argument>
223+
<argument>${project.build.directory}/${project.name}.jar</argument>
225224
</arguments>
225+
<successCodes>
226+
<successCode>0</successCode>
227+
<successCode>1</successCode>
228+
</successCodes>
226229
</configuration>
227230
</execution>
228231
</executions>
@@ -273,7 +276,7 @@
273276
<dependency>
274277
<groupId>org.junit.jupiter</groupId>
275278
<artifactId>junit-jupiter</artifactId>
276-
<version>5.11.0-M2</version>
279+
<version>5.11.0-RC1</version>
277280
<scope>test</scope>
278281
</dependency>
279282

snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: rpncalc
2-
version: '5.2.9'
2+
version: '5.2.10'
33
summary: The command line Reverse Polish Notation (RPN) calculator
44
description: |
55
RPNCalc is an easy to use command line based Reverse Polish

src/main/java/org/fross/rpncalc/Display.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,25 @@ public class Display {
4242
*/
4343
@SuppressWarnings("MalformedFormatString") // No idea what's wrong with the return statement
4444
public static String Comma(BigDecimal bd) {
45-
Output.debugPrintln("BigDecimal Scale: " + bd.scale());
45+
// Output.debugPrintln("Comma: BigDecimal Scale: " + bd.scale());
4646
return String.format("%,." + bd.scale() + "f", bd);
4747
}
4848

49+
/**
50+
* queryDecimalIndex(): Return the index of the decimal in a string. If none is given assume it's at the end.
51+
*
52+
* @param str - String with or without a decimal point in it
53+
* @return An integer with the location of the decimal (or the end if it doesn't exist)
54+
*/
55+
public static int queryDecimalIndex(String str) {
56+
// Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end
57+
int di = str.indexOf(".");
58+
if (di == -1) {
59+
di = str.length();
60+
}
61+
return di;
62+
}
63+
4964
/**
5065
* DisplayStatusLine(): Display the last line of the header and the separator line. This is a separate function given it also
5166
* inserts the loaded stack and spaces everything correctly.

src/main/java/org/fross/rpncalc/Main.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public static void main(String[] args) {
150150
currentStackItem = Display.Comma(calcStack.get(i));
151151
}
152152

153-
// Determine where the decimal point is located
154-
decimalIndex = currentStackItem.indexOf(".");
153+
// Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end
154+
decimalIndex = Display.queryDecimalIndex(currentStackItem);
155155

156156
// If current stack item has more digits ahead of decimal make that the max - commas are included.
157157
if (maxDigitsBeforeDecimal < decimalIndex) {
@@ -165,9 +165,9 @@ public static void main(String[] args) {
165165

166166
}
167167

168-
// Uncomment to debug alignment issues
169-
// Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal);
170-
// Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers);
168+
// Output information for alignment debugging
169+
Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal);
170+
Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers);
171171

172172
// Display the current stack contents
173173
for (int i = 0; i < calcStack.size(); i++) {
@@ -184,18 +184,17 @@ public static void main(String[] args) {
184184
String stkLineNumber = String.format("%0" + LINE_NUMBER_DIGITS + "d: ", calcStack.size() - i);
185185
Output.printColor(Ansi.Color.CYAN, stkLineNumber);
186186

187-
// Decimal Alignment - insert spaces before number to align to the decimal point
187+
// DECIMAL ALIGNMENT: Insert spaces before number to align to the decimal point
188188
if (configAlignment.compareTo("d") == 0) {
189-
for (int k = 0; k < (maxDigitsBeforeDecimal - currentStackItem.indexOf(".")); k++) {
190-
Output.print(" ");
191-
}
189+
int decimalIndex = Display.queryDecimalIndex(currentStackItem);
190+
191+
// Output the spaces in front so the decimals align
192+
Output.print(" ".repeat(maxDigitsBeforeDecimal - decimalIndex));
192193
}
193194

194-
// Right Alignment - insert spaces before number to right align
195+
// RIGHT ALIGNMENT: Insert spaces before number to right align
195196
if (configAlignment.compareTo("r") == 0) {
196-
for (int k = 0; k < (maxLenOfNumbers - currentStackItem.length()); k++) {
197-
Output.print(" ");
198-
}
197+
Output.print(" ".repeat(maxLenOfNumbers - currentStackItem.length()));
199198
}
200199

201200
// Now that the spaces are inserted (for decimal/right) display the number

src/test/java/org/fross/rpncalc/DisplayTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import static org.junit.jupiter.api.Assertions.assertEquals;
3434

3535
public class DisplayTest {
36-
3736
@Test
3837
void testComma() {
39-
4038
assertEquals("1", Display.Comma(new BigDecimal("1")));
4139
assertEquals("1.01", Display.Comma(new BigDecimal("1.01")));
4240
assertEquals("-2.000000001231", Display.Comma(new BigDecimal("-2.000000001231")));
@@ -46,4 +44,17 @@ void testComma() {
4644
assertEquals("-1,987,654,321,987,654,321", Display.Comma(new BigDecimal("-1987654321987654321")));
4745
}
4846

47+
@Test
48+
void testQueryDecimalIndex() {
49+
assertEquals(6, Display.queryDecimalIndex("50,000"));
50+
assertEquals(1, Display.queryDecimalIndex("1"));
51+
assertEquals(1, Display.queryDecimalIndex("1.01"));
52+
assertEquals(5, Display.queryDecimalIndex("-1234.4321"));
53+
assertEquals(0, Display.queryDecimalIndex(".00001"));
54+
assertEquals(18, Display.queryDecimalIndex("-1,123,454,678,999.1234532643"));
55+
assertEquals(5, Display.queryDecimalIndex("-8e21"));
56+
assertEquals(1, Display.queryDecimalIndex("1.02e+22"));
57+
assertEquals(6, Display.queryDecimalIndex("-1e+22"));
58+
}
59+
4960
}

0 commit comments

Comments
 (0)