-
Notifications
You must be signed in to change notification settings - Fork 2
2주차 미션 다시 구현 #11
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: oliviarla
Are you sure you want to change the base?
2주차 미션 다시 구현 #11
Changes from 1 commit
2c8a53d
72369e7
9ee8c96
44464e3
bf277a4
2cfa9fb
15c6b05
01dd974
8024763
eb92b3d
a9185d4
5f8046b
b294dca
19461c4
be73fe2
8829032
967f871
a1bb02d
f13c04b
01e34cb
5b260ec
6c2a62c
65d6ef8
bbf4efa
f7a0fa7
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,31 +1,31 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.domain.Ball; | ||
| import baseball.domain.Baseball; | ||
| import baseball.domain.BaseballStatus; | ||
| import baseball.service.BaseballService; | ||
| import baseball.service.RandomNumGenerator; | ||
| import baseball.service.RandomBallGenerator; | ||
| import baseball.view.InputView; | ||
| import baseball.view.OutputView; | ||
|
|
||
| import java.util.stream.Collectors; | ||
|
|
||
| public class application { | ||
| public static void main(String[] args) throws Exception { | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| RandomNumGenerator randomNumGenerator = new RandomNumGenerator(); | ||
| RandomBallGenerator randomBallGenerator = new RandomBallGenerator(); | ||
| BaseballService baseballService = new BaseballService(); | ||
|
|
||
| while(true){ | ||
| Baseball randomBaseball = new Baseball(randomNumGenerator.makeNum()); | ||
| BaseballStatus baseballStatus = new BaseballStatus(); | ||
|
|
||
| while(!outputView.exitGame(baseballStatus)){ | ||
| do { | ||
| Baseball randomBaseball = new Baseball(randomBallGenerator.makeNum()); | ||
| BaseballStatus baseballStatus; | ||
| do { | ||
| Baseball userBaseball = inputView.inputBall(); | ||
| baseballStatus = baseballService.compare(userBaseball, randomBaseball); | ||
| outputView.printBaseballStatus(baseballStatus); | ||
| } | ||
| if(!inputView.resumeGame()){ | ||
| break; | ||
| } | ||
| } | ||
| } while (!outputView.exitGame(baseballStatus)); | ||
|
|
||
| }while(inputView.resumeGame()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package baseball.domain; | ||
|
|
||
| public class Ball { | ||
|
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. 추가적으로 Ball의 생애주기와 Baseball의 생애주기가 같은지에 대해서도 고민해보시는 걸 추천 드립니다! 힌트를 드리자면, 엔티티와 값 객체의 차이가 되겠군요! 👍 |
||
| private final int number; | ||
|
|
||
| public Ball(final int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return number; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,28 @@ | ||
| package baseball.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Baseball { | ||
|
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. InputView에서 Balseball의 리스르 크기 검사를 Baseball 구현체에서 진행하는건 어떨까요? |
||
| List<Integer> baseballs; | ||
| private final List<Ball> baseballs; | ||
|
|
||
| public Baseball(List<Integer> balls) { | ||
| this.baseballs = balls; | ||
| public Baseball(final List<Ball> ballList){ | ||
| this.baseballs= new ArrayList<>(ballList); | ||
| isDistinct(); | ||
| } | ||
|
|
||
| public List<Integer> getBaseballs() { | ||
| return baseballs; | ||
| public List<Ball> getBaseballs() { | ||
| return Collections.unmodifiableList(baseballs); | ||
|
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 isDistinct(){ | ||
|
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. 해당 메서드의 depth가 4칸 정도 되는 것 같아요! depth를 낮추는 방법이 있을까요? |
||
| for(int i=0;i<this.baseballs.size();i++){ | ||
| for(int j=0;j<i;j++){ | ||
| if(this.baseballs.get(i).getNumber() == this.baseballs.get(j).getNumber()){ | ||
| throw new RuntimeException("중복된 값이 있습니다."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,19 @@ | ||
| package baseball.domain; | ||
|
|
||
| public class BaseballStatus { | ||
| int ball; | ||
| int strike; | ||
| private final int ball; | ||
| private final int strike; | ||
|
|
||
| public BaseballStatus(int ball, int strike) throws Exception { | ||
| this.ball = ball; | ||
| this.strike = strike; | ||
| this.isValid(); | ||
|
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 isValid() throws Exception{ | ||
| if (!this.existsBall() && !this.existsStrike() && !this.nothing()) | ||
| throw new Exception("결과를 반환할 수 없습니다"); | ||
| } | ||
|
|
||
|
|
||
| public int getBall() { | ||
| return ball; | ||
|
|
@@ -12,14 +23,6 @@ public int getStrike() { | |
| return strike; | ||
| } | ||
|
|
||
| public void setBall(int ball) { | ||
| this.ball = ball; | ||
| } | ||
|
|
||
| public void setStrike(int strike) { | ||
| this.strike = strike; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BaseballStatus{" + | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,31 @@ | ||
| package baseball.service; | ||
|
|
||
| import baseball.domain.Action; | ||
| import baseball.domain.Ball; | ||
| import baseball.domain.Baseball; | ||
| import baseball.domain.BaseballStatus; | ||
|
|
||
| public class BaseballService { | ||
| public boolean isStrike(int userNum, int randomNum) { | ||
| return userNum == randomNum; | ||
| public boolean isStrike(Ball userBall, Ball randomBall) { | ||
| return userBall.getNumber() == randomBall.getNumber(); | ||
| } | ||
|
|
||
| public boolean isBall(int userNum, int userIdx, Baseball randomBall) { | ||
| if (!isStrike(userNum, randomBall.getBaseballs().get(userIdx))) { | ||
| return randomBall.getBaseballs().contains(userNum); | ||
| } | ||
| return false; | ||
| public boolean isBall(Ball userBall, Baseball randomBall) { | ||
| return randomBall.getBaseballs().stream().anyMatch(ball -> ball.getNumber()==userBall.getNumber()); | ||
| } | ||
|
|
||
| public BaseballStatus compare(Baseball userBall, Baseball randomBall) { | ||
| int strike = 0, ball = 0; | ||
| public BaseballStatus compare(Baseball userBall, Baseball randomBall) throws Exception { | ||
| int strike = 0; | ||
| int ball = 0; | ||
| for (int i = 0; i < userBall.getBaseballs().size(); i++) { | ||
| if (isStrike(userBall.getBaseballs().get(i), randomBall.getBaseballs().get(i))) { | ||
| strike++; | ||
| continue; | ||
| } | ||
| if (isBall(userBall.getBaseballs().get(i), i, randomBall)) { | ||
| if (isBall(userBall.getBaseballs().get(i), randomBall)) { | ||
| ball++; | ||
| } | ||
| } | ||
| BaseballStatus baseballStatus = new BaseballStatus(); | ||
| baseballStatus.setBall(ball); | ||
| baseballStatus.setStrike(strike); | ||
| return baseballStatus; | ||
| return new BaseballStatus(ball, strike); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package baseball.service; | ||
|
|
||
| import baseball.domain.Ball; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class RandomBallGenerator { | ||
| static int MAX_SIZE = 3; | ||
| static int MAX_NUM = 9; | ||
|
Comment on lines
+8
to
+9
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. static을 사용해서 변수를 공유할 수 있게 됐군요! 그리고 추가적으로 접근 제어자를 선언하지 않는다면 기본적으로 설정되는 접근 제어자가 무엇일지 고민해봐도 좋을 것 같습니다. |
||
|
|
||
| public List<Ball> makeNum(){ | ||
| Random random = new Random(); | ||
| Set<Integer> set = new HashSet<>(); | ||
|
|
||
| while(set.size()<MAX_SIZE){ | ||
| int num = random.nextInt(MAX_NUM)+1; | ||
| set.add(num); | ||
| } | ||
|
|
||
| List<Ball> balls = new ArrayList<>(); | ||
| for(int e: set){ | ||
| Ball ball = new Ball(e); | ||
| balls.add(ball); | ||
| } | ||
| return balls; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,30 @@ | ||
| package baseball.view; | ||
|
|
||
| import baseball.domain.Ball; | ||
| import baseball.domain.Baseball; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
||
| public class InputView { | ||
| public Baseball inputBall() throws Exception { | ||
| static int MAX_SIZE = 3; | ||
|
|
||
| private void isValidInput(List<Ball> ballList) throws RuntimeException{ | ||
| if(ballList.size()!=MAX_SIZE){ | ||
|
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. 줄 정렬이 필요해보이는 코드이군요 |
||
| throw new RuntimeException("3자리 숫자여야 합니다."); | ||
| } | ||
| if(!ballList.stream().allMatch(ball -> ball.getNumber()>0 && ball.getNumber()<=9)){ | ||
| throw new RuntimeException("범위를 벗어납니다."); | ||
| } | ||
| } | ||
|
|
||
| public Baseball inputBall() throws RuntimeException { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.print("숫자를 입력해 주세요 : "); | ||
| String input = scanner.next(); | ||
| List<Integer> list = Arrays.stream(input.split("")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()); | ||
| if(list.size()!=3){ | ||
| throw new Exception("3자리 숫자여야 합니다."); | ||
| } | ||
|
|
||
| List<Ball> list = Arrays.stream(input.split("")).mapToInt(Integer::parseInt).mapToObj(Ball::new).collect(Collectors.toList()); | ||
| isValidInput(list); | ||
| return new Baseball(list); | ||
| } | ||
|
|
||
|
|
@@ -26,4 +34,6 @@ public boolean resumeGame() { | |
| int input = scanner.nextInt(); | ||
| return input == 1; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
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.
Ball 객체를 구현하셨군요!! 👍
해당 Ball 객체를 구현하면서 들어가게 되는 숫자의 유효성 검사를 해당 Ball에서 구현하는건 어떨까요?
즉, InputView에서 Balseball에 들어가는 숫자들의 유효성 검사를 Ball 구현체에서 진행하게 된다면 InputView와 그 외 객체들의 결합력을 낮추고 Ball이라는 구현체의 응집도를 높일 수 있을 것 같아요!