Skip to content

Commit f0c939e

Browse files
committed
Feat : 삼각형 기능 구현 및 테스트 진행
1 parent c727b99 commit f0c939e

File tree

5 files changed

+135
-6
lines changed

5 files changed

+135
-6
lines changed

src/main/java/coordinate/domain/CoordinatesCalculator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public CoordinatesCalculator(String expression) {
1212
return;
1313
}
1414

15+
if (expression.split(REGEX).length == 3) {
16+
shapes = new Triangle(expression);
17+
return;
18+
}
19+
1520
if (expression.split(REGEX).length == 4) {
1621
shapes = new Rectangle(expression);
1722
return;

src/main/java/coordinate/domain/Rectangle.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6-
import java.util.stream.Collectors;
76

87
import static java.lang.Math.abs;
98

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package coordinate.domain;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import static java.lang.Math.abs;
8+
import static java.lang.Math.sqrt;
9+
10+
public class Triangle implements Shapes {
11+
public static final String REGEX = "-";
12+
private final List<Coordinate> coordinates;
13+
14+
15+
public Triangle(String expression) {
16+
17+
if (!expression.contains(REGEX)) {
18+
throw new IllegalArgumentException("식이 틀렸습니다.");
19+
}
20+
21+
if (expression.split(REGEX).length != 3) {
22+
throw new IllegalArgumentException("좌표는 세 개만 들어가야 합니다.");
23+
}
24+
25+
List<Coordinate> coordinateList = Arrays.asList(
26+
new Coordinate(expression.split(REGEX)[0]),
27+
new Coordinate(expression.split(REGEX)[1]),
28+
new Coordinate(expression.split(REGEX)[2])
29+
);
30+
31+
this.coordinates = coordinateList;
32+
}
33+
34+
@Override
35+
public double getResult() {
36+
return getArea(this.coordinates);
37+
}
38+
39+
private double getArea(List<Coordinate> coordinates) {
40+
double a = getDistance(coordinates.get(0), coordinates.get(1));
41+
double b = getDistance(coordinates.get(1), coordinates.get(2));
42+
double c = getDistance(coordinates.get(2), coordinates.get(0));
43+
double s = (a + b + c) / 2;
44+
return sqrt(s * (s - a) * (s - b) * (s - c));
45+
}
46+
47+
@Override
48+
public List<Coordinate> findCoordinates() {
49+
return new ArrayList<>(this.coordinates);
50+
}
51+
52+
private double getDistance(Coordinate coordinate, Coordinate otherCoordinate) {
53+
return sqrt(
54+
abs((coordinate.getX() - otherCoordinate.getX()) * (coordinate.getX() - otherCoordinate.getX())
55+
+ (coordinate.getY() - otherCoordinate.getY()) * (coordinate.getY() - otherCoordinate.getY())));
56+
}
57+
}

src/test/java/coordinate/domain/CoordinatesCalculatorTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.DisplayName;
5-
import org.junit.jupiter.api.Test;
65
import org.junit.jupiter.params.ParameterizedTest;
76
import org.junit.jupiter.params.provider.ValueSource;
87

98
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.offset;
1010

1111
class CoordinatesCalculatorTest {
1212

1313
@ParameterizedTest
14-
@ValueSource(strings = {"(10,10)-(14,15)", "(10,10)-(22,10)-(22,18)-(10,18)"})
14+
@ValueSource(strings = {"(10,10)-(14,15)", "(10,10)-(22,10)-(22,18)-(10,18)", "(10,10)-(14,15)-(20,8)"})
1515
@DisplayName("입력을 받습니다.")
1616
void test1(String ) {
1717
Assertions.assertDoesNotThrow(
@@ -20,7 +20,7 @@ void test1(String 식) {
2020
}
2121

2222
@ParameterizedTest
23-
@ValueSource(strings = {"(10,10)-(14,15)", "(10,10)-(22,10)-(22,18)-(10,18)"})
23+
@ValueSource(strings = {"(10,10)-(14,15)", "(10,10)-(22,10)-(22,18)-(10,18)", "(10,10)-(14,15)-(20,8)"})
2424
@DisplayName("입력이 선 인지 직사각형인지 판단합니다.")
2525
void test2(String ) {
2626
CoordinatesCalculator calculator = new CoordinatesCalculator();
@@ -32,10 +32,14 @@ void test2(String 식) {
3232
if (.equals("(10,10)-(22,10)-(22,18)-(10,18)")) {
3333
assertThat(calculator.getShapes()).isInstanceOf(Rectangle.class);
3434
}
35+
36+
if (.equals("(10,10)-(14,15)-(20,8)")) {
37+
assertThat(calculator.getShapes()).isInstanceOf(Triangle.class);
38+
}
3539
}
3640

3741
@ParameterizedTest
38-
@ValueSource(strings = {"(10,10)-(10,15)", "(10,10)-(22,10)-(22,18)-(10,18)"})
42+
@ValueSource(strings = {"(10,10)-(10,15)", "(10,10)-(22,10)-(22,18)-(10,18)", "(10,10)-(14,15)-(20,8)"})
3943
@DisplayName("답을 출력합니다.")
4044
void test3(String ) {
4145
CoordinatesCalculator calculator = new CoordinatesCalculator();
@@ -47,10 +51,14 @@ void test3(String 식) {
4751
if (.equals("(10,10)-(22,10)-(22,18)-(10,18)")) {
4852
assertThat(calculator.getResult()).isEqualTo(96.0);
4953
}
54+
55+
if (.equals("(10,10)-(14,15)-(20,8)")) {
56+
assertThat(calculator.getResult()).isEqualTo(29.0, offset(0.0009));
57+
}
5058
}
5159

5260
@ParameterizedTest
53-
@ValueSource(strings = {"(10,10)-(10,15)", "(10,10)-(22,10)-(22,18)-(10,18)"})
61+
@ValueSource(strings = {"(10,10)-(10,15)", "(10,10)-(22,10)-(22,18)-(10,18)", "(10,10)-(14,15)-(20,8)"})
5462
@DisplayName("좌표를 출력합니다.")
5563
void test4(String ) {
5664
CoordinatesCalculator calculator = new CoordinatesCalculator();
@@ -62,5 +70,9 @@ void test4(String 식) {
6270
if (.equals("(10,10)-(22,10)-(22,18)-(10,18)")) {
6371
assertThat(calculator.findCoordinates()).hasSize(4);
6472
}
73+
74+
if (.equals("(10,10)-(14,15)-(20,8)")) {
75+
assertThat(calculator.findCoordinates()).hasSize(3);
76+
}
6577
}
6678
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package coordinate.domain;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.ValueSource;
7+
8+
import static org.assertj.core.api.Assertions.*;
9+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10+
11+
class TriangleTest {
12+
13+
@Test
14+
@DisplayName("삼각형을 그릴 수 있습니다.")
15+
void test() {
16+
String = "(10,10)-(14,15)-(20,8)";
17+
assertDoesNotThrow(
18+
() -> new Triangle()
19+
);
20+
}
21+
22+
@Test
23+
@DisplayName("식이 틀리면 예외가 발생합니다.")
24+
void test1() {
25+
String = "(10,10)(14,15)-(20,8)";
26+
assertThatThrownBy(
27+
() -> new Triangle()
28+
).isInstanceOf(IllegalArgumentException.class);
29+
}
30+
31+
32+
@ParameterizedTest
33+
@ValueSource(strings = {"(14,15)-(20,8)", "(14,15)-(20,8)-(20,15)-(14,8)"})
34+
@DisplayName("좌표가 세개 이외의 값이 들어오면 예외가 발생합니다.")
35+
void test2(String ) {
36+
assertThatThrownBy(
37+
() -> new Triangle()
38+
).isInstanceOf(IllegalArgumentException.class);
39+
}
40+
41+
@Test
42+
@DisplayName("답인 삼각형의 넓이를 출력합니다.")
43+
void test3() {
44+
String = "(10,10)-(14,15)-(20,8)";
45+
Triangle 삼각형 = new Triangle();
46+
assertThat(삼각형.getResult()).isEqualTo(29.0, offset(0.00099));
47+
}
48+
49+
@Test
50+
@DisplayName("좌표를 그리기 위해 좌표들을 출력합니다.")
51+
void test4() {
52+
String = "(10,10)-(14,15)-(20,8)";
53+
Triangle 삼각형 = new Triangle();
54+
assertThat(삼각형.findCoordinates()).hasSize(3);
55+
}
56+
}

0 commit comments

Comments
 (0)