Skip to content

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

Open
wants to merge 3 commits into
base: jhm9595
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
* 사람 이름을 5자 기준으로 출력하기 때문에 사다리 폭도 넓어져야 한다.
* 사다리 타기가 정상적으로 동작하려면 라인이 겹치지 않도록 해야 한다.
* |-----|-----| 모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할지 결정할 수 없다.

# 프로그래밍 요구사항
* 자바 8의 스트림과 람다를 적용해 프로그래밍한다.
* 규칙 6: 모든 엔티티를 작게 유지한다.
26 changes: 26 additions & 0 deletions src/main/java/nextstep/LadderApplication.java
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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String 배열을 사용해야만 하는 이유가 있을까요?


int ladderHeight = InputView.inputLadderHeight();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

값 객체를 활용해보시면 좋을것 같습니다.


LinkedList<Line> lines = new LinkedList<>();
Copy link

Choose a reason for hiding this comment

The 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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/nextstep/domain/Line.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트가 불가능해보입니다.

}
12 changes: 12 additions & 0 deletions src/main/java/nextstep/util/RandomUtils.java
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();
}
}
19 changes: 19 additions & 0 deletions src/main/java/nextstep/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nextstep.util;

public class StringUtils {
Copy link

Choose a reason for hiding this comment

The 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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/nextstep/view/InputView.java
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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/nextstep/view/ResultView.java
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);
}
}