-
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 #1856
base: hj1115hj
Are you sure you want to change the base?
Step2 #1856
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,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); | ||
Ladder ladder= new Ladder(inputView.ladderHeight, inputView.names.size()); | ||
PrintView.printResult(inputView.names, ladder); | ||
|
||
} | ||
|
||
} |
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 { | ||
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. TDD를 중점적으로 학습하는 과정인데, 핵심 도메인 클래스들에 대한 단위 테스트가 작성되어 있지 않네요 😢 |
||
private List<Line> lines; | ||
|
||
public Ladder(int height, int nameSize) { | ||
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. 만약 사다리 높이 값이 음수로 전달된다면 문제가 생길 수 있을 것 같아요. |
||
lines = IntStream.range(0, height) | ||
.mapToObj(i -> new Line(nameSize)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<Line> getLines(){ | ||
return lines; | ||
} | ||
|
||
} |
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 { | ||
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 final List<Boolean> points = new ArrayList<>(); | ||
|
||
public Line(int countOfPerson) { | ||
IntStream.range(0, countOfPerson - 1) | ||
.mapToObj(i -> isInValidPosition(i) ? false : RandomValueGenerator.generate()) | ||
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. 삼항 연산자 또한 |
||
.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; | ||
} | ||
} |
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 { | ||
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 final Scanner scanner = new Scanner(System.in); | ||
public List<String> names; | ||
public int ladderHeight = 0; | ||
|
||
public void saveInput() { | ||
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.
|
||
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(","); | ||
} | ||
|
||
|
||
} |
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 { | ||
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 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
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. 해당 부분을 별도의 메서드로 추출해보는 건 어떨까요? |
||
}) | ||
.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){ | ||
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. 이번 과정에서는 |
||
System.out.print("-----"); | ||
} else{ | ||
System.out.print(" "); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
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.
중간에 사다리게임 참가자 이름을 출력하는 요구사항은 없기 때문에 제거하셔도 될 것 같습니다.