Skip to content

Commit

Permalink
Added in2ft and ft2in commands
Browse files Browse the repository at this point in the history
Added tests for the new converesions
Updated user guide
Updated shade plugin from 3.5.2 -> 3.5.3
Updated jUnit from 5.10.2 -> 5.11.0-M1
Updated jLine from 3.25.1 -> 3.26.1
  • Loading branch information
frossm committed Apr 25, 2024
1 parent 43b0b69 commit 67f0561
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 31 deletions.
28 changes: 15 additions & 13 deletions mdbook/src/Chapters/Conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ The `frac [base]` command takes the item on the top of the stack (`line1`) and d
if `frac 5` would have been entered (which means 1/5 is maximum granularity), you get `1.1234 is approximately 1 1/5`.


|<div style="width:90px">Command</div>|Description|
|-------|-----------|
|to%|Converts `line1` from a "number" to a percent. For example, `0.4455` becomes `44.55%`. This is simply done by multiplying the number by 100|
|from%|Converts `line1` from a percent to a "number". For example, `93.124%` becomes `.93124`. This is simply done by multiplying the number by 0.01|
|frac [base]|Display a fractional estimate of the last stack item (`line1`) with the maximum granularity of 1/[base]. See the above description for more detail|
|in2mm|Converts the value in `line1` from inches to millimeters|
|mm2in|Converts the value in `line1` from millimeters to inches|
|deg2rad|Convert `line1` from degrees into [radians](https://en.wikipedia.org/wiki/Radian)|
|rad2deg|Convert `line1` from radians into degrees|
|gram2oz<br>grams2oz|Convert `line1` from grams into ounces using the constant of 0.035274 ounces / gram|
|oz2gram<br>oz2grams|Convert `line1` from ounces into grams using the constant of 28.349523125 grams / ounce|
|kg2lbs<br>kgs2lbs|convert `line1` from kilograms to US pounds using the constant of 2.2046226218 lbs/kg|
|lbs2kg<br>lbs2kgs|convert `line1` from US pounds using the constant of 0.45359237 kg/lbs|
| <div style="width:90px">Command</div> | Description |
|---------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| to% | Converts `line1` from a "number" to a percent. For example, `0.4455` becomes `44.55%`. This is simply done by multiplying the number by 100 |
| from% | Converts `line1` from a percent to a "number". For example, `93.124%` becomes `.93124`. This is simply done by multiplying the number by 0.01 |
| frac [base] | Display a fractional estimate of the last stack item (`line1`) with the maximum granularity of 1/[base]. See the above description for more detail |
| in2mm | Converts the value in `line1` from inches to millimeters |
| mm2in | Converts the value in `line1` from millimeters to inches |
| in2ft | Converts the value in `line1` from inches to feet |
| ft2in | Converts the value in `line1` from feet to inches |
| deg2rad | Convert `line1` from degrees into [radians](https://en.wikipedia.org/wiki/Radian) |
| rad2deg | Convert `line1` from radians into degrees |
| gram2oz<br>grams2oz | Convert `line1` from grams into ounces using the constant of 0.035274 ounces / gram |
| oz2gram<br>oz2grams | Convert `line1` from ounces into grams using the constant of 28.349523125 grams / ounce |
| kg2lbs<br>kgs2lbs | convert `line1` from kilograms to US pounds using the constant of 2.2046226218 lbs/kg |
| lbs2kg<br>lbs2kgs | convert `line1` from US pounds using the constant of 0.45359237 kg/lbs |
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>5.2.5</version>
<version>5.2.6</version>
<packaging>jar</packaging>

<name>rpncalc</name>
Expand Down Expand Up @@ -113,7 +113,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<version>3.5.3</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -273,23 +273,23 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<version>5.11.0-M1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jline/jline-reader -->
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-reader</artifactId>
<version>3.25.1</version>
<version>3.26.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jline/jline-terminal-jansi -->
<!-- Needed to remove jline warning when app starts -->
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
<version>3.25.1</version>
<version>3.26.1</version>
</dependency>

</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: rpncalc
version: '5.2.5'
version: '5.2.6'
summary: The command line Reverse Polish Notation (RPN) calculator
description: |
RPNCalc is an easy to use command line based Reverse Polish
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/fross/rpncalc/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,22 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu

// Convert inches to millimeters
case "in2mm":
StackConversions.cmdConvertIN2MM(calcStack);
StackConversions.cmdIn2Mm(calcStack);
break;

// Convert millimeters to inches
case "mm2in":
StackConversions.cmdConvertMM2IN(calcStack);
StackConversions.cmdMm2In(calcStack);
break;

// Convert inches to feet
case "in2ft":
StackConversions.cmdIn2Ft(calcStack);
break;

// Convert feet to inches
case "ft2in":
StackConversions.cmdFt2In(calcStack);
break;

// Convert to Radians
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/fross/rpncalc/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public static void Display() {
Output.printColorln(Ansi.Color.WHITE, " rad2deg Convert line1 from radians to degrees");
Output.printColorln(Ansi.Color.WHITE, " in2mm Convert line1 from inches into millimeters");
Output.printColorln(Ansi.Color.WHITE, " mm2in Convert line1 from millimeters to inches");
Output.printColorln(Ansi.Color.WHITE, " in2ft Convert line1 from inches into feet");
Output.printColorln(Ansi.Color.WHITE, " ft2in Convert line1 from feet to inches");
Output.printColorln(Ansi.Color.WHITE, " gram2oz Convert line1 from grams to US ounces");
Output.printColorln(Ansi.Color.WHITE, " oz2gram Convert line1 from US ounces to grams");
Output.printColorln(Ansi.Color.WHITE, " kg2lbs Convert line1 from kilograms to US pounds");
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/org/fross/rpncalc/StackConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static void cmdToPercent(StackObj calcStack) {
}

/**
* cmdConvertMM(): Assumes Line1 is in inches and converts to millimeters
* cmdConvertIn2Mm(): Assumes Line1 is in inches and converts to millimeters
*/
public static void cmdConvertIN2MM(StackObj calcStack) {
public static void cmdIn2Mm(StackObj calcStack) {
// Verify at least one elements exists
if (calcStack.isEmpty()) {
Output.printColorln(Ansi.Color.RED, "Error: There must be at least 1 element on the stack to convert");
Expand All @@ -92,9 +92,9 @@ public static void cmdConvertIN2MM(StackObj calcStack) {
}

/**
* cmdConvertIN(): Assumes Line1 is in millimeters and converts to inches
* cmdConvertMm2In(): Assumes Line1 is in millimeters and converts to inches
*/
public static void cmdConvertMM2IN(StackObj calcStack) {
public static void cmdMm2In(StackObj calcStack) {
// Verify at least one elements exists
if (calcStack.isEmpty()) {
Output.printColorln(Ansi.Color.RED, "Error: There must be at least 1 element on the stack to convert");
Expand All @@ -108,6 +108,40 @@ public static void cmdConvertMM2IN(StackObj calcStack) {
calcStack.push(calcStack.pop().divide(new BigDecimal("25.4"), MathContext.DECIMAL128));
}

/**
* cmdConvertIn2Ft(): Assumes Line1 is in inches and converts to feet
*/
public static void cmdIn2Ft(StackObj calcStack) {
// Verify at least one elements exists
if (calcStack.isEmpty()) {
Output.printColorln(Ansi.Color.RED, "Error: There must be at least 1 element on the stack to convert");
return;
}

// Save current calcStack to the undoStack
calcStack.saveUndo();

// Pop off the last value, convert, and push it back
calcStack.push(calcStack.pop().divide(new BigDecimal("12"), MathContext.DECIMAL128));
}

/**
* cmdConvertFt2In(): Assumes Line1 is in feet and converts to inches
*/
public static void cmdFt2In(StackObj calcStack) {
// Verify at least one elements exists
if (calcStack.isEmpty()) {
Output.printColorln(Ansi.Color.RED, "Error: There must be at least 1 element on the stack to convert");
return;
}

// Save current calcStack to the undoStack
calcStack.saveUndo();

// Pop off the last value, convert, and push it back
calcStack.push(calcStack.pop().multiply(new BigDecimal("12")));
}

/**
* cmdFraction(): Display the last stack item as a fraction with a minimum base of the provided number. For example, sending
* 64 would produce a fraction of 1/64th but will be reduced if possible.
Expand Down
48 changes: 42 additions & 6 deletions src/test/java/org/fross/rpncalc/StackConversionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,39 +92,39 @@ void testCmdToPercent() {
}

/**
* Test method for {@link org.fross.rpncalc.StackConversions#cmdConvertIN2MM(org.fross.rpncalc.StackObj)}.
* Test method for {@link org.fross.rpncalc.StackConversions#cmdIn2Mm(org.fross.rpncalc.StackObj)}.
*/
@Test
void testCmdConvertIN2MM() {
StackObj stk = new StackObj();

stk.push(31.6);
StackConversions.cmdConvertIN2MM(stk);
StackConversions.cmdIn2Mm(stk);
assertEquals(802.64, stk.peek().doubleValue());
assertEquals(1, stk.size());

stk.push(1.234e12);
StackConversions.cmdConvertIN2MM(stk);
StackConversions.cmdIn2Mm(stk);
assertEquals("3.13436E+13", stk.peek().toString());
assertEquals(2, stk.size());

}

/**
* Test method for {@link org.fross.rpncalc.StackConversions#cmdConvertMM2IN(org.fross.rpncalc.StackObj)}.
* Test method for {@link org.fross.rpncalc.StackConversions#cmdMm2In(org.fross.rpncalc.StackObj)}.
*/
@Test
void testCmdConvertMM2IN() {
StackObj stk = new StackObj();

stk.push(666.0);
StackConversions.cmdConvertMM2IN(stk);
StackConversions.cmdMm2In(stk);
StackCommands.cmdRound(stk, "4");
assertEquals(26.2205, stk.peek().doubleValue());
assertEquals(1, stk.size());

stk.push(1.234e12);
StackConversions.cmdConvertMM2IN(stk);
StackConversions.cmdMm2In(stk);
StackCommands.cmdRound(stk, "10");
assertEquals("48582677165.3543307087", stk.peek().toEngineeringString());
assertEquals(2, stk.size());
Expand Down Expand Up @@ -498,4 +498,40 @@ void testLbs2Kg() {
assertEquals("1959972630770000000.00", stk.pop().toEngineeringString());
}

/**
* Test Inches to Feet conversion
*/
@Test
void testIn2Ft() {
StackObj stk = new StackObj();

stk.push(123.321);
StackConversions.cmdIn2Ft(stk);
StackCommands.cmdRound(stk, "5");
assertEquals(10.27675, stk.pop().doubleValue());

stk.push(-50.987654);
StackConversions.cmdIn2Ft(stk);
StackCommands.cmdRound(stk, "5");
assertEquals(-4.24897, stk.pop().doubleValue());
}

/**
* Test Feet to Inches conversion
*/
@Test
void testFt2In() {
StackObj stk = new StackObj();

stk.push(7117.44);
StackConversions.cmdFt2In(stk);
StackCommands.cmdRound(stk, "5");
assertEquals(85409.28, stk.pop().doubleValue());

stk.push(-32.0011);
StackConversions.cmdFt2In(stk);
StackCommands.cmdRound(stk, "5");
assertEquals(-384.0132, stk.pop().doubleValue());
}

}

0 comments on commit 67f0561

Please sign in to comment.