Browse Source

Fertige Abgabe mit mehreren Tests

master
juleslepitschka 2 years ago
parent
commit
b0cc3ac20d
2 changed files with 79 additions and 10 deletions
  1. 34
    8
      src/TriangleChecker.java
  2. 45
    2
      test/TriangleCheckerTest.java

+ 34
- 8
src/TriangleChecker.java View File

import java.util.Scanner; import java.util.Scanner;


public class TriangleChecker { public class TriangleChecker {
public enum TriangleType { public enum TriangleType {
NONE,
NORMAL,
NONE, // kein Dreieck
NORMAL, // Dreieck
ISOSCELES, // Gleichschenklig ISOSCELES, // Gleichschenklig
EQUILATERAL // Gleichseitig EQUILATERAL // Gleichseitig
} }
switch (type) { switch (type) {
case NONE: case NONE:
System.out.println("Kein Dreieck"); System.out.println("Kein Dreieck");
break;
break;
case NORMAL: case NORMAL:
System.out.println("Dreieck"); System.out.println("Dreieck");
break;
break;
case ISOSCELES: case ISOSCELES:
System.out.println("Gleichschenkliges Dreieck"); System.out.println("Gleichschenkliges Dreieck");
break;
break;
case EQUILATERAL: case EQUILATERAL:
System.out.println("Gleichseitiges Dreieck"); System.out.println("Gleichseitiges Dreieck");
break;
break;
} }
} }


// Analyse der Dreiecksart
// Analysen
public static TriangleType checkTriangle(float a, float b, float c) { public static TriangleType checkTriangle(float a, float b, float c) {
if(checkIfNotValid(a,b,c)) return TriangleType.NONE;
if(checkIfEquilateral(a,b,c)) return TriangleType.EQUILATERAL;
if(checkIfIsosceles(a,b,c)) return TriangleType.ISOSCELES;
if(checkIfNormal(a,b,c)) return TriangleType.NORMAL;
return TriangleType.NONE; return TriangleType.NONE;
} }


// Analyse kein Dreieck
private static boolean checkIfNotValid(float a, float b, float c){
if (a + b <= c || a + c <= b || b + c <= a) return true;
return false;
}

// Analyse normales Dreieck
private static boolean checkIfNormal(float a, float b, float c){
if (a != b && b != c && a != c) return true;
return false;
}


// Analyse gleichschenkliges Dreieck
private static boolean checkIfIsosceles(float a, float b, float c){
if (a == b || a == c || b == c) return true;
return false;
}

// Analyse gleichseitiges Dreieck
private static boolean checkIfEquilateral(float a, float b, float c){
if (a == b && b == c) return true;
return false;
}
} }

+ 45
- 2
test/TriangleCheckerTest.java View File

import static org.junit.Assert.*; import static org.junit.Assert.*;


public class TriangleCheckerTest { public class TriangleCheckerTest {
@Test @Test
public void testValidTriangle() { public void testValidTriangle() {




// Assert // Assert
assertEquals(TriangleChecker.TriangleType.NORMAL, type); assertEquals(TriangleChecker.TriangleType.NORMAL, type);
}

@Test
public void testIfNotValidTriangle_SumOfTwoSidesEqualsThird() {


// Arrange
float a = 10;
float b = 20;
float c = 30;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.NONE, type);
} }


}
@Test
public void testEquilateralTriangle() {

// Arrange
float a = 1;
float b = 1;
float c = 1;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.EQUILATERAL, type);
}

@Test
public void testIsoscelesTriangleIfAEqualsB() {

// Arrange
float a = 10;
float b = 10;
float c = 5;

// Act
TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c);

// Assert
assertEquals(TriangleChecker.TriangleType.ISOSCELES, type);
}
}

Loading…
Cancel
Save