Skip to content

Commit a79bbda

Browse files
committed
Add up and down commands
Updated user guide Updated screenshots Bumped dependencies to new versions
1 parent f6bd8bc commit a79bbda

File tree

10 files changed

+154
-9
lines changed

10 files changed

+154
-9
lines changed

graphics/ScreenShot1.jpg

44.4 KB
Loading

graphics/ScreenShot1.xcf

22.3 KB
Binary file not shown.

mdbook/src/Chapters/CalculatorCommands.md

Lines changed: 5 additions & 3 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 4 additions & 4 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.7.0</version>
6+
<version>5.8.0</version>
77
<packaging>jar</packaging>
88

99
<name>rpncalc</name>
@@ -264,7 +264,7 @@
264264
<dependency>
265265
<groupId>org.jline</groupId>
266266
<artifactId>jline-terminal-jansi</artifactId>
267-
<version>3.29.0</version>
267+
<version>3.30.3</version>
268268
</dependency>
269269

270270
<!-- https://github.com/frossm/library -->
@@ -279,15 +279,15 @@
279279
<dependency>
280280
<groupId>org.junit.jupiter</groupId>
281281
<artifactId>junit-jupiter</artifactId>
282-
<version>5.13.0-M3</version>
282+
<version>5.13.0-RC1</version>
283283
<scope>test</scope>
284284
</dependency>
285285

286286
<!-- https://mvnrepository.com/artifact/org.jline/jline-reader -->
287287
<dependency>
288288
<groupId>org.jline</groupId>
289289
<artifactId>jline</artifactId>
290-
<version>3.29.0</version>
290+
<version>3.30.3</version>
291291
</dependency>
292292

