Skip to content
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 #1856

Open
wants to merge 5 commits into
base: hj1115hj
Choose a base branch
from
Open

Step2 #1856

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
16 changes: 16 additions & 0 deletions src/main/java/ladder/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ladder;

import ladder.ui.InputView;
import ladder.ui.PrintView;

public class Application {
public static void main(String[] args) {
InputView inputView = new InputView();
inputView.saveInput();
System.out.println(inputView.names);

Choose a reason for hiding this comment

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

중간에 사다리게임 참가자 이름을 출력하는 요구사항은 없기 때문에 제거하셔도 될 것 같습니다.

Ladder ladder= new Ladder(inputView.ladderHeight, inputView.names.size());
PrintView.printResult(inputView.names, ladder);

}

}
21 changes: 21 additions & 0 deletions src/main/java/ladder/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ladder;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Ladder {

Choose a reason for hiding this comment

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

TDD를 중점적으로 학습하는 과정인데, 핵심 도메인 클래스들에 대한 단위 테스트가 작성되어 있지 않네요 😢
Ladder에 대한 단위 테스트를 작성해보면 어떨까요?

private List<Line> lines;

public Ladder(int height, int nameSize) {

Choose a reason for hiding this comment

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

만약 사다리 높이 값이 음수로 전달된다면 문제가 생길 수 있을 것 같아요.

lines = IntStream.range(0, height)
.mapToObj(i -> new Line(nameSize))
.collect(Collectors.toList());
}

public List<Line> getLines(){
return lines;
}

}
30 changes: 30 additions & 0 deletions src/main/java/ladder/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ladder;

import ladder.util.RandomValueGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class Line {

Choose a reason for hiding this comment

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

Line 클래스에 대한 단위 테스트도 작성해보면 어떨까요?

private final List<Boolean> points = new ArrayList<>();

public Line(int countOfPerson) {
IntStream.range(0, countOfPerson - 1)
.mapToObj(i -> isInValidPosition(i) ? false : RandomValueGenerator.generate())

Choose a reason for hiding this comment

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

삼항 연산자 또한 if-else의 축약형이기 때문에, 다른 방식으로 작성해보시면 좋을 것 같습니다.

.forEach(points::add);
}

private boolean isInValidPosition(int position) {
return isLeftTrue(position);
}

private boolean isLeftTrue(int position) {
return (position - 1 >= 0 && points.get(position - 1));
}


public List<Boolean> getPoints() {
return points;
}
}
25 changes: 25 additions & 0 deletions src/main/java/ladder/ui/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ladder.ui;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class InputView {

Choose a reason for hiding this comment

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

InputViewPrintvView와 같이 정적 메서드만을 갖는 유틸리티 클래스로 만들어보면 어떨까요?

private final Scanner scanner = new Scanner(System.in);
public List<String> names;
public int ladderHeight = 0;

public void saveInput() {

Choose a reason for hiding this comment

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

  • 하나의 메서드에서 참가자 이름도 입력 받고 사다리 높이도 입력을 받는 두 가지 기능을 수행하고 있는데요,
    각각의 기능별로 메서드를 분리해보면 어떨까요?
  • 사용자로부터 입력 받는 값들을 내부에 상태로 가지기 보다는 메서드에서 바로 반환해도 충분할 것 같습니다.

System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
String nameStr = scanner.nextLine();
names= Arrays.asList(split(nameStr));
System.out.println("최대 사다리 높이는 몇개 인가요?");
ladderHeight = scanner.nextInt();
}

private String[] split(String str) {
return str.split(",");
}


}
49 changes: 49 additions & 0 deletions src/main/java/ladder/ui/PrintView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ladder.ui;

import ladder.Ladder;
import ladder.Line;

import java.util.List;

public class PrintView {

Choose a reason for hiding this comment

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

정적 메서드만을 포함하는 유틸리티 클래스이니 인스턴스를 생성하지 않아도 사용이 가능합니다만,
누군가는 실수로 생성자를 호출하여 인스턴스를 생성하려고 할 수도 있으니,
private 생성자를 추가하여 불필요한 인스턴스화를 막아보면 어떨까요?

public static void printResult(List<String> names, Ladder ladder) {
System.out.println("실행결과");
printName(names);
printLadder(ladder);
}

private static void printName(List<String> names) {
names.stream()
.map(name -> {
int padding = 5 - name.length();
String paddedName = name + " ".repeat(padding);
return paddedName;
Comment on lines +18 to +20

Choose a reason for hiding this comment

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

해당 부분을 별도의 메서드로 추출해보는 건 어떨까요?
메서드 이름을 통해 어떤 로직을 수행하는지를 보다 쉽게 표현해볼 수 있지 않을까 생각합니다.

})
.forEach(System.out::print);
System.out.println();
}

private static void printLadder(Ladder ladder) {
List<Line> lines = ladder.getLines();
lines.forEach(line -> {
printColumn();
line.getPoints().forEach(point -> {
printRow(point);
printColumn();
});
System.out.println();
});
}

private static void printColumn(){
System.out.print("|");
}

private static void printRow(boolean point){
if(point){

Choose a reason for hiding this comment

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

이번 과정에서는 if-else를 사용하지 않기로 했으니, early return 방식으로 바꿔보시면 좋을 것 같아요.
https://jheloper.github.io/2019/06/write-early-return-code/

System.out.print("-----");
} else{
System.out.print(" ");
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/ladder/util/RandomValueGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ladder.util;

import java.util.Random;

public class RandomValueGenerator {
private static Random random = new Random();

public static boolean generate() {
return random.nextBoolean();
}
}