-
Notifications
You must be signed in to change notification settings - Fork 39
[LBP] 권지후 사다리 2-5단계 미션 제출합니다 #44
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: jihoo2002
Are you sure you want to change the base?
Changes from 10 commits
8ff892d
272e3f3
79a89dd
58449af
a15ea17
61dac53
5894df8
72d6d7d
cf29f3e
8409652
26d4ae4
03c0883
586cbf0
4cc30cc
c7151e9
e7385c6
7a08d40
ed8b69e
62e1bce
8b1cb04
6f4ef52
a2a5707
c3e9176
522379a
8aafd4f
a7f648c
42140af
93c7578
871892e
c2facf9
5ff0bac
5ae6601
2b0164d
1335675
903cbf5
71f176b
3a1f208
1e16ec9
0c4b52f
c938035
2020d05
c678058
34ca870
38a1359
31cca69
70f1f56
8f215c1
9e6496d
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 |
---|---|---|
@@ -1,26 +1,43 @@ | ||
package controller; | ||
|
||
import model.*; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import static model.Point.HAS_POINT; | ||
|
||
public class LadderController { | ||
|
||
private static final int LADDER_SIZE = 4; | ||
private static final int CHUNK_SIZE = 3; | ||
private final ResultView resultView = new ResultView(); | ||
private final InputView inputView = new InputView(); | ||
|
||
public void startLadder() { | ||
PointGenerator pointGenerator = new PointGenerator(new Random()); | ||
LadderGame ladderGame = new LadderGame(new Size(LADDER_SIZE), new Size(LADDER_SIZE), pointGenerator); | ||
List<Point> ladderPoints = ladderGame.getLadderPoints(); | ||
List<Boolean> points = formatLadderPoints(ladderPoints); | ||
Players player = new Players(inputView.inputNames()); | ||
Prizes prizes = Prizes.form(inputView.inputResult(), player); | ||
Height height = new Height(inputView.getMaxLadderHeight()); | ||
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. Players 타입의 변수명에 player -> players로 작성하면 더 좋을 것 같아요.
정적 팩터리 메서드 명을 더 명확하게 작성해주면 좋을 것 같아요! 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. 다른 데 신경 쓰다 보니 변수명을 놓친 것 같습니다 ㅠㅠ |
||
PointGenerator pointGenerator = new PointGenerator(new RandomValueGenerator()); | ||
LadderGame ladderGame = LadderGame.createGame(player, height, pointGenerator, prizes); | ||
|
||
List<Boolean> points = formatLadderPoints(ladderGame.getLadderPoints()); | ||
List<List<Boolean>> ladderLines = processLadderLines(points); | ||
resultView.printLadder(ladderLines); | ||
resultView.printLadder(ladderLines, player.getPlayers(), prizes.getPrize()); | ||
printResult(ladderGame); | ||
} | ||
|
||
private void printResult(LadderGame ladderGame) { | ||
while (true) { | ||
String targetPlayerName = inputView.getTargetPlayerName(); | ||
|
||
if (targetPlayerName.equals("all")) { | ||
resultView.printAllResults(ladderGame.getAllResultForPlayers()); | ||
break; | ||
} | ||
if (ladderGame.hasResultForPlayer(targetPlayerName)) { | ||
resultView.printSingleResult(ladderGame.getResultForPlayer(targetPlayerName)); | ||
} | ||
} | ||
} | ||
|
||
private List<Boolean> formatLadderPoints(List<Point> ladderPoints) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package model; | ||
|
||
public class Height { | ||
private static final int MINIMUM_HEIGHT = 2; | ||
|
||
private final int value; | ||
|
||
public Height(int value) { | ||
validateValue(value); | ||
this.value = value; | ||
} | ||
|
||
private void validateValue(int value) { | ||
if (value < MINIMUM_HEIGHT) { | ||
throw new IllegalArgumentException("사다리 높이는 2 이상이여야 합니다."); | ||
} | ||
} | ||
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. 예외 메시지도 상수를 사용하는 것에 대해서는 어떻게 생각하시나요? 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.
package exception;
public enum ExceptionMessage {
LADDER_HEIGHT_MIN_VALUE("사다리 높이는 최소 2 이상이어야 합니다."),
LADDER_SIZE_NEGATIVE("사다리 사이즈가 0보다 작을 수는 없습니다."),
LADDER_HEIGHT_NOT_NUMBER("사다리 높이는 숫자여야 합니다."),
PLAYER_NAME_MAX_LENGTH_EXCEEDED("참가자 이름은 최대 5글자를 초과할 수 없습니다."),
MIN_PLAYERS_REQUIRED("참가자는 최소 2명 이상이여야 합니다."),
RESULT_COUNT_MISMATCH("실행결과 개수와 참가자의 수는 동일해야 합니다."),
RESULT_NOT_NULL_OR_EMPTY("실행결과는 null이거나 공백일 수는 없습니다."),
NULL_OR_EMPTY_INPUT("입력값이 null이거나 비어있을 순 없습니다.");
private final String message;
ExceptionMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
} 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 class Height {
private static final int MINIMUM_HEIGHT = 2;
private final int value;
public Height(int value) {
validateValue(value);
this.value = value;
}
private void validateValue(int value) {
if (value < MINIMUM_HEIGHT) {
throw new IllegalArgumentException("사다리 높이는 %d 이상이여야 합니다.".formatted(MINIMUM_HEIGHT));
}
} 처럼 변경될 수 있는 조건을 상수로 선언했던 것을 활용하는 것에 관한 이야기였어요! 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. 별개로 예외 메시지를 Enum으로 관리했을 때의 장점은 무엇인가요? |
||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,46 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.IntStream; | ||
|
||
public class LadderGame { | ||
private final Ladder ladder; | ||
private final PointGenerator generator; | ||
private final LadderResult ladderResult; | ||
|
||
public LadderGame(Size width, Size height, PointGenerator generator) { | ||
this.generator = generator; | ||
this.ladder = new Ladder(generateLines(width, height)); | ||
public LadderGame(Ladder ladder, LadderResult ladderResult) { | ||
this.ladder = ladder; | ||
this.ladderResult = ladderResult; | ||
} | ||
|
||
private List<Line> generateLines(Size width, Size height) { | ||
List<Line> lines = new ArrayList<>(); | ||
for (int i = 0; i < height.getSize(); i++) { | ||
List<Point> points = generator.createLinePoints(width); | ||
lines.add(new Line(points)); | ||
} | ||
return List.copyOf(lines); | ||
public static LadderGame createGame(Players players, Height maxHeight, | ||
PointGenerator pointGenerator, | ||
Prizes prizes) { | ||
Ladder ladder = new Ladder(generateLines(players.size(), maxHeight.getValue(), pointGenerator)); | ||
LadderResult ladderResult = new LadderResult(ladder); | ||
ladderResult.calculateResults(players.getPlayers(), prizes); | ||
return new LadderGame(ladder, ladderResult); | ||
} | ||
|
||
private static List<Line> generateLines(int playerCount, int maxHeight, PointGenerator pointGenerator) { | ||
return IntStream.range(0, maxHeight) | ||
.mapToObj(i -> new Line(pointGenerator.createLinePoints(new Size(playerCount)))) | ||
.toList(); | ||
} | ||
|
||
public List<Point> getLadderPoints() { | ||
return ladder.getPointsFromLines(); | ||
} | ||
|
||
public String getResultForPlayer(String name) { | ||
return ladderResult.getResultForPlayer(name); | ||
} | ||
|
||
public Map<String, String> getAllResultForPlayers() { | ||
return ladderResult.getValue(); | ||
} | ||
|
||
public boolean hasResultForPlayer(String name) { | ||
return ladderResult.getValue().containsKey(name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package model; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LadderResult { | ||
private static final String NO_RESULT = "결과 없음"; | ||
private final Map<String, String> results; | ||
private final Ladder ladder; | ||
Comment on lines
+8
to
+11
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. 도메인, 출력 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 LadderResult(Ladder ladder) { | ||
this.ladder = ladder; | ||
this.results = new HashMap<>(); | ||
} | ||
|
||
public void calculateResults(List<String> playerNames, Prizes prizes) { | ||
List<Line> lines = ladder.getLines(); | ||
List<String> prizeValues = prizes.getPrize(); | ||
|
||
for (String playerName : playerNames) { | ||
int playerIndex = playerNames.indexOf(playerName); | ||
for (Line line : lines) { | ||
playerIndex = getNewIndexMove(playerIndex, line.getPointGroups()); | ||
} | ||
results.put(playerName, prizeValues.get(playerIndex)); | ||
} | ||
} | ||
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.
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 void calculateResults(List<String> playerNames, Prizes prizes) {
List<String> prizeValues = prizes.getPrize();
for (String playerName : playerNames) {
int playerIndex = playerNames.indexOf(playerName);
playerIndex = ladder.move(playerIndex);
results.put(playerName, prizeValues.get(playerIndex));
}
} |
||
|
||
private int getNewIndexMove(int currentIndex, List<Point> points) { | ||
if (canMoveLeft(currentIndex, points)) { | ||
return currentIndex - 1; | ||
} | ||
if (canMoveRight(currentIndex, points)) { | ||
return currentIndex + 1; | ||
} | ||
return currentIndex; | ||
} | ||
|
||
private boolean canMoveLeft(int currentIndex, List<Point> points) { | ||
return currentIndex > 0 && points.get(currentIndex - 1) == Point.HAS_POINT; | ||
} | ||
|
||
private boolean canMoveRight(int currentIndex, List<Point> points) { | ||
return currentIndex < points.size() && points.get(currentIndex) == Point.HAS_POINT; | ||
} | ||
|
||
public String getResultForPlayer(String name) { | ||
return results.getOrDefault(name, NO_RESULT); | ||
} | ||
|
||
public Map<String, String> getValue() { | ||
return Collections.unmodifiableMap(results); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package model; | ||
|
||
public class Player { | ||
|
||
private static final int MAX_NAME_LENGTH = 5; | ||
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.
그러나 현재 프로젝트에서는 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 String value; | ||
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. Player의 value는 무엇일까요? 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 Player(String value) { | ||
validateValues(value); | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
private void validateValues(String values) { | ||
if (values.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException("참가자 이름은 5글자를 초과할 수 없습니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||||||||||||||||
package model; | ||||||||||||||||||||||
|
||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||
|
||||||||||||||||||||||
public class Players { | ||||||||||||||||||||||
|
||||||||||||||||||||||
private static final int MITMUM_PLAYER_LENGTH = 2; | ||||||||||||||||||||||
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.
Suggested change
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<Player> players; | ||||||||||||||||||||||
|
||||||||||||||||||||||
public Players(List<String> players) { | ||||||||||||||||||||||
validatePlayers(players); | ||||||||||||||||||||||
this.players = generatePlayers(players); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public int size() { | ||||||||||||||||||||||
return players.size(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public List<String> getPlayers() { | ||||||||||||||||||||||
return players.stream() | ||||||||||||||||||||||
.map(Player::getValue) | ||||||||||||||||||||||
.toList(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
private List<Player> generatePlayers(List<String> players) { | ||||||||||||||||||||||
return players.stream() | ||||||||||||||||||||||
.map(Player::new) | ||||||||||||||||||||||
.toList(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
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.
Suggested change
더 명확히 써볼 수 있을 것 같아요. 😄 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 void validatePlayers(List<String> players) { | ||||||||||||||||||||||
if (players.size() < MITMUM_PLAYER_LENGTH) { | ||||||||||||||||||||||
throw new IllegalArgumentException("참가자는 2명 이상이여야 합니다."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||||||||||||||||||
package model; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public class Prize { | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
private final String value; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public Prize(String value) { | ||||||||||||||||||||||||
validateValue(value); | ||||||||||||||||||||||||
this.value = value; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public String getValue() { | ||||||||||||||||||||||||
return value; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
private void validateValue(String value) { | ||||||||||||||||||||||||
if(value == null || value.isEmpty()) { | ||||||||||||||||||||||||
throw new IllegalArgumentException("실행결과는 null이거나 공백일 수는 없습니다."); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
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.
Suggested change
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,35 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Prizes { | ||
|
||
private final List<Prize> prize; | ||
|
||
public Prizes(List<Prize> prize) { | ||
this.prize = prize; | ||
} | ||
|
||
public static Prizes form(List<String> result, Players players) { | ||
validatePrize(result, players); | ||
return new Prizes(generatePrize(result)); | ||
} | ||
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. form은 오타인가요? 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 List<String> getPrize() { | ||
return prize.stream() | ||
.map(Prize::getValue) | ||
.toList(); | ||
} | ||
|
||
private static List<Prize> generatePrize(List<String> result) { | ||
return result.stream() | ||
.map(Prize::new) | ||
.toList(); | ||
} | ||
|
||
private static void validatePrize(List<String> prizes, Players players) { | ||
if (prizes.size() != players.size()) { | ||
throw new IllegalArgumentException("실행결과 개수와 참가자의 수는 동일해야 합니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package model; | ||
|
||
public interface RandomUtil { | ||
int generateRandomNumber(); | ||
} | ||
Comment on lines
+3
to
+5
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,13 @@ | ||
package model; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomValueGenerator implements RandomUtil { | ||
private static final int RANDOM_BOUND = 2; | ||
private static final Random random = new Random(); | ||
|
||
@Override | ||
public int generateRandomNumber() { | ||
return random.nextInt(RANDOM_BOUND); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package test; | ||
|
||
import model.RandomUtil; | ||
|
||
public class FixedNumberGenerator implements RandomUtil { | ||
private final int fixedNumber; | ||
|
||
public FixedNumberGenerator(int fixedNumber) { | ||
this.fixedNumber = fixedNumber; | ||
} | ||
|
||
@Override | ||
public int generateRandomNumber() { | ||
return fixedNumber; | ||
} | ||
} | ||
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. test를 위한 구현체는 테스트 폴더 하위에 두면 어떨까요? 실제 어플리케이션을 실행할 때는 필요하지 않을 것 같아서요. 😄 학습 키워드
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. 단순히 프로덕션 코드에 |
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.
*로 import 하신 특별한 이유가 있나요?
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.
제가 의도하지 않은 부분 같습니다..
확인해봤더니 클래스를
import
할 때, 자동으로 와일드카드(*)
로 통합되는 경우인거 같은데,인텔리제이 내에서 각각의 클래스들을 명시적으로
import
문에 써주도록 설정해두겠습니다 감사합니다 !