-
Notifications
You must be signed in to change notification settings - Fork 709
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
Step2 - 사다리(생성) #1438
base: 0319easy
Are you sure you want to change the base?
Step2 - 사다리(생성) #1438
Changes from all commits
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,18 @@ | ||
package ladder; | ||
|
||
import java.util.List; | ||
import ladder.domain.Ladder; | ||
import ladder.ui.LadderScanner; | ||
import ladder.ui.ResultView; | ||
|
||
public class LadderMain { | ||
public static void main(String[] args) { | ||
String input = LadderScanner.insertParticipant(); | ||
List<String> participants = List.of(input.split(",")); | ||
int height = LadderScanner.insertLadderHeight(); | ||
|
||
Ladder ladder = new Ladder(participants.size() - 1, height); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
ResultView.printResult(participants, ladder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private List<Line> lines = new ArrayList<>(); | ||
|
||
public Ladder(int width, int height) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. width, height은 1이상 이어야 하지 않나요? |
||
for (int i = 0; i < height; ++i) { | ||
lines.add(Line.of(width)); | ||
} | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Line { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일급컬렉션 사용 좋습니다 👍 |
||
private List<Point> points; | ||
|
||
Line(List<Point> points) { | ||
this.points = points; | ||
} | ||
|
||
static Line of(int size) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정적 팩토리 메소드를 사용해주셨네요 👍👍 추가적으로 전체적으로 접근제한자 default, public 를 혼용해서 사용하셨는데 이유가 있을까요?? :) |
||
List<Point> points = new ArrayList<>(); | ||
|
||
points.add(new Point()); | ||
for (int i = 1; i < size; i++) { | ||
points.add(points.get(i - 1).nextPoint()); | ||
} | ||
|
||
return new Line(points); | ||
} | ||
|
||
public List<Point> getPoints() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 내부 상태인 List를 그대로 반환하면 |
||
return points; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Random; | ||
|
||
public class Point { | ||
private final boolean exist; | ||
|
||
Point() { | ||
this(new Random().nextBoolean()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 랜덤한 값이 있다면 테스트할 수 없고 전략 패턴을 사용해서 테스트 가능한 구조로 변경해보는건 어떨까요? :) |
||
} | ||
|
||
Point(boolean exist) { | ||
this.exist = exist; | ||
} | ||
|
||
public boolean isExist() { | ||
return exist; | ||
} | ||
|
||
Point nextPoint() { | ||
if (isExist()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 멤버 변수에 대한 접근인데 불필요한 getter 사용아닐까요? |
||
return new Point(false); | ||
} | ||
|
||
return new Point(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ladder.ui; | ||
|
||
public class InputView { | ||
public static void printInsertParticipantPhrase() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
} | ||
|
||
public static void printInsertLadderHeightPhrase() { | ||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ladder.ui; | ||
|
||
import java.util.Scanner; | ||
|
||
public class LadderScanner { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LadderScanner 가 하는 일을 InputView에서 해도 좋을 것 같은데 별도의 클래스로 분리해주신 이유가 있나요? |
||
public static Scanner scanner = new Scanner(System.in); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바 상수 컨벤션에 맞게 수정해볼까요? |
||
|
||
public static String insertParticipant() { | ||
InputView.printInsertParticipantPhrase(); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static int insertLadderHeight() { | ||
InputView.printInsertLadderHeightPhrase(); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||||
package ladder.ui; | ||||||||||||||
|
||||||||||||||
import java.util.List; | ||||||||||||||
import ladder.domain.Ladder; | ||||||||||||||
import ladder.domain.Line; | ||||||||||||||
import ladder.domain.Point; | ||||||||||||||
|
||||||||||||||
public class ResultView { | ||||||||||||||
public static void printResult(List<String> participants, Ladder ladder) { | ||||||||||||||
participants.forEach(it -> { | ||||||||||||||
it = " " + it; | ||||||||||||||
it = it.substring(it.length() - 6); | ||||||||||||||
System.out.print(it); | ||||||||||||||
}); | ||||||||||||||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스트림을 활용해주셨네요 👍
Suggested change
11 ~ 13 라인을 메소드로 추출한다면 좀 더 간결한 코드를 작성할 수 있을 것 같아요 :) |
||||||||||||||
System.out.println(); | ||||||||||||||
|
||||||||||||||
List<Line> lines = ladder.getLines(); | ||||||||||||||
|
||||||||||||||
lines.forEach(it -> printLine(it.getPoints())); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static void printLine(List<Point> points) { | ||||||||||||||
System.out.print(" "); | ||||||||||||||
points.forEach(it -> { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it 대신 적절하게 point 로 네이밍을 변경하는 것도 좋을 것 같아요 :) |
||||||||||||||
if (it.isExist()) { | ||||||||||||||
System.out.print("|-----"); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
System.out.print("| "); | ||||||||||||||
}); | ||||||||||||||
System.out.println("|"); | ||||||||||||||
} | ||||||||||||||
} |
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.
참가자 객체를 만들고 해당 객체에서 이름 길이에 대한 검증을 해보는건 어떨까요?