293293
</dependencies>

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.7.0'
2+
version: '5.8.0'
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/CommandParser.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu
5151
switch (cmdInputCmd) {
5252

5353
/*------------------------------------------------------------------------------
54-
* Stack Calculator Commands
54+
* Calculator Commands
5555
*-----------------------------------------------------------------------------*/
5656
// Undo
5757
case "undo":
@@ -84,6 +84,16 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu
8484
StackCommands.cmdDelete(calcStack, cmdInputParam);
8585
break;
8686

87+
// Down: Shift the stack down so Line2 becomes Line1 and the original Line1 goes to the bottom
88+
case "down":
89+
StackCommands.cmdDown(calcStack);
90+
break;
91+
92+
// Up: Shift the stack up so Line1 becomes Line2 and the last stack items moves into Line1
93+
case "up":
94+
StackCommands.cmdUp(calcStack);
95+
break;
96+
8797
// Factorial
8898
case "factorial":
8999
case "fact":

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public static void Display() {
8080
Output.printColorln(Ansi.Color.WHITE, " copy [#] Copy line1 or the provided line number and add it to the stack");
8181
Output.printColorln(Ansi.Color.WHITE, " d [#] [#-#] Delete line1, the line number provided, or a range of lines provided");
8282
Output.printColorln(Ansi.Color.WHITE, " dice XdY Roll a Y sided die X times. Default is 1d6");
83+
Output.printColorln(Ansi.Color.WHITE, " down Shift the stack down moving the Line1 to the bottom of the stack");
8384
Output.printColorln(Ansi.Color.WHITE, " fact Take a factorial of line1. Decimals will be dropped");
8485
Output.printColorln(Ansi.Color.WHITE, " f Flip the sign of the element at line1");
8586
Output.printColorln(Ansi.Color.WHITE, " int Convert line1 to an integer. No rounding is performed");
@@ -95,6 +96,7 @@ public static void Display() {
9596
Output.printColorln(Ansi.Color.WHITE, " s [#] [#] Swap the last two elements in the stack or the lines provided");
9697
Output.printColorln(Ansi.Color.WHITE, " sd [keep] Standard deviation of stack items. 'keep' will retain stack");
9798
Output.printColorln(Ansi.Color.WHITE, " u [STACK #] Undo last action or back to the undo stack defined in 'list undo'");
99+
Output.printColorln(Ansi.Color.WHITE, " up Shift the stack up. Line1 becomes Line2, last stack items becomes Line1");
98100

99101
Output.printColorln(Ansi.Color.YELLOW, "\nConversions:");
100102
Output.printColorln(Ansi.Color.WHITE, " to% Convert line1 from a number to a percent (0.715 -> 71.5%)");

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,25 @@ public static void cmdDice(StackObj calcStack, String param) {
355355
}
356356
}
357357

358+
/**
359+
* cmdDown(): Shift the stack up so Line2 becomes Line1 and the original Line1 goes to the bottom
360+
*
361+
* @param calcStack Primary Stack
362+
*/
363+
public static void cmdDown(StackObj calcStack) {
364+
// Save current calcStack to the undoStack
365+
calcStack.saveUndo();
366+
367+
// Ensure we have at least 2 values on the stack
368+
if (calcStack.size() < 2) {
369+
Output.printColorln(Ansi.Color.RED, "Error: There must be at least two items on the stack");
370+
return;
371+
}
372+
373+
// Pop the top item and place it at the bottom
374+
calcStack.push(calcStack.pop().toString(), 0);
375+
}
376+
358377
/**
359378
* cmdFactorial(): Take the factorial of the top of stack item dropping decimals if present
360379
*
@@ -1027,4 +1046,26 @@ public static void cmdUndo(StackObj calcStack, String arg) {
10271046
}
10281047
}
10291048

1049+
/**
1050+
* cmdUp(): Shifts the stack up. The top of the stack item (`line1`) will be pushed back and the last item
1051+
* will move to the top (`line1`). This is the reverse of `down`
1052+
*
1053+
* @param calcStack Primary Stack
1054+
*/
1055+
public static void cmdUp(StackObj calcStack) {
1056+
// Save current calcStack to the undoStack
1057+
calcStack.saveUndo();
1058+
1059+
// Ensure we have at least 2 values on the stack
1060+
if (calcStack.size() < 2) {
1061+
Output.printColorln(Ansi.Color.RED, "Error: There must be at least two items on the stack");
1062+
return;
1063+
}
1064+
1065+
// Move the bottom item to the top
1066+
BigDecimal bottomItem = calcStack.get(0);
1067+
calcStack.remove(0);
1068+
calcStack.push(bottomItem);
1069+
}
1070+
10301071
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ public void push(String item) {
154154
}
155155
}
156156

157+
/**
158+
* push(): Add a value to the stack at a specific position
159+
*
160+
* @param item
161+
* @param location
162+
*/
163+
public void push(String item, int location) {
164+
BigDecimal[] array = new BigDecimal[calcStack.size()];
165+
166+
// Loop through stack and build an array of the current stack
167+
for (int i = 0; i < calcStack.size(); i++) {
168+
array[i] = calcStack.get(i);
169+
}
170+
171+
// Clear the stack and get ready to reload it
172+
calcStack.clear();
173+
174+
// Reload the stack pushing the new value at the right spot
175+
for (int i = 0; i < array.length; i++) {
176+
if (i == location) {
177+
calcStack.push(new BigDecimal(item, this.mc));
178+
calcStack.push(array[i]);
179+
} else {
180+
calcStack.push(array[i]);
181+
}
182+
}
183+
}
184+
157185
/**
158186
* push(): Add an item onto the top of the stack
159187
*

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,68 @@ void testCmdDeleteParmError() {
426426
assertEquals(5, stk.size());
427427
}
428428

429+
/**
430+
* Test the `Down` command where we shift the stack down so Line2 becomes Line1 and the original Line1 goes to the end
431+
*/
432+
@Test
433+
void testDown() {
434+
StackObj stk = new StackObj();
435+
stk.push(5.0);
436+
stk.push(4.0);
437+
stk.push(3.0);
438+
stk.push(2.0);
439+
stk.push(1.0);
440+
441+
StackCommands.cmdDown(stk);
442+
assertEquals(5, stk.size());
443+
assertEquals("1.0", stk.get(0).toString());
444+
assertEquals("5.0", stk.get(1).toString());
445+
assertEquals("4.0", stk.get(2).toString());
446+
assertEquals("3.0", stk.get(3).toString());
447+
assertEquals("2.0", stk.get(4).toString());
448+
assertEquals(5, stk.size());
449+
450+
StackCommands.cmdDown(stk);
451+
assertEquals(5, stk.size());
452+
assertEquals("2.0", stk.get(0).toString());
453+
assertEquals("1.0", stk.get(1).toString());
454+
assertEquals("5.0", stk.get(2).toString());
455+
assertEquals("4.0", stk.get(3).toString());
456+
assertEquals("3.0", stk.get(4).toString());
457+
assertEquals(5, stk.size());
458+
}
459+
460+
/**
461+
* Test the `Up` command where we shift the stack up so Line1 becomes Line2 and the last item becomes Line1
462+
*/
463+
@Test
464+
void testUp() {
465+
StackObj stk = new StackObj();
466+
stk.push(5.0);
467+
stk.push(4.0);
468+
stk.push(3.0);
469+
stk.push(2.0);
470+
stk.push(1.0);
471+
472+
StackCommands.cmdUp(stk);
473+
assertEquals(5, stk.size());
474+
assertEquals("4.0", stk.get(0).toString());
475+
assertEquals("3.0", stk.get(1).toString());
476+
assertEquals("2.0", stk.get(2).toString());
477+
assertEquals("1.0", stk.get(3).toString());
478+
assertEquals("5.0", stk.get(4).toString());
479+
assertEquals(5, stk.size());
480+
481+
StackCommands.cmdUp(stk);
482+
assertEquals(5, stk.size());
483+
assertEquals("3.0", stk.get(0).toString());
484+
assertEquals("2.0", stk.get(1).toString());
485+
assertEquals("1.0", stk.get(2).toString());
486+
assertEquals("5.0", stk.get(3).toString());
487+
assertEquals("4.0", stk.get(4).toString());
488+
assertEquals(5, stk.size());
489+
}
490+
429491
/**
430492
* Test those bones! This is hard to get an exact test since the results are random, but we can check the ranges and
431493
* quantities

0 commit comments

Comments
 (0)