-
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
3단계 - 사다리(게임 실행) #1638
base: jwoo-o
Are you sure you want to change the base?
3단계 - 사다리(게임 실행) #1638
Changes from all commits
680de22
85ae3f8
0fd9aa3
01910e9
de72b93
80cdb39
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,28 +1,43 @@ | ||
package ladder.controller; | ||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.Results; | ||
import ladder.domain.Users; | ||
import ladder.service.LadderResultService; | ||
import ladder.view.InputView; | ||
import ladder.view.OutputView; | ||
|
||
public class LadderController { | ||
|
||
private static LadderController ladderController; | ||
private final LadderResultService ladderResultService; | ||
|
||
private LadderController(LadderResultService ladderResultService) | ||
{ | ||
this.ladderResultService = ladderResultService; | ||
} | ||
|
||
public static LadderController getInstance() { | ||
if (ladderController == null) { | ||
ladderController = new LadderController(); | ||
ladderController = new LadderController(LadderResultService.getInstance()); | ||
} | ||
|
||
return ladderController; | ||
} | ||
|
||
public void run() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
Users users = Users.from(InputView.getNames()); | ||
|
||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
Results results = Results.of(InputView.getResult(), users.getUserCount()); | ||
Ladder ladder = Ladder.of(users.getUserCount(), InputView.getHeight()); | ||
|
||
OutputView.printLadder(users.getUserList(), ladder.getLines()); | ||
OutputView.printLadder(users.getUserList(), ladder.getLines(), results.getResults()); | ||
|
||
boolean isEnd = false; | ||
while (!isEnd) { | ||
isEnd = OutputView | ||
.printLadderResult(ladderResultService.getLadderResult(InputView.getTargetPlayer(), users.getUserList() | ||
, ladder, results.getResults())); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ladder.domain; | ||
|
||
public class 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. 해당 코드는 도메인으로 관리되기엔 도메인 로직이 없는 객체 같아요!! 혹시 어떠한 의도로 생성했을까요? |
||
{ | ||
private String result; | ||
|
||
private Result(String result) | ||
{ | ||
this.result = result; | ||
} | ||
|
||
public static Result from(String result) { | ||
return new Result(result); | ||
} | ||
|
||
public String getResult() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Results | ||
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<Result> results; | ||
|
||
private Results(List<Result> results) | ||
{ | ||
this.results = results; | ||
} | ||
|
||
public static Results of(String[] resultArray, int userSize) { | ||
if (userSize != resultArray.length) { | ||
throw new IllegalArgumentException("결과값과 인원수가 다릅니다"); | ||
} | ||
return new Results(Arrays.stream(resultArray) | ||
.map(Result::from) | ||
.collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
public List<Result> getResults() { | ||
return results; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ladder.service; | ||
|
||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.Result; | ||
import ladder.domain.User; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LadderResultService | ||
{ | ||
private static LadderResultService ladderResultService; | ||
|
||
public static LadderResultService getInstance() | ||
{ | ||
if (ladderResultService == null) | ||
{ | ||
ladderResultService = new LadderResultService(); | ||
} | ||
|
||
return ladderResultService; | ||
} | ||
|
||
public List<String> getLadderResult(String targetPlayer, List<User> userList | ||
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. 하나의 함수에 너무 많은 파라미터를 받고 있어요!! 한번 파라미터의 개수를 줄일 수 있도록 리팩토링 해보는 것은 어떨까요? |
||
, Ladder ladder, List<Result> results) { | ||
if (targetPlayer.equals("all")) { | ||
return allLadderResult(userList, ladder, results); | ||
} | ||
return userLadderResult(findUserIndex(targetPlayer, userList), ladder, results); | ||
} | ||
|
||
private List<String> userLadderResult(int userIndex, Ladder ladder, List<Result> results) { | ||
return List.of(results.get(ladder.move(userIndex)).getResult()); | ||
} | ||
|
||
private List<String> allLadderResult(List<User> userList, Ladder ladder, List<Result> results) { | ||
return IntStream.range(0, userList.size()) | ||
.mapToObj(index -> userList.get(index) + " : " + results.get(ladder.move(index))) | ||
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. 해당 라인은 출력문의 로직이 아닐까요? |
||
.collect(Collectors.toList()); | ||
} | ||
|
||
private int findUserIndex(String targetPlayer, List<User> userList) { | ||
return IntStream.range(0, userList.size()) | ||
.filter(i -> Objects.equals(userList.get(i).getName(), targetPlayer)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("사다리게임에 참여하지 않은 사람입니다.")); | ||
} | ||
} |
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.
삼항 연산자를 사용하지 말고 코드를 작성해보면 좋을 것 같아요!!