forked from next-step/java-coordinate-playground
-
Notifications
You must be signed in to change notification settings - Fork 3
두 번째 버전 (Factory pattern) #3
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
ca1af
wants to merge
6
commits into
CODE-CLEANERS:main
Choose a base branch
from
ca1af:DK_V2
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78048a5
for PullRequest
ca1af fa301aa
feat : 두번째 과제 - Square 완성
ca1af 659d0d3
모든 기능 완성. 무조건 리팩토링 해야 하는 버전
ca1af 88e8feb
Refactor : 두번째 버전
ca1af 00f76b2
Refactor : 두번째 버전
ca1af 654aae9
Refactor : 정적 팩토리 매서드를 사용한 Dot 클래스의 생성
ca1af 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,56 @@ | ||
| package calculator; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class DotV1 { | ||
| private final int x; | ||
| private final int y; | ||
| private static final String INVALID_INPUT_FORMAT = "Invalid input format"; | ||
| private static final String REGEX_FOR_STRING_INPUT = "[()]"; | ||
| private static final int MAX_LOCATION_FOR_DOT = 24; | ||
| public DotV1(int x, int y) { | ||
| validateLocations(x, y); | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public DotV1(String input) { | ||
| String[] parts = input.replaceAll(REGEX_FOR_STRING_INPUT, "").split(","); | ||
| if (parts.length != 2) { | ||
| throw new IllegalArgumentException(INVALID_INPUT_FORMAT); | ||
| } | ||
| int x = Integer.parseInt(parts[0].trim()); | ||
| int y = Integer.parseInt(parts[1].trim()); | ||
| validateLocations(x, y); | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| private void validateLocations(int x, int y) { | ||
| if (x > MAX_LOCATION_FOR_DOT || y > MAX_LOCATION_FOR_DOT) throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| public double getDistanceBetween(DotV1 secondDotV1) { | ||
| return Math.sqrt(Math.pow((secondDotV1.x - this.x), 2) + Math.pow((secondDotV1.y - this.y), 2)); | ||
| } | ||
| public boolean isXParallel(DotV1 dotV1) { | ||
| return this.y == dotV1.y; | ||
| } | ||
|
|
||
| public boolean isYParallel(DotV1 dotV1) { | ||
| return this.x == dotV1.x; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| DotV1 dotV1 = (DotV1) o; | ||
| return x == dotV1.x && y == dotV1.y; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(x, 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,141 @@ | ||
| package calculator; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Dots { | ||
| private final List<DotV1> dotV1s; | ||
|
|
||
| /** | ||
| * CONSTANTS | ||
| */ | ||
| private static final String SPLIT_EXPRESSION = "-"; | ||
| private static final String IS_NOT_LINE = "선이 아니다."; | ||
| private static final String IS_NOT_SQUARE = "사각형이 아니다."; | ||
|
|
||
| public Dots(String input) { | ||
| validateInput(input); | ||
| this.dotV1s = Arrays.stream(input.split("-")).map(DotV1::new).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Dots(DotV1 first, DotV1 second){ | ||
| this.dotV1s = new ArrayList<>(); | ||
| dotV1s.add(first); | ||
| dotV1s.add(second); | ||
| } | ||
|
|
||
| public static Dots asLine(DotV1 first, DotV1 second){ | ||
| return new Dots(first, second); | ||
| } | ||
|
|
||
| /** | ||
| * validations & common methods | ||
| */ | ||
| private void validateInput(String input) { | ||
| if (!input.contains(SPLIT_EXPRESSION)){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public int size() { | ||
| return this.dotV1s.size(); | ||
| } | ||
|
|
||
| /** | ||
| * line methods | ||
| */ | ||
| public boolean isLine() { | ||
| return this.dotV1s.size() == 2; | ||
| } | ||
|
|
||
| public boolean isXParallelLine(){ | ||
| if (!isLine()) { | ||
| throw new IllegalArgumentException(IS_NOT_LINE); | ||
| } | ||
| return dotV1s.get(0).isXParallel(dotV1s.get(1)); | ||
| } | ||
|
|
||
| public boolean isYParallelLine(){ | ||
| if (!isLine()) { | ||
| throw new IllegalArgumentException(IS_NOT_LINE); | ||
| } | ||
| return dotV1s.get(0).isYParallel(dotV1s.get(1)); | ||
| } | ||
|
|
||
| public double getDistanceBetween(){ | ||
| if (!this.isLine()) { | ||
| throw new IllegalArgumentException(IS_NOT_LINE); | ||
| } | ||
| return this.dotV1s.get(0).getDistanceBetween(dotV1s.get(1)); | ||
| } | ||
|
|
||
| /** | ||
| * square methods | ||
| */ | ||
| public boolean isSquare() { | ||
| if (this.dotV1s.size() != 4) { | ||
| return false; | ||
| } | ||
| return isAllParallel(); | ||
| } | ||
|
|
||
| public boolean isAllParallel() { | ||
| return IntStream.range(0, 4).allMatch(i -> { | ||
| DotV1 dotV1 = dotV1s.get(i); | ||
| DotV1 dotV12 = dotV1s.get((i + 1) % 4); // 다음 점 | ||
| return dotV1.isXParallel(dotV12) || dotV1.isYParallel(dotV12); | ||
| }); | ||
| } | ||
|
|
||
| public int getSquareArea(){ | ||
| if (!isSquare()) { | ||
| throw new IllegalArgumentException(IS_NOT_SQUARE); | ||
| } | ||
|
|
||
| List<Double> collect = IntStream.range(0, 4) | ||
| .mapToObj(i -> dotV1s.get(i).getDistanceBetween(dotV1s.get((i + 1) % 4))) | ||
| .distinct().collect(Collectors.toList()); | ||
|
|
||
| if (collect.size() == 1) return (int) Math.pow(collect.get(0), 2); | ||
| return (int) (collect.get(0) * collect.get(1)); | ||
| } | ||
|
|
||
| /** | ||
| * triangle | ||
| */ | ||
| public boolean isTriangle() { | ||
| return this.dotV1s.size() == 3; | ||
| } | ||
|
|
||
| public double getLinesLength(){ | ||
| double lineLength = 0; | ||
| for (int i = 0; i < this.dotV1s.size(); i++) { | ||
| DotV1 first = dotV1s.get(i); | ||
| DotV1 second = dotV1s.get((i + 1) % dotV1s.size()); | ||
| LineV1 lineV1 = new LineV1(first, second); | ||
| lineLength += lineV1.getDotsDistance(); | ||
| } | ||
| return lineLength; | ||
| } | ||
|
|
||
| //헤론의 공식 | ||
| //s = (a + b + c) / 2 | ||
| //Area = √(s * (s - a) * (s - b) * (s - c)) | ||
| public double getTriangleArea(){ | ||
| List<Double> lineLengths = new ArrayList<>(); | ||
| for (int i = 0; i < this.dotV1s.size(); i++) { | ||
| DotV1 first = dotV1s.get(i); | ||
| DotV1 second = dotV1s.get((i + 1) % dotV1s.size()); | ||
| LineV1 lineV1 = new LineV1(first, second); | ||
| lineLengths.add(lineV1.getDotsDistance()); | ||
| } | ||
| double area = getLinesLength() / 2; | ||
| for (Double lineLength : lineLengths) { | ||
| area *= (getLinesLength() / 2) - lineLength; | ||
| } | ||
| return Math.sqrt(area); | ||
| } | ||
| } |
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 calculator; | ||
|
|
||
| public class LineV1 { | ||
| private final Dots dots; | ||
|
|
||
| public LineV1(Dots dots) { | ||
| this.dots = dots; | ||
| } | ||
|
|
||
| public LineV1(DotV1 first, DotV1 second){ | ||
| this.dots = Dots.asLine(first, second); | ||
| } | ||
|
|
||
| public double getDotsDistance(){ | ||
| return this.dots.getDistanceBetween(); | ||
| } | ||
|
|
||
| public boolean isXParallel() { | ||
| return this.dots.isXParallelLine(); | ||
| } | ||
|
|
||
| public boolean isYParallel(){ | ||
| return this.dots.isYParallelLine(); | ||
| } | ||
| } |
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 calculator; | ||
|
|
||
| public class SquareV1 { | ||
| private final Dots dots; | ||
|
|
||
| public SquareV1(Dots dots) { | ||
| if (!dots.isSquare()){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| this.dots = dots; | ||
| } | ||
|
|
||
| public int getArea() { | ||
| return this.dots.getSquareArea(); | ||
| } | ||
| } |
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,5 @@ | ||
| package calculator; | ||
|
|
||
| public class TriangleV1 { | ||
|
|
||
| } |
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 figure; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public abstract class AbstractFigure implements Figure{ | ||
| private final List<Dot> dotList; | ||
|
|
||
| protected AbstractFigure(List<Dot> dotList) { | ||
| if (dotList.size() != this.size()){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| this.dotList = dotList; | ||
| } | ||
| public Dot getDot(int index){ | ||
| return this.dotList.get(index); | ||
| } | ||
| @Override | ||
| public List<Dot> getDots(){ | ||
| return this.dotList; | ||
| } | ||
| } | ||
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,56 @@ | ||
| package figure; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Dot { | ||
| private final int x; | ||
| private final int y; | ||
| private static final String INVALID_INPUT_FORMAT = "Invalid input format"; | ||
| private static final String REGEX_FOR_STRING_INPUT = "[()]"; | ||
| private static final int MAX_LOCATION_FOR_DOT = 24; | ||
| private Dot(int x, int y) { | ||
| validateLocations(x, y); | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
| public static Dot ofTest(int x, int y){ | ||
| return new Dot(x,y); | ||
| } | ||
| public static Dot of(String input){ | ||
| String[] parts = input.replaceAll(REGEX_FOR_STRING_INPUT, "").split(","); | ||
| if (parts.length != 2) { | ||
| throw new IllegalArgumentException(INVALID_INPUT_FORMAT); | ||
| } | ||
| int x = Integer.parseInt(parts[0].trim()); | ||
| int y = Integer.parseInt(parts[1].trim()); | ||
| return new Dot(x, y); | ||
| } | ||
|
|
||
| private void validateLocations(int x, int y) { | ||
| if (x > MAX_LOCATION_FOR_DOT || y > MAX_LOCATION_FOR_DOT) throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| public double getDistanceBetween(Dot secondDot) { | ||
| return Math.sqrt(Math.pow((secondDot.x - this.x), 2) + Math.pow((secondDot.y - this.y), 2)); | ||
| } | ||
| public boolean isXParallel(Dot dot) { | ||
| return this.y == dot.y; | ||
| } | ||
|
|
||
| public boolean isYParallel(Dot dot) { | ||
| return this.x == dot.x; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Dot dot = (Dot) o; | ||
| return x == dot.x && y == dot.y; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(x, 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,10 @@ | ||
| package figure; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface Figure { | ||
| String getName(); | ||
| int size(); | ||
| double getArea(); | ||
| List<Dot> getDots(); | ||
| } |
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; | ||
|
|
||
| public enum FigureEnum { | ||
| LINE("Line", 2), | ||
| TRIANGLE("Triangle", 3), | ||
| SQUARE("Square", 4); | ||
|
|
||
| private final String name; | ||
| private final int size; | ||
| FigureEnum(String name, int size) { | ||
| this.name = name; | ||
| this.size = size; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getSize(){ | ||
| return this.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,21 @@ | ||
| package figure; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class FigureFactory { | ||
| private final List<Figure> figureList; | ||
| private static final Map<Integer, Figure> figureMap = new HashMap<>(); | ||
|
|
||
| public FigureFactory(List<Figure> figureList) { | ||
| this.figureList = figureList; | ||
| for (Figure figure : figureList) { | ||
| figureMap.put(figure.size(), figure); | ||
| } | ||
| } | ||
|
|
||
| static Figure getInstance(List<Dot> points) { | ||
| return figureMap.get(points.size()); | ||
| } | ||
| } |
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.
getDots 도 함께 인터페이스에서 선언을 해준 이유가 무엇인가요?
AbstractFigure 클래스를 상속받는 클래스가 모두 사용할 거라면, 인터페이스에 선언하지 않고 추상클래스에서 바로 작성을 해도 될것같아서요!
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.
getDots 의 경우 팩토리패턴 예제(원본 깃헙 레포) 를 차용해서 해당 방식과 같이 진행했습니다 ㅎㅎ 다희님 말씀대로 추상클래스에서 바로 선언해도 될 것 같습니다!