Repository navigation
Step3 #129
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
Step3 #129
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,25 +1,23 @@ | ||
| //import model.Line; | ||
|
|
||
| import model.Line; | ||
| import model.MakeLine; | ||
| import model.*; | ||
| import view.InputView; | ||
| import view.PrintView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| PrintView printView = new PrintView(); | ||
| String coordinateNum = InputView.inputCoordinate(); | ||
| Parser.makeStrings(coordinateNum); | ||
| try { | ||
| Line line = MakeLine.makeLine(coordinateNum); | ||
| printView.printAll(line); | ||
| List<Point> points = PointsFactory.makePoints(coordinateNum); | ||
| AbstractFigures figures = Parser.makeFigures(points); | ||
| PrintView printView = new PrintView(); | ||
| printView.printAll(figures, points); | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| main(args); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //point (x,y)만 담고있는 클래스를 만들기. | ||
| //point를 여러개 갖고있는 클래스 만들기 |
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 model; | ||
|
|
||
| public abstract class AbstractFigures { | ||
| public abstract double getValue(); | ||
|
|
||
| public static int exponent(int num) { | ||
| return (int) Math.pow(num, 2); | ||
| } | ||
| } |
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,25 @@ | ||
| package model; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| // static block 클래스에 List<Points>를 넣으려고 많은 과정을 거치며 클래스 변수는 | ||
| // 클래스 메소드에 들어갈 수 없다는 것을 알게 되었다. | ||
| // static 메솓를 넣으려고 할 때, java.lang.ExceptionInInitializerError에가 나오는 것을 명심하자! | ||
|
|
||
| public class Figures { | ||
|
|
||
| private static List<Point> points; | ||
| private static Map<Integer, AbstractFigures> map = new HashMap<>(); | ||
|
|
||
| public Figures(List<Point> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public AbstractFigures select(int size) { | ||
| map.put(2, new Line(points)); | ||
| map.put(3, new Triangle(points)); | ||
| map.put(4, new Rectangle(points)); | ||
| return map.get(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 |
|---|---|---|
| @@ -1,47 +1,43 @@ | ||
| package model; | ||
|
|
||
| public class Line { | ||
| import java.util.List; | ||
|
|
||
| private Point a; | ||
| private Point b; | ||
| public class Line extends AbstractFigures { | ||
|
|
||
| public Line(Point a, Point b) { | ||
| this.a = a; | ||
| this.b = b; | ||
| private List<Point> points; | ||
|
|
||
| public Line(List<Point> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public Point getA() { | ||
| return a; | ||
| public Point getA(int x) { | ||
| return points.get(x); | ||
| } | ||
|
|
||
| public Point getB() { | ||
| return b; | ||
| public Point getB(int y) { | ||
| return points.get(y); | ||
| } | ||
|
|
||
| //Math.abs는 절대값 | ||
| public int minusX() { | ||
| return Math.abs(a.getX() - b.getX()); | ||
| return Math.abs(getA(0).minusXaxis(getB(1))); | ||
| } | ||
|
|
||
| public int minusY() { | ||
| return Math.abs(a.getY() - b.getY()); | ||
| } | ||
|
|
||
| public int square(int num) { | ||
| return num * num; | ||
| return Math.abs(getA(0).minusYaxis(getB(1))); | ||
| } | ||
|
|
||
| public double lineDistance() { | ||
| @Override | ||
| public double getValue() { | ||
| int x = minusX(); | ||
| int y = minusY(); | ||
| return Math.sqrt(square(x) + square(y)); | ||
| return Math.sqrt(exponent(x) + exponent(y)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Line{" + | ||
| "a=" + a + | ||
| ", b=" + b + | ||
| "points=" + points + | ||
| '}'; | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Parser { | ||
|
|
||
| public static String[] makeStrings(String coordinate) { | ||
| String removeBraket = coordinate.replace("(", "").replace(")", ""); | ||
| String[] groups = removeBraket.split("-"); | ||
| return groups; | ||
| } | ||
|
|
||
| public static AbstractFigures makeFigures(List<Point> points) { | ||
| Figures figures = new Figures(points); | ||
| return figures.select(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
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 @@ | ||
| package model; | ||
|
|
||
| public class PointCreator { | ||
|
|
||
| public static Point position(String string) { | ||
| String[] list = string.split(","); | ||
| return new Point(Integer.parseInt(list[0]), Integer.parseInt(list[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,16 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PointsFactory { | ||
|
|
||
| public static List<Point> makePoints(String coordinates) { | ||
| String[] groups = Parser.makeStrings(coordinates); | ||
| ArrayList<Point> points = new ArrayList<>(); | ||
| for (int i = 0; i < groups.length; i++) { | ||
| points.add(PointCreator.position(groups[i])); | ||
| } | ||
| return points; | ||
| } | ||
| } |
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,101 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Rectangle extends AbstractFigures { | ||
| private static final int FIRST_SIZE = 0; | ||
| private static final int RECT_SIZE = 4; | ||
| private List<Point> points; | ||
| private Point pointFirst; | ||
|
|
||
| public Rectangle(List<Point> points) { | ||
| this.points = points; | ||
| this.pointFirst = points.get(0); | ||
| } | ||
|
|
||
| public int minusX(int num) { | ||
| if (num == FIRST_SIZE) { | ||
| return pointFirst.minusXaxis(yNumber()); | ||
| } | ||
| if (num > FIRST_SIZE && num < RECT_SIZE) { | ||
| return points.get(num).minusXaxis(xNumber()); | ||
| } | ||
| throw new IllegalArgumentException("직각 사각형으로 만들어주세요."); | ||
| } | ||
|
|
||
| // 예외처리 | ||
| public int minusY(int num) { | ||
| if (num == FIRST_SIZE) { | ||
| return pointFirst.minusYaxis(xNumber()); | ||
| } | ||
| if (num > FIRST_SIZE && num < RECT_SIZE) { | ||
| return points.get(num).minusYaxis(yNumber()); | ||
| } | ||
| throw new IllegalArgumentException("직각 사각형으로 만들어주세요."); | ||
| } | ||
|
|
||
| public Point xNumber() { | ||
| for (int i = 1; i < RECT_SIZE; i++) { | ||
| if (pointFirst.isSameXaxis(points.get(i))) { | ||
| return points.get(i); | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("y 축이 잘못 되었습니다."); | ||
| } | ||
|
|
||
|
|
||
| public Point yNumber() { | ||
| for (int i = 1; i < RECT_SIZE; i++) { | ||
| if (pointFirst.isSameYaxis(points.get(i))) { | ||
| return points.get(i); | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("x 축이 잘못 되었습니다."); | ||
| } | ||
|
|
||
| public int square(int x, int y) { | ||
| return exponent(x) + exponent(y); | ||
| } | ||
|
|
||
| public int doubleSquare(int x, int y) { | ||
| return exponent(x) * exponent(y); | ||
| } | ||
|
|
||
| public int differentFirst() { | ||
| for (int i = 1; i < RECT_SIZE; i++) { | ||
| if (pointFirst.isPassable(points.get(i))) { | ||
| return i; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("사각형이 아닙니다."); | ||
| } | ||
|
|
||
| public int diagonol() { | ||
| int minusX = xNumber().minusXaxis(yNumber()); | ||
| int minusY = xNumber().minusYaxis(yNumber()); | ||
| return square(minusX, minusY); | ||
| } | ||
|
|
||
| public int extent(int num) { | ||
| return square(minusX(num), minusY(num)); | ||
| } | ||
|
|
||
| public double area() { | ||
| return doubleSquare(minusX(FIRST_SIZE), minusY(FIRST_SIZE)); | ||
| } | ||
|
|
||
| @Override | ||
| public double getValue() { | ||
| if (diagonol() != extent(FIRST_SIZE) || diagonol() != extent(differentFirst())) { | ||
| throw new IllegalArgumentException("직각 사각형으로 만들어 주십시오."); | ||
| } | ||
| return diagonol(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Rectangle{" + | ||
| "points=" + points + | ||
| '}'; | ||
| } | ||
| } |
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.
이와 같이 구현할 경우 매번 Map을 초기화하는 문제가 있다.
Map이 프로그램이 시작할 때 한번 초기화하고 재사용 가능한 구조로 구현해 본다.
힌트는 다음과 같은 인터페이스를 활용해야 한다.
인터페이스 학습 후 반영해 본다.