From 8fd57fc6c660444857bd16a5ce842193aad4d23c Mon Sep 17 00:00:00 2001 From: Hannes Date: Fri, 24 Dec 2021 12:01:01 +0100 Subject: [PATCH] new program --- src/TriangleChecker.java | 27 ++++++++++++---- test/TriangleCheckerTest.java | 61 +++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/TriangleChecker.java b/src/TriangleChecker.java index f638057..58f4f14 100644 --- a/src/TriangleChecker.java +++ b/src/TriangleChecker.java @@ -1,9 +1,9 @@ import java.util.Scanner; public class TriangleChecker { - + public enum TriangleType { - NONE, + NONE, NORMAL, ISOSCELES, // Gleichschenklig EQUILATERAL // Gleichseitig @@ -32,21 +32,34 @@ public class TriangleChecker { switch (type) { case NONE: System.out.println("Kein Dreieck"); - break; + break; case NORMAL: System.out.println("Dreieck"); - break; + break; case ISOSCELES: System.out.println("Gleichschenkliges Dreieck"); - break; + break; case EQUILATERAL: System.out.println("Gleichseitiges Dreieck"); - break; + break; } } // Analyse der Dreiecksart public static TriangleType checkTriangle(float a, float b, float c) { - return TriangleType.NORMAL; + + if (checkTriangleBasePossibility(a, b, c)) { + if (a == b && a == c) return TriangleType.EQUILATERAL; + if (a == b || a == c || b == c) return TriangleType.ISOSCELES; + return TriangleType.NORMAL; + } + return TriangleType.NONE; } + + public static boolean checkTriangleBasePossibility(float a, float b, float c) { + if (a <= 0 || b <= 0 || c <= 0) return false; + return a + b > c && a + c > b && b + c > a; + } + + } \ No newline at end of file diff --git a/test/TriangleCheckerTest.java b/test/TriangleCheckerTest.java index b338b71..649c7af 100644 --- a/test/TriangleCheckerTest.java +++ b/test/TriangleCheckerTest.java @@ -2,14 +2,14 @@ import org.junit.Test; import static org.junit.Assert.*; public class TriangleCheckerTest { - + @Test - public void testValidTriangle() { + public void testIsTriangle() { // Arrange - float a = 7; - float b = 3; - float c = 1; + float a = 3; + float b = 2; + float c = 4; // Act TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c); @@ -17,6 +17,55 @@ public class TriangleCheckerTest { // Assert assertEquals(TriangleChecker.TriangleType.NORMAL, type); + // Arrange + a = -2; + b = 5; + c = 2; + + // Act + type = TriangleChecker.checkTriangle(a, b, c); + + // Assert + assertEquals(TriangleChecker.TriangleType.NONE, type); + + // Arrange + a = 5; + b = 5; + c = 10; + + // Act + type = TriangleChecker.checkTriangle(a, b, c); + + // Assert + assertEquals(TriangleChecker.TriangleType.NONE, type); } -} + @Test + public void testIsoTriangle(){ + // Arrange + float a = 6; + float b = 6; + float c = 3; + + // Act + TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c); + + // Assert + assertEquals(TriangleChecker.TriangleType.ISOSCELES, type); + } + + @Test + public void testEqualTriangle(){ + // Arrange + float a = 5; + float b = 5; + float c = 5; + + // Act + TriangleChecker.TriangleType type = TriangleChecker.checkTriangle(a, b, c); + + // Assert + assertEquals(TriangleChecker.TriangleType.EQUILATERAL, type); + } + +} \ No newline at end of file