Repository navigation
coordinate step 2 완료했습니다. #126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| //import model.Line; | ||
|
|
||
| import model.Line; | ||
| import model.MakeLine; | ||
| import view.InputView; | ||
| import view.PrintView; | ||
|
|
||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| PrintView.printAll(); | ||
| PrintView printView = new PrintView(); | ||
| String coordinateNum = InputView.inputCoordinate(); | ||
| try { | ||
| Line line = MakeLine.makeLine(coordinateNum); | ||
| printView.printAll(line); | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| main(args); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //point (x,y)만 담고있는 클래스를 만들기. | ||
| //point를 여러개 갖고있는 클래스 만들기 |
Empty file.
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,47 @@ | ||
| package model; | ||
|
|
||
| public class Line { | ||
|
|
||
| private Point a; | ||
| private Point b; | ||
|
|
||
| public Line(Point a, Point b) { | ||
| this.a = a; | ||
| this.b = b; | ||
| } | ||
|
|
||
| public Point getA() { | ||
| return a; | ||
| } | ||
|
|
||
| public Point getB() { | ||
| return b; | ||
| } | ||
|
|
||
| //Math.abs는 절대값 | ||
| public int minusX() { | ||
| return Math.abs(a.getX() - b.getX()); | ||
| } | ||
|
|
||
| public int minusY() { | ||
| return Math.abs(a.getY() - b.getY()); | ||
| } | ||
|
|
||
| public int square(int num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| public double lineDistance() { | ||
| int x = minusX(); | ||
| int y = minusY(); | ||
| return Math.sqrt(square(x) + square(y)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Line{" + | ||
| "a=" + a + | ||
| ", b=" + b + | ||
| '}'; | ||
| } | ||
| } |
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,12 @@ | ||
| package model; | ||
|
|
||
| public class MakeLine { | ||
|
|
||
| public static Line makeLine(String coordinate) throws Exception { | ||
| String removeBraket = coordinate.replace("(", "").replace(")", ""); | ||
| String[] groups = removeBraket.split("-"); | ||
| Point a = Point.position(groups[0]); | ||
| Point b = Point.position(groups[1]); | ||
| return new Line(a, b); | ||
| } | ||
| } |
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,45 @@ | ||
| package model; | ||
|
|
||
| public class Point { | ||
|
|
||
| private int x; | ||
| private int y; | ||
|
|
||
| private Point(int x, int y) throws Exception { | ||
| // IllegalArgumentException 매개변수가 의도하지 않은 상황을 유발시킬 때 | ||
| // IllegalStateException 메소드를 호출하기 위한 상태가 아닐 때 | ||
| if (x < 0 || x > 24) { | ||
| throw new IllegalArgumentException("0이상 또는 24 이하만 입력해주십시오."); | ||
| } | ||
| if (y < 0 || y > 24) { | ||
| throw new IllegalArgumentException("0이상 또는 24 이하만 입력해주십시오."); | ||
| } | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| // 항상 view 패키지가 아니면 클래스 메소드를 사용하면 안된다고 생각했었다. | ||
| //팩토리 메소드를 처음 알게 되었고, 자세히 알아보고 써봐야겠다. | ||
| public static Point position(String string) throws Exception { | ||
| String[] list = string.split(","); | ||
| return new Point(Integer.parseInt(list[0]), Integer.parseInt(list[1])); | ||
| } | ||
|
|
||
| public int getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public int getY() { | ||
| return y; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Point{" + | ||
| "x=" + x + | ||
| ", y=" + 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,14 @@ | ||
| package view; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class InputView { | ||
| static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static String inputCoordinate() { | ||
| System.out.println("좌표를 입력하세요"); | ||
| String seperateDash = scanner.next(); | ||
| return seperateDash; | ||
| } | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -1,48 +1,101 @@ | ||
| package view; | ||
|
|
||
| import model.Line; | ||
| import model.Point; | ||
|
|
||
| public class PrintView { | ||
| private static StringBuilder stringBuilder = new StringBuilder(); | ||
| private static StringBuilder coordinatePrint = new StringBuilder(); | ||
| public static final int COORDINATE = 24; | ||
|
|
||
| public static void printAll() { | ||
| printCoordinate(); | ||
| System.out.println(String.valueOf(stringBuilder)); | ||
|
|
||
| public void printAll(Line line) { | ||
| Point a = line.getA(); | ||
| Point b = line.getB(); | ||
| printCoordinate(a, b); | ||
| printDistance(line.lineDistance()); | ||
| System.out.println(coordinatePrint.toString()); | ||
| } | ||
|
|
||
| public static void printCoordinate() { | ||
| printBarY(); | ||
| stringBuilder.append(String.format("%3s", "┼")); | ||
| public static void printCoordinate(Point a, Point b) { | ||
| printBarY(a, b); | ||
| coordinatePrint.append(String.format("%3s", "┼")); | ||
| printBarX(); | ||
| } | ||
|
|
||
| private static void printBarX() { | ||
| for (int i = 0; i < COORDINATE; i++) { | ||
| printRow(); | ||
| } | ||
| stringBuilder.append("\n"); | ||
| coordinatePrint.append("\n"); | ||
| coordinatePrint.append(" "); | ||
| for (int j = 0; j <= COORDINATE; j += 2) { | ||
| stringBuilder.append(String.format("%2d", j)); | ||
| stringBuilder.append(String.format("%6s", " ")); | ||
| coordinatePrint.append(String.format("%2d", j)); | ||
| coordinatePrint.append(String.format("%6s", " ")); | ||
| } | ||
| coordinatePrint.append("\n"); | ||
| } | ||
|
|
||
| private static void printBarY() { | ||
| for (int i = COORDINATE; i > 0; i -= 2) { | ||
| stringBuilder.append(String.format("%2d", i)); | ||
| private static void printBarY(Point a, Point b) { | ||
| for (int i = COORDINATE; i >= 1; i--) { | ||
| printNumY(i); | ||
| printHeight(); | ||
| stringBuilder.append("\n"); | ||
| if (i == a.getY()) { | ||
| skipDot(a); | ||
| } | ||
| if (i == b.getY()) { | ||
| aMatchYBar(a, b); | ||
| aMisMatchYBar(a, b); | ||
| } | ||
| coordinatePrint.append("\n"); | ||
| } | ||
| } | ||
|
|
||
| private static void skipDot(Point a) { | ||
| for (int j = 0; j < a.getX(); j++) { | ||
| coordinatePrint.append(String.format("%4s", " ")); | ||
| } | ||
| coordinatePrint.append("*"); | ||
| } | ||
|
|
||
| private static void aMatchYBar(Point a, Point b) { | ||
| if (a.getY() == b.getY()) { | ||
| for (int j = 0; j < Math.abs(b.getX() - a.getX()); j++) { | ||
| coordinatePrint.append(String.format("%4s", " ")); | ||
| } | ||
| coordinatePrint.append("*"); | ||
| } | ||
| } | ||
|
|
||
| private static void aMisMatchYBar(Point a, Point b) { | ||
| if (a.getY() != b.getY()) { | ||
| for (int j = 0; j < b.getX(); j++) { | ||
| coordinatePrint.append(String.format("%4s", " ")); | ||
| } | ||
| coordinatePrint.append("*"); | ||
| } | ||
| } | ||
|
|
||
| private static void printNumY(int i) { | ||
| if (i % 2 == 0) { | ||
| coordinatePrint.append(String.format("%2d", i)); | ||
| } | ||
| if (i % 2 == 1) { | ||
| coordinatePrint.append(String.format("%2s", " ")); | ||
| } | ||
| } | ||
|
|
||
| public static void printRow() { | ||
| for (int i = 0; i < 2; i++) { | ||
| stringBuilder.append("─"); | ||
| coordinatePrint.append("─"); | ||
| } | ||
| } | ||
|
|
||
| public static void printHeight() { | ||
| stringBuilder.append("┃"); | ||
| stringBuilder.append("\n"); | ||
| stringBuilder.append(String.format("%3s", "┃")); | ||
| coordinatePrint.append("┃"); | ||
| } | ||
|
|
||
| private static void printDistance(double lineDistance) { | ||
| coordinatePrint.append("점과 점사이의 간격 : "); | ||
| coordinatePrint.append(String.format("%-10.3f", lineDistance)); | ||
| } | ||
| } | ||
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,43 @@ | ||
| package model; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
|
|
||
| public class LineTest { | ||
| Point a = Point.position("1,2"); | ||
| Point b = Point.position("3,4"); | ||
| Line line = new Line(a, b); | ||
|
|
||
| public LineTest() throws Exception { | ||
| } | ||
|
|
||
| @Test | ||
| public void minusX() { | ||
| assertThat(line.minusX()).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
| public void minusY() { | ||
| assertThat(line.minusY()).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
| public void square() { | ||
| int x = line.minusX(); | ||
| int y = line.minusY(); | ||
| assertThat(line.square(x)).isEqualTo(4); | ||
| assertThat(line.square(y)).isEqualTo(4); | ||
| } | ||
|
|
||
| @Test | ||
| public void lineDistance() { | ||
| int x = line.minusX(); | ||
| int y = line.minusY(); | ||
| int xSquare = line.square(x); | ||
| int ySquare = line.square(y); | ||
| double distance = line.lineDistance(); | ||
| assertThat(distance).isEqualTo(xSquare + ySquare); | ||
| } | ||
| } |
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.
a.matchY(b)와 같이 구현해 본다.