From dc84b30ca6104f91873b9f17ffa60a4b3616ae54 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 12:27:54 +0900 Subject: [PATCH 01/44] =?UTF-8?q?feat:=20=EC=B0=A8=EB=9F=89=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=97=B0=EB=A3=8C=20=EC=A3=BC=EC=9E=85=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/car/domain/Avante.java | 16 +++++++++++ src/main/java/car/domain/Car.java | 17 +++++++++++ src/main/java/car/domain/K5.java | 16 +++++++++++ src/main/java/car/domain/RentCompany.java | 27 ++++++++++++++++++ src/main/java/car/domain/Sonata.java | 17 +++++++++++ src/main/java/empty.txt | 0 src/test/java/car/domain/RentCompanyTest.java | 28 +++++++++++++++++++ src/test/java/empty.txt | 0 8 files changed, 121 insertions(+) create mode 100644 src/main/java/car/domain/Avante.java create mode 100644 src/main/java/car/domain/Car.java create mode 100644 src/main/java/car/domain/K5.java create mode 100644 src/main/java/car/domain/RentCompany.java create mode 100644 src/main/java/car/domain/Sonata.java delete mode 100644 src/main/java/empty.txt create mode 100644 src/test/java/car/domain/RentCompanyTest.java delete mode 100644 src/test/java/empty.txt diff --git a/src/main/java/car/domain/Avante.java b/src/main/java/car/domain/Avante.java new file mode 100644 index 00000000..d4cbfc33 --- /dev/null +++ b/src/main/java/car/domain/Avante.java @@ -0,0 +1,16 @@ +package car.domain; + +public class Avante extends Car { + private static final String NAME = "Avante"; + private static final double FUEL_EFFICIENCY = 15; + + public Avante(double distance) { + super.distance = distance; + super.fuelEffiency = FUEL_EFFICIENCY; + } + + @Override + String getName() { + return NAME; + } +} diff --git a/src/main/java/car/domain/Car.java b/src/main/java/car/domain/Car.java new file mode 100644 index 00000000..6b7f537e --- /dev/null +++ b/src/main/java/car/domain/Car.java @@ -0,0 +1,17 @@ +package car.domain; + +public abstract class Car { + double distance; + double fuelEffiency; + /** + * 차종의 이름 + */ + abstract String getName(); + + /** + * 주입해야할 연료량을 구한다. + */ + double getChargeQuantity() { + return distance / fuelEffiency; + } +} diff --git a/src/main/java/car/domain/K5.java b/src/main/java/car/domain/K5.java new file mode 100644 index 00000000..9c7df961 --- /dev/null +++ b/src/main/java/car/domain/K5.java @@ -0,0 +1,16 @@ +package car.domain; + +public class K5 extends Car{ + private static final String NAME = "K5"; + private static final double FUEL_EFFICIENCY = 13; + + public K5(double distance) { + super.distance = distance; + super.fuelEffiency = FUEL_EFFICIENCY; + } + + @Override + String getName() { + return NAME; + } +} diff --git a/src/main/java/car/domain/RentCompany.java b/src/main/java/car/domain/RentCompany.java new file mode 100644 index 00000000..ec075bb7 --- /dev/null +++ b/src/main/java/car/domain/RentCompany.java @@ -0,0 +1,27 @@ +package car.domain; + +import java.util.ArrayList; +import java.util.List; + +public class RentCompany { + private static final String NEWLINE = System.getProperty("line.separator"); + private List cars = new ArrayList<>(); + + private RentCompany() {} + + public static RentCompany create() { + return new RentCompany(); + } + + public void addCar(Car car) { + cars.add(car); + } + + public String generateReport() { + StringBuilder sb = new StringBuilder(); + for (Car car : cars) { + sb.append(car.getName()).append(" : ").append((int)car.getChargeQuantity()).append("리터" + NEWLINE); + } + return sb.toString(); + } +} diff --git a/src/main/java/car/domain/Sonata.java b/src/main/java/car/domain/Sonata.java new file mode 100644 index 00000000..d60f2cb2 --- /dev/null +++ b/src/main/java/car/domain/Sonata.java @@ -0,0 +1,17 @@ +package car.domain; + +public class Sonata extends Car { + private static final String NAME = "Sonata"; + private static final double FUEL_EFFICIENCY = 10; + + public Sonata(double distance) { + super.distance = distance; + super.fuelEffiency = FUEL_EFFICIENCY; + } + + @Override + String getName() { + return NAME; + } + +} diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/test/java/car/domain/RentCompanyTest.java b/src/test/java/car/domain/RentCompanyTest.java new file mode 100644 index 00000000..555a3a9c --- /dev/null +++ b/src/test/java/car/domain/RentCompanyTest.java @@ -0,0 +1,28 @@ +package car.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RentCompanyTest { + private static final String NEWLINE = System.getProperty("line.separator"); + + @Test + public void report() throws Exception { + RentCompany company = RentCompany.create(); // factory method를 사용해 생성 + company.addCar(new Sonata(150)); + company.addCar(new K5(260)); + company.addCar(new Sonata(120)); + company.addCar(new Avante(300)); + company.addCar(new K5(390)); + + String report = company.generateReport(); + assertThat(report).isEqualTo( + "Sonata : 15리터" + NEWLINE + + "K5 : 20리터" + NEWLINE + + "Sonata : 12리터" + NEWLINE + + "Avante : 20리터" + NEWLINE + + "K5 : 30리터" + NEWLINE + ); + } +} diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29b..00000000 From a3204db2a2a8d77cced8bc8035b0d240aa6265fc Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 12:28:42 +0900 Subject: [PATCH 02/44] =?UTF-8?q?style:=20=EC=BD=94=EB=93=9C=20=ED=8F=AC?= =?UTF-8?q?=EB=A9=94=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/car/domain/Car.java | 5 +++-- src/main/java/car/domain/K5.java | 2 +- src/main/java/car/domain/RentCompany.java | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/car/domain/Car.java b/src/main/java/car/domain/Car.java index 6b7f537e..b8b05035 100644 --- a/src/main/java/car/domain/Car.java +++ b/src/main/java/car/domain/Car.java @@ -1,8 +1,9 @@ package car.domain; public abstract class Car { - double distance; - double fuelEffiency; + double distance; + double fuelEffiency; + /** * 차종의 이름 */ diff --git a/src/main/java/car/domain/K5.java b/src/main/java/car/domain/K5.java index 9c7df961..6ab8b84f 100644 --- a/src/main/java/car/domain/K5.java +++ b/src/main/java/car/domain/K5.java @@ -1,6 +1,6 @@ package car.domain; -public class K5 extends Car{ +public class K5 extends Car { private static final String NAME = "K5"; private static final double FUEL_EFFICIENCY = 13; diff --git a/src/main/java/car/domain/RentCompany.java b/src/main/java/car/domain/RentCompany.java index ec075bb7..cfb9fe2b 100644 --- a/src/main/java/car/domain/RentCompany.java +++ b/src/main/java/car/domain/RentCompany.java @@ -7,7 +7,8 @@ public class RentCompany { private static final String NEWLINE = System.getProperty("line.separator"); private List cars = new ArrayList<>(); - private RentCompany() {} + private RentCompany() { + } public static RentCompany create() { return new RentCompany(); @@ -20,7 +21,7 @@ public void addCar(Car car) { public String generateReport() { StringBuilder sb = new StringBuilder(); for (Car car : cars) { - sb.append(car.getName()).append(" : ").append((int)car.getChargeQuantity()).append("리터" + NEWLINE); + sb.append(car.getName()).append(" : ").append((int) car.getChargeQuantity()).append("리터" + NEWLINE); } return sb.toString(); } From f6e439e6e8d9b75b60b1b857c541dd0dc399b865 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 14:56:15 +0900 Subject: [PATCH 03/44] =?UTF-8?q?feat:=20=EC=A2=8C=ED=91=9C=20X.class,=20Y?= =?UTF-8?q?.class=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++++++-- src/main/java/coordinate/domain/X.java | 27 ++++++++++++++++++++++ src/main/java/coordinate/domain/Y.java | 27 ++++++++++++++++++++++ src/test/java/coordinate/domain/XTest.java | 18 +++++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/main/java/coordinate/domain/X.java create mode 100644 src/main/java/coordinate/domain/Y.java create mode 100644 src/test/java/coordinate/domain/XTest.java diff --git a/README.md b/README.md index fe80152b..b12c6123 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,21 @@ # java-coordinate 좌표계산기 미션을 진행하기 위한 저장소 -## 우아한테크코스 코드리뷰 -* [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) +## 1단계 + +### - 요구사항 + + 사용자가 점에 대한 좌표 정보를 입력하는 메뉴를 구성한다. + + 좌표 정보는 괄호"(", ")"로 둘러쌓여 있으며 쉼표(,)로 x값과 y값을 구분한다. + + X, Y좌표 모두 최대 24까지만 입력할 수 있다. + + 입력 범위를 초과할 경우 에러 문구를 출력하고 다시 입력을 받는다. + + 정상적인 좌표값을 입력한 경우, 해당 좌표에 특수문자를 표시한다. + + 좌표값을 두 개 입력한 경우, 두 점을 있는 직선으로 가정한다. 좌표값과 좌표값 사이는 '-' 문자로 구분한다. + + 직선인 경우는 두 점 사이 거리를 계산해서 출력한다. + +### - 필요한 기능 + 1. 좌표 + - 0 ~ 24 사이의 값을 가지는지 + - ~~null 검사~~ + 2. 점 + + \ No newline at end of file diff --git a/src/main/java/coordinate/domain/X.java b/src/main/java/coordinate/domain/X.java new file mode 100644 index 00000000..d70130c0 --- /dev/null +++ b/src/main/java/coordinate/domain/X.java @@ -0,0 +1,27 @@ +package coordinate.domain; + +import java.util.Objects; + +public class X { + private final int coordinate; + + public X(final int coordinate) { + if (coordinate < 0 || coordinate > 24) { + throw new IllegalArgumentException(); + } + this.coordinate = coordinate; + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final X x = (X) o; + return coordinate == x.coordinate; + } + + @Override + public int hashCode() { + return Objects.hash(coordinate); + } +} diff --git a/src/main/java/coordinate/domain/Y.java b/src/main/java/coordinate/domain/Y.java new file mode 100644 index 00000000..562e9969 --- /dev/null +++ b/src/main/java/coordinate/domain/Y.java @@ -0,0 +1,27 @@ +package coordinate.domain; + +import java.util.Objects; + +public class Y { + private final int coordinate; + + public Y(final int coordinate) { + if (coordinate < 0 || coordinate > 24) { + throw new IllegalArgumentException(); + } + this.coordinate = coordinate; + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Y y = (Y) o; + return coordinate == y.coordinate; + } + + @Override + public int hashCode() { + return Objects.hash(coordinate); + } +} diff --git a/src/test/java/coordinate/domain/XTest.java b/src/test/java/coordinate/domain/XTest.java new file mode 100644 index 00000000..1220814f --- /dev/null +++ b/src/test/java/coordinate/domain/XTest.java @@ -0,0 +1,18 @@ +package coordinate.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class XTest { + @Test + void 생성() { + assertThat(new X(10)).isEqualTo(new X(10)); + } + + @Test + void 숫자_범위_테스트() { + assertThrows(IllegalArgumentException.class, () -> new X(25)); + } +} From 8f49171f108096d6d527538a3b5e5821fc49a966 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 15:04:06 +0900 Subject: [PATCH 04/44] =?UTF-8?q?feat:=20Point.class=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++-- src/main/java/coordinate/domain/Point.java | 27 +++++++++++++++++++ .../java/coordinate/domain/PointTest.java | 12 +++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/main/java/coordinate/domain/Point.java create mode 100644 src/test/java/coordinate/domain/PointTest.java diff --git a/README.md b/README.md index b12c6123..73c7d892 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ + 직선인 경우는 두 점 사이 거리를 계산해서 출력한다. ### - 필요한 기능 - 1. 좌표 + 1. 좌표 (X, Y) - 0 ~ 24 사이의 값을 가지는지 - ~~null 검사~~ - 2. 점 + 2. 점 (Point) + \ No newline at end of file diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java new file mode 100644 index 00000000..b0b8f3cf --- /dev/null +++ b/src/main/java/coordinate/domain/Point.java @@ -0,0 +1,27 @@ +package coordinate.domain; + +import java.util.Objects; + +public class Point { + private final X x; + private final Y y; + + public Point(final int x, final int y) { + this.x = new X(x); + this.y = new Y(y); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Point point = (Point) o; + return Objects.equals(x, point.x) && + Objects.equals(y, point.y); + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } +} diff --git a/src/test/java/coordinate/domain/PointTest.java b/src/test/java/coordinate/domain/PointTest.java new file mode 100644 index 00000000..2f15f3a1 --- /dev/null +++ b/src/test/java/coordinate/domain/PointTest.java @@ -0,0 +1,12 @@ +package coordinate.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PointTest { + @Test + void 생성() { + assertThat(new Point(10, 10)).isEqualTo(new Point(10, 10)); + } +} From b5e02d7b3b3b69e85674f316fcc9fd216c5c45c9 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 15:44:40 +0900 Subject: [PATCH 05/44] =?UTF-8?q?feat:=20X.class,=20Y.class=20getter=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/coordinate/domain/X.java | 4 ++++ src/main/java/coordinate/domain/Y.java | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 73c7d892..1b0f4053 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - 0 ~ 24 사이의 값을 가지는지 - ~~null 검사~~ 2. 점 (Point) - + 3. 선 (Line) + - 거리 계산 \ No newline at end of file diff --git a/src/main/java/coordinate/domain/X.java b/src/main/java/coordinate/domain/X.java index d70130c0..dd4e38ca 100644 --- a/src/main/java/coordinate/domain/X.java +++ b/src/main/java/coordinate/domain/X.java @@ -12,6 +12,10 @@ public X(final int coordinate) { this.coordinate = coordinate; } + public int getX() { + return coordinate; + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/src/main/java/coordinate/domain/Y.java b/src/main/java/coordinate/domain/Y.java index 562e9969..feadc03f 100644 --- a/src/main/java/coordinate/domain/Y.java +++ b/src/main/java/coordinate/domain/Y.java @@ -12,6 +12,10 @@ public Y(final int coordinate) { this.coordinate = coordinate; } + public int getY() { + return coordinate; + } + @Override public boolean equals(final Object o) { if (this == o) return true; From 3e670f5fa212e013d4562a82d77963a9c50baf35 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 15:45:03 +0900 Subject: [PATCH 06/44] =?UTF-8?q?feat:=20Line.class=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 거리 계산 구현 --- src/main/java/coordinate/domain/Line.java | 17 +++++++++++++++++ src/main/java/coordinate/domain/Point.java | 10 ++++++++++ src/test/java/coordinate/domain/LineTest.java | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/main/java/coordinate/domain/Line.java create mode 100644 src/test/java/coordinate/domain/LineTest.java diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java new file mode 100644 index 00000000..0abf9e1f --- /dev/null +++ b/src/main/java/coordinate/domain/Line.java @@ -0,0 +1,17 @@ +package coordinate.domain; + +import java.util.List; + +public class Line { + private final List points; + + public Line(List points) { + this.points = points; + } + + public double calculateLineLength() { + Point a = points.get(0); + Point b = points.get(1); + return a.length(b); + } +} diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java index b0b8f3cf..3bf986a6 100644 --- a/src/main/java/coordinate/domain/Point.java +++ b/src/main/java/coordinate/domain/Point.java @@ -11,6 +11,16 @@ public Point(final int x, final int y) { this.y = new Y(y); } + public double length(final Point other) { + double squareOfX = square(this.x.getX(), other.x.getX()); + double squareOfY = square(this.y.getY(), other.y.getY()); + return Math.sqrt(squareOfX + squareOfY); + } + + private double square(final int first, final int second) { + return Math.pow(first - second, 2); + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/src/test/java/coordinate/domain/LineTest.java b/src/test/java/coordinate/domain/LineTest.java new file mode 100644 index 00000000..937810d5 --- /dev/null +++ b/src/test/java/coordinate/domain/LineTest.java @@ -0,0 +1,18 @@ +package coordinate.domain; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Java6Assertions.offset; + +public class LineTest { + + @Test + void 거리_계산() { + double expect = Math.sqrt(8); + assertThat(new Line(Arrays.asList(new Point(0, 0), new Point(2, 2))).calculateLineLength()) + .isEqualTo(expect, offset(0.00099)); + } +} From c5f737ac7f0912d59c706b129c5367b406a7eee0 Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 16:00:06 +0900 Subject: [PATCH 07/44] =?UTF-8?q?feat:=20InputView.class=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 좌표 입력 기능 --- src/main/java/coordinate/view/InputView.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/coordinate/view/InputView.java diff --git a/src/main/java/coordinate/view/InputView.java b/src/main/java/coordinate/view/InputView.java new file mode 100644 index 00000000..efbbed31 --- /dev/null +++ b/src/main/java/coordinate/view/InputView.java @@ -0,0 +1,12 @@ +package coordinate.view; + +import java.util.Scanner; + +public class InputView { + private static Scanner scanner = new Scanner(System.in); + + public static String inputCooridinate() { + System.out.println("좌표를 입력하세요."); + return scanner.nextLine(); + } +} From 8106fc2ada37d21aba90e634d9e8fb4f83cd10da Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 16:32:21 +0900 Subject: [PATCH 08/44] =?UTF-8?q?feat:=20PointsGenerator.class=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - input 유효성 검사 - input 값을 통해 List 생성 --- .../domain/generator/PointsGenerator.java | 34 +++++++++++++++++++ .../domain/generator/PointsGeneratorTest.java | 23 +++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/coordinate/domain/generator/PointsGenerator.java create mode 100644 src/test/java/coordinate/domain/generator/PointsGeneratorTest.java diff --git a/src/main/java/coordinate/domain/generator/PointsGenerator.java b/src/main/java/coordinate/domain/generator/PointsGenerator.java new file mode 100644 index 00000000..59197c2b --- /dev/null +++ b/src/main/java/coordinate/domain/generator/PointsGenerator.java @@ -0,0 +1,34 @@ +package coordinate.domain.generator; + +import coordinate.domain.Point; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class PointsGenerator { + private static final String REGEX = "\\([0-9]{1,2},[0-9]{1,2}\\)-\\([0-9]{1,2},[0-9]{1,2}\\)"; + private static final String DELIMITER = "-"; + + private final String[] coordinates; + + public PointsGenerator(String coordinates) { + if (!coordinates.matches(REGEX)) { + throw new IllegalArgumentException(); + } + this.coordinates = Arrays.stream(coordinates.split(DELIMITER)) + .map(s -> s.substring(1, s.length() - 1)) + .toArray(String[]::new); + } + + public List generate() { + List points = new ArrayList<>(); + for (String coordinate : coordinates) { + int index = coordinate.indexOf(","); + int x = Integer.parseInt(coordinate.substring(0, index)); + int y = Integer.parseInt(coordinate.substring(index+1)); + points.add(new Point(x, y)); + } + return points; + } +} diff --git a/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java new file mode 100644 index 00000000..768ad4df --- /dev/null +++ b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java @@ -0,0 +1,23 @@ +package coordinate.domain.generator; + +import coordinate.domain.Point; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class PointsGeneratorTest { + + @Test + void 대쉬_없는_경우_테스트() { + assertThrows(IllegalArgumentException.class, () -> new PointsGenerator("(1,2)(3,4)")); + } + + @Test + void generate_테스트() { + assertThat(new PointsGenerator("(1,2)-(3,4)").generate()) + .isEqualTo(Arrays.asList(new Point(1, 2), new Point(3, 4))); + } +} \ No newline at end of file From 3297d78fdd47db87042ec24de483a351bb3e204e Mon Sep 17 00:00:00 2001 From: pkch93 Date: Tue, 21 May 2019 17:51:36 +0900 Subject: [PATCH 09/44] =?UTF-8?q?feat:=20Square=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 점이 4개인지 예외처리 - 직사각형인지 여부 예외처리 - 중복된 점 예외처리 --- README.md | 20 ++++++- src/main/java/coordinate/CoordinateApp.java | 10 ++++ src/main/java/coordinate/domain/Point.java | 12 ++++- src/main/java/coordinate/domain/Square.java | 52 +++++++++++++++++++ src/main/java/coordinate/domain/X.java | 2 +- src/main/java/coordinate/domain/Y.java | 2 +- .../java/coordinate/domain/SquareTest.java | 34 ++++++++++++ 7 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 src/main/java/coordinate/CoordinateApp.java create mode 100644 src/main/java/coordinate/domain/Square.java create mode 100644 src/test/java/coordinate/domain/SquareTest.java diff --git a/README.md b/README.md index 1b0f4053..615cfb7e 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,24 @@ 1. 좌표 (X, Y) - 0 ~ 24 사이의 값을 가지는지 - ~~null 검사~~ + 파라미터가 원시타입이므로 null 검사가 필요없다고 판단. 2. 점 (Point) 3. 선 (Line) - - 거리 계산 - + - 거리 계산 + +## 2단계 + +### - 요구사항 + + 좌표값을 두 개 입력한 경우, 두 점을 있는 직선으로 가정한다. 좌표값과 좌표값 사이는 '-' 문자로 구분한다. + + 좌표값을 네 개 입력한 경우, 네 점을 연결하는 사각형으로 가정한다. + + 네 점이 뒤틀어진 사다리꼴이나 마름모는 제외하고 직사각형만 허용하도록 검사한다. + + 사각형인 경우 사각형의 넓이를 계산해서 출력한다 + +### - 필요한 기능 + 1. 사각형 + - 중복된 Point가 있는지 여부 + - 직사각형 여부 + - Point가 4개인지 + - null 검사 + - 넓이 계산 \ No newline at end of file diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java new file mode 100644 index 00000000..59cad4eb --- /dev/null +++ b/src/main/java/coordinate/CoordinateApp.java @@ -0,0 +1,10 @@ +package coordinate; + +import coordinate.view.InputView; + +public class CoordinateApp { + + public static void main(String[] args) { + InputView.inputCooridinate(); + } +} diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java index 3bf986a6..214cb56d 100644 --- a/src/main/java/coordinate/domain/Point.java +++ b/src/main/java/coordinate/domain/Point.java @@ -12,8 +12,8 @@ public Point(final int x, final int y) { } public double length(final Point other) { - double squareOfX = square(this.x.getX(), other.x.getX()); - double squareOfY = square(this.y.getY(), other.y.getY()); + double squareOfX = square(this.x.value(), other.x.value()); + double squareOfY = square(this.y.value(), other.y.value()); return Math.sqrt(squareOfX + squareOfY); } @@ -21,6 +21,14 @@ private double square(final int first, final int second) { return Math.pow(first - second, 2); } + public boolean isMatchX(final Point point) { + return this.x.value() == point.x.value(); + } + + public boolean isMatchY(final Point point) { + return this.y.value() == point.y.value(); + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java new file mode 100644 index 00000000..471c1edf --- /dev/null +++ b/src/main/java/coordinate/domain/Square.java @@ -0,0 +1,52 @@ +package coordinate.domain; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class Square { + + private final List points; + + public Square(final List points) { + this.points = new ArrayList<>(points); + validate(); + } + + private void validate() { + validateSize(); + validateDuplication(); + validateRectangle(); + } + + private void validateRectangle() { + // todo: 개선점 생각하기 + int cnt = 0; + for (int i = 0; i < points.size(); i++) { + for (int j = 0; j < points.size(); j++) { + if (i == j) continue; + if (points.get(i).isMatchX(points.get(j))){ + cnt++; + } + if (points.get(i).isMatchY(points.get(j))) { + cnt++; + } + } + } + if (cnt != 8) { + throw new IllegalArgumentException("직사각형이 아닙니다."); + } + } + + private void validateSize() { + if (this.points.size() != 4) { + throw new IllegalArgumentException("점이 4개가 아닙니다."); + } + } + + private void validateDuplication() { + if (new HashSet<>(this.points).size() != 4) { + throw new IllegalArgumentException("중복된 점이 있습니다."); + } + } +} diff --git a/src/main/java/coordinate/domain/X.java b/src/main/java/coordinate/domain/X.java index dd4e38ca..e9926ce0 100644 --- a/src/main/java/coordinate/domain/X.java +++ b/src/main/java/coordinate/domain/X.java @@ -12,7 +12,7 @@ public X(final int coordinate) { this.coordinate = coordinate; } - public int getX() { + public int value() { return coordinate; } diff --git a/src/main/java/coordinate/domain/Y.java b/src/main/java/coordinate/domain/Y.java index feadc03f..bbc0cda2 100644 --- a/src/main/java/coordinate/domain/Y.java +++ b/src/main/java/coordinate/domain/Y.java @@ -12,7 +12,7 @@ public Y(final int coordinate) { this.coordinate = coordinate; } - public int getY() { + public int value() { return coordinate; } diff --git a/src/test/java/coordinate/domain/SquareTest.java b/src/test/java/coordinate/domain/SquareTest.java new file mode 100644 index 00000000..50893811 --- /dev/null +++ b/src/test/java/coordinate/domain/SquareTest.java @@ -0,0 +1,34 @@ +package coordinate.domain; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class SquareTest { + @Test + void null_테스트() { + assertThrows(NullPointerException.class, () -> new Square(null)); + } + + @Test + void 사각형_점이_4개인지_테스트() { + assertThrows(IllegalArgumentException.class, () -> + new Square(Arrays.asList(new Point(1,2)))); + } + + @Test + void 중복된_점이_있는_경우_테스트() { + assertThrows(IllegalArgumentException.class, () -> + new Square(Arrays.asList(new Point(1,2), new Point(1, 2), + new Point(2, 3), new Point(3,4)))); + } + + @Test + void 직사각형_여부_테스트() { + assertThrows(IllegalArgumentException.class, () -> + new Square(Arrays.asList(new Point(1,2), new Point(4,5), + new Point(2, 3), new Point(3,4)))); + } +} From 8963e328a7ff29042e122952a7a8b6bc500fed3e Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 10:46:08 +0900 Subject: [PATCH 10/44] =?UTF-8?q?refactor:=20X.class,=20Y.class=20->=20Coo?= =?UTF-8?q?rdinate.class=20=EB=A1=9C=20=ED=95=A9=EC=B9=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 같은 기능을 하는 X,Y를 Coordinate로 합침 --- .../domain/{X.java => Coordinate.java} | 17 +++++++--- src/main/java/coordinate/domain/Point.java | 22 +++++-------- src/main/java/coordinate/domain/Y.java | 31 ------------------- .../{XTest.java => CoordinateTest.java} | 6 ++-- 4 files changed, 23 insertions(+), 53 deletions(-) rename src/main/java/coordinate/domain/{X.java => Coordinate.java} (53%) delete mode 100644 src/main/java/coordinate/domain/Y.java rename src/test/java/coordinate/domain/{XTest.java => CoordinateTest.java} (60%) diff --git a/src/main/java/coordinate/domain/X.java b/src/main/java/coordinate/domain/Coordinate.java similarity index 53% rename from src/main/java/coordinate/domain/X.java rename to src/main/java/coordinate/domain/Coordinate.java index e9926ce0..bd3efb1e 100644 --- a/src/main/java/coordinate/domain/X.java +++ b/src/main/java/coordinate/domain/Coordinate.java @@ -2,16 +2,23 @@ import java.util.Objects; -public class X { +public class Coordinate { + private static final int MAX_BOUND = 24; + private static final int MIN_BOUND = 0; + private final int coordinate; - public X(final int coordinate) { - if (coordinate < 0 || coordinate > 24) { + private Coordinate(final int coordinate) { + if (coordinate < MIN_BOUND || coordinate > MAX_BOUND) { throw new IllegalArgumentException(); } this.coordinate = coordinate; } + public static Coordinate of(final int coordinate) { + return new Coordinate(coordinate); + } + public int value() { return coordinate; } @@ -20,8 +27,8 @@ public int value() { public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - final X x = (X) o; - return coordinate == x.coordinate; + final Coordinate coordinate = (Coordinate) o; + return this.coordinate == coordinate.coordinate; } @Override diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java index 214cb56d..7775af3e 100644 --- a/src/main/java/coordinate/domain/Point.java +++ b/src/main/java/coordinate/domain/Point.java @@ -1,14 +1,12 @@ package coordinate.domain; -import java.util.Objects; - public class Point { - private final X x; - private final Y y; + private final Coordinate x; + private final Coordinate y; public Point(final int x, final int y) { - this.x = new X(x); - this.y = new Y(y); + this.x = Coordinate.of(x); + this.y = Coordinate.of(y); } public double length(final Point other) { @@ -30,16 +28,12 @@ public boolean isMatchY(final Point point) { } @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final Point point = (Point) o; - return Objects.equals(x, point.x) && - Objects.equals(y, point.y); + public int hashCode() { + return super.hashCode(); } @Override - public int hashCode() { - return Objects.hash(x, y); + public boolean equals(final Object obj) { + return super.equals(obj); } } diff --git a/src/main/java/coordinate/domain/Y.java b/src/main/java/coordinate/domain/Y.java deleted file mode 100644 index bbc0cda2..00000000 --- a/src/main/java/coordinate/domain/Y.java +++ /dev/null @@ -1,31 +0,0 @@ -package coordinate.domain; - -import java.util.Objects; - -public class Y { - private final int coordinate; - - public Y(final int coordinate) { - if (coordinate < 0 || coordinate > 24) { - throw new IllegalArgumentException(); - } - this.coordinate = coordinate; - } - - public int value() { - return coordinate; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final Y y = (Y) o; - return coordinate == y.coordinate; - } - - @Override - public int hashCode() { - return Objects.hash(coordinate); - } -} diff --git a/src/test/java/coordinate/domain/XTest.java b/src/test/java/coordinate/domain/CoordinateTest.java similarity index 60% rename from src/test/java/coordinate/domain/XTest.java rename to src/test/java/coordinate/domain/CoordinateTest.java index 1220814f..b6050e05 100644 --- a/src/test/java/coordinate/domain/XTest.java +++ b/src/test/java/coordinate/domain/CoordinateTest.java @@ -5,14 +5,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -public class XTest { +public class CoordinateTest { @Test void 생성() { - assertThat(new X(10)).isEqualTo(new X(10)); + assertThat(Coordinate.of(10)).isEqualTo(Coordinate.of(10)); } @Test void 숫자_범위_테스트() { - assertThrows(IllegalArgumentException.class, () -> new X(25)); + assertThrows(IllegalArgumentException.class, () -> Coordinate.of(25)); } } From 9a2c9526e13648f5977b33ce5b54813623d56531 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 10:47:31 +0900 Subject: [PATCH 11/44] =?UTF-8?q?refactor:=20Line.calculateLineLength()=20?= =?UTF-8?q?->=20length()=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Line.java | 2 +- src/test/java/coordinate/domain/LineTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java index 0abf9e1f..f953379e 100644 --- a/src/main/java/coordinate/domain/Line.java +++ b/src/main/java/coordinate/domain/Line.java @@ -9,7 +9,7 @@ public Line(List points) { this.points = points; } - public double calculateLineLength() { + public double length() { Point a = points.get(0); Point b = points.get(1); return a.length(b); diff --git a/src/test/java/coordinate/domain/LineTest.java b/src/test/java/coordinate/domain/LineTest.java index 937810d5..32b45b2a 100644 --- a/src/test/java/coordinate/domain/LineTest.java +++ b/src/test/java/coordinate/domain/LineTest.java @@ -12,7 +12,7 @@ public class LineTest { @Test void 거리_계산() { double expect = Math.sqrt(8); - assertThat(new Line(Arrays.asList(new Point(0, 0), new Point(2, 2))).calculateLineLength()) + assertThat(new Line(Arrays.asList(new Point(0, 0), new Point(2, 2))).length()) .isEqualTo(expect, offset(0.00099)); } } From c69454144b49cfb0eb81a8de5278bb889972c0ff Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 11:14:14 +0900 Subject: [PATCH 12/44] =?UTF-8?q?refactor:=20Points.class=20Point=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=89=EC=BB=AC=EB=A0=89=EC=85=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Line.java | 14 ++++-- src/main/java/coordinate/domain/Points.java | 43 +++++++++++++++++++ src/main/java/coordinate/domain/Square.java | 16 ++----- .../domain/generator/PointsGenerator.java | 6 ++- src/test/java/coordinate/domain/LineTest.java | 11 ++++- .../java/coordinate/domain/PointsTest.java | 22 ++++++++++ .../java/coordinate/domain/SquareTest.java | 13 ++---- 7 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 src/main/java/coordinate/domain/Points.java create mode 100644 src/test/java/coordinate/domain/PointsTest.java diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java index f953379e..9892dea9 100644 --- a/src/main/java/coordinate/domain/Line.java +++ b/src/main/java/coordinate/domain/Line.java @@ -1,12 +1,20 @@ package coordinate.domain; -import java.util.List; public class Line { - private final List points; + private static final int NUMBER_OF_POINTS = 2; - public Line(List points) { + private final Points points; + + public Line(Points points) { this.points = points; + validateSize(points); + } + + private void validateSize(final Points points) { + if (points.size() != NUMBER_OF_POINTS) { + throw new IllegalArgumentException("선은 점이 두개 필요합니다."); + } } public double length() { diff --git a/src/main/java/coordinate/domain/Points.java b/src/main/java/coordinate/domain/Points.java new file mode 100644 index 00000000..83be9e7a --- /dev/null +++ b/src/main/java/coordinate/domain/Points.java @@ -0,0 +1,43 @@ +package coordinate.domain; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class Points { + private final List points; + + private Points(final List points) { + this.points = new ArrayList<>(points); + validate(this.points); + } + + public static Points of(final List points) { + return new Points(points); + } + + private void validate(final List points) { + validateEmpty(points); + validateDuplication(points); + } + + private void validateEmpty(final List points) { + if (points.isEmpty()) { + throw new IllegalArgumentException("점이 없습니다."); + } + } + + private void validateDuplication(final List points) { + if (new HashSet<>(points).size() != points.size()) { + throw new IllegalArgumentException("중복되는 점은 있으면 안됩니다."); + } + } + + public Point get(final int index) { + return points.get(index); + } + + public int size() { + return points.size(); + } +} diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index 471c1edf..73ea7608 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -1,21 +1,16 @@ package coordinate.domain; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; - public class Square { - private final List points; + private final Points points; - public Square(final List points) { - this.points = new ArrayList<>(points); + public Square(final Points points) { + this.points = points; validate(); } private void validate() { validateSize(); - validateDuplication(); validateRectangle(); } @@ -44,9 +39,4 @@ private void validateSize() { } } - private void validateDuplication() { - if (new HashSet<>(this.points).size() != 4) { - throw new IllegalArgumentException("중복된 점이 있습니다."); - } - } } diff --git a/src/main/java/coordinate/domain/generator/PointsGenerator.java b/src/main/java/coordinate/domain/generator/PointsGenerator.java index 59197c2b..1cefaa0a 100644 --- a/src/main/java/coordinate/domain/generator/PointsGenerator.java +++ b/src/main/java/coordinate/domain/generator/PointsGenerator.java @@ -1,12 +1,14 @@ package coordinate.domain.generator; import coordinate.domain.Point; +import coordinate.domain.Points; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class PointsGenerator { + //TODO 정규식 수정 private static final String REGEX = "\\([0-9]{1,2},[0-9]{1,2}\\)-\\([0-9]{1,2},[0-9]{1,2}\\)"; private static final String DELIMITER = "-"; @@ -21,7 +23,7 @@ public PointsGenerator(String coordinates) { .toArray(String[]::new); } - public List generate() { + public Points generate() { List points = new ArrayList<>(); for (String coordinate : coordinates) { int index = coordinate.indexOf(","); @@ -29,6 +31,6 @@ public List generate() { int y = Integer.parseInt(coordinate.substring(index+1)); points.add(new Point(x, y)); } - return points; + return Points.of(points); } } diff --git a/src/test/java/coordinate/domain/LineTest.java b/src/test/java/coordinate/domain/LineTest.java index 32b45b2a..1a421f1b 100644 --- a/src/test/java/coordinate/domain/LineTest.java +++ b/src/test/java/coordinate/domain/LineTest.java @@ -1,18 +1,25 @@ package coordinate.domain; +import coordinate.domain.generator.PointsGenerator; import org.junit.jupiter.api.Test; import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Java6Assertions.offset; +import static org.junit.jupiter.api.Assertions.assertThrows; public class LineTest { @Test - void 거리_계산() { + void 거리_테스트() { double expect = Math.sqrt(8); - assertThat(new Line(Arrays.asList(new Point(0, 0), new Point(2, 2))).length()) + assertThat(new Line(new PointsGenerator("(0,0)-(2,2)").generate()).length()) .isEqualTo(expect, offset(0.00099)); } + + @Test + void 사이즈_테스트() { + assertThrows(IllegalArgumentException.class, () -> new Line(new PointsGenerator("(0,0)").generate())); + } } diff --git a/src/test/java/coordinate/domain/PointsTest.java b/src/test/java/coordinate/domain/PointsTest.java new file mode 100644 index 00000000..509f8c6b --- /dev/null +++ b/src/test/java/coordinate/domain/PointsTest.java @@ -0,0 +1,22 @@ +package coordinate.domain; + +import coordinate.domain.generator.PointsGenerator; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PointsTest { + @Test + void 중복_테스트() { + assertThrows(IllegalArgumentException.class, () -> { + Points points = new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,10)").generate(); + }); + } + + @Test + void 점이_없을때_테스트() { + assertThrows(IllegalArgumentException.class, () -> { + Points points = new PointsGenerator("").generate(); + }); + } +} diff --git a/src/test/java/coordinate/domain/SquareTest.java b/src/test/java/coordinate/domain/SquareTest.java index 50893811..36a4932f 100644 --- a/src/test/java/coordinate/domain/SquareTest.java +++ b/src/test/java/coordinate/domain/SquareTest.java @@ -1,5 +1,6 @@ package coordinate.domain; +import coordinate.domain.generator.PointsGenerator; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -15,20 +16,12 @@ public class SquareTest { @Test void 사각형_점이_4개인지_테스트() { assertThrows(IllegalArgumentException.class, () -> - new Square(Arrays.asList(new Point(1,2)))); - } - - @Test - void 중복된_점이_있는_경우_테스트() { - assertThrows(IllegalArgumentException.class, () -> - new Square(Arrays.asList(new Point(1,2), new Point(1, 2), - new Point(2, 3), new Point(3,4)))); + new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)").generate())); } @Test void 직사각형_여부_테스트() { assertThrows(IllegalArgumentException.class, () -> - new Square(Arrays.asList(new Point(1,2), new Point(4,5), - new Point(2, 3), new Point(3,4)))); + new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,1000)").generate())); } } From f8268595ba02795c06559e87d66416fbc966c065 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 11:19:07 +0900 Subject: [PATCH 13/44] =?UTF-8?q?refactor:=20PointsGenerator.class=20?= =?UTF-8?q?=EC=A0=95=EA=B7=9C=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/coordinate/domain/generator/PointsGenerator.java | 3 +-- .../coordinate/domain/generator/PointsGeneratorTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/coordinate/domain/generator/PointsGenerator.java b/src/main/java/coordinate/domain/generator/PointsGenerator.java index 1cefaa0a..3e487738 100644 --- a/src/main/java/coordinate/domain/generator/PointsGenerator.java +++ b/src/main/java/coordinate/domain/generator/PointsGenerator.java @@ -8,8 +8,7 @@ import java.util.List; public class PointsGenerator { - //TODO 정규식 수정 - private static final String REGEX = "\\([0-9]{1,2},[0-9]{1,2}\\)-\\([0-9]{1,2},[0-9]{1,2}\\)"; + private static final String REGEX = "(\\([0-9]{1,2},[0-9]{1,2}\\)-)+\\([0-9]{1,2},[0-9]{1,2}\\)"; private static final String DELIMITER = "-"; private final String[] coordinates; diff --git a/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java index 768ad4df..549f69be 100644 --- a/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java +++ b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java @@ -20,4 +20,11 @@ class PointsGeneratorTest { assertThat(new PointsGenerator("(1,2)-(3,4)").generate()) .isEqualTo(Arrays.asList(new Point(1, 2), new Point(3, 4))); } + + @Test + void 사이즈_테스트() { + assertThat(new PointsGenerator("(1,2)-(3,4)").generate().size()).isEqualTo(2); + assertThat(new PointsGenerator("(1,2)-(3,4)-(5,6)").generate().size()).isEqualTo(3); + assertThat(new PointsGenerator("(1,2)-(3,4)-(5,6)-(5,9)").generate().size()).isEqualTo(4); + } } \ No newline at end of file From fbb972471fac470e894c21acff28da035328e5e9 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 11:31:27 +0900 Subject: [PATCH 14/44] =?UTF-8?q?refactor:=20PointsGenerator.class=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/generator/PointsGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/coordinate/domain/generator/PointsGenerator.java b/src/main/java/coordinate/domain/generator/PointsGenerator.java index 3e487738..b2101e90 100644 --- a/src/main/java/coordinate/domain/generator/PointsGenerator.java +++ b/src/main/java/coordinate/domain/generator/PointsGenerator.java @@ -15,7 +15,7 @@ public class PointsGenerator { public PointsGenerator(String coordinates) { if (!coordinates.matches(REGEX)) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("정규 표현식 에러"); } this.coordinates = Arrays.stream(coordinates.split(DELIMITER)) .map(s -> s.substring(1, s.length() - 1)) From e303307af8fc06052a5c384535bb7129d91820dc Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 11:32:06 +0900 Subject: [PATCH 15/44] =?UTF-8?q?feat:=20Square.class=20=EB=A9=B4=EC=A0=91?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Square.java | 12 ++++++++++-- src/test/java/coordinate/domain/SquareTest.java | 11 ++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index 73ea7608..d8822beb 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -1,8 +1,11 @@ package coordinate.domain; -public class Square { +import java.util.Arrays; +public class Square { private final Points points; + private Line height; + private Line width; public Square(final Points points) { this.points = points; @@ -20,11 +23,13 @@ private void validateRectangle() { for (int i = 0; i < points.size(); i++) { for (int j = 0; j < points.size(); j++) { if (i == j) continue; - if (points.get(i).isMatchX(points.get(j))){ + if (points.get(i).isMatchX(points.get(j))) { cnt++; + height = new Line(Points.of(Arrays.asList(points.get(i), points.get(j)))); } if (points.get(i).isMatchY(points.get(j))) { cnt++; + width = new Line(Points.of(Arrays.asList(points.get(i), points.get(j)))); } } } @@ -39,4 +44,7 @@ private void validateSize() { } } + public double area() { + return height.length() * width.length(); + } } diff --git a/src/test/java/coordinate/domain/SquareTest.java b/src/test/java/coordinate/domain/SquareTest.java index 36a4932f..23f86eff 100644 --- a/src/test/java/coordinate/domain/SquareTest.java +++ b/src/test/java/coordinate/domain/SquareTest.java @@ -3,8 +3,7 @@ import coordinate.domain.generator.PointsGenerator; import org.junit.jupiter.api.Test; -import java.util.Arrays; - +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; public class SquareTest { @@ -22,6 +21,12 @@ public class SquareTest { @Test void 직사각형_여부_테스트() { assertThrows(IllegalArgumentException.class, () -> - new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,1000)").generate())); + new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,23)").generate())); + } + + @Test + void 면적_테스트(){ + Square square = new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,18)").generate()); + assertThat(square.area()).isEqualTo(96); } } From 0375e65ecbf6b47b08caaab5eca53ad1e43a113d Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 11:55:39 +0900 Subject: [PATCH 16/44] =?UTF-8?q?feat:=20Triangle.class=20=EB=A9=B4?= =?UTF-8?q?=EC=A0=81=20=EA=B5=AC=ED=95=98=EA=B8=B0=EA=B9=8C=EC=A7=80=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Triangle.java | 35 +++++++++++++++++++ .../java/coordinate/domain/TriangleTest.java | 30 ++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/main/java/coordinate/domain/Triangle.java create mode 100644 src/test/java/coordinate/domain/TriangleTest.java diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java new file mode 100644 index 00000000..a3c8cd74 --- /dev/null +++ b/src/main/java/coordinate/domain/Triangle.java @@ -0,0 +1,35 @@ +package coordinate.domain; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Triangle { + + private static final int NUMBER_OF_POINTS = 3; + private final Points points; + + public Triangle(final Points points) { + this.points = points; + validateSize(this.points); + } + + private void validateSize(final Points points) { + if (points.size() != NUMBER_OF_POINTS) { + throw new IllegalArgumentException("삼각형은 점이 3개 필요합니다."); + } + } + + public double area() { + List lines = new ArrayList<>(); + for (int i = 0; i < points.size() - 1; i++) { + for (int j = i+1; j < points.size(); j++) { + lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); + } + } + double s = (lines.get(0).length() + lines.get(1).length() + lines.get(2).length()) / 2.0d; + double x = (s * (s - lines.get(0).length()) * (s - lines.get(1).length()) * (s - lines.get(2).length())); + double area = Math.sqrt(x); + return area; + } +} diff --git a/src/test/java/coordinate/domain/TriangleTest.java b/src/test/java/coordinate/domain/TriangleTest.java new file mode 100644 index 00000000..c14142d2 --- /dev/null +++ b/src/test/java/coordinate/domain/TriangleTest.java @@ -0,0 +1,30 @@ +package coordinate.domain; + +import coordinate.domain.generator.PointsGenerator; +import org.assertj.core.data.Offset; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TriangleTest { + @Test + void name() { + double s = (4 + 5 + 3) / 2.0d; + double x = (s * (s - 4) * (s - 5) * (s - 3)); + double Area = Math.sqrt(x); + System.out.println(Area); + } + + + @Test + void 사이즈_테스트() { + assertThrows(IllegalArgumentException.class, () -> new Triangle(new PointsGenerator("(10,10)-(14,15)").generate())); + } + + @Test + void 면적_테스트() { + Triangle triangle = new Triangle(new PointsGenerator("(10,10)-(14,15)-(20,8)").generate()); + assertThat(triangle.area()).isEqualTo(29.0, Offset.offset(0.00009)); + } +} From e1f6b40c0afc4c3324f141e6549356c74f4947a8 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 14:26:02 +0900 Subject: [PATCH 17/44] =?UTF-8?q?feat:=20Lines.class=20=EC=9D=BC=EA=B8=89?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98,=20LinesGenerator=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Lines.java | 17 +++++++++++ .../domain/generator/LinesGenerator.java | 8 +++++ .../generator/LinesSquareGenerator.java | 10 +++++++ .../generator/LinesTriangleGenerator.java | 30 +++++++++++++++++++ .../java/coordinate/domain/LinesTest.java | 20 +++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/main/java/coordinate/domain/Lines.java create mode 100644 src/main/java/coordinate/domain/generator/LinesGenerator.java create mode 100644 src/main/java/coordinate/domain/generator/LinesSquareGenerator.java create mode 100644 src/main/java/coordinate/domain/generator/LinesTriangleGenerator.java create mode 100644 src/test/java/coordinate/domain/LinesTest.java diff --git a/src/main/java/coordinate/domain/Lines.java b/src/main/java/coordinate/domain/Lines.java new file mode 100644 index 00000000..7bea3045 --- /dev/null +++ b/src/main/java/coordinate/domain/Lines.java @@ -0,0 +1,17 @@ +package coordinate.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Lines { + private final List lines; + + public Lines(final List lines) { + this.lines = new ArrayList<>(lines); + } + + public List lengths() { + return lines.stream().mapToDouble(Line::length).boxed().collect(Collectors.toList()); + } +} diff --git a/src/main/java/coordinate/domain/generator/LinesGenerator.java b/src/main/java/coordinate/domain/generator/LinesGenerator.java new file mode 100644 index 00000000..7faebb8f --- /dev/null +++ b/src/main/java/coordinate/domain/generator/LinesGenerator.java @@ -0,0 +1,8 @@ +package coordinate.domain.generator; + +import coordinate.domain.Lines; + +@FunctionalInterface +public interface LinesGenerator { + Lines generate(); +} diff --git a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java new file mode 100644 index 00000000..81f86d5f --- /dev/null +++ b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java @@ -0,0 +1,10 @@ +package coordinate.domain.generator; + +import coordinate.domain.Lines; + +public class LinesSquareGenerator implements LinesGenerator{ + @Override + public Lines generate() { + return null; + } +} diff --git a/src/main/java/coordinate/domain/generator/LinesTriangleGenerator.java b/src/main/java/coordinate/domain/generator/LinesTriangleGenerator.java new file mode 100644 index 00000000..637f5505 --- /dev/null +++ b/src/main/java/coordinate/domain/generator/LinesTriangleGenerator.java @@ -0,0 +1,30 @@ +package coordinate.domain.generator; + +import coordinate.domain.Line; +import coordinate.domain.Lines; +import coordinate.domain.Points; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class LinesTriangleGenerator implements LinesGenerator { + private final Points points; + + public LinesTriangleGenerator(final Points points) { + this.points = points; + } + + @Override + public Lines generate() { + List lines = new ArrayList<>(); + + for (int i = 0; i < points.size(); i++) { + for (int j = i + 1; j < points.size(); j++) { + lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); + } + } + + return new Lines(lines); + } +} diff --git a/src/test/java/coordinate/domain/LinesTest.java b/src/test/java/coordinate/domain/LinesTest.java new file mode 100644 index 00000000..7da75b19 --- /dev/null +++ b/src/test/java/coordinate/domain/LinesTest.java @@ -0,0 +1,20 @@ +package coordinate.domain; + +import coordinate.domain.generator.PointsGenerator; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.offset; + +class LinesTest { + @Test + void 길이_테스트() { + List lineList = Arrays.asList(new Line(new PointsGenerator("(10,10)-(14,15)").generate())); + Lines lines = new Lines(lineList); + + assertThat(lines.lengths().get(0)).isEqualTo(6.403124, offset(0.00009)); + } +} \ No newline at end of file From 55d291dd1050979c605d94503608bca281e5e396 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 14:27:23 +0900 Subject: [PATCH 18/44] =?UTF-8?q?feat:=20Figure.class=20=EC=B6=94=EC=83=81?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Figure.class 는 Triangle, Square의 상위 클래스다. --- src/main/java/coordinate/domain/Figure.java | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/coordinate/domain/Figure.java diff --git a/src/main/java/coordinate/domain/Figure.java b/src/main/java/coordinate/domain/Figure.java new file mode 100644 index 00000000..c1ad31d5 --- /dev/null +++ b/src/main/java/coordinate/domain/Figure.java @@ -0,0 +1,32 @@ +package coordinate.domain; + +import coordinate.domain.generator.LinesGenerator; + +public abstract class Figure { + private Points points; + private Lines lines; + + protected Figure(final Points points, final LinesGenerator linesGenerator) { + this.points = points; + this.lines = lines = linesGenerator.generate(); + validate(); + } + + protected abstract void validate(); + + protected void validateSize(int size){ + if(size!=points.size()) { + throw new IllegalArgumentException(); + } + } + + public abstract double area(); + + public Points getPoints() { + return points; + } + + public Lines getLines() { + return lines; + } +} From 7e16458a590b5ce2cc98b2c48881be4733791104 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 14:27:55 +0900 Subject: [PATCH 19/44] =?UTF-8?q?refactor:=20Triangle.class=20Figure=20?= =?UTF-8?q?=EC=83=81=EC=86=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Triangle.java | 38 +++++++++---------- .../java/coordinate/domain/TriangleTest.java | 1 - 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index a3c8cd74..d304ea75 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -1,34 +1,32 @@ package coordinate.domain; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class Triangle { +import coordinate.domain.generator.LinesTriangleGenerator; +public final class Triangle extends Figure { private static final int NUMBER_OF_POINTS = 3; - private final Points points; public Triangle(final Points points) { - this.points = points; - validateSize(this.points); + super(points, new LinesTriangleGenerator(points)); + } + + @Override + protected void validate() { + validateSize(); } - private void validateSize(final Points points) { - if (points.size() != NUMBER_OF_POINTS) { - throw new IllegalArgumentException("삼각형은 점이 3개 필요합니다."); - } + private void validateSize() { + super.validateSize(NUMBER_OF_POINTS); } + @Override public double area() { - List lines = new ArrayList<>(); - for (int i = 0; i < points.size() - 1; i++) { - for (int j = i+1; j < points.size(); j++) { - lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); - } - } - double s = (lines.get(0).length() + lines.get(1).length() + lines.get(2).length()) / 2.0d; - double x = (s * (s - lines.get(0).length()) * (s - lines.get(1).length()) * (s - lines.get(2).length())); + Lines lines = super.getLines(); + double a = lines.lengths().get(0); + double b = lines.lengths().get(1); + double c = lines.lengths().get(2); + + double s = (a+b+c) / 2.0d; + double x = (s * (s - a) * (s - b) * (s - c)); double area = Math.sqrt(x); return area; } diff --git a/src/test/java/coordinate/domain/TriangleTest.java b/src/test/java/coordinate/domain/TriangleTest.java index c14142d2..c8ac3100 100644 --- a/src/test/java/coordinate/domain/TriangleTest.java +++ b/src/test/java/coordinate/domain/TriangleTest.java @@ -16,7 +16,6 @@ void name() { System.out.println(Area); } - @Test void 사이즈_테스트() { assertThrows(IllegalArgumentException.class, () -> new Triangle(new PointsGenerator("(10,10)-(14,15)").generate())); From 500a7ed4e03b8202edc1bdb5b7cde93dca96fb58 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 15:43:24 +0900 Subject: [PATCH 20/44] =?UTF-8?q?feat:=20LineSquareGenerator.class=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/LinesSquareGenerator.java | 25 +++++++++++++++++-- .../java/coordinate/domain/SquareTest.java | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java index 81f86d5f..157d618c 100644 --- a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java +++ b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java @@ -1,10 +1,31 @@ package coordinate.domain.generator; +import coordinate.domain.Line; import coordinate.domain.Lines; +import coordinate.domain.Points; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class LinesSquareGenerator implements LinesGenerator { + private final Points points; + + public LinesSquareGenerator(final Points points) { + this.points = points; + } -public class LinesSquareGenerator implements LinesGenerator{ @Override public Lines generate() { - return null; + List lines = new ArrayList<>(); + for (int i = 0; i < points.size(); i++) { + for (int j = i + 1; j < points.size(); j++) { + lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); + } + } + lines.sort((line1, line2) -> (int) (line1.length() - line2.length())); + lines.remove(lines.size() - 1); + lines.remove(lines.size() - 1); + return new Lines(lines); } } diff --git a/src/test/java/coordinate/domain/SquareTest.java b/src/test/java/coordinate/domain/SquareTest.java index 23f86eff..20dd056f 100644 --- a/src/test/java/coordinate/domain/SquareTest.java +++ b/src/test/java/coordinate/domain/SquareTest.java @@ -21,7 +21,7 @@ public class SquareTest { @Test void 직사각형_여부_테스트() { assertThrows(IllegalArgumentException.class, () -> - new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,23)").generate())); + new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,3)").generate())); } @Test From 965f33089f321c2528984777c68a709ca0712c4e Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 15:43:56 +0900 Subject: [PATCH 21/44] =?UTF-8?q?refactor:=20Square.class=20Figure=20?= =?UTF-8?q?=EC=83=81=EC=86=8D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Point.java | 8 ++-- src/main/java/coordinate/domain/Square.java | 46 +++++++-------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java index 7775af3e..dd4a77be 100644 --- a/src/main/java/coordinate/domain/Point.java +++ b/src/main/java/coordinate/domain/Point.java @@ -19,12 +19,12 @@ private double square(final int first, final int second) { return Math.pow(first - second, 2); } - public boolean isMatchX(final Point point) { - return this.x.value() == point.x.value(); + public int getX() { + return x.value(); } - public boolean isMatchY(final Point point) { - return this.y.value() == point.y.value(); + public int getY() { + return y.value(); } @Override diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index d8822beb..c4af3408 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -1,50 +1,36 @@ package coordinate.domain; -import java.util.Arrays; +import coordinate.domain.generator.LinesSquareGenerator; -public class Square { - private final Points points; - private Line height; - private Line width; + +public final class Square extends Figure { + private static final int NUMBER_OF_POINTS = 4; public Square(final Points points) { - this.points = points; + super(points, new LinesSquareGenerator(points)); validate(); } - private void validate() { + protected void validate() { validateSize(); validateRectangle(); } - private void validateRectangle() { - // todo: 개선점 생각하기 - int cnt = 0; - for (int i = 0; i < points.size(); i++) { - for (int j = 0; j < points.size(); j++) { - if (i == j) continue; - if (points.get(i).isMatchX(points.get(j))) { - cnt++; - height = new Line(Points.of(Arrays.asList(points.get(i), points.get(j)))); - } - if (points.get(i).isMatchY(points.get(j))) { - cnt++; - width = new Line(Points.of(Arrays.asList(points.get(i), points.get(j)))); - } - } - } - if (cnt != 8) { - throw new IllegalArgumentException("직사각형이 아닙니다."); - } + private void validateSize() { + super.validateSize(NUMBER_OF_POINTS); } - private void validateSize() { - if (this.points.size() != 4) { - throw new IllegalArgumentException("점이 4개가 아닙니다."); + private void validateRectangle() { + Points points = super.getPoints(); + double distance1 = Math.hypot(points.get(0).getX() - points.get(2).getX(), points.get(0).getY() - points.get(2).getY()); + double distance2 = Math.hypot(points.get(1).getX() - points.get(3).getX(), points.get(1).getY() - points.get(3).getY()); + if (distance1 != distance2) { + throw new IllegalArgumentException("직사각형이 아닙니다"); } } + @Override public double area() { - return height.length() * width.length(); + return super.getLines().lengths().get(0) * super.getLines().lengths().get(2); } } From f1a0dc2f14f739204debde9083783f06c4adbc6b Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 15:48:20 +0900 Subject: [PATCH 22/44] =?UTF-8?q?feat:=20AreaCalculator=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/AreaCalculator.java | 6 ++++++ src/main/java/coordinate/domain/Figure.java | 11 +++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 src/main/java/coordinate/domain/AreaCalculator.java diff --git a/src/main/java/coordinate/domain/AreaCalculator.java b/src/main/java/coordinate/domain/AreaCalculator.java new file mode 100644 index 00000000..3c1fa0ac --- /dev/null +++ b/src/main/java/coordinate/domain/AreaCalculator.java @@ -0,0 +1,6 @@ +package coordinate.domain; + +@FunctionalInterface +public interface AreaCalculator { + double area(); +} diff --git a/src/main/java/coordinate/domain/Figure.java b/src/main/java/coordinate/domain/Figure.java index c1ad31d5..0e80c097 100644 --- a/src/main/java/coordinate/domain/Figure.java +++ b/src/main/java/coordinate/domain/Figure.java @@ -2,7 +2,7 @@ import coordinate.domain.generator.LinesGenerator; -public abstract class Figure { +public abstract class Figure implements AreaCalculator { private Points points; private Lines lines; @@ -14,13 +14,16 @@ protected Figure(final Points points, final LinesGenerator linesGenerator) { protected abstract void validate(); - protected void validateSize(int size){ - if(size!=points.size()) { + protected void validateSize(int size) { + if (size != points.size()) { throw new IllegalArgumentException(); } } - public abstract double area(); + @Override + public double area() { + throw new UnsupportedOperationException(); + } public Points getPoints() { return points; From eeb1d3e0a07401843e6dc1bd951dfa9fb6ba80f5 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 15:48:46 +0900 Subject: [PATCH 23/44] =?UTF-8?q?style:=20Figure.class=20=EC=98=A4?= =?UTF-8?q?=ED=83=88=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Figure.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/coordinate/domain/Figure.java b/src/main/java/coordinate/domain/Figure.java index 0e80c097..043bf4be 100644 --- a/src/main/java/coordinate/domain/Figure.java +++ b/src/main/java/coordinate/domain/Figure.java @@ -8,7 +8,7 @@ public abstract class Figure implements AreaCalculator { protected Figure(final Points points, final LinesGenerator linesGenerator) { this.points = points; - this.lines = lines = linesGenerator.generate(); + this.lines = linesGenerator.generate(); validate(); } From 0e1ba2bba4b0a94814b3870fb170c54855ae34ae Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 16:57:58 +0900 Subject: [PATCH 24/44] =?UTF-8?q?refactor:=20Triangle.area()=20stream=20?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Triangle.java | 21 ++++++++++++------- .../java/coordinate/domain/TriangleTest.java | 8 ------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index d304ea75..0bf21d25 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -21,13 +21,18 @@ private void validateSize() { @Override public double area() { Lines lines = super.getLines(); - double a = lines.lengths().get(0); - double b = lines.lengths().get(1); - double c = lines.lengths().get(2); - - double s = (a+b+c) / 2.0d; - double x = (s * (s - a) * (s - b) * (s - c)); - double area = Math.sqrt(x); - return area; + return Math.sqrt(calculateX(lines, calculateS(lines))); + } + + private double calculateS(final Lines lines) { + return lines.lengths().stream().mapToDouble(Double::doubleValue).sum() / 2.0; + } + + private double calculateX(final Lines lines, final double s) { + return s * lines.lengths() + .stream() + .mapToDouble(length -> s - length) + .reduce(1, (a, b) -> a * b); + } } diff --git a/src/test/java/coordinate/domain/TriangleTest.java b/src/test/java/coordinate/domain/TriangleTest.java index c8ac3100..8e9277ba 100644 --- a/src/test/java/coordinate/domain/TriangleTest.java +++ b/src/test/java/coordinate/domain/TriangleTest.java @@ -8,14 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; public class TriangleTest { - @Test - void name() { - double s = (4 + 5 + 3) / 2.0d; - double x = (s * (s - 4) * (s - 5) * (s - 3)); - double Area = Math.sqrt(x); - System.out.println(Area); - } - @Test void 사이즈_테스트() { assertThrows(IllegalArgumentException.class, () -> new Triangle(new PointsGenerator("(10,10)-(14,15)").generate())); From e444aff9399cb1612d422a87e6cfc9aa410be830 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 16:59:05 +0900 Subject: [PATCH 25/44] =?UTF-8?q?fix:=20Square.class=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Square.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index c4af3408..e649f3db 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -8,7 +8,6 @@ public final class Square extends Figure { public Square(final Points points) { super(points, new LinesSquareGenerator(points)); - validate(); } protected void validate() { @@ -21,10 +20,9 @@ private void validateSize() { } private void validateRectangle() { - Points points = super.getPoints(); - double distance1 = Math.hypot(points.get(0).getX() - points.get(2).getX(), points.get(0).getY() - points.get(2).getY()); - double distance2 = Math.hypot(points.get(1).getX() - points.get(3).getX(), points.get(1).getY() - points.get(3).getY()); - if (distance1 != distance2) { + double diagonal1 = super.getLines().lengths().get(4); + double diagonal2 = super.getLines().lengths().get(5); + if (diagonal1 != diagonal2) { throw new IllegalArgumentException("직사각형이 아닙니다"); } } From 0cd03e795ffb9b29412cf10c849b2c35125ee243 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 17:10:21 +0900 Subject: [PATCH 26/44] =?UTF-8?q?feat:=20FigureFactory.class=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/generator/FigureFactory.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/coordinate/domain/generator/FigureFactory.java diff --git a/src/main/java/coordinate/domain/generator/FigureFactory.java b/src/main/java/coordinate/domain/generator/FigureFactory.java new file mode 100644 index 00000000..8920b437 --- /dev/null +++ b/src/main/java/coordinate/domain/generator/FigureFactory.java @@ -0,0 +1,20 @@ +package coordinate.domain.generator; + +import coordinate.domain.Figure; +import coordinate.domain.Points; +import coordinate.domain.Square; +import coordinate.domain.Triangle; + +public class FigureFactory { + private static final int TRIANGLE = 3; + private static final int SQUARE = 4; + + public static Figure generate(Points points) { + if (points.size() == SQUARE) { + return new Square(points); + } else if (points.size() == TRIANGLE) { + return new Triangle(points); + } + throw new IllegalArgumentException("삼각형 또는 사각형만"); + } +} From 261751925721fc662aeb5ba88efe67ea011cc4cc Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 17:19:18 +0900 Subject: [PATCH 27/44] refactor: LinesSquareGenerator.class --- .../java/coordinate/domain/generator/LinesSquareGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java index 157d618c..7276bcc3 100644 --- a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java +++ b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java @@ -23,9 +23,8 @@ public Lines generate() { lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); } } + //TODO 로직 다시 생각 lines.sort((line1, line2) -> (int) (line1.length() - line2.length())); - lines.remove(lines.size() - 1); - lines.remove(lines.size() - 1); return new Lines(lines); } } From 9bd9c3b4d60869d832449a6b17134449de05e611 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 17:19:31 +0900 Subject: [PATCH 28/44] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/CoordinateApp.java | 15 ++++++++++++++- src/main/java/coordinate/view/OutputView.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/main/java/coordinate/view/OutputView.java diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java index 59cad4eb..0bc5686b 100644 --- a/src/main/java/coordinate/CoordinateApp.java +++ b/src/main/java/coordinate/CoordinateApp.java @@ -1,10 +1,23 @@ package coordinate; +import coordinate.domain.Figure; +import coordinate.domain.Line; +import coordinate.domain.Points; +import coordinate.domain.generator.FigureFactory; +import coordinate.domain.generator.PointsGenerator; import coordinate.view.InputView; +import coordinate.view.OutputView; public class CoordinateApp { public static void main(String[] args) { - InputView.inputCooridinate(); + Points points = new PointsGenerator(InputView.inputCooridinate()).generate(); + if (points.size() == 2) { + Line line = new Line(points); + OutputView.printResult(line); + } else { + Figure figure = FigureFactory.generate(points); + OutputView.printResult(figure); + } } } diff --git a/src/main/java/coordinate/view/OutputView.java b/src/main/java/coordinate/view/OutputView.java new file mode 100644 index 00000000..672f6b2e --- /dev/null +++ b/src/main/java/coordinate/view/OutputView.java @@ -0,0 +1,15 @@ +package coordinate.view; + +import coordinate.domain.Figure; +import coordinate.domain.Line; + +public class OutputView { + + public static void printResult(final Figure figure) { + System.out.println("넓이는 " + figure.area()); + } + + public static void printResult(final Line line) { + System.out.println("두 점 사이 거리는 " + line.length()); + } +} From 2371af01af85d1763c670ec44b20fa83fa73d922 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 18:09:40 +0900 Subject: [PATCH 29/44] =?UTF-8?q?refactor:=20FigureFactory=20enum=20?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/generator/FigureFactory.java | 22 +++++++++++++------ .../domain/generator/FigureGenerator.java | 9 ++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/main/java/coordinate/domain/generator/FigureGenerator.java diff --git a/src/main/java/coordinate/domain/generator/FigureFactory.java b/src/main/java/coordinate/domain/generator/FigureFactory.java index 8920b437..9ed7da1b 100644 --- a/src/main/java/coordinate/domain/generator/FigureFactory.java +++ b/src/main/java/coordinate/domain/generator/FigureFactory.java @@ -5,15 +5,23 @@ import coordinate.domain.Square; import coordinate.domain.Triangle; -public class FigureFactory { - private static final int TRIANGLE = 3; - private static final int SQUARE = 4; +public enum FigureFactory { + TRIANGLE(3, Triangle::new), + SQUARE(4, Square::new); + + private final int numberOfPoints; + private final FigureGenerator figureGenerator; + + FigureFactory(final int numberOfPoints, final FigureGenerator figureGenerator) { + this.numberOfPoints = numberOfPoints; + this.figureGenerator = figureGenerator; + } public static Figure generate(Points points) { - if (points.size() == SQUARE) { - return new Square(points); - } else if (points.size() == TRIANGLE) { - return new Triangle(points); + for (FigureFactory type : FigureFactory.values()) { + if (type.numberOfPoints == points.size()) { + return type.figureGenerator.create(points); + } } throw new IllegalArgumentException("삼각형 또는 사각형만"); } diff --git a/src/main/java/coordinate/domain/generator/FigureGenerator.java b/src/main/java/coordinate/domain/generator/FigureGenerator.java new file mode 100644 index 00000000..90a48315 --- /dev/null +++ b/src/main/java/coordinate/domain/generator/FigureGenerator.java @@ -0,0 +1,9 @@ +package coordinate.domain.generator; + +import coordinate.domain.Figure; +import coordinate.domain.Points; + +@FunctionalInterface +public interface FigureGenerator { + Figure create(Points points); +} \ No newline at end of file From 4c7627eb893b3578acef002e761242e674d055b0 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 18:19:09 +0900 Subject: [PATCH 30/44] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/UI.java | 21 +++++++++++++++ src/main/java/coordinate/domain/Line.java | 4 +++ src/main/java/coordinate/domain/Square.java | 5 ++++ src/main/java/coordinate/domain/Triangle.java | 4 +++ src/main/java/coordinate/view/OutputView.java | 26 ++++++++++++++++++- 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/java/coordinate/UI.java diff --git a/src/main/java/coordinate/UI.java b/src/main/java/coordinate/UI.java new file mode 100644 index 00000000..e2128083 --- /dev/null +++ b/src/main/java/coordinate/UI.java @@ -0,0 +1,21 @@ +package coordinate; + +import coordinate.domain.Points; + +public class UI { + private boolean[][] coordinates; + + public UI() { + this.coordinates = new boolean[25][25]; + } + + public void drawPoints(Points points){ + for (int i = 0; i < points.size(); i++) { + coordinates[points.get(i).getX()][points.get(i).getY()] = true; + } + } + + public boolean[][] getCoordinates() { + return coordinates; + } +} diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java index 9892dea9..d608364e 100644 --- a/src/main/java/coordinate/domain/Line.java +++ b/src/main/java/coordinate/domain/Line.java @@ -22,4 +22,8 @@ public double length() { Point b = points.get(1); return a.length(b); } + + public Points getPoints() { + return points; + } } diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index e649f3db..13b03b74 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -31,4 +31,9 @@ private void validateRectangle() { public double area() { return super.getLines().lengths().get(0) * super.getLines().lengths().get(2); } + + @Override + public String toString() { + return "사각형"; + } } diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index 0bf21d25..58ba2197 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -33,6 +33,10 @@ private double calculateX(final Lines lines, final double s) { .stream() .mapToDouble(length -> s - length) .reduce(1, (a, b) -> a * b); + } + @Override + public String toString() { + return "삼각형"; } } diff --git a/src/main/java/coordinate/view/OutputView.java b/src/main/java/coordinate/view/OutputView.java index 672f6b2e..73b76db3 100644 --- a/src/main/java/coordinate/view/OutputView.java +++ b/src/main/java/coordinate/view/OutputView.java @@ -1,15 +1,39 @@ package coordinate.view; +import coordinate.UI; import coordinate.domain.Figure; import coordinate.domain.Line; +import coordinate.domain.Points; public class OutputView { + private static final String POLE_HEIGHT = "|"; + private static final String NEW_LINE = "\n"; public static void printResult(final Figure figure) { - System.out.println("넓이는 " + figure.area()); + printCoordinates(figure.getPoints()); + System.out.println(figure.toString() + " 넓이는 " + figure.area()); } public static void printResult(final Line line) { + printCoordinates(line.getPoints()); System.out.println("두 점 사이 거리는 " + line.length()); } + + private static void printCoordinates(final Points points) { + UI ui = new UI(); + ui.drawPoints(points); + StringBuilder sb = new StringBuilder(); + boolean[][] coordinates = ui.getCoordinates(); + + for (int i = coordinates.length - 1; i >= 0; i--) { + sb.append(i).append(POLE_HEIGHT); + for (int j = 0; j < coordinates.length; j++) { + sb.append(coordinates[j][i] ? "* " : " "); + } + sb.append(NEW_LINE); + } + System.out.println(sb.toString()); + System.out.println("------------------------------------------------------------------------------"); + System.out.println("0 2 4 6 8 10 12 14 16 18 20 22"); + } } From d12fb875306a6f503908d7056b81995150efc73e Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 18:28:57 +0900 Subject: [PATCH 31/44] =?UTF-8?q?refactor:=20Square.class=20=EC=A7=81?= =?UTF-8?q?=EC=82=AC=EA=B0=81=ED=98=95=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20?= =?UTF-8?q?=EA=B2=80=EC=82=AC=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Square.java | 9 +++++++-- .../domain/generator/LinesSquareGenerator.java | 12 ++++++++++-- src/test/java/coordinate/domain/SquareTest.java | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index 13b03b74..687dc40f 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -20,13 +20,18 @@ private void validateSize() { } private void validateRectangle() { - double diagonal1 = super.getLines().lengths().get(4); - double diagonal2 = super.getLines().lengths().get(5); + Points points = super.getPoints(); + double diagonal1 = getDiagonal(points, 0, 2); + double diagonal2 = getDiagonal(points, 1, 3); if (diagonal1 != diagonal2) { throw new IllegalArgumentException("직사각형이 아닙니다"); } } + private double getDiagonal(final Points points, final int i, final int i2) { + return Math.hypot(points.get(i).length(points.get(i2)), points.get(i).length(points.get(i2))); + } + @Override public double area() { return super.getLines().lengths().get(0) * super.getLines().lengths().get(2); diff --git a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java index 7276bcc3..91155520 100644 --- a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java +++ b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; public class LinesSquareGenerator implements LinesGenerator { private final Points points; @@ -23,8 +24,15 @@ public Lines generate() { lines.add(new Line(Points.of(Arrays.asList(points.get(i), points.get(j))))); } } - //TODO 로직 다시 생각 - lines.sort((line1, line2) -> (int) (line1.length() - line2.length())); + lines = filterDiagonal(lines); return new Lines(lines); } + + private List filterDiagonal(List lines) { + lines = lines.stream() + .sorted((line1, line2) -> (int) (line1.length() - line2.length())) + .limit(lines.size() - 2) + .collect(Collectors.toList()); + return lines; + } } diff --git a/src/test/java/coordinate/domain/SquareTest.java b/src/test/java/coordinate/domain/SquareTest.java index 20dd056f..f075ad49 100644 --- a/src/test/java/coordinate/domain/SquareTest.java +++ b/src/test/java/coordinate/domain/SquareTest.java @@ -25,7 +25,7 @@ public class SquareTest { } @Test - void 면적_테스트(){ + void 면적_테스트() { Square square = new Square(new PointsGenerator("(10,10)-(22,10)-(22,18)-(10,18)").generate()); assertThat(square.area()).isEqualTo(96); } From 7cc360c76acad5a37212f64b56c400f58b117207 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 18:29:53 +0900 Subject: [PATCH 32/44] =?UTF-8?q?style:=20InputView.inputCoordinate()=20?= =?UTF-8?q?=EC=98=A4=ED=83=88=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/CoordinateApp.java | 2 +- src/main/java/coordinate/view/InputView.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java index 0bc5686b..3f3185df 100644 --- a/src/main/java/coordinate/CoordinateApp.java +++ b/src/main/java/coordinate/CoordinateApp.java @@ -11,7 +11,7 @@ public class CoordinateApp { public static void main(String[] args) { - Points points = new PointsGenerator(InputView.inputCooridinate()).generate(); + Points points = new PointsGenerator(InputView.inputCoordinate()).generate(); if (points.size() == 2) { Line line = new Line(points); OutputView.printResult(line); diff --git a/src/main/java/coordinate/view/InputView.java b/src/main/java/coordinate/view/InputView.java index efbbed31..db38464d 100644 --- a/src/main/java/coordinate/view/InputView.java +++ b/src/main/java/coordinate/view/InputView.java @@ -5,7 +5,7 @@ public class InputView { private static Scanner scanner = new Scanner(System.in); - public static String inputCooridinate() { + public static String inputCoordinate() { System.out.println("좌표를 입력하세요."); return scanner.nextLine(); } From 37d2b2ebd88805740cef28a6ce7a8ff0a3c7769f Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 22 May 2019 18:38:19 +0900 Subject: [PATCH 33/44] =?UTF-8?q?refactor:=20CoordinateApp.class=20?= =?UTF-8?q?=EC=9E=98=EB=AA=BB=EB=90=9C=20=EC=A2=8C=ED=91=9C=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EC=8B=9C=20=EC=9E=AC=EC=9E=85=EB=A0=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/CoordinateApp.java | 14 ++++++++++++-- .../domain/generator/PointsGenerator.java | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java index 3f3185df..5416e179 100644 --- a/src/main/java/coordinate/CoordinateApp.java +++ b/src/main/java/coordinate/CoordinateApp.java @@ -9,10 +9,11 @@ import coordinate.view.OutputView; public class CoordinateApp { + private static final int LINE = 2; public static void main(String[] args) { - Points points = new PointsGenerator(InputView.inputCoordinate()).generate(); - if (points.size() == 2) { + Points points = generatePoints(); + if (points.size() == LINE) { Line line = new Line(points); OutputView.printResult(line); } else { @@ -20,4 +21,13 @@ public static void main(String[] args) { OutputView.printResult(figure); } } + + private static Points generatePoints() { + try { + return new PointsGenerator(InputView.inputCoordinate()).generate(); + }catch (IllegalArgumentException e){ + System.err.println(e.getMessage()); + return generatePoints(); + } + } } diff --git a/src/main/java/coordinate/domain/generator/PointsGenerator.java b/src/main/java/coordinate/domain/generator/PointsGenerator.java index b2101e90..33476510 100644 --- a/src/main/java/coordinate/domain/generator/PointsGenerator.java +++ b/src/main/java/coordinate/domain/generator/PointsGenerator.java @@ -15,7 +15,7 @@ public class PointsGenerator { public PointsGenerator(String coordinates) { if (!coordinates.matches(REGEX)) { - throw new IllegalArgumentException("정규 표현식 에러"); + throw new IllegalArgumentException("유효한 입력이 아닙니다. ex) (10,10)-(10,10)-(20,8)"); } this.coordinates = Arrays.stream(coordinates.split(DELIMITER)) .map(s -> s.substring(1, s.length() - 1)) From 6d0ab2d38b94e8bd165184ae7e867292c0fe0b07 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Thu, 23 May 2019 11:01:09 +0900 Subject: [PATCH 34/44] =?UTF-8?q?fix:=20Point.class,=20Points.class=20equa?= =?UTF-8?q?ls()=20&=20hashcode()=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Point.java | 14 ++++++++++---- src/main/java/coordinate/domain/Points.java | 14 ++++++++++++++ .../domain/generator/PointsGeneratorTest.java | 3 ++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/coordinate/domain/Point.java b/src/main/java/coordinate/domain/Point.java index dd4a77be..540b090d 100644 --- a/src/main/java/coordinate/domain/Point.java +++ b/src/main/java/coordinate/domain/Point.java @@ -1,5 +1,7 @@ package coordinate.domain; +import java.util.Objects; + public class Point { private final Coordinate x; private final Coordinate y; @@ -28,12 +30,16 @@ public int getY() { } @Override - public int hashCode() { - return super.hashCode(); + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Point point = (Point) o; + return Objects.equals(x, point.x) && + Objects.equals(y, point.y); } @Override - public boolean equals(final Object obj) { - return super.equals(obj); + public int hashCode() { + return Objects.hash(x, y); } } diff --git a/src/main/java/coordinate/domain/Points.java b/src/main/java/coordinate/domain/Points.java index 83be9e7a..ce26529f 100644 --- a/src/main/java/coordinate/domain/Points.java +++ b/src/main/java/coordinate/domain/Points.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Objects; public class Points { private final List points; @@ -40,4 +41,17 @@ public Point get(final int index) { public int size() { return points.size(); } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Points points1 = (Points) o; + return Objects.equals(points, points1.points); + } + + @Override + public int hashCode() { + return Objects.hash(points); + } } diff --git a/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java index 549f69be..ccb56a94 100644 --- a/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java +++ b/src/test/java/coordinate/domain/generator/PointsGeneratorTest.java @@ -1,6 +1,7 @@ package coordinate.domain.generator; import coordinate.domain.Point; +import coordinate.domain.Points; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -18,7 +19,7 @@ class PointsGeneratorTest { @Test void generate_테스트() { assertThat(new PointsGenerator("(1,2)-(3,4)").generate()) - .isEqualTo(Arrays.asList(new Point(1, 2), new Point(3, 4))); + .isEqualTo(Points.of(Arrays.asList(new Point(1, 2), new Point(3, 4)))); } @Test From d52b2491122fb6da2fd9be63492415c2766e0613 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Thu, 23 May 2019 11:01:53 +0900 Subject: [PATCH 35/44] =?UTF-8?q?refactor:=20Figure.validateSize()=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Figure.java | 10 +++++----- src/main/java/coordinate/domain/Square.java | 13 +++++-------- src/main/java/coordinate/domain/Triangle.java | 14 +++++--------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/coordinate/domain/Figure.java b/src/main/java/coordinate/domain/Figure.java index 043bf4be..5bd5a796 100644 --- a/src/main/java/coordinate/domain/Figure.java +++ b/src/main/java/coordinate/domain/Figure.java @@ -9,17 +9,17 @@ public abstract class Figure implements AreaCalculator { protected Figure(final Points points, final LinesGenerator linesGenerator) { this.points = points; this.lines = linesGenerator.generate(); - validate(); + validateSize(); } - protected abstract void validate(); - - protected void validateSize(int size) { - if (size != points.size()) { + protected void validateSize() { + if (size() != points.size()) { throw new IllegalArgumentException(); } } + protected abstract int size(); + @Override public double area() { throw new UnsupportedOperationException(); diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index 687dc40f..e0c050d1 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -8,17 +8,9 @@ public final class Square extends Figure { public Square(final Points points) { super(points, new LinesSquareGenerator(points)); - } - - protected void validate() { - validateSize(); validateRectangle(); } - private void validateSize() { - super.validateSize(NUMBER_OF_POINTS); - } - private void validateRectangle() { Points points = super.getPoints(); double diagonal1 = getDiagonal(points, 0, 2); @@ -32,6 +24,11 @@ private double getDiagonal(final Points points, final int i, final int i2) { return Math.hypot(points.get(i).length(points.get(i2)), points.get(i).length(points.get(i2))); } + @Override + protected int size() { + return NUMBER_OF_POINTS; + } + @Override public double area() { return super.getLines().lengths().get(0) * super.getLines().lengths().get(2); diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index 58ba2197..2cc69c9d 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -9,15 +9,6 @@ public Triangle(final Points points) { super(points, new LinesTriangleGenerator(points)); } - @Override - protected void validate() { - validateSize(); - } - - private void validateSize() { - super.validateSize(NUMBER_OF_POINTS); - } - @Override public double area() { Lines lines = super.getLines(); @@ -35,6 +26,11 @@ private double calculateX(final Lines lines, final double s) { .reduce(1, (a, b) -> a * b); } + @Override + protected int size() { + return NUMBER_OF_POINTS; + } + @Override public String toString() { return "삼각형"; From 2eeace991154f61ddc941fc7b1149a9028544387 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Fri, 24 May 2019 10:25:48 +0900 Subject: [PATCH 36/44] =?UTF-8?q?refactor:=20FigureFactory.class=20?= =?UTF-8?q?=EC=83=81=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/CoordinateApp.java | 4 +--- src/main/java/coordinate/domain/Line.java | 2 +- src/main/java/coordinate/domain/Square.java | 2 +- src/main/java/coordinate/domain/Triangle.java | 2 +- src/main/java/coordinate/domain/generator/FigureFactory.java | 4 ++-- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java index 5416e179..100497e2 100644 --- a/src/main/java/coordinate/CoordinateApp.java +++ b/src/main/java/coordinate/CoordinateApp.java @@ -9,11 +9,9 @@ import coordinate.view.OutputView; public class CoordinateApp { - private static final int LINE = 2; - public static void main(String[] args) { Points points = generatePoints(); - if (points.size() == LINE) { + if (points.size() == Line.NUMBER_OF_POINTS) { Line line = new Line(points); OutputView.printResult(line); } else { diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java index d608364e..7c71c351 100644 --- a/src/main/java/coordinate/domain/Line.java +++ b/src/main/java/coordinate/domain/Line.java @@ -2,7 +2,7 @@ public class Line { - private static final int NUMBER_OF_POINTS = 2; + public static final int NUMBER_OF_POINTS = 2; private final Points points; diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index e0c050d1..807a1551 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -4,7 +4,7 @@ public final class Square extends Figure { - private static final int NUMBER_OF_POINTS = 4; + public static final int NUMBER_OF_POINTS = 4; public Square(final Points points) { super(points, new LinesSquareGenerator(points)); diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index 2cc69c9d..33b003d9 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -3,7 +3,7 @@ import coordinate.domain.generator.LinesTriangleGenerator; public final class Triangle extends Figure { - private static final int NUMBER_OF_POINTS = 3; + public static final int NUMBER_OF_POINTS = 3; public Triangle(final Points points) { super(points, new LinesTriangleGenerator(points)); diff --git a/src/main/java/coordinate/domain/generator/FigureFactory.java b/src/main/java/coordinate/domain/generator/FigureFactory.java index 9ed7da1b..38ec3717 100644 --- a/src/main/java/coordinate/domain/generator/FigureFactory.java +++ b/src/main/java/coordinate/domain/generator/FigureFactory.java @@ -6,8 +6,8 @@ import coordinate.domain.Triangle; public enum FigureFactory { - TRIANGLE(3, Triangle::new), - SQUARE(4, Square::new); + TRIANGLE(Triangle.NUMBER_OF_POINTS, Triangle::new), + SQUARE(Square.NUMBER_OF_POINTS, Square::new); private final int numberOfPoints; private final FigureGenerator figureGenerator; From 20534823c9efac5ffd446f11651513edf9d0a61b Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 12:31:29 +0900 Subject: [PATCH 37/44] =?UTF-8?q?refactor:=20Coordinate.class=20=EB=AF=B8?= =?UTF-8?q?=EB=A6=AC=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=ED=95=B4=EB=86=93=EA=B3=A0=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=EB=90=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/coordinate/domain/Coordinate.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/coordinate/domain/Coordinate.java b/src/main/java/coordinate/domain/Coordinate.java index bd3efb1e..efa81098 100644 --- a/src/main/java/coordinate/domain/Coordinate.java +++ b/src/main/java/coordinate/domain/Coordinate.java @@ -1,22 +1,36 @@ package coordinate.domain; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class Coordinate { private static final int MAX_BOUND = 24; private static final int MIN_BOUND = 0; + private static Map map = new HashMap<>(); + + static { + for (int i = MIN_BOUND; i <= MAX_BOUND; i++) { + map.put(i, of(i)); + } + } + private final int coordinate; private Coordinate(final int coordinate) { if (coordinate < MIN_BOUND || coordinate > MAX_BOUND) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("좌표는 0~24만 입력 가능합니다."); } this.coordinate = coordinate; } public static Coordinate of(final int coordinate) { - return new Coordinate(coordinate); + Coordinate instance = map.get(coordinate); + if(map.get(coordinate)==null) { + return new Coordinate(coordinate); + } + return instance; } public int value() { From e15ae3a033bf0a7ec98c86dbcec61549321bdd17 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:04:07 +0900 Subject: [PATCH 38/44] =?UTF-8?q?refactor:=20UI.class=20=ED=95=98=EB=93=9C?= =?UTF-8?q?=EC=BD=94=EB=94=A9=20=EC=83=81=EC=88=98=EB=A1=9C=20=EC=B6=94?= =?UTF-8?q?=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/UI.java | 5 +++-- src/main/java/coordinate/domain/Coordinate.java | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/coordinate/UI.java b/src/main/java/coordinate/UI.java index e2128083..74aeec66 100644 --- a/src/main/java/coordinate/UI.java +++ b/src/main/java/coordinate/UI.java @@ -1,15 +1,16 @@ package coordinate; +import coordinate.domain.Coordinate; import coordinate.domain.Points; public class UI { private boolean[][] coordinates; public UI() { - this.coordinates = new boolean[25][25]; + this.coordinates = new boolean[Coordinate.MAX_BOUND][Coordinate.MAX_BOUND]; } - public void drawPoints(Points points){ + public void drawPoints(Points points) { for (int i = 0; i < points.size(); i++) { coordinates[points.get(i).getX()][points.get(i).getY()] = true; } diff --git a/src/main/java/coordinate/domain/Coordinate.java b/src/main/java/coordinate/domain/Coordinate.java index efa81098..97702510 100644 --- a/src/main/java/coordinate/domain/Coordinate.java +++ b/src/main/java/coordinate/domain/Coordinate.java @@ -5,13 +5,13 @@ import java.util.Objects; public class Coordinate { - private static final int MAX_BOUND = 24; + public static final int MAX_BOUND = 25; private static final int MIN_BOUND = 0; private static Map map = new HashMap<>(); static { - for (int i = MIN_BOUND; i <= MAX_BOUND; i++) { + for (int i = MIN_BOUND; i < MAX_BOUND; i++) { map.put(i, of(i)); } } @@ -19,7 +19,7 @@ public class Coordinate { private final int coordinate; private Coordinate(final int coordinate) { - if (coordinate < MIN_BOUND || coordinate > MAX_BOUND) { + if (coordinate < MIN_BOUND || coordinate >= MAX_BOUND) { throw new IllegalArgumentException("좌표는 0~24만 입력 가능합니다."); } this.coordinate = coordinate; From 3dd322a1872f28dc29b470f211cf17d7191f8bea Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:04:43 +0900 Subject: [PATCH 39/44] =?UTF-8?q?style:=20=EC=98=A4=ED=86=A0=20=ED=8F=AC?= =?UTF-8?q?=EB=A7=B7=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/Coordinate.java | 2 +- src/main/java/coordinate/view/OutputView.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/coordinate/domain/Coordinate.java b/src/main/java/coordinate/domain/Coordinate.java index 97702510..c4e980fe 100644 --- a/src/main/java/coordinate/domain/Coordinate.java +++ b/src/main/java/coordinate/domain/Coordinate.java @@ -27,7 +27,7 @@ private Coordinate(final int coordinate) { public static Coordinate of(final int coordinate) { Coordinate instance = map.get(coordinate); - if(map.get(coordinate)==null) { + if (map.get(coordinate) == null) { return new Coordinate(coordinate); } return instance; diff --git a/src/main/java/coordinate/view/OutputView.java b/src/main/java/coordinate/view/OutputView.java index 73b76db3..3c02ea0e 100644 --- a/src/main/java/coordinate/view/OutputView.java +++ b/src/main/java/coordinate/view/OutputView.java @@ -5,6 +5,9 @@ import coordinate.domain.Line; import coordinate.domain.Points; +import java.util.HashMap; +import java.util.Map; + public class OutputView { private static final String POLE_HEIGHT = "|"; private static final String NEW_LINE = "\n"; From e6a16c74ff3644568aa92777dc60e678a88ca779 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:49:03 +0900 Subject: [PATCH 40/44] =?UTF-8?q?refactor:=20Line.class=20=EA=B9=8C?= =?UTF-8?q?=EC=A7=80=20Figure=20=EC=83=81=EC=86=8D=EB=B0=9B=EC=9D=84=20?= =?UTF-8?q?=EC=88=98=20=EC=9E=88=EA=B2=8C=20=EC=B6=94=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/CoordinateApp.java | 12 ++----- .../coordinate/domain/AbstractFigure.java | 22 ++++++++++++ src/main/java/coordinate/domain/Figure.java | 34 +++---------------- src/main/java/coordinate/domain/Line.java | 27 +++++++-------- src/main/java/coordinate/domain/Lines.java | 2 +- .../java/coordinate/domain/RealFigure.java | 16 +++++++++ src/main/java/coordinate/domain/Square.java | 6 ++-- src/main/java/coordinate/domain/Triangle.java | 18 +++++----- .../domain/generator/FigureFactory.java | 6 ++-- .../generator/LinesSquareGenerator.java | 2 +- src/test/java/coordinate/domain/LineTest.java | 4 +-- 11 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 src/main/java/coordinate/domain/AbstractFigure.java create mode 100644 src/main/java/coordinate/domain/RealFigure.java diff --git a/src/main/java/coordinate/CoordinateApp.java b/src/main/java/coordinate/CoordinateApp.java index 100497e2..4c1c5e3c 100644 --- a/src/main/java/coordinate/CoordinateApp.java +++ b/src/main/java/coordinate/CoordinateApp.java @@ -1,7 +1,6 @@ package coordinate; import coordinate.domain.Figure; -import coordinate.domain.Line; import coordinate.domain.Points; import coordinate.domain.generator.FigureFactory; import coordinate.domain.generator.PointsGenerator; @@ -11,19 +10,14 @@ public class CoordinateApp { public static void main(String[] args) { Points points = generatePoints(); - if (points.size() == Line.NUMBER_OF_POINTS) { - Line line = new Line(points); - OutputView.printResult(line); - } else { - Figure figure = FigureFactory.generate(points); - OutputView.printResult(figure); - } + Figure figure = FigureFactory.generate(points); + OutputView.printResult(figure); } private static Points generatePoints() { try { return new PointsGenerator(InputView.inputCoordinate()).generate(); - }catch (IllegalArgumentException e){ + } catch (IllegalArgumentException e) { System.err.println(e.getMessage()); return generatePoints(); } diff --git a/src/main/java/coordinate/domain/AbstractFigure.java b/src/main/java/coordinate/domain/AbstractFigure.java new file mode 100644 index 00000000..15ec35fb --- /dev/null +++ b/src/main/java/coordinate/domain/AbstractFigure.java @@ -0,0 +1,22 @@ +package coordinate.domain; + +public abstract class AbstractFigure implements Figure { + private Points points; + + AbstractFigure(final Points points) { + this.points = points; + validateSize(); + } + + private void validateSize() { + if (size() != points.size()) { + throw new IllegalArgumentException(getName() + "는 " + points + " 개의 점이 필요합니다."); + } + } + + @Override + public Points getPoints() { + return points; + } +} + diff --git a/src/main/java/coordinate/domain/Figure.java b/src/main/java/coordinate/domain/Figure.java index 5bd5a796..62e51938 100644 --- a/src/main/java/coordinate/domain/Figure.java +++ b/src/main/java/coordinate/domain/Figure.java @@ -1,35 +1,11 @@ package coordinate.domain; -import coordinate.domain.generator.LinesGenerator; +public interface Figure { + int size(); -public abstract class Figure implements AreaCalculator { - private Points points; - private Lines lines; + double area(); - protected Figure(final Points points, final LinesGenerator linesGenerator) { - this.points = points; - this.lines = linesGenerator.generate(); - validateSize(); - } + Points getPoints(); - protected void validateSize() { - if (size() != points.size()) { - throw new IllegalArgumentException(); - } - } - - protected abstract int size(); - - @Override - public double area() { - throw new UnsupportedOperationException(); - } - - public Points getPoints() { - return points; - } - - public Lines getLines() { - return lines; - } + String getName(); } diff --git a/src/main/java/coordinate/domain/Line.java b/src/main/java/coordinate/domain/Line.java index 7c71c351..da8e4e18 100644 --- a/src/main/java/coordinate/domain/Line.java +++ b/src/main/java/coordinate/domain/Line.java @@ -1,29 +1,26 @@ package coordinate.domain; - -public class Line { +public class Line extends AbstractFigure { public static final int NUMBER_OF_POINTS = 2; - private final Points points; - public Line(Points points) { - this.points = points; - validateSize(points); + super(points); } - private void validateSize(final Points points) { - if (points.size() != NUMBER_OF_POINTS) { - throw new IllegalArgumentException("선은 점이 두개 필요합니다."); - } + @Override + public int size() { + return NUMBER_OF_POINTS; } - public double length() { - Point a = points.get(0); - Point b = points.get(1); + @Override + public double area() { + Point a = getPoints().get(0); + Point b = getPoints().get(1); return a.length(b); } - public Points getPoints() { - return points; + @Override + public String getName() { + return "선 "; } } diff --git a/src/main/java/coordinate/domain/Lines.java b/src/main/java/coordinate/domain/Lines.java index 7bea3045..5f637d4f 100644 --- a/src/main/java/coordinate/domain/Lines.java +++ b/src/main/java/coordinate/domain/Lines.java @@ -12,6 +12,6 @@ public Lines(final List lines) { } public List lengths() { - return lines.stream().mapToDouble(Line::length).boxed().collect(Collectors.toList()); + return lines.stream().mapToDouble(Line::area).boxed().collect(Collectors.toList()); } } diff --git a/src/main/java/coordinate/domain/RealFigure.java b/src/main/java/coordinate/domain/RealFigure.java new file mode 100644 index 00000000..ee85bc09 --- /dev/null +++ b/src/main/java/coordinate/domain/RealFigure.java @@ -0,0 +1,16 @@ +package coordinate.domain; + +import coordinate.domain.generator.LinesGenerator; + +public abstract class RealFigure extends AbstractFigure { + private Lines lines; + + public RealFigure(final Points points, final LinesGenerator linesGenerator) { + super(points); + this.lines = linesGenerator.generate(); + } + + Lines getLines() { + return lines; + } +} diff --git a/src/main/java/coordinate/domain/Square.java b/src/main/java/coordinate/domain/Square.java index 807a1551..281ba95f 100644 --- a/src/main/java/coordinate/domain/Square.java +++ b/src/main/java/coordinate/domain/Square.java @@ -3,7 +3,7 @@ import coordinate.domain.generator.LinesSquareGenerator; -public final class Square extends Figure { +public final class Square extends RealFigure { public static final int NUMBER_OF_POINTS = 4; public Square(final Points points) { @@ -25,7 +25,7 @@ private double getDiagonal(final Points points, final int i, final int i2) { } @Override - protected int size() { + public int size() { return NUMBER_OF_POINTS; } @@ -35,7 +35,7 @@ public double area() { } @Override - public String toString() { + public String getName() { return "사각형"; } } diff --git a/src/main/java/coordinate/domain/Triangle.java b/src/main/java/coordinate/domain/Triangle.java index 33b003d9..5c2a1d80 100644 --- a/src/main/java/coordinate/domain/Triangle.java +++ b/src/main/java/coordinate/domain/Triangle.java @@ -2,19 +2,13 @@ import coordinate.domain.generator.LinesTriangleGenerator; -public final class Triangle extends Figure { +public final class Triangle extends RealFigure { public static final int NUMBER_OF_POINTS = 3; public Triangle(final Points points) { super(points, new LinesTriangleGenerator(points)); } - @Override - public double area() { - Lines lines = super.getLines(); - return Math.sqrt(calculateX(lines, calculateS(lines))); - } - private double calculateS(final Lines lines) { return lines.lengths().stream().mapToDouble(Double::doubleValue).sum() / 2.0; } @@ -27,12 +21,18 @@ private double calculateX(final Lines lines, final double s) { } @Override - protected int size() { + public int size() { return NUMBER_OF_POINTS; } @Override - public String toString() { + public double area() { + Lines lines = super.getLines(); + return Math.sqrt(calculateX(lines, calculateS(lines))); + } + + @Override + public String getName() { return "삼각형"; } } diff --git a/src/main/java/coordinate/domain/generator/FigureFactory.java b/src/main/java/coordinate/domain/generator/FigureFactory.java index 38ec3717..0bddb25a 100644 --- a/src/main/java/coordinate/domain/generator/FigureFactory.java +++ b/src/main/java/coordinate/domain/generator/FigureFactory.java @@ -1,11 +1,9 @@ package coordinate.domain.generator; -import coordinate.domain.Figure; -import coordinate.domain.Points; -import coordinate.domain.Square; -import coordinate.domain.Triangle; +import coordinate.domain.*; public enum FigureFactory { + LINE(Line.NUMBER_OF_POINTS, Line::new), TRIANGLE(Triangle.NUMBER_OF_POINTS, Triangle::new), SQUARE(Square.NUMBER_OF_POINTS, Square::new); diff --git a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java index 91155520..e3259d43 100644 --- a/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java +++ b/src/main/java/coordinate/domain/generator/LinesSquareGenerator.java @@ -30,7 +30,7 @@ public Lines generate() { private List filterDiagonal(List lines) { lines = lines.stream() - .sorted((line1, line2) -> (int) (line1.length() - line2.length())) + .sorted((line1, line2) -> (int) (line1.area() - line2.area())) .limit(lines.size() - 2) .collect(Collectors.toList()); return lines; diff --git a/src/test/java/coordinate/domain/LineTest.java b/src/test/java/coordinate/domain/LineTest.java index 1a421f1b..f847d7a8 100644 --- a/src/test/java/coordinate/domain/LineTest.java +++ b/src/test/java/coordinate/domain/LineTest.java @@ -3,8 +3,6 @@ import coordinate.domain.generator.PointsGenerator; import org.junit.jupiter.api.Test; -import java.util.Arrays; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Java6Assertions.offset; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -14,7 +12,7 @@ public class LineTest { @Test void 거리_테스트() { double expect = Math.sqrt(8); - assertThat(new Line(new PointsGenerator("(0,0)-(2,2)").generate()).length()) + assertThat(new Line(new PointsGenerator("(0,0)-(2,2)").generate()).area()) .isEqualTo(expect, offset(0.00099)); } From 4819794bf87927337397d0ea55c30e3241375e32 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:49:24 +0900 Subject: [PATCH 41/44] =?UTF-8?q?refactor:=20UI.class=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EC=83=9D=EC=84=B1=EA=B3=BC=20=EB=8F=99?= =?UTF-8?q?=EC=8B=9C=EC=97=90=20=EC=A2=8C=ED=91=9C=20=EA=B7=B8=EB=A0=A4?= =?UTF-8?q?=EC=A7=80=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/UI.java | 7 ++++--- src/main/java/coordinate/view/OutputView.java | 16 +++------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main/java/coordinate/UI.java b/src/main/java/coordinate/UI.java index 74aeec66..052b2232 100644 --- a/src/main/java/coordinate/UI.java +++ b/src/main/java/coordinate/UI.java @@ -6,17 +6,18 @@ public class UI { private boolean[][] coordinates; - public UI() { + public UI(Points points) { this.coordinates = new boolean[Coordinate.MAX_BOUND][Coordinate.MAX_BOUND]; + drawPoints(points); } - public void drawPoints(Points points) { + private void drawPoints(Points points) { for (int i = 0; i < points.size(); i++) { coordinates[points.get(i).getX()][points.get(i).getY()] = true; } } public boolean[][] getCoordinates() { - return coordinates; + return coordinates.clone(); } } diff --git a/src/main/java/coordinate/view/OutputView.java b/src/main/java/coordinate/view/OutputView.java index 3c02ea0e..97827195 100644 --- a/src/main/java/coordinate/view/OutputView.java +++ b/src/main/java/coordinate/view/OutputView.java @@ -2,32 +2,22 @@ import coordinate.UI; import coordinate.domain.Figure; -import coordinate.domain.Line; import coordinate.domain.Points; -import java.util.HashMap; -import java.util.Map; - public class OutputView { private static final String POLE_HEIGHT = "|"; private static final String NEW_LINE = "\n"; public static void printResult(final Figure figure) { printCoordinates(figure.getPoints()); - System.out.println(figure.toString() + " 넓이는 " + figure.area()); - } - - public static void printResult(final Line line) { - printCoordinates(line.getPoints()); - System.out.println("두 점 사이 거리는 " + line.length()); + System.out.println(figure.getName() + " 넓이는 " + figure.area()); } private static void printCoordinates(final Points points) { - UI ui = new UI(); - ui.drawPoints(points); - StringBuilder sb = new StringBuilder(); + UI ui = new UI(points); boolean[][] coordinates = ui.getCoordinates(); + StringBuilder sb = new StringBuilder(); for (int i = coordinates.length - 1; i >= 0; i--) { sb.append(i).append(POLE_HEIGHT); for (int j = 0; j < coordinates.length; j++) { From 15354271f310636f51cba0303e3cea0c7d5f9b00 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:56:05 +0900 Subject: [PATCH 42/44] =?UTF-8?q?refactor:=20Car=20=EC=9D=B8=ED=84=B0?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/car/domain/AbstractCar.java | 12 ++++++++++++ src/main/java/car/domain/Avante.java | 4 ++-- src/main/java/car/domain/Car.java | 17 +++-------------- src/main/java/car/domain/K5.java | 4 ++-- src/main/java/car/domain/RentCompany.java | 10 +++++----- src/main/java/car/domain/Sonata.java | 4 ++-- 6 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 src/main/java/car/domain/AbstractCar.java diff --git a/src/main/java/car/domain/AbstractCar.java b/src/main/java/car/domain/AbstractCar.java new file mode 100644 index 00000000..eb1ee5b4 --- /dev/null +++ b/src/main/java/car/domain/AbstractCar.java @@ -0,0 +1,12 @@ +package car.domain; + +public abstract class AbstractCar { + double distance; + double fuelEfficiency; + + abstract String getName(); + + double getChargeQuantity() { + return distance / fuelEfficiency; + } +} diff --git a/src/main/java/car/domain/Avante.java b/src/main/java/car/domain/Avante.java index d4cbfc33..d5deccbf 100644 --- a/src/main/java/car/domain/Avante.java +++ b/src/main/java/car/domain/Avante.java @@ -1,12 +1,12 @@ package car.domain; -public class Avante extends Car { +public class Avante extends AbstractCar { private static final String NAME = "Avante"; private static final double FUEL_EFFICIENCY = 15; public Avante(double distance) { super.distance = distance; - super.fuelEffiency = FUEL_EFFICIENCY; + super.fuelEfficiency = FUEL_EFFICIENCY; } @Override diff --git a/src/main/java/car/domain/Car.java b/src/main/java/car/domain/Car.java index b8b05035..fa767fbb 100644 --- a/src/main/java/car/domain/Car.java +++ b/src/main/java/car/domain/Car.java @@ -1,18 +1,7 @@ package car.domain; -public abstract class Car { - double distance; - double fuelEffiency; +public interface Car { + String getName(); - /** - * 차종의 이름 - */ - abstract String getName(); - - /** - * 주입해야할 연료량을 구한다. - */ - double getChargeQuantity() { - return distance / fuelEffiency; - } + double getChargeQuantity(); } diff --git a/src/main/java/car/domain/K5.java b/src/main/java/car/domain/K5.java index 6ab8b84f..f3a0b8e2 100644 --- a/src/main/java/car/domain/K5.java +++ b/src/main/java/car/domain/K5.java @@ -1,12 +1,12 @@ package car.domain; -public class K5 extends Car { +public class K5 extends AbstractCar { private static final String NAME = "K5"; private static final double FUEL_EFFICIENCY = 13; public K5(double distance) { super.distance = distance; - super.fuelEffiency = FUEL_EFFICIENCY; + super.fuelEfficiency = FUEL_EFFICIENCY; } @Override diff --git a/src/main/java/car/domain/RentCompany.java b/src/main/java/car/domain/RentCompany.java index cfb9fe2b..f4f57bb2 100644 --- a/src/main/java/car/domain/RentCompany.java +++ b/src/main/java/car/domain/RentCompany.java @@ -5,7 +5,7 @@ public class RentCompany { private static final String NEWLINE = System.getProperty("line.separator"); - private List cars = new ArrayList<>(); + private List abstractCars = new ArrayList<>(); private RentCompany() { } @@ -14,14 +14,14 @@ public static RentCompany create() { return new RentCompany(); } - public void addCar(Car car) { - cars.add(car); + public void addCar(AbstractCar abstractCar) { + abstractCars.add(abstractCar); } public String generateReport() { StringBuilder sb = new StringBuilder(); - for (Car car : cars) { - sb.append(car.getName()).append(" : ").append((int) car.getChargeQuantity()).append("리터" + NEWLINE); + for (AbstractCar abstractCar : abstractCars) { + sb.append(abstractCar.getName()).append(" : ").append((int) abstractCar.getChargeQuantity()).append("리터" + NEWLINE); } return sb.toString(); } diff --git a/src/main/java/car/domain/Sonata.java b/src/main/java/car/domain/Sonata.java index d60f2cb2..668241da 100644 --- a/src/main/java/car/domain/Sonata.java +++ b/src/main/java/car/domain/Sonata.java @@ -1,12 +1,12 @@ package car.domain; -public class Sonata extends Car { +public class Sonata extends AbstractCar { private static final String NAME = "Sonata"; private static final double FUEL_EFFICIENCY = 10; public Sonata(double distance) { super.distance = distance; - super.fuelEffiency = FUEL_EFFICIENCY; + super.fuelEfficiency = FUEL_EFFICIENCY; } @Override From 300fd6fc09ffb5a729d593cdbd89881b5686ae34 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:57:24 +0900 Subject: [PATCH 43/44] =?UTF-8?q?refactor:=20RealFigure.class=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EC=A0=91=EA=B7=BC=EC=A0=9C=EC=96=B4?= =?UTF-8?q?=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/RealFigure.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/coordinate/domain/RealFigure.java b/src/main/java/coordinate/domain/RealFigure.java index ee85bc09..706e0fb5 100644 --- a/src/main/java/coordinate/domain/RealFigure.java +++ b/src/main/java/coordinate/domain/RealFigure.java @@ -5,7 +5,7 @@ public abstract class RealFigure extends AbstractFigure { private Lines lines; - public RealFigure(final Points points, final LinesGenerator linesGenerator) { + RealFigure(final Points points, final LinesGenerator linesGenerator) { super(points); this.lines = linesGenerator.generate(); } From bc1a5219d6a6de4501477b3e3f6f7a081bf94b47 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 25 May 2019 16:57:36 +0900 Subject: [PATCH 44/44] =?UTF-8?q?refactor:=20AreaCalculator.class=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/coordinate/domain/AreaCalculator.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 src/main/java/coordinate/domain/AreaCalculator.java diff --git a/src/main/java/coordinate/domain/AreaCalculator.java b/src/main/java/coordinate/domain/AreaCalculator.java deleted file mode 100644 index 3c1fa0ac..00000000 --- a/src/main/java/coordinate/domain/AreaCalculator.java +++ /dev/null @@ -1,6 +0,0 @@ -package coordinate.domain; - -@FunctionalInterface -public interface AreaCalculator { - double area(); -}