-
Notifications
You must be signed in to change notification settings - Fork 0
Point, Points 도메인 구현 #2
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
base: feature/fuel-injection
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,51 @@ | ||
# 차량에 따른 연료 주입 실습 | ||
|
||
## 기능 요구사항 | ||
|
||
우리 회사는 렌터카를 운영하고 있다. 현재 보유하고 있는 차량은 Sonata 2대, Avante 1대, K5 2대로 총 5대의 차량을 보유하고 있다. | ||
우리 회사는 고객이 인터넷으로부터 예약할 때 여행할 목적지의 대략적인 이동거리를 입력 받는다. 이 이동거리를 활용해 차량 별로 필요한 연료를 주입한다. | ||
|
||
차량 별로 주입해야 할 연료량을 확인할 수 있는 보고서를 생성해야 한다. | ||
|
||
## 프로그래밍 요구사항 - 1단계 | ||
|
||
상속과 추상 메소드를 활용한다. | ||
위 요구사항을 if/else 절을 쓰지 않고 구현해야 한다. | ||
|
||
## 프로그래밍 요구사항 - 2단계 | ||
인터페이스를 적용해 구현한다. | ||
|
||
인터페이스를 적용해 구현한다. | ||
|
||
# 좌표계산기(선 길이) | ||
|
||
## 기능 요구사항 | ||
|
||
사용자가 점에 대한 좌표 정보를 입력하는 메뉴를 구성한다. | ||
좌표 정보는 괄호"(", ")"로 둘러쌓여 있으며 쉼표(,)로 x값과 y값을 구분한다. | ||
X, Y좌표 모두 최대 24까지만 입력할 수 있다. | ||
입력 범위를 초과할 경우 에러 문구를 출력하고 다시 입력을 받는다. | ||
정상적인 좌표값을 입력한 경우, 해당 좌표에 특수문자를 표시한다. | ||
좌표값을 두 개 입력한 경우, 두 점을 있는 직선으로 가정한다. 좌표값과 좌표값 사이는 '-' 문자로 구분한다. | ||
직선인 경우는 두 점 사이 거리를 계산해서 출력한다. | ||
|
||
## 도메인 | ||
- Points | ||
- Point | ||
- Polygon (추상 클래스) | ||
- Straight | ||
- Triangle | ||
- Square | ||
- Calculatable (인터페이스) | ||
- StraightCalculation | ||
- TriangleCalculation | ||
- SquareCalculation | ||
- PolygonFactory | ||
- CalculationFactory | ||
- CoordinatePrinter | ||
|
||
1. 사용자의 입력을 받아서 Points를 만든다 | ||
2. CalculationFactory한테 Points를 던져서 구체적인 Calculation을 받는다 | ||
3. PolygonFactory한테 Points를 던져서 구체적인 Polygon Type을 받는다 | ||
4. Polygon 2에서 받은 계산 전략을 주입한다. | ||
5. Polygon.calculate() 라는 abstract method를 실행해서 값을 구한다. | ||
6. CoordinatePrinter에 Polygon을 보내서 그린다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.dongko.coordiate.domain; | ||
|
||
import com.dongko.coordiate.domain.exception.IllegalPointException; | ||
|
||
import java.util.Objects; | ||
|
||
public class Point { | ||
private final int x; | ||
private final int y; | ||
public Point(int x, int y) { | ||
if (x < 0 || x > 24) { | ||
throw new IllegalPointException(); | ||
} | ||
if (y < 0 || y > 24) { | ||
throw new IllegalPointException(); | ||
} | ||
this.x = x; | ||
this.y = 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.dongko.coordiate.domain; | ||
|
||
import com.dongko.coordiate.domain.exception.IllegalPointsException; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Points { | ||
private static String regex = "\\((\\d+),(\\d+)\\)-\\((\\d+),(\\d+)\\)"; | ||
|
||
private final List<Point> points; | ||
public Points(String pointsString) { | ||
Pattern pattern = Pattern.compile(regex); | ||
Matcher matcher = pattern.matcher(pointsString); | ||
if (!matcher.matches()) { | ||
throw new IllegalPointsException(); | ||
} | ||
int x1 = Integer.parseInt(matcher.group(1)); | ||
int y1 = Integer.parseInt(matcher.group(2)); | ||
int x2 = Integer.parseInt(matcher.group(3)); | ||
int y2 = Integer.parseInt(matcher.group(4)); | ||
|
||
points = List.of(new Point(x1, y1), new Point(x2, y2)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Points points1 = (Points) o; | ||
return Objects.equals(points, points1.points); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(points); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 인텔리제이 자동 추천 코드인가요? 이런 식으로 추천해주더라구요.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 이거 인덴트가 안먹나 보기 힘드네;;; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인텔리제이 기본 추천 코드고 내부까보니 비슷하게 생겼네용 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hash collision 막기 위해서 상대적으로 작은 소수를 곱하는데 그게 보통 31입니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전 오늘 인텔리제이 라이선스 샀습니당.. 후.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 stub, hashcollision,,, 공부할게 많네여ㅎㅎ |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.dongko.coordiate.domain.exception; | ||
|
||
public class CoordinateCalculatorDomainException extends RuntimeException{ | ||
|
||
public CoordinateCalculatorDomainException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.dongko.coordiate.domain.exception; | ||
|
||
public class IllegalPointException extends CoordinateCalculatorDomainException{ | ||
|
||
public IllegalPointException() { | ||
super("좌표는 0 - 24 사이의 값만 가능합니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.dongko.coordiate.domain.exception; | ||
|
||
public class IllegalPointsException extends CoordinateCalculatorDomainException{ | ||
public IllegalPointsException() { | ||
super("잘못된 좌표 값 입니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.dongko.coordiate.domain; | ||
|
||
import com.dongko.coordiate.domain.Point; | ||
import com.dongko.coordiate.domain.exception.IllegalPointException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PointTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({"25,2", "-1,24", "100,1000", "-100,-10", "24,-2"}) | ||
@DisplayName("좌표값은 음수나 24초과 할 수 없다.") | ||
void test(String xString, String yString) { | ||
int x = Integer.parseInt(xString); | ||
int y = Integer.parseInt(yString); | ||
assertThrows(IllegalPointException.class, () -> | ||
new Point(x, y) | ||
); | ||
} | ||
|
||
@Test | ||
void 객체비교테스트() { | ||
assertEquals(new Point(1,2), new Point(1,2)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.dongko.coordiate.domain; | ||
|
||
import com.dongko.coordiate.domain.exception.IllegalPointsException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PointsTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({"ashjkd", "(10,10,123)-(14,15)", "(10,10)-(14,151,123)", "12379612679"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. valueSource, methodSource말고 csvsource도 있었군요..! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 근데 valueSource랑 어떤 차이가 있나요..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 원래는 csv file에서 테스트 stub을 가져오기 위해 사용하는 어노테이션.. 파일경로 입력가능. 근데 valueSource처럼 쓸 수도 있습니다. methodSource는 첨들어보는데 무엇에 쓰는 물건인고? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지난번 레이싱 경주 코드 중 CarsTest에서 가져왔습니당
|
||
@DisplayName("(x1,y1)-(x2,y2) 형식의 입력 값만 받을 수 있다.") | ||
void test(String pointsString) { | ||
assertThrows(IllegalPointsException.class, () -> | ||
new Points(pointsString) | ||
); | ||
} | ||
|
||
@Test | ||
void 객체비교테스트() { | ||
assertEquals(new Points("(10,10)-(14,15)"), new Points("(10,10)-(14,15)")); | ||
} | ||
|
||
@Test | ||
void 객체비교테스트2() { | ||
assertNotEquals(new Points("(10,10)-(14,15)"), new Points("(10,9)-(14,15)")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체간의 비교는 getter를 사용하는것이 아닌 equals, hashcode를 구현해서 객체간 비교를 한다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흠,,, 객체비교를 해야 하는 군요 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문서가 자세해서 너무 좋습니다ㅎㅎ 덩달아 힌트도 얻어가네요!