Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package htw.berlin.prog2.ha1;

import java.text.NumberFormat;

/**
* Eine Klasse, die das Verhalten des Online Taschenrechners imitiert, welcher auf
* https://www.online-calculator.com/ aufgerufen werden kann (ohne die Memory-Funktionen)
Expand Down Expand Up @@ -78,10 +80,16 @@ public void pressUnaryOperationKey(String operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
case "1/x" -> 1 / Double.parseDouble(screen);
case "recip" -> (1 / (Double.parseDouble(screen)));
default -> throw new IllegalArgumentException();
};
screen = Double.toString(result);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
//screen = Double.toString(result);
//if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

// QuellIdee https://www.youtube.com/watch?v=KLvnQhdx4BI Java: formatting numbers with NumberFormat
NumberFormat nf = NumberFormat.getNumberInstance(); //initialize numberformat
nf.setMaximumFractionDigits(8); //max number of decimal digits
screen = nf.format(result);//rounded operation

}

Expand All @@ -93,7 +101,7 @@ public void pressUnaryOperationKey(String operation) {
* Beim zweimaligem Drücken, oder wenn bereits ein Trennzeichen angezeigt wird, passiert nichts.
*/
public void pressDotKey() {
if(!screen.endsWith(".")) screen = screen + ".";
if(!screen.contains(".")) screen = screen + ".";
}

/**
Expand Down
58 changes: 57 additions & 1 deletion app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,68 @@ void testSquareRoot() {
calc.pressDigitKey(2);
calc.pressUnaryOperationKey("√");

String expected = "1.41421356";
String expected = "1,41421356";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen
@Test
@DisplayName("should display correct number after pressing digit keys")
void testDigitInput() {
Calculator calc = new Calculator();

calc.pressDigitKey(4);
calc.pressDigitKey(2);

String expected = "42";
String actual = calc.readScreen();

assertEquals(expected, actual);
}
@Test
@DisplayName("should clear all latest values and inputs by pressing the CE button")
void testClearAllValues() {
Calculator calc = new Calculator();
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("+");
calc.pressClearKey();

String expected = "0";
String actual = calc.readScreen();

assertEquals(expected, actual);
}
@Test
@DisplayName("should display reciprocal of a number")
void testReciprocal() {
Calculator calc = new Calculator();

calc.pressDigitKey(6);
calc.pressUnaryOperationKey("recip");

String expected = "0,16666667";
String actual = calc.readScreen();

assertEquals(expected, actual);
}
@Test
@DisplayName("should display only one decimal point")
void testDotKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(6);
calc.pressDotKey();
calc.pressDigitKey(6);
calc.pressDotKey();

String expected = "6.6";
String actual = calc.readScreen();
System.out.println(calc.readScreen());


assertEquals(expected, actual);
}
}