forked from next-step/java-coordinate-playground
-
Notifications
You must be signed in to change notification settings - Fork 3
3차 코드 구현 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhlee128
wants to merge
4
commits into
CODE-CLEANERS:main
Choose a base branch
from
dhlee128:DH
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
3차 코드 구현 #2
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package Figure; | ||
|
|
||
| public interface Calculator { | ||
|
|
||
| double calculate(); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package Figure; | ||
|
|
||
| import domain.Point; | ||
| import domain.Points; | ||
|
|
||
| public class Line implements Calculator { | ||
|
|
||
| private Points points; | ||
|
|
||
| public Line(Points points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| @Override | ||
| public double calculate() { | ||
|
|
||
| Point point1 = points.getPoint(0); | ||
| Point point2 = points.getPoint(1); | ||
|
|
||
| return point1.getDifferDistance(point2); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package Figure; | ||
|
|
||
| import domain.Point; | ||
| import domain.Points; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Square implements Calculator { | ||
|
|
||
| private Points points; | ||
|
|
||
| public Square(Points points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| @Override | ||
| public double calculate() { | ||
|
|
||
| Point stand = points.getPoint(0); | ||
|
|
||
| List<Double> lens = new ArrayList<>(); | ||
|
|
||
| for(int i=1; i<4; i++) { | ||
| lens.add(stand.getDifferDistance(points.getPoint(i))); | ||
| } | ||
|
|
||
| Collections.sort(lens); | ||
|
|
||
| return lens.get(0)*lens.get(1); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package Figure; | ||
|
|
||
| import domain.Point; | ||
| import domain.Points; | ||
|
|
||
| public class Triangle implements Calculator { | ||
|
|
||
| private Points points; | ||
|
|
||
| public Triangle(Points points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| @Override | ||
| public double calculate() { | ||
|
|
||
| Point pointA = points.getPoint(0); | ||
| Point pointB = points.getPoint(1); | ||
| Point pointC = points.getPoint(2); | ||
|
|
||
| double area = 0.5 * ( | ||
| this.getTriangleLine(pointA, pointB, pointC) + | ||
| this.getTriangleLine(pointB, pointC, pointA) + | ||
| this.getTriangleLine(pointC, pointA, pointB) | ||
| ); | ||
|
|
||
| return Math.abs(area); | ||
|
|
||
| } | ||
|
|
||
| private int getTriangleLine(Point pointA, Point pointB, Point pointC) { | ||
| return pointA.getX() * (pointB.getY() - pointC.getY()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import coordinate.Process; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Process process = new Process(); | ||
| process.run(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package coordinate; | ||
|
|
||
| import Figure.Line; | ||
| import Figure.Square; | ||
| import Figure.Triangle; | ||
| import domain.Points; | ||
| import domain.Type; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class Process { | ||
|
|
||
| OutputView oView = new OutputView(); | ||
| InputView inputView = new InputView(); | ||
|
|
||
| public void run() { | ||
|
|
||
| oView.msgInputPoints(); | ||
|
|
||
| Points points = Points.newInstance(inputView.inputPoints()); | ||
|
|
||
| if(points.getSize()== Type.LINE.getCount()) { | ||
|
|
||
| oView.msgOutputLine(new Line(points).calculate()); | ||
|
|
||
| } else if(points.getSize()==Type.TRIANGLE.getCount()) { | ||
|
|
||
| oView.msgOutputTriangle(new Triangle(points).calculate()); | ||
|
|
||
| } else if(points.getSize()==Type.RECTANGLE.getCount()) { | ||
|
|
||
| oView.msgOutputSquare(new Square(points).calculate()); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package domain; | ||
|
|
||
| import utils.Constants; | ||
|
|
||
| public class Point { | ||
|
|
||
| private int x; | ||
| private int y; | ||
|
|
||
| private Point(int x, int y) { | ||
|
|
||
| this.x = x; | ||
| this.y = y; | ||
|
|
||
| } | ||
|
|
||
| public static Point newInstance(int x, int y) { | ||
|
|
||
| if (x < 0 || x > 24 || y < 0 || y > 24) { | ||
| throw new IllegalArgumentException(Constants.INPUT_RANGE_CHECK); | ||
| } | ||
|
|
||
| return new Point(x, y); | ||
| } | ||
|
|
||
| public static Point inputStrSeparator(String inStr) { | ||
|
|
||
| String[] values = inStr.replaceAll("[()]", "").split(","); | ||
|
|
||
| return newInstance(Integer.parseInt(values[0]), Integer.parseInt(values[1])); | ||
|
|
||
| } | ||
|
|
||
| public double getDifferDistance(Point point) { | ||
|
|
||
| return Math.sqrt(Math.pow(x - point.getX(), 2) + Math.pow(x - point.getY(), 2)); | ||
|
|
||
| } | ||
|
|
||
| public int getX() { | ||
|
|
||
| return this.x; | ||
|
|
||
| } | ||
|
|
||
| public int getY() { | ||
|
|
||
| return this.y; | ||
|
|
||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Points { | ||
|
|
||
| private List<Point> points; | ||
|
|
||
| private Points(List<Point> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public static Points newInstance(List<Point> points) { | ||
| return new Points(points); | ||
| } | ||
|
|
||
| public Point getPoint(int idx) { | ||
| return points.get(idx); | ||
| } | ||
|
|
||
| public int getSize() { | ||
| return points.size(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package domain; | ||
|
|
||
| public enum Type { | ||
|
|
||
| LINE(2), | ||
| TRIANGLE(3), | ||
| RECTANGLE(4); | ||
|
|
||
| private final int count; | ||
|
|
||
| Type(int count) { | ||
| this.count = count; | ||
| } | ||
|
|
||
| public int getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package utils; | ||
|
|
||
| public class Constants { | ||
|
|
||
| public static String INPUT_FORMAT_CHECK = "입력 좌표의 포맷을 확인"; | ||
|
|
||
| public static String INPUT_RANGE_CHECK = "좌표(x,y)는 모두 최소0 부터 최대 24까지만 허용"; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package view; | ||
|
|
||
| import domain.Point; | ||
| import domain.Type; | ||
| import utils.Constants; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class InputView { | ||
|
|
||
| public List<Point> inputPoints() { | ||
|
|
||
| List<Point> points = new ArrayList<>(); | ||
|
|
||
| try { | ||
|
|
||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| String line = br.readLine(); | ||
|
|
||
| InputView.validInputFormat(line); | ||
|
|
||
| String[] pointsStr = line.split("-"); | ||
|
|
||
| for(int i=0; i< pointsStr.length; i++) { | ||
|
|
||
| points.add(Point.inputStrSeparator(pointsStr[i])); | ||
|
|
||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
| return points; | ||
| } | ||
|
|
||
| public static void validInputFormat(String inStr) { | ||
|
|
||
| String[] strArr = inStr.split("-"); | ||
|
|
||
| if(strArr.length != Type.LINE.getCount() && strArr.length != Type.LINE.getCount() && strArr.length != Type.LINE.getCount()) { | ||
| throw new IllegalArgumentException(Constants.INPUT_FORMAT_CHECK); | ||
| } | ||
|
|
||
| for(String str:strArr) { | ||
| String[] checkArr = str.replaceAll("[()]", "").split(","); | ||
|
|
||
| if(checkArr.length!=2) throw new IllegalArgumentException(Constants.INPUT_FORMAT_CHECK); | ||
|
|
||
| try { | ||
| Integer.parseInt(checkArr[0]); | ||
| Integer.parseInt(checkArr[1]); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException(Constants.INPUT_FORMAT_CHECK); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package view; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| public void msgInputPoints() { | ||
| System.out.println("좌표를 입력하세요."); | ||
| } | ||
|
|
||
| public void msgOutputLine(double result) { | ||
| System.out.println("두 점 사이의 거리는 "+result); | ||
| } | ||
|
|
||
| public void msgOutputSquare(double result) { | ||
| System.out.println("사각형 넓이는 "+result); | ||
| } | ||
|
|
||
| public void msgOutputTriangle(double result) { | ||
| System.out.println("삼각형 넓이는 "+result); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이경우 직선만이 아닌 대각선의 길이도 구해야 하는데, 직선만 구해서 계산하는 방법은 어떤 것이 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵,, 특정 한 점으로부터 나머지 세 점까지의 거리(getDifferDistance)를 구했습니다. 3개 중 가장 큰것이 대각선의 길이라서 나머지 두개만을 사용을 해봤는데 ㅎㅎ
이런식으로 계산하지않고 다른 방식이 있는지 고민해보겠습니다.