Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/main/java/coordinate/CoordinateMain.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package coordinate;

import coordinate.domain.Coordinate;
import coordinate.domain.Line;
import coordinate.domain.Point;
import coordinate.dto.PointDtoFactory;
import coordinate.inputview.InputView;
import coordinate.resultview.ResultView;

import java.util.ArrayList;

public class CoordinateMain {
public class CoordinateMain extends IllegalAccessException {
public static void main(String[] args) {
ArrayList<Point> points = InputView.input();
ArrayList<Point> points;
try {
points = InputView.input();
PointDtoFactory pointDtoFactory = new PointDtoFactory(points);

//dto 만들기
PointDtoFactory pointDtoFactory = new PointDtoFactory(points);
Coordinate coordinate = new Coordinate();
coordinate.drawPoint(pointDtoFactory.getPointDTO());

Coordinate coordinate = new Coordinate();
coordinate.drawPoint(pointDtoFactory.getPointDTO());
ResultView resultView = new ResultView(coordinate.getCoordinatesAxis(), coordinate.getCoordinatePlane());
resultView.toDo(points);

ResultView resultView = new ResultView(coordinate.getCoordinatesAxis(), coordinate.getCoordinatePlane());
resultView.disPlay(Line.calculatorLine(points));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
main(args);
}
}
}
11 changes: 9 additions & 2 deletions src/main/java/coordinate/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import java.util.ArrayList;

public class Line {
private ArrayList<Point> points;

public static double calculatorLine(ArrayList<Point> points) {
return points.get(0).calculateLIne(points.get(1));
public Line(ArrayList<Point> points) {
this.points = points;
}
public String calculate() {
if (points.size() == 2) {
return "직선의 길이는 " + points.get(0).calculateLIne(points.get(1)) + " 입니다.";
}
return "직선이 아닙니다.";
}

}
48 changes: 46 additions & 2 deletions src/main/java/coordinate/domain/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import coordinate.dto.PointDTO;

import java.util.Objects;

public class Point {
private int xPoint;
private int yPoint;

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

public double calculateLIne(Point point) {
Expand All @@ -17,6 +19,34 @@ public double calculateLIne(Point point) {
return Math.sqrt((Math.pow(a,2) + Math.pow(b,2)));
}

public Boolean commpareXPoint(Point point) {
if (this.xPoint == point.xPoint) {
return true;
}
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return this.xPoint == point.xPoint;과 같이 구현 가능함.
Y도 같음

}

public Boolean commpareYPoint(Point point) {
if (this.yPoint == point.yPoint) {
return true;
}
return false;
}

public int subtractionXPoint(Point point) {
return Math.abs(this.xPoint - point.xPoint);
}
public int subtractionYPoint(Point point) {
return Math.abs(this.yPoint - point.yPoint);
}


public int isException(int num) {
if (num >25 || num < 0) {
throw new IllegalArgumentException("유효한 범위를 초과 합니다.");
}
return num;
}
public PointDTO makePointDTO() {
return new PointDTO(this.xPoint,this.yPoint);
}
Expand All @@ -28,4 +58,18 @@ public int getXPoint() {
public int getYPoint() {
return yPoint;
}

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

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

import java.util.ArrayList;
import java.util.HashSet;

public class Rectangle {
private ArrayList<Point> points;
private HashSet<Integer> base = new HashSet<>();
private HashSet<Integer> height = new HashSet<>();

public Rectangle(ArrayList<Point> points) {
this.points = points;
for (Point point : this.points) {
this.base.add(point.getXPoint());
this.height.add(point.getYPoint());
}
}

public String calculate() {
if (this.points.size() < 4) {
return "사각형이 아닙니다.";
}
if (isRectangle().equals(false)) {
throw new IllegalArgumentException("사각형이 아닙니다.");
}
int[] a = findBaseHeight();
return "사각형의 면적은 " + (a[0] * a[1]) + " 입니다.";
}

private int[] findBaseHeight() {
int[] BaseAndHeight = new int[2];
for (int i = 1; i < points.size(); i++) {
if (points.get(0).commpareXPoint(points.get(i)).equals(true)) {
BaseAndHeight[0] = points.get(0).subtractionYPoint(points.get(i));
}
if (points.get(0).commpareYPoint(points.get(i)).equals(true)) {
BaseAndHeight[1] = points.get(0).subtractionXPoint(points.get(i));
}
}
return BaseAndHeight;
}

private Boolean isRectangle() {
return (this.base.size() == 2 && this.height.size() == 2);
}
}
17 changes: 13 additions & 4 deletions src/main/java/coordinate/resultview/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package coordinate.resultview;

import coordinate.domain.CoordinateYLine;
import coordinate.domain.Line;
import coordinate.domain.Point;
import coordinate.domain.Rectangle;
import coordinate.utils.CoordinateString;

import java.util.ArrayList;
import java.util.HashMap;

public class ResultView {
private static final int FOUR = 4;
private static final int TWO = 2;

private static final int MAXiMUM_NUM = 24;
private static final String DASH = "---";
private static final String PLUS = " +";
Expand Down Expand Up @@ -39,10 +46,12 @@ private void displayXCoordinate() {
System.out.println();
}

public void disPlay(double pointLine) {
displayYCoordinate();
displayXCoordinate();
System.out.println("두 점 사이 거리는 " + pointLine);
public void toDo(ArrayList<Point> points) {
HashMap<Integer, String> toDo = new HashMap<>();
toDo.put(TWO, new Line(points).calculate());
toDo.put(FOUR, new Rectangle(points).calculate());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구현 잘 했네요. 👍

displayTest();
System.out.println(toDo.get(points.size()));
}

public void displayTest() {
Expand Down
44 changes: 40 additions & 4 deletions src/test/java/coordinate/CoordinateTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package coordinate;

import coordinate.domain.Coordinate;
import coordinate.domain.CoordinateYLine;
import coordinate.domain.Point;
import coordinate.domain.PointFactory;
import coordinate.domain.*;
import coordinate.resultview.ResultView;
import coordinate.utils.CoordinateString;
import org.junit.Test;
Expand All @@ -17,6 +14,45 @@ public class CoordinateTest {
//
//
///
/* @Test
public void pointtest() {
Point point1 = new Point(-1,2);
System.out.println(point1);
}*/

/*@Test
public void squareFalseTest() {
String input = "(12,12) - (13, 22) - (22,22) - (22,12)";
ArrayList<Point> points = PointFactory.testInput(input);
assertThat(new Rectangle(points).calculate()).isEqualTo("사각형이 아닙니다.");
}*/

@Test
public void squareTrueTest() {
ArrayList<Point> points;
String input = "(12,12) - (12, 22) - (22,22) - (22,12)";
points = PointFactory.testInput(input);
assertThat(new Rectangle(points).calculate()).isEqualTo("사각형의 면적은 100 입니다.");

String input1 = "(4,3) - (4, 6) - (7,3) - (7,6)";
points = PointFactory.testInput(input1);
assertThat(new Rectangle(points).calculate()).isEqualTo("사각형의 면적은 9 입니다.");

String input2 = "(5,5) - (10, 5) - (5,10) - (10,10)";
points = PointFactory.testInput(input2);
assertThat(new Rectangle(points).calculate()).isEqualTo("사각형의 면적은 25 입니다.");
}

@Test
public void hashMapTests() {
String input = "(12,12) - (12, 22)";
ArrayList<Point> points = PointFactory.testInput(input);
Coordinate coordinate = new Coordinate();

ResultView resultView = new ResultView(coordinate.getCoordinatesAxis(), coordinate.getCoordinatePlane());
resultView.toDo(points);
}

@Test
public void pointCalculator() {
String input = "(4,4) - (4,2)";
Expand Down