Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request from Ilie Chicioroaga #587248 #113

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
26 changes: 22 additions & 4 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Calculator {

private String latestOperation = "";

private int count=0;

/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand Down Expand Up @@ -80,8 +82,10 @@ public void pressUnaryOperationKey(String operation) {
case "1/x" -> 1 / Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};

screen = Double.toString(result);
if(screen.equals("NaN")) screen = "Error";
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

}
Expand All @@ -105,7 +109,8 @@ public void pressDotKey() {
* entfernt und der Inhalt fortan als positiv interpretiert.
*/
public void pressNegativeKey() {
screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen;
screen = screen.startsWith("-") ? "-"+screen.substring(0) : "-" + screen;

}

/**
Expand All @@ -118,14 +123,27 @@ public void pressNegativeKey() {
* und das Ergebnis direkt angezeigt.
*/
public void pressEqualsKey() {
var result = switch(latestOperation) {
case "+" -> latestValue + Double.parseDouble(screen);
case "-" -> latestValue - Double.parseDouble(screen);
System.out.println(screen);

var result = switch(latestOperation) {
case "+" -> latestValue + (Double.parseDouble(screen));
case "-" -> latestValue - (Double.parseDouble(screen));
case "x" -> latestValue * Double.parseDouble(screen);
case "/" -> latestValue / Double.parseDouble(screen);
//lösung für den zweiten finalen roten Test
case ""->Double.parseDouble(screen);
default -> throw new IllegalArgumentException();
};


screen = Double.toString(result);
System.out.println(" asdfa"+screen);
//latestValue=Double.parseDouble(screen);//3 which is the result

System.out.println(screen+"the screen");
//System.out.println("The third value:"+latestValue);
//latestValue=Double.parseDouble(screen);

if(screen.equals("Infinity")) screen = "Error";
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
Expand Down
107 changes: 107 additions & 0 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,112 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen

//test which tests if screen equals 0 if C is pressed
@Test
@DisplayName("should clear screen")

void testClearKey(){
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(1);
calc.pressEqualsKey();
calc.pressClearKey();
String expected="0";
String actual=calc.readScreen();
assertEquals(expected,actual);
}

@Test
@DisplayName("Test double click of Binary Operations")

void TestDoubleClickBinaryOperationKey(){
Calculator calc=new Calculator();
calc.pressDigitKey(8);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(2);
calc.pressBinaryOperationKey("/");
String expected="4";
String actual=calc.readScreen();
assertEquals(expected,actual);
}


@Test
@DisplayName("Test binary operations")

void TestBinaryOperations(){
Calculator calc=new Calculator();
calc.pressDigitKey(4);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(2);
calc.pressEqualsKey();
String expected="2";
String actual=calc.readScreen();
assertEquals(expected,actual);
}

@Test
@DisplayName("Test result of square root result when result should have no decimals")

void TestUnaryOperations(){
Calculator calc=new Calculator();
calc.pressDigitKey(4);
calc.pressUnaryOperationKey("√");
String expected="2";
String actual=calc.readScreen();
assertEquals(expected,actual);
}

@Test
@DisplayName("Test another click of = after some operation has been made")

void TestBinaryOperationsAgain(){
Calculator calc=new Calculator();
calc.pressDigitKey(6);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(1);
calc.pressEqualsKey();
calc.pressEqualsKey();
String expected="4";
String actual=calc.readScreen();
assertEquals(expected,actual);
}

@Test
@DisplayName("Test Negative key")

void TestNegativeKeyPress(){
Calculator calc=new Calculator();
calc.pressNegativeKey();
calc.pressDigitKey(6);
calc.pressNegativeKey();
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(6);
calc.pressEqualsKey();
String expected="0";
String actual=calc.readScreen();
assertEquals(expected,actual);
}


@Test
@DisplayName("Test when pressing Equals key")

void Test234(){
Calculator calc=new Calculator();
calc.pressDigitKey(9);
calc.pressEqualsKey();
String expected="9";
String actual=calc.readScreen();
assertEquals(expected,actual);
}






}