-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implements coordinate calculator except rectangle #7
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
base: oliviarla
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import coordinate.Figure; | ||
| import coordinate.FigureFactory; | ||
| import coordinate.Points; | ||
| import view.InputView; | ||
|
|
||
| public class CoordinateApplication { | ||
| private static InputView inputView; | ||
| private static FigureFactory figureFactory = new FigureFactory(); | ||
|
|
||
| public static void main(String[] args) { | ||
| inputView = new InputView(); | ||
|
||
| Points points = inputView.inputPoint(); | ||
| Figure figure = figureFactory.getInstance(points); | ||
| figure.output(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package coordinate; | ||
|
|
||
| public abstract class AbstractFigure implements Figure { | ||
| protected final Points points; | ||
|
|
||
| public AbstractFigure(Points points) { | ||
| if (points.size() != size()) { | ||
| throw new IllegalArgumentException(getName() + "의 길이는 " + size() + "이어야 합니다."); | ||
| } | ||
|
|
||
| this.points = points; | ||
| } | ||
|
|
||
| // protected Point getPoint(int index) { | ||
| // return points.getPoints().get(index); | ||
| // } | ||
|
||
|
|
||
| @Override | ||
| public Points getPoints() { | ||
| return points; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package coordinate; | ||
|
|
||
| public interface Figure { | ||
| Points getPoints(); | ||
|
|
||
| int size(); | ||
|
|
||
| String getName(); | ||
|
|
||
| double area(); | ||
|
|
||
| void output(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package coordinate; | ||
|
|
||
| public interface FigureCreator { | ||
| Figure create(Points points); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package coordinate; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class FigureFactory { | ||
| public static final Map<Integer, FigureCreator> FIGURE_MAP = new HashMap<>(); | ||
|
|
||
| static{ | ||
| FIGURE_MAP.put(Line.LINE_POINT_SIZE, Line::new); | ||
| FIGURE_MAP.put(Triangle.TRIANGLE_POINT_SIZE, Triangle::new); | ||
| FIGURE_MAP.put(Rectangle.RECTANGLE_POINT_SIZE, Rectangle::new); | ||
| } | ||
|
|
||
| public Figure getInstance(Points points) { | ||
| Figure figure; | ||
| try{ | ||
| figure = FIGURE_MAP.get(points.size()).create(points); | ||
| } catch (NullPointerException e){ | ||
| throw new IllegalArgumentException("점의 개수는 2~4개여야 합니다."); | ||
| } | ||
| return figure; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package coordinate; | ||
|
|
||
| public class Line extends AbstractFigure{ | ||
| public static final int LINE_POINT_SIZE = 2; | ||
|
|
||
| public Line(Points points) { | ||
| super(points); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return LINE_POINT_SIZE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "선"; | ||
| } | ||
|
|
||
| @Override | ||
| public double area() { | ||
| return points.get(0).getDistance(points.get(1)); | ||
| } | ||
|
|
||
| @Override | ||
| public void output(){ | ||
| System.out.printf("두 점 사이의 거리는 %f", area()); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| package coordinate; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| public class Point { | ||||||||||||||||||||||||||||||||||||||||||||
| private final int x; | ||||||||||||||||||||||||||||||||||||||||||||
| private final int y; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private Point(int x, int y) { | ||||||||||||||||||||||||||||||||||||||||||||
| this.x = x; | ||||||||||||||||||||||||||||||||||||||||||||
| if (x < 0 || x > 24) { | ||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| this.x = x; | |
| if (x < 0 || x > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| if (x < 0 || x > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| this.x = x; |
Outdated
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.
이렇게 선언하는 방식이 가독성이 좋을 것 같은데 어떻게 생각하시나요?
| this.x = x; | |
| if (x < 0 || x > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| this.y = y; | |
| if (y < 0 || y > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| if (x < 0 || x > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| if (y < 0 || y > 24) { | |
| throw new IllegalArgumentException(); | |
| } | |
| this.x = x; | |
| this.y = y; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package coordinate; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Points { | ||
| protected static final List<Point> points = new ArrayList<>(); | ||
|
|
||
| public void addPoint(Point point) { | ||
| this.points.add(point); | ||
| } | ||
|
|
||
| public Point get(int index) { | ||
| return points.get(index); | ||
| } | ||
|
|
||
| public static List<Point> getPoints() { | ||
| return points; | ||
| } | ||
|
||
|
|
||
| public int size(){ | ||
| return this.points.size(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package coordinate; | ||
|
|
||
| public class Rectangle extends AbstractFigure { | ||
| public static final int RECTANGLE_POINT_SIZE = 4; | ||
|
|
||
| public Rectangle(Points points) { | ||
| super(points); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return RECTANGLE_POINT_SIZE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "사각형"; | ||
| } | ||
|
|
||
| @Override | ||
| public double area() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void output() { | ||
| System.out.printf("사각형 넓이는 %d", area()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package coordinate; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Triangle extends AbstractFigure { | ||
| public static final int TRIANGLE_POINT_SIZE = 3; | ||
|
|
||
| public Triangle(Points points) { | ||
| super(points); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return TRIANGLE_POINT_SIZE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "삼각형"; | ||
| } | ||
|
|
||
| @Override | ||
| public double area() { | ||
| List<Double> distances = new ArrayList<>(); | ||
| distances.add(points.get(0).getDistance(points.get(1))); | ||
| distances.add(points.get(1).getDistance(points.get(2))); | ||
| distances.add(points.get(2).getDistance(points.get(0))); | ||
|
||
| Double s = distances.stream().mapToDouble(i->i).sum() /2; | ||
|
||
| Double result = s; | ||
| for(Double d: distances){ | ||
| result *= (s-d); | ||
| } | ||
|
||
|
|
||
| return Math.sqrt(result); | ||
| } | ||
|
|
||
| @Override | ||
| public void output() { | ||
| System.out.printf("삼각형 넓이는 %f", area()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||||
| package view; | ||||||||
|
|
||||||||
| import coordinate.Point; | ||||||||
| import coordinate.Points; | ||||||||
|
|
||||||||
| import java.util.ArrayList; | ||||||||
| import java.util.Arrays; | ||||||||
| import java.util.List; | ||||||||
| import java.util.Scanner; | ||||||||
| import java.util.stream.Collectors; | ||||||||
|
|
||||||||
| public class InputView { | ||||||||
| Scanner scanner = new Scanner(System.in); | ||||||||
|
||||||||
|
|
||||||||
| public Points inputPoint(){ | ||||||||
| System.out.println("좌표를 입력하세요."); | ||||||||
| String s = scanner.next(); | ||||||||
|
|
||||||||
| Points points = new Points(); | ||||||||
| String[] strings = s.split("-"); | ||||||||
| for(String str: strings){ | ||||||||
| List<Integer> numbers = new ArrayList<>(); | ||||||||
| List<String> nums; | ||||||||
| nums = Arrays.stream(str.split("\\(|,|\\)")).filter(t -> !t.isEmpty()).collect(Collectors.toList()); | ||||||||
|
||||||||
| List<String> nums; | |
| nums = Arrays.stream(str.split("\\(|,|\\)")).filter(t -> !t.isEmpty()).collect(Collectors.toList()); | |
| List<String> nums = Arrays.stream(str.split("\\(|,|\\)")).filter(t -> !t.isEmpty()).collect(Collectors.toList()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package view; | ||
|
|
||
| public class OutputView { | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
두 필드 전부 final 키워드를 붙여서 방어적인 코드로 설계하는 것이 좋아보입니다!