-
Notifications
You must be signed in to change notification settings - Fork 55
[BCSD Lab] 이동훈 4차시 미션제출합니다. #77
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
Changes from all commits
485f7b7
ad573e8
a63a639
3359aee
904758b
8cf5691
b12cbc8
baf76a8
b34c673
c4f8c24
d41b964
826d490
c7cd40a
50bdc72
9e82cca
d22773e
efbeb0e
4453751
6112d22
6a31326
a9ddb5d
43ffec6
bd68c6f
4c332df
fa4a311
47e7017
41ad7e0
f23302f
d3dd9f3
ddfa603
3ee9373
0076346
2bc1732
708d795
e23e431
ec6e161
add62bc
b2dc9e0
1e47ba4
a8bf54f
cd04c6a
414ef4d
af25e2b
fe893f7
2d755db
d24d850
bd9ddb0
3327486
112211a
117f4ac
acedebe
3d32ae6
16c09ce
b1ed65a
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,24 @@ | ||
import java.util.Random; | ||
import java.util.Scanner; | ||
|
||
import controller.LadderGameController; | ||
import model.LadderGenerator; | ||
import view.InputView; | ||
import view.LadderView; | ||
import view.LineView; | ||
import view.OutputView; | ||
|
||
public class LadderGameApplication { | ||
|
||
public static void main(String[] args) { | ||
LineView lineView = new LineView(); | ||
LadderView ladderView = new LadderView(lineView); | ||
OutputView outputView = new OutputView(ladderView); | ||
InputView inputView = new InputView(new Scanner(System.in)); | ||
LadderGenerator ladderGenerator = new LadderGenerator(new Random()); | ||
|
||
LadderGameController ladderGameController = new LadderGameController(ladderGenerator, outputView, inputView); | ||
|
||
ladderGameController.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package controller; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.Scanner; | ||
|
||
import model.*; | ||
import util.Parser; | ||
import view.InputView; | ||
import view.LadderView; | ||
import view.LineView; | ||
import view.OutputView; | ||
|
||
public class LadderGameController { | ||
|
||
private final LadderGenerator ladderGenerator; | ||
private final OutputView outputView; | ||
private final InputView inputView; | ||
|
||
private static final String VIEW_ALL = "all"; | ||
|
||
public LadderGameController( | ||
LadderGenerator ladderGenerator, | ||
OutputView outputView, | ||
InputView inputView | ||
) { | ||
this.ladderGenerator = new LadderGenerator(new Random()); | ||
this.outputView = new OutputView(new LadderView(new LineView())); | ||
this.inputView = new InputView(new Scanner(System.in)); | ||
} | ||
|
||
public void run() { | ||
Players players = inputPlayers(); | ||
Prizes prizes = inputPrizes(); | ||
|
||
Width width = new Width(players.size()); | ||
Height height = new Height(inputView.inputHeight()); | ||
Ladder ladder = ladderGenerator.generate(height, width); | ||
|
||
outputView.printExecuteResult(ladder, players, prizes); | ||
|
||
printResultByViewerName(ladder, players, prizes); | ||
} | ||
|
||
private Players inputPlayers() { | ||
String strPlayerNames = inputView.inputPlayerNames(); | ||
List<String> playerNames = Parser.parseStringToList(strPlayerNames); | ||
|
||
return new Players(playerNames); | ||
} | ||
|
||
private Prizes inputPrizes() { | ||
String strPrizeNames = inputView.inputPrizeNames(); | ||
List<String> prizeNames = Parser.parseStringToList(strPrizeNames); | ||
|
||
return new Prizes(prizeNames); | ||
} | ||
|
||
private void printResultByViewerName(Ladder ladder, Players players, Prizes prizes) { | ||
String resultViewerName = inputView.inputResultViewerName(); | ||
|
||
if (resultViewerName.equalsIgnoreCase(VIEW_ALL)) { | ||
outputView.printAllPlayerResult(ladder, players, prizes); | ||
} else { | ||
outputView.printSinglePlayerResult(ladder, players, prizes, resultViewerName); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package exception; | ||
|
||
public class CustomException extends IllegalArgumentException { | ||
|
||
public CustomException(ErrorMessage errorMessage) { | ||
super(errorMessage.getMessage()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package exception; | ||
|
||
public enum ErrorMessage { | ||
|
||
NOT_INTEGER_FORMAT("int 타입 정수가 아닌 값이 입력되었습니다."), | ||
NEGATIVE_NUMBER("음수가 입력되었습니다."), | ||
EMPTY_NAME("이름이 비어있습니다."), | ||
NAME_LENGTH("이름이 5글자를 넘었습니다."), | ||
PLAYER_NAMES_EMPTY("참여자가 존재하지 않습니다"), | ||
PRIZE_EMPTY("당첨 결과가 비어있습니다."), | ||
PRIZES_EMPTY("당첨 결과 리스트가 비어있습니다."), | ||
PLAYER_NOT_FOUND("해당 플레이어를 찾을 수 없습니다."), | ||
NULL_INPUT_STRING("입력 문자열로 null이 입력 되었습니다."); | ||
Comment on lines
+5
to
+13
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 message; | ||
|
||
ErrorMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package model; | ||
|
||
import util.SizeValidator; | ||
|
||
public class Height { | ||
|
||
private final int height; | ||
|
||
public Height(int height) { | ||
SizeValidator.validateNotNegative(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. 이렇게 분리했을때의 장점은 뭐가 있을까요? 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.
|
||
this.height = height; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Ladder { | ||
|
||
private final Height height; | ||
private final List<Line> lines; | ||
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. List을 총괄하는 Lines 일급 컬렉션을 만들어도 좋을 것 같아요! 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 Ladder(Height height, List<Line> lines) { | ||
this.height = height; | ||
this.lines = lines; | ||
} | ||
|
||
public int move(int index) { | ||
int position = index; | ||
|
||
for (Line line : lines) { | ||
position = line.move(position); | ||
} | ||
|
||
return position; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class LadderGenerator { | ||
|
||
private final Random random; | ||
|
||
public LadderGenerator(Random random) { | ||
this.random = random; | ||
} | ||
|
||
public Ladder generate(Height height, Width width) { | ||
List<Line> lines = new ArrayList<>(); | ||
|
||
for (int i = 0; i < height.getHeight(); i++) { | ||
lines.add(new Line(generateConnections(width))); | ||
} | ||
|
||
return new Ladder(height, lines); | ||
} | ||
|
||
private List<Boolean> generateConnections(Width width) { | ||
List<Boolean> connections = new ArrayList<>(); | ||
|
||
for (int i = 0; i < width.getWidth() - 1; i++) { | ||
if (i > 0 && connections.get(i - 1)) { | ||
connections.add(false); | ||
} else { | ||
connections.add(random.nextBoolean()); | ||
} | ||
} | ||
|
||
return connections; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
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.
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> connections; | ||
|
||
public Line(List<Boolean> connections) { | ||
this.connections = connections; | ||
} | ||
|
||
public int move(int index) { | ||
if (index < connections.size() && connections.get(index)) { | ||
return index + 1; | ||
} else if (index > 0 && connections.get(index - 1)) { | ||
return index - 1; | ||
} | ||
|
||
return index; | ||
} | ||
|
||
public List<Boolean> getConnections() { | ||
return connections; | ||
} | ||
Comment on lines
+23
to
+25
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. 해당 부분은 기능 자체에서는 사용하지 않지만 테스트 코드를 작성하면서 필요하기에 추가했습니다. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package model; | ||
|
||
import exception.CustomException; | ||
import exception.ErrorMessage; | ||
|
||
public class Player { | ||
|
||
private final String name; | ||
|
||
private static final int MAX_NAME_LENGTH = 5; | ||
|
||
public Player(String name) { | ||
validate(name); | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
private void validate(String name) { | ||
validateNameEmpty(name); | ||
validateNameLength(name); | ||
} | ||
|
||
private void validateNameEmpty(String name) { | ||
if (name == null || name.isBlank()) { | ||
throw new CustomException(ErrorMessage.EMPTY_NAME); | ||
} | ||
} | ||
|
||
private void validateNameLength(String name) { | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new CustomException(ErrorMessage.NAME_LENGTH); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import exception.CustomException; | ||
import exception.ErrorMessage; | ||
|
||
public class Players { | ||
|
||
private final List<Player> players; | ||
|
||
public Players(List<String> playerNames) { | ||
validateEmpty(playerNames); | ||
this.players = init(playerNames); | ||
} | ||
|
||
public void validateEmpty(List<String> playerNames) { | ||
if (playerNames == null || playerNames.isEmpty()) { | ||
throw new CustomException(ErrorMessage.PLAYER_NAMES_EMPTY); | ||
} | ||
} | ||
|
||
private List<Player> init(List<String> playerNames) { | ||
List<Player> result = new ArrayList<>(); | ||
|
||
for (String name : playerNames) { | ||
result.add(new Player(name)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public int size() { | ||
return players.size(); | ||
} | ||
|
||
public List<Player> getPlayers() { | ||
return players; | ||
} | ||
|
||
public int indexOf(String name) { | ||
for (int i = 0; i < players.size(); i++) { | ||
if (players.get(i).getName().equals(name)) { | ||
return i; | ||
} | ||
} | ||
throw new CustomException(ErrorMessage.PLAYER_NOT_FOUND); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package model; | ||
|
||
import exception.CustomException; | ||
import exception.ErrorMessage; | ||
|
||
public class Prize { | ||
|
||
private final String prize; | ||
|
||
public Prize(String prize) { | ||
validateEmpty(prize); | ||
this.prize = prize; | ||
} | ||
|
||
private void validateEmpty(String prize) { | ||
if (prize == null || prize.isBlank()) { | ||
throw new CustomException(ErrorMessage.PRIZE_EMPTY); | ||
} | ||
} | ||
|
||
public String getPrize() { | ||
return prize; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import exception.CustomException; | ||
import exception.ErrorMessage; | ||
|
||
public class Prizes { | ||
|
||
private final List<Prize> prizes; | ||
|
||
public Prizes(List<String> prizes) { | ||
validateEmpty(prizes); | ||
this.prizes = init(prizes); | ||
} | ||
|
||
private void validateEmpty(List<String> prizes) { | ||
if (prizes == null || prizes.isEmpty()) { | ||
throw new CustomException(ErrorMessage.PRIZES_EMPTY); | ||
} | ||
} | ||
|
||
private List<Prize> init(List<String> prizes) { | ||
List<Prize> result = new ArrayList<>(); | ||
|
||
for (String name : prizes) { | ||
result.add(new Prize(name)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<Prize> getPrizes() { | ||
return prizes; | ||
} | ||
} |
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.
run()
메서드가 확실히 깔끔해진것 같네요메서드 분리 잘하신것 같아요 👍