Conversation
|
쓸데없는 주석은 지우고 다시 올리자..! |
| if(coordinateList.size() == 4){ | ||
| return new SquareCalculator(coordinateList); | ||
| } | ||
| return null; |
There was a problem hiding this comment.
null을 리턴하는 것 외에 다른 방법을 써보면 어떨까요?
- 디폴트로 리턴하는 녀석을 지정하거나 (if를 아무것도 안 탈 시 리턴할 녀석)
- 예외를 던지는 방법
정도가 있을 것 같습니다!
There was a problem hiding this comment.
오.. 안그래도 null로 리턴 시킬 경우에 NullPointerException이 걱정되었는데 아예 예외를 던지는것도 좋은 방법인것 같습니다.
| public double calculator() throws IOException { | ||
| System.out.println("사각형 넓이 구하기"); | ||
| //삐뚤어진 사각형인지 아닌지 먼저 검증 | ||
| validateSquare(); | ||
| //가로길이 구하기 | ||
| double width = calculateWidth(coordinateList); | ||
| //세로길이 구하기 | ||
| double height = calculateHeight(coordinateList); | ||
| //넓이 계산하기 | ||
| return width * height; | ||
| } |
There was a problem hiding this comment.
아주 세세한 예외로직 멋집니다 ㅎㅎ 더불어서 매소드로 나누어주셔서 읽기 편하네요!
| private double calculateWidth(List<Coordinate> coordinateList){ | ||
| //y좌표가 같은것끼리 찾아서 ax-bx를 하면 가로길이 나옴. | ||
| Coordinate aCoordinate; | ||
| Coordinate bCoordinate = null; | ||
|
|
||
| aCoordinate = coordinateList.get(0); | ||
|
|
||
| for (int i = 1; i < coordinateList.size(); i++) { | ||
| if(aCoordinate.getY()==coordinateList.get(i).getY()){ | ||
| bCoordinate = coordinateList.get(i); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return Math.abs(aCoordinate.getX()-bCoordinate.getX()); | ||
| } | ||
|
|
||
| private double calculateHeight(List<Coordinate> coordinateList){ | ||
| //x좌표가 같은것끼리 찾아서 ay-by를 하면 세로길이 나옴. | ||
| Coordinate aCoordinate = null; | ||
| Coordinate bCoordinate = null; | ||
|
|
||
| aCoordinate = coordinateList.get(0); | ||
|
|
||
| for (int i = 1; i < coordinateList.size(); i++) { | ||
| if(aCoordinate.getX()==coordinateList.get(i).getX()){ | ||
| bCoordinate = coordinateList.get(i); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return Math.abs(aCoordinate.getY()-bCoordinate.getY()); | ||
| } | ||
| } |
There was a problem hiding this comment.
이 매서드들을 하나로 합칠 수 있는 방법은 없을까요?
Coordinate aCoordinate = null;이부분에서 null로 초기화를 한 이유가 궁금합니다!
ca1af
left a comment
There was a problem hiding this comment.
너무 멋집니다 ㅎㅎ 이제 구현에 자신감을 가져도 되지 않을까 싶습니다!
다만 main 매서드에서는 로직을 포함하지 않는 부분을 한번 연습하면 좋을 것 같아요.
| String input; | ||
|
|
||
| public String[] creatCoordinate() throws IOException { | ||
| System.out.println("(x,y)-(x,y)또는 (x,y)-(x,y)-(x,y)또는 (x,y)-(x,y)-(x,y)-(x,y)형식으로"); |
There was a problem hiding this comment.
아직 inputView 와 outputView 나누는 것에 조금 헷갈려요. 다른사람의 코드를 보고 조금 더 참고해서 다음 도전때는 좀 더 분리해보도록 할게요
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Coordinate { |
There was a problem hiding this comment.
Coordinate 라는 변수명은 조금 어색한 것 같아요! 아마도 점의 위치를 표현하는 객체같아서, Point 내지 Dot 이라는 변수명은 어떨까요?
| this.result = result; | ||
| } | ||
|
|
||
| public Calculator calculateFactory(String[] splitInput) throws IOException { |
There was a problem hiding this comment.
입력에 따라 다른 Caculator를 생성하는 메소드라면 createCalculator나 getCalculator 같은 메소드 이름이 더 알아보기 쉬울 것 같습니다.
|
|
||
| public class CalculatorFactory { | ||
| private List<Coordinate> coordinateList; | ||
| private double result; |
There was a problem hiding this comment.
부끄럽지만 안쓰입니다.. ㅠ.. 코드를 수정하면서 다른건 다 뺐는데 CalculatorFactory에 하나 남아있었네요..ㅠㅠ
No description provided.