-
Notifications
You must be signed in to change notification settings - Fork 746
Step2 : 사다리(생성) #2412
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: jhm9595
Are you sure you want to change the base?
Step2 : 사다리(생성) #2412
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,26 @@ | ||
package nextstep; | ||
|
||
import nextstep.domain.Line; | ||
import nextstep.view.InputView; | ||
import nextstep.view.ResultView; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class LadderApplication { | ||
|
||
public static void main(String[] args) { | ||
|
||
String[] names = InputView.inputNames(); | ||
|
||
int ladderHeight = InputView.inputLadderHeight(); | ||
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. 값 객체를 활용해보시면 좋을것 같습니다. |
||
|
||
LinkedList<Line> lines = new LinkedList<>(); | ||
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. line의 타입을 LinkedList라고 명시적으로 노출해야하는 이유가 있을까요? |
||
lines.add(new Line(ladderHeight)); // 첫번째 라인은 랜덤으로 생성 | ||
|
||
for (int i = 1; i < names.length - 1; i++) { | ||
lines.addLast(new Line(lines.getLast())); | ||
} | ||
|
||
ResultView.printResult(names, ladderHeight, lines); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package nextstep.domain; | ||
|
||
import nextstep.util.RandomUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Line { | ||
private List<Boolean> points = new ArrayList<>(); | ||
|
||
public List<Boolean> getPoints() { | ||
return points; | ||
} | ||
|
||
public boolean isPoint(int idx) { | ||
return points.get(idx); | ||
} | ||
|
||
public Line(int height) { | ||
for (int i = 0; i < height; i++) { | ||
points.add(RandomUtils.nextBoolean()); | ||
} | ||
} | ||
|
||
public Line(Line previousLine) { | ||
for (Boolean hasPoint : previousLine.getPoints()) { | ||
points.add(!hasPoint && RandomUtils.nextBoolean()); | ||
} | ||
} | ||
Comment on lines
+25
to
+29
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. 테스트가 불가능해보입니다. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package nextstep.util; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomUtils { | ||
|
||
private static final Random random = new Random(); | ||
|
||
public static boolean nextBoolean() { | ||
return random.nextBoolean(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nextstep.util; | ||
|
||
public class StringUtils { | ||
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 lpad(String name, int maxLength) { | ||
|
||
int length = name.length(); | ||
|
||
if (length > maxLength) { | ||
throw new IllegalArgumentException("사람 이름을 5자를 넘지 못합니다."); | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i <= maxLength - name.length(); i++) { | ||
sb.append(" "); | ||
} | ||
return sb.toString() + name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package nextstep.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final int MIN_LADDER_HEIGHT = 1; | ||
private static final int MIN_NAME_LENGTH = 2; | ||
|
||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
|
||
public static String[] inputNames(){ | ||
|
||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
|
||
String[] names = scanner.next().split(","); | ||
|
||
if(names.length < MIN_NAME_LENGTH){ | ||
throw new IllegalArgumentException("최소 2명 이상이어야 합니다."); | ||
} | ||
|
||
return names; | ||
} | ||
|
||
public static int inputLadderHeight() { | ||
|
||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
int ladderHeight = scanner.nextInt(); | ||
|
||
if(ladderHeight < MIN_LADDER_HEIGHT){ | ||
throw new IllegalArgumentException("높이는 1 이상이여야 합니다."); | ||
} | ||
return ladderHeight; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package nextstep.view; | ||
|
||
import nextstep.domain.Line; | ||
import nextstep.util.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.stream.IntStream; | ||
|
||
public class ResultView { | ||
|
||
public static final String EMPTY_LADDER = " "; | ||
public static final String FULL_LADDER = "-----"; | ||
public static final String PIPE = "|"; | ||
public static final int MAX_NAME_LENGTH = 5; | ||
public static final String NL = System.lineSeparator(); | ||
|
||
|
||
public static void printResult(String[] names, int ladderHeight, LinkedList<Line> lines) { | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
|
||
sb.append("실행 결과") | ||
.append(NL) | ||
.append(Arrays.stream(names) | ||
.map(name -> StringUtils.lpad(name, MAX_NAME_LENGTH)) | ||
.reduce("", String::concat)) | ||
.append(NL); | ||
|
||
IntStream.range(0, ladderHeight) | ||
.forEach(idx -> { | ||
sb.append(EMPTY_LADDER) | ||
.append(lines.stream() | ||
.map(line -> draw(line.isPoint(idx))) | ||
.reduce("", String::concat)) | ||
.append(PIPE) | ||
.append(NL); | ||
}); | ||
|
||
System.out.println(sb.toString()); | ||
} | ||
|
||
private static String draw(boolean point) { | ||
return PIPE.concat(point ? FULL_LADDER : EMPTY_LADDER); | ||
} | ||
} |
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.
String 배열을 사용해야만 하는 이유가 있을까요?