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

feat: 좌표계산기(선길이) 기능 구현 #12

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b352ced
feat(RentCompanyTest): 스켈레톤 코드 작성
kimhyunso May 24, 2024
6d2e748
feat(RentCompany): Car 인터페이스 및 구현체 구현
kimhyunso May 25, 2024
6dfa8b5
refactor(Car): Car 구현체들 수정
kimhyunso May 25, 2024
8a4064c
feat(RentCompany): 차량에 따른 연료 주입 기능 구현
kimhyunso May 27, 2024
ebff611
refactor(package): 패키지 이동
kimhyunso May 27, 2024
36a3fb3
feat(연료주입): 차량에 따른 연료 주입 실습
kimhyunso May 27, 2024
9d6103b
feat(TODO): todo리스트 작성
kimhyunso May 27, 2024
4e82123
feat(ValidationCheck): 입력된 값 중 특수문자를 제거하여 x값과 y값을 추출하는 기능 구현
kimhyunso May 27, 2024
c3d1ee7
feat(ValidationCheck): 입력값 중 하나라도 24초과라면 에러발생하는 기능 구현
kimhyunso May 27, 2024
607a9cb
feat(Point): 포인트 두 점 사이의 거리 계산 기능 구현
kimhyunso May 29, 2024
45d87af
feat(InputView): InputView, PointReport 클래스 추가
kimhyunso May 29, 2024
3a51fd9
feat(Application): 어플리케이션 클래스 추가
kimhyunso May 30, 2024
baae40f
feat(Utils): 입력된 값이 숫자가 아닌 문자일 경우 검증하는 기능 추가
kimhyunso May 31, 2024
749d4f3
feat(PointReport): 두 점 사이의 거리 기능 및 UI 구현
kimhyunso May 31, 2024
88c73cc
feat(InputView): UI 수정
kimhyunso May 31, 2024
c0f84bb
git pull mege
kimhyunso Jun 1, 2024
c9aa3c8
feat(Application): 좌표계산기(선길이) 기능 구현
kimhyunso Jun 1, 2024
dbcdd37
전부 삭제
kimhyunso Jun 4, 2024
6cd801b
feat: 입력 문자 중 특수문자를 제거하는 기능 구현
kimhyunso Jun 4, 2024
06e4e6f
feat: 특수문자가 제거된 리스트를 Point 객체로 변경하는 기능
kimhyunso Jun 4, 2024
4a7a982
feat: width, height 구하는 기능
kimhyunso Jun 4, 2024
956c06b
fix: todo 리스트 일부 수정
kimhyunso Jun 4, 2024
17053f3
fix: 일부 객체 들여쓰기 수정
kimhyunso Jun 4, 2024
3ed288b
feat: 사각형 넓이 구하는 기능 추가
kimhyunso Aug 26, 2024
e5a43da
feat: 삼각형 넓이 구하는 기능 추가
kimhyunso Aug 26, 2024
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
27 changes: 27 additions & 0 deletions TODO_MATRIX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
사용자가 점에 대한 좌표 정보를 입력하는 메뉴를 구성한다.
좌표 정보는 괄호"(", ")"로 둘러쌓여 있으며 쉼표(,)로 x값과 y값을 구분한다.
X, Y좌표 모두 최대 24까지만 입력할 수 있다.
입력 범위를 초과할 경우 에러 문구를 출력하고 다시 입력을 받는다.
정상적인 좌표값을 입력한 경우, 해당 좌표에 특수문자를 표시한다.
좌표값을 두 개 입력한 경우, 두 점을 있는 직선으로 가정한다. 좌표값과 좌표값 사이는 '-' 문자로 구분한다.
직선인 경우는 두 점 사이 거리를 계산해서 출력한다.

PointA, PointB

좌표 정보 괄호'()'로 둘러쌓여 있으며 쉼표(,)로 x값과 y값을 구분한다.
좌표값과 좌표값 사이는 '-' 문자로 구분한다.
- [v] PointA(10,10) - PointB(14,15)
- [v] 괄호 및 쉼표 제거 후 x값과 y값을 가져오기
[v] X, Y좌표 모두 최대 24까지만 입력할 수 있다.
- 입력 범위 초과할 경우 에러 문구를 출력하고 다시 입력을 받는다.
- 정상적인 좌표값을 입력한 경우, 해당 좌표에 특수문자를 표시한다.
[v] 직선인 경우는 두 점 사이 거리를 계산해서 출력한다
- 두 점 사이의 거리를 계산한다.

좌표를 입력하세요.
(10,10)-(14,15)

두 점 사이 거리는 제곱근((A.x - B.x)^제곱 + (A.y - B.y)^제곱) 공식으로 계산할 수 있다.
제곱근을 구하는 수학 함수는 Math.sqrt()를 활용한다.
테스트 코드의 경우 double일 때 근사치를 테스트하는 경우가 많다.

40 changes: 40 additions & 0 deletions TODO_RACTANGLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
좌표값을 두 개 입력한 경우, 두 점을 있는 직선으로 가정한다.
좌표값과 좌표값 사이는 '-' 문자로 구분한다.
좌표값을 네 개 입력한 경우, 네 점을 연결하는 사각형으로 가정한다.

네 점이 뒤틀어진 사다리꼴이나 마름모는 제외하고 직사각형만 허용하도록 검사한다.
사각형인 경우 사각형의 넓이를 계산해서 출력한다.

좌표를 입력하세요.
(10,10)-(22,10)-(22,18)-(10,18)

사각형 면적은 width * height 방식으로 계산할 수 있다.
Point라는 객체를 추가해 x, y 좌표를 관리하도록 한다.

1. [v] 좌표값과 좌표값 사이는 '-' 문자로 구분한다. :: [-, (, )] 제거 후
2. [v] Rectangle 클래스에 의존성 주입하기
3. [v] width, height 구하기
## [v] 첫번째 포인트와 두번째 포인트가 width가 됨 또는 세번째 포인트와 네번째 포인트가 width가 됨
## [v] 두번째 포인트와 세번째 포인트가 height가 됨 또는 두번째 포인트와 세번째 포인트가 height가 됨
## [v] Rectangle([Point(x, y), Point(x, y), Point(x, y), Point(x, y)])

2. 네 점이 뒤틀어진 사다리꼴이나 마름모는 제외하고 직사각형(정사각형 포함)만 허용하도록 검사한다.
## Rectangle([Point(x, y), Point(x, y), Point(x, y), Point(x, y)])

## 1번째 방법
1. 첫번째 포인트 y좌표와 두번째 포인트 y좌표가 동일 선상에 있어야한다. Rectangle#isHorizon()
1. 또는 세번째 포인트 y좌표와 네번째 포인트 y좌표가 동일 선상에 있어야한다.
2. 두번째 포인트 x좌표와 세번째 포인트 x좌표가 동일 선상에 있어야한다. Rectangle#isVertical()
2. 또는 세번째 포인트 x좌표와 첫번째 포인트 x좌표가 동일 선상에 있어야한다.

## 2번째 방법 :: 이것이 편해보임
width:(첫번째 포인트 x좌표와 두번째 포인트 x좌표의 차이) 비교 (세번째 포인트 x좌표와 네번째 포인트 x좌표의 차이)
height:(첫번째 포인트 y좌표와 네번째 포인트 y좌표의 차이) 비교 (두번째 포인트 y좌표와 세번째 포인트 y좌표의 차이)

만약, 둘이 같지 않다면 평행하지 않으므로 => 직사각형이 아님!! Rectangle#compare(width, height)

3. 사각형인 경우 사각형의 넓이를 계산해서 출력한다.
## 두번째 포인트에서 첫번재 포인트의 x차이 만큼이 width가 됨 또는 세번째 포인트와 네번째 포인트의 x차이만큼이 width가 됨
## 세번째 포인트에서 두번째 포인트의 y차이 만큼이 height가 됨 또는 네번째 포인트와 첫번째 포인트의 y차이만큼이 height가 됨
## Rectangle([Point(x, y), Point(x, y), Point(x, y), Point(x, y)])

5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ repositories {
}

dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

testCompileOnly 'org.projectlombok:lombok:1.18.12' // 테스트 의존성 추가
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12' // 테스트 의존성 추가
testImplementation "org.junit.jupiter:junit-jupiter:5.7.2"
testImplementation "org.assertj:assertj-core:3.19.0"
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/rectangle/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package rectangle;

import java.util.Objects;

public class Point {
protected final int x;
protected final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int width(Point targetPoint) {
return Math.abs(this.x - targetPoint.x);
}

public int height(Point targetPoint) {
return Math.abs(this.y - targetPoint.y);
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
41 changes: 41 additions & 0 deletions src/main/java/rectangle/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rectangle;

import java.util.ArrayList;
import java.util.List;
public class Rectangle{
private final int width;
private final int height;

private List<Point> points = new ArrayList<>();
private static final int ARRAY_SIZE = 2;

public Rectangle(List<Integer> inputs) {
init(inputs);
this.width = getWith();
this.height = getHeight();
}

public int getArea() {
return width * height;
}

private void init(List<Integer> inputs) {
for (int pointIndex = 0; pointIndex < inputs.size(); pointIndex++) {
points.add(new Point(inputs.get(pointIndex), inputs.get(++pointIndex)));
}
}

private int getWith() {
return points.get(0).width(points.get(1));
}

private int getHeight() {
return points.get(1).height(points.get(3));
}

@Override
public String toString() {
return points.toString();
}

}
35 changes: 35 additions & 0 deletions src/main/java/triangle/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package triangle;

import java.util.Objects;

public class Point {
protected final int x;
protected final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int width(Point targetPoint) {
return Math.abs(this.x - targetPoint.x);
}

public int height(Point targetPoint) {
return Math.abs(this.y - targetPoint.y);
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
36 changes: 36 additions & 0 deletions src/main/java/triangle/Triangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package triangle;

import java.util.ArrayList;
import java.util.List;

public class Triangle {

private List<Point> points = new ArrayList<>();
public Triangle(List<Integer> inputs) {
init(inputs);
}

private void init(List<Integer> inputs) {
for (int pointIndex = 0; pointIndex < inputs.size(); pointIndex++) {
points.add(new Point(inputs.get(pointIndex), inputs.get(++pointIndex)));
}
}

public double getArea() {
int a = points.get(0).height(points.get(1));
int b = points.get(1).height(points.get(2));
int c = points.get(0).width(points.get(2));
int s = s(a, b, c);

return Math.floor(Math.sqrt(s * (s - a) * (s - b) * (s - c)));
}

public int s(int a, int b, int c) {
return (a + b + c) / 2;
}

@Override
public String toString() {
return points.toString();
}
}
20 changes: 20 additions & 0 deletions src/main/java/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Utils {
public static final String REGEX = "[(|,|)|-]";

public static List<Integer> split(String input) {
return Arrays.stream(input.split(REGEX))
.filter(Utils::isNotEmpty)
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
}
private static boolean isNotEmpty(String input){
return !input.isEmpty();
}
}
60 changes: 60 additions & 0 deletions src/test/java/rectangle/RectangleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package rectangle;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.setRemoveAssertJRelatedElementsFromStackTrace;

public class RectangleTest {
private Point pointA;
private Point pointB;
private Point pointC;

private List<Integer> inputs;

@BeforeEach
void setUp() {
pointA = new Point(10, 10);
pointB = new Point(22, 10);
pointC = new Point(22, 18);
inputs = Arrays.asList(10, 10, 22, 10, 22, 18, 10, 18);
}

@Test
@DisplayName("특수문자가 제거된 리스트를 point 객체로 바꾸는 기능")
void 특수문자제거_point객체로변경() {
List<Point> points = Arrays.asList(new Point(10, 10), new Point(22, 10), new Point(22, 18), new Point(10, 18));

Rectangle rectangle = new Rectangle(inputs);
assertThat(points).hasToString(rectangle.toString());
}

@Test
@DisplayName("width 구하는 기능")
void getWidth() {
assertThat(pointA.width(pointB)).isEqualTo(12);
}

@Test
@DisplayName("height 구하는 기능")
void getHeight() {
assertThat(pointB.height(pointC)).isEqualTo(8);
}

@Test
@DisplayName("사각형의 넓이를 구하는 기능")
void getArea() {
Rectangle rectangle = new Rectangle(inputs);
int area = rectangle.getArea();

assertThat(area).isEqualTo(96);
}



}
41 changes: 41 additions & 0 deletions src/test/java/triangle/TriangleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package triangle;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import utils.Utils;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.in;

public class TriangleTest {

private List<Integer> inputs;


@BeforeEach
void setUp() {
inputs = Utils.split("(10,10)-(14,15)-(20,8)");
}

@Test
@DisplayName("특수문자가 제거된 리스트를 point 객체로 바꾸는 기능")
void 특수문자제거_point객체로변경() {
List<Point> points = Arrays.asList(new Point(10, 10), new Point(14, 15), new Point(20, 8));

Triangle triangle = new Triangle(inputs);
assertThat(points).hasToString(triangle.toString());
}

@Test
@DisplayName("삼각형 넓이 구하는 기능")
void getArea() {
Triangle triangle = new Triangle(inputs);
double area = triangle.getArea();
assertThat(area).isEqualTo(16.0);
}

}
20 changes: 20 additions & 0 deletions src/test/java/utils/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.assertj.core.api.AssertionsForClassTypes.*;

public class UtilsTest {

@Test
@DisplayName("입력 문자 중 특수문자를 제거 후 하는 기능")
void 특수문자제거(){
assertThat(Utils.split("(10,10)-(22,10)-(22,18)-(10,18)")).isEqualTo(Arrays.asList(10, 10, 22, 10, 22, 18, 10, 18));
}



}