-
Notifications
You must be signed in to change notification settings - Fork 60
[사다리 미션] 황승준 미션 제출입니다. #23
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: davidolleh
Are you sure you want to change the base?
Changes from 32 commits
b07a49c
f493a05
2254785
e0ffcfb
02b6a8d
7cf4555
dcf7dc4
06cc958
fc66931
2e22fee
613461c
ed047ab
71d47c7
dd7174d
5781a34
9b5c7b5
125810f
92408bb
431bd39
77f88a7
9d693f0
91e4ceb
b70c3a5
c76ef4a
a09be2f
9bacca2
d767643
6aef072
5402201
726700e
a2f5884
f567502
e278cc8
bf26804
915c745
d5eda95
6bf7754
39d71d8
0c759b5
d67a2e9
936a471
5aa5433
7057283
d766f09
f3dd305
1d25eb1
8f5d014
3268173
19585d2
afe1ed8
a866afb
0c974c9
8bda0a9
4547ffe
7481d5d
a08a3c9
27da6f5
7e3f2e8
81e327f
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,3 @@ | ||
| - [x] 연결 여부는 랜덤으로 결정한다. | ||
| - [x] 사다리는 라인이 겹치지 않아야 한다. | ||
| - [x] 사람의 이름은 1자 이상 5자 이하여야 한다. | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class LadderGame { | ||
| public static void main(String[] args) { | ||
| LadderGameController ladderGameController = new LadderGameController( | ||
|
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. LadderGame 파일을 하나 더 만들어서 LadderGameController를 생성하는 방식으로 구현하셨네요. |
||
| new InputView(), | ||
| new OutputView() | ||
| ); | ||
|
|
||
| ladderGameController.ladderGame(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import domain.*; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LadderGameController { | ||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
|
|
||
| public LadderGameController(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void ladderGame() { | ||
| List<Person> participants = readParticipants(); | ||
| List<Prize> prizes = readPrizes(); | ||
|
|
||
| checkPrizeCountValidation(participants.size(), prizes.size()); | ||
|
|
||
| int height = readLadderHeight(); | ||
|
|
||
| Ladder ladder = new Ladder(height, participants.size(), new RandomLadderGeneratorImpl()); | ||
|
|
||
| outputView.printResult(participants, ladder, prizes); | ||
|
|
||
| Statistic statistic = new Statistic(ladder, participants, prizes); | ||
|
|
||
| printSpecificParticipantResult(statistic, participants); | ||
|
|
||
| outputView.printParticipantsPrizesResult(statistic.getParticipantPrize(), participants); | ||
| } | ||
|
|
||
| private List<Person> readParticipants() { | ||
| outputView.printParticipantInquiry(); | ||
| List<Person> participants = inputView.readParticipants(); | ||
| outputView.printEmptyLine(); | ||
|
|
||
| return participants; | ||
| } | ||
|
|
||
| private List<Prize> readPrizes() { | ||
| outputView.printResultPrizeInquiry(); | ||
| List<Prize> prizes = inputView.readPrizeResults(); | ||
| outputView.printEmptyLine(); | ||
|
|
||
| return prizes; | ||
| } | ||
|
|
||
| private int readLadderHeight() { | ||
| outputView.printLadderHeightInquiry(); | ||
| int height = inputView.readLadderHeight(); | ||
| outputView.printEmptyLine(); | ||
|
|
||
| return height; | ||
| } | ||
|
|
||
| private String readParticipantName() { | ||
| outputView.printParticipantPrizeInquiry(); | ||
| String name = inputView.readPersonName(); | ||
| outputView.printEmptyLine(); | ||
|
|
||
| return name; | ||
| } | ||
|
|
||
| private void printSpecificParticipantResult(Statistic statistic, List<Person> participants) { | ||
| while(true) { | ||
| String name = readParticipantName(); | ||
|
|
||
| if (name.equals("all")) { | ||
| break; | ||
| } | ||
|
|
||
| Person person = new Person(name); | ||
| checkParticipantValidation(participants, person); | ||
| outputView.printParticipantPrizeResult(statistic.getParticipantPrize(person)); | ||
| } | ||
| } | ||
|
|
||
| private void checkPrizeCountValidation(int participantCount, int prizeCount) { | ||
| if (participantCount != prizeCount) { | ||
| throw new IllegalArgumentException("The number of participants does not match the number of prizes."); | ||
| } | ||
| } | ||
|
|
||
| private void checkParticipantValidation(List<Person> participants, Person person) { | ||
| if (!participants.contains(person)) { | ||
| throw new IllegalArgumentException("참여자가 아닙니다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package domain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public enum Connection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CONNECTED, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UNCONNECTED, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+6
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. 👍
Comment on lines
+3
to
+6
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에 함수형 인터페이스 넣어보기 2함수형 인터페이스를 필드로 선언해서, 각각의 enum마다 다음 동작을 정의할 수 있겠네요!
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package domain; | ||
|
|
||
| public enum Direction { | ||
| LEFT, RIGHT, DOWN | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package domain; | ||
|
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. domain을 기능별로 나누어서 여러 파일로 구현하셨네요! |
||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Ladder { | ||
| private final List<Line> lines; | ||
| private final int ladderWidth; | ||
| private final int lineHeight; | ||
|
||
| private final RandomLadderGenerator randomLadderGenerator; | ||
|
|
||
|
|
||
| public Ladder(int lineHeight, int ladderWidth, RandomLadderGenerator randomLadderGenerator) { | ||
| this.randomLadderGenerator = randomLadderGenerator; | ||
| this.ladderWidth = ladderWidth; | ||
| this.lineHeight = lineHeight; | ||
|
|
||
| List<Line> lines = this.randomLadderGenerator.generateLadder(lineHeight, ladderWidth); | ||
| checkLadderValidation(lines); | ||
| this.lines = lines; | ||
| } | ||
|
||
|
|
||
| public List<Line> getLines() { | ||
| return lines; | ||
| } | ||
|
|
||
| public int getLadderWidth() { | ||
| return ladderWidth; | ||
| } | ||
|
|
||
| public int getLineHeight() { | ||
| return lineHeight; | ||
| } | ||
|
|
||
| private void checkLadderValidation(List<Line> lines) { | ||
| for (int i = 0; i < lineHeight; i++) { | ||
| checkRowValidation(i, lines); | ||
| } | ||
| } | ||
|
|
||
| private void checkRowValidation(int columnIndex, List<Line> lines) { | ||
| List<Direction> rowDirections = lines.stream() | ||
| .map(l -> l.getPoints().get(columnIndex)) | ||
| .toList(); | ||
|
|
||
| int rowSize = rowDirections.size(); | ||
| int rightCount = 0; | ||
|
|
||
| for (int i = 0; i < rowSize; i++) { | ||
| if (rowDirections.get(i) == Direction.RIGHT) { | ||
| checkRightValidation(i, rowSize, rowDirections); | ||
| rightCount++; | ||
| } | ||
|
|
||
| if (rowDirections.get(i) == Direction.LEFT) { | ||
| checkLeftValidation(rightCount); | ||
| rightCount--; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void checkRightValidation(int columnIndex, int rowSize, List<Direction> rowDirections) { | ||
| if (columnIndex == rowSize - 1 || rowDirections.get(columnIndex + 1) != Direction.LEFT) { | ||
| throw new RuntimeException("사다리가 잘못 만들어졌습니다."); | ||
| } | ||
| } | ||
|
|
||
| private void checkLeftValidation(int rightCount) { | ||
| if (rightCount == 0) { | ||
| throw new RuntimeException("사다리가 잘못 만들어졌습니다."); | ||
| } | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Line { | ||
| private final List<Direction> points; | ||
|
|
||
| public Line(List<Direction> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public List<Direction> getPoints() { | ||
| return points; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Person { | ||
| private final String name; | ||
| private static final int MAX_LENGTH = 5; | ||
|
|
||
| public Person(String name) { | ||
| nameValidation(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| private void nameValidation(String name) { | ||
|
||
| if (name.length() > MAX_LENGTH || name.isEmpty()) | ||
| throw new IllegalArgumentException("사람 이름은 1글자 이상 5글자 이하여야 합니다."); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Person person = (Person) o; | ||
| return Objects.equals(name, person.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(name); | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||
| package domain; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| public class Prize { | ||||||||||||||||||||||||||||
| private final int cost; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| public Prize(int cost) { | ||||||||||||||||||||||||||||
| this.cost = cost; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| public String costToString() { | ||||||||||||||||||||||||||||
| if (cost ==0) | ||||||||||||||||||||||||||||
| return "꽝"; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return String.valueOf(cost); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| public String costToString() { | |
| if (cost ==0) | |
| return "꽝"; | |
| return String.valueOf(cost); | |
| } | |
| public String costToString() { | |
| if (cost == 0) { | |
| return "꽝"; | |
| } | |
| return String.valueOf(cost); | |
| } |
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.
Prize는 금액을 변수로 갖고 있다는 요구사항을 추가하였고 "꽝"에 대한 표현은 view(프론트)로 역할을 넘겼습니다!
Outdated
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.
[Approve]
👍
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface RandomLadderGenerator { | ||
| List<Line> generateLadder(int lineHeight, int ladderWidth); | ||
| } |



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.
리뷰가 처음이여서 부족할 수 있습니다.. 참고 부탁드립니다 :)