-
Notifications
You must be signed in to change notification settings - Fork 708
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
2단계 - 사다리(생성) #2238
base: owljoa
Are you sure you want to change the base?
2단계 - 사다리(생성) #2238
Changes from all commits
cc9be7a
dad07b7
6d1be45
9f42c48
e2ba939
f52d3be
51dac86
99e0f94
942bb04
ddd864b
d79d64d
ed8223c
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,9 @@ | ||
package nextstep.ladder; | ||
|
||
import nextstep.ladder.controller.LadderController; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
LadderController.startLadderGame(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nextstep.ladder.controller; | ||
|
||
import nextstep.ladder.domain.Ladder; | ||
import nextstep.ladder.domain.User; | ||
import nextstep.ladder.view.InputView; | ||
import nextstep.ladder.view.ResultView; | ||
|
||
import java.util.List; | ||
|
||
public class LadderController { | ||
public static void startLadderGame() { | ||
List<User> users = InputView.inputUsers(); | ||
int ladderMaxHeight = InputView.inputLadderMaxHeight(); | ||
|
||
Ladder ladder = new Ladder(users.size(), ladderMaxHeight); | ||
|
||
ResultView.printResult(ladder, users); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class HorizontalLine { | ||
private final List<Boolean> hasLineOrNotList; | ||
|
||
private HorizontalLine(List<Boolean> hasLineOrNotList) { | ||
this.hasLineOrNotList = hasLineOrNotList; | ||
} | ||
Comment on lines
+10
to
+12
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 HorizontalLine(int userCount) { | ||
this(generateLines(userCount)); | ||
} | ||
Comment on lines
+14
to
+16
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 static List<Boolean> generateLines(int userCount) { | ||
Random random = new Random(); | ||
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. 이전 단계에서도 경험하셨겠지만 random은 테스트하기 어려운 구조로 만들게 됩니다. |
||
List<Boolean> lines = new ArrayList<>(); | ||
Boolean isPreviousLineExist = null; | ||
int maxBooleanCountForLine = userCount - 1; | ||
|
||
while (lines.size() < maxBooleanCountForLine) { | ||
boolean isLineExist = generateIsLineExist(random, isPreviousLineExist); | ||
lines.add(isLineExist); | ||
isPreviousLineExist = isLineExist; | ||
} | ||
|
||
return lines; | ||
} | ||
|
||
private static boolean generateIsLineExist(Random random, Boolean isPreviousLineExist) { | ||
boolean isLineExist = random.nextBoolean(); | ||
while (isPreviousLineExist != null && isLineExist == isPreviousLineExist) { | ||
isLineExist = random.nextBoolean(); | ||
} | ||
return isLineExist; | ||
} | ||
|
||
public int size() { | ||
return hasLineOrNotList.size(); | ||
} | ||
|
||
public Boolean hasLineOnIndex(int index) { | ||
return hasLineOrNotList.get(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private final List<HorizontalLine> horizontalLines; | ||
|
||
public Ladder(List<HorizontalLine> horizontalLines) { | ||
this.horizontalLines = horizontalLines; | ||
} | ||
|
||
public Ladder(int userCount, int ladderMaxHeight) { | ||
this(generate(userCount, ladderMaxHeight)); | ||
} | ||
|
||
private static List<HorizontalLine> generate(int userCount, int ladderMaxHeight) { | ||
List<HorizontalLine> horizontalLines = new LinkedList<>(); | ||
|
||
for (int i = 0; i < ladderMaxHeight; i++) { | ||
horizontalLines.add(new HorizontalLine(userCount)); | ||
} | ||
|
||
return horizontalLines; | ||
} | ||
|
||
public int size() { | ||
return horizontalLines.size(); | ||
} | ||
|
||
public HorizontalLine getByIndex(int index) { | ||
return horizontalLines.get(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nextstep.ladder.domain; | ||
|
||
public class User { | ||
private static final int MAX_NAME_LENGTH = 5; | ||
|
||
private final String name; | ||
|
||
public User(String name) { | ||
if (name == null || name.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException("참여자의 이름은 최대 5글자입니다"); | ||
Comment on lines
+9
to
+10
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. null인 경우는 메시지가 달라야 하지 않을까요? |
||
} | ||
|
||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package nextstep.ladder.view; | ||
|
||
import nextstep.ladder.domain.User; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
public class InputView { | ||
private static final String DELIMITER = ","; | ||
|
||
public static List<User> inputUsers() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
String userNames = getInputString(); | ||
return parseUsers(userNames); | ||
} | ||
|
||
public static int inputLadderMaxHeight() { | ||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
return getInputInteger(); | ||
} | ||
|
||
private static List<User> parseUsers(String userNames) { | ||
return Arrays.stream(userNames.split(DELIMITER)) | ||
.map(User::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static int getInputInteger() { | ||
try { | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextInt(); | ||
} catch (Exception e) { | ||
return 0; | ||
} | ||
} | ||
Comment on lines
+30
to
+37
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 static String getInputString() { | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextLine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nextstep.ladder.view; | ||
|
||
import nextstep.ladder.domain.HorizontalLine; | ||
import nextstep.ladder.domain.Ladder; | ||
import nextstep.ladder.domain.User; | ||
|
||
import java.util.List; | ||
|
||
public class ResultView { | ||
public static void printResult(Ladder ladder, List<User> users) { | ||
System.out.println("실행결과"); | ||
users.forEach(user -> System.out.printf("%-6s", user.getName())); | ||
System.out.println(); | ||
printLadder(ladder); | ||
} | ||
|
||
private static void printLadder(Ladder ladder) { | ||
for (int index = 0; index < ladder.size(); index++) { | ||
System.out.print("|"); | ||
HorizontalLine horizontalLine = ladder.getByIndex(index); | ||
printHorizontalLine(horizontalLine); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
private static void printHorizontalLine(HorizontalLine horizontalLine) { | ||
for (int i = 0; i < horizontalLine.size(); i++) { | ||
Boolean hasLineOnIndex = horizontalLine.hasLineOnIndex(i); | ||
printHorizontalLineOrBlank(hasLineOnIndex); | ||
System.out.print("|"); | ||
} | ||
Comment on lines
+27
to
+31
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. size(), hasLineOnIndex() 는 출력에 필요한 메서드라고 생각되는데요. |
||
} | ||
|
||
private static void printHorizontalLineOrBlank(boolean hasLine) { | ||
if (hasLine) { | ||
System.out.print("-----"); | ||
return; | ||
} | ||
System.out.print(" "); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HorizontalLineTest { | ||
@Test | ||
void 생성된_가로선은_연속되지_않아야_한다() { | ||
// given | ||
int userCount = 4; | ||
|
||
// when | ||
HorizontalLine horizontalLine = new HorizontalLine(userCount); | ||
|
||
// then | ||
for (int i = 0; i < horizontalLine.size() - 1; i++) { | ||
boolean currentGeneratedLine = horizontalLine.hasLineOnIndex(i); | ||
boolean nextGeneratedLine = horizontalLine.hasLineOnIndex(i + 1); | ||
Assertions.assertThat(currentGeneratedLine).isNotEqualTo(nextGeneratedLine); | ||
} | ||
Comment on lines
+8
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. 모든 값을 꺼내어서 결과를 검증하다보니 테스트 코드가 복잡해질 수 밖에 없을 것 같아요. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LadderTest { | ||
|
||
@Test | ||
void 사다리_생성() { | ||
int userCount = 4; | ||
int ladderMaxHeight = 5; | ||
|
||
// when | ||
Ladder ladder = new Ladder(userCount, ladderMaxHeight); | ||
|
||
// then | ||
Assertions.assertThat(ladder.size()).isEqualTo(ladderMaxHeight); | ||
Comment on lines
+13
to
+17
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. size 검증만으로 정상 사다리라고 보기 어렵지 않을까요. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package nextstep.ladder.domain; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class UserTest { | ||
@Test | ||
void 참여자의_이름은_최대_5글자() { | ||
String name = "michael"; | ||
|
||
Assertions.assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> new User(name)) | ||
.withMessage("참여자의 이름은 최대 5글자입니다"); | ||
|
||
} | ||
} |
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.
변수명이 쉽지 않네요 😅
조금 더 간단하게 표현하면 어떨까요?