Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/BaseballApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import study.model.Baseball;
import study.model.BaseballAnswer;
import study.model.BaseballScore;
import study.model.BaseballUser;
import study.view.ResultView;

public class BaseballApplication {
public static void main(String[] args) {
BaseballUser user = new BaseballUser("123");
Baseball baseball = new Baseball(new BaseballScore(), new BaseballAnswer());
BaseballScore score = baseball.play(user);

ResultView resultView = new ResultView();
System.out.println(resultView.printScore(score));
}
}
6 changes: 6 additions & 0 deletions src/main/java/study/message/ErrorMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package study.message;

public class ErrorMessages {
public static final String INVALID_INPUT_NUMBER = "숫자만 입력 가능합니다.";
public static final String INVALID_INPUT_LENGTH = "3자리 수만 입력 가능합니다.";
}
35 changes: 35 additions & 0 deletions src/main/java/study/model/Baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package study.model;

import java.util.*;

public class Baseball {
private static final int PLAY_ROUND = 3;
private final List<Integer> answer;
private final BaseballScore score;

public Baseball(BaseballScore score, BaseballAnswer baseballAnswer){
this.score = score;
this.answer = baseballAnswer.getAnswer();
}

public BaseballScore play(BaseballUser user) {
System.out.println(answer.toString());
for(int i=0; i<PLAY_ROUND; i++){
calculateScore(i, user.getUserInputNumber(i));
}

return score;
}

public void calculateScore(int index, int input){
boolean isStrike = answer.get(index).equals(input);

if (isStrike){
score.addStrike();
}
if (!isStrike && answer.contains(input)){
score.addBall();
}
}

}
35 changes: 35 additions & 0 deletions src/main/java/study/model/BaseballAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package study.model;

import study.utils.BaseballUtils;

import java.util.ArrayList;
import java.util.List;

public class BaseballAnswer {
private static final int NUMBER_OF_ANSWER = 3;
private final List<Integer> answer;

public BaseballAnswer(){
this.answer = new ArrayList<>();
createAnswer();
}

public List<Integer> getAnswer() {
return answer;
}

public void createAnswer() {
while(answer.size() < NUMBER_OF_ANSWER){
addUniqueNumber(answer);
}

}

public void addUniqueNumber(List<Integer> answer) {
int num = BaseballUtils.getRandomNumber();

if (!answer.contains(num)){
answer.add(num);
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/study/model/BaseballScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package study.model;

public class BaseballScore {
private int strike;
private int ball;

public BaseballScore(){
this.strike = 0;
this.ball = 0;
}

public void addStrike(){
this.strike++;
}

public void addBall() {
this.ball++;
}

public int getStrike() {
return strike;
}

public int getBall() {
return ball;
}

public boolean isThreeStrike() {
return strike == 3;
}
}
35 changes: 35 additions & 0 deletions src/main/java/study/model/BaseballUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package study.model;

import study.utils.BaseballUtils;

import java.util.List;

public class BaseballUser {

private final List<Integer> userInput;

public BaseballUser(String input){
InputValidate inputValidate = new InputValidate(input);
this.userInput = inputValidate.validate();
}

public int getUserInputNumber(int index){
return userInput.get(index);
}

public static class InputValidate {
private final String input;

public InputValidate(String input){
this.input = BaseballUtils.removeSpaces(input);
}

public List<Integer> validate(){
BaseballUtils.checkLength(input);
return BaseballUtils.stringToIntegerList(input);
}

}

}

58 changes: 58 additions & 0 deletions src/main/java/study/utils/BaseballUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package study.utils;

import study.message.ErrorMessages;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BaseballUtils {

private static final int MAX_LIST_SIZE = 3;
private static final int MAX_RANDOM_NUMBER = 9;
private static final Random random = new Random();


public static int getRandomNumber() {
return random.nextInt(MAX_RANDOM_NUMBER)+1;
}

public static String removeSpaces(String input){
return input.replaceAll(" ", "");
}

public static void checkLength(String input) {
if (input.length() != MAX_LIST_SIZE){
throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH);
}
}

public static String[] stringToArrays(String input){
return input.split("");
}

public static List<Integer> stringToIntegerList(String input) {
List<Integer> list = new ArrayList<>();
String[] strArr = stringToArrays(input);

for (String s : strArr) {
list.add(BaseballUtils.stringParseInt(s));
}

return list;
}

public static int stringParseInt(String str){
int value = 0;

try {
value = Integer.parseInt(str);
} catch (NumberFormatException e){
throw new NumberFormatException(ErrorMessages.INVALID_INPUT_NUMBER);

}

return value;
}

}
27 changes: 27 additions & 0 deletions src/main/java/study/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package study.view;

import study.model.BaseballScore;

public class ResultView {
public String printScore(BaseballScore score) {
StringBuilder sb = new StringBuilder();

int ball = score.getBall();
int strike = score.getStrike();

if (ball != 0){
sb.append(ball).append("볼");
}
if (ball != 0 && strike != 0){
sb.append(" ");
}
if (strike != 0){
sb.append(strike).append("스트라이크");
}
if (ball == 0 && strike == 0){
sb.append("낫싱");
}

return sb.toString();
}
}
13 changes: 0 additions & 13 deletions src/test/java/study/StringTest.java

This file was deleted.

30 changes: 30 additions & 0 deletions src/test/java/study/model/BaseballAnswerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package study.model;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class BaseballAnswerTest {

@Test
@DisplayName("정답 생성")
void createAnswer(){
// given
BaseballAnswer baseballAnswer = new BaseballAnswer();

// when
baseballAnswer.createAnswer();

// then
List<Integer> response = baseballAnswer.getAnswer();
assertEquals(response.size(), 3);
assertNotEquals(response.get(0), response.get(1));
assertNotEquals(response.get(0), response.get(2));
assertNotEquals(response.get(1), response.get(2));
}

}
69 changes: 69 additions & 0 deletions src/test/java/study/model/BaseballScoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package study.model;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BaseballScoreTest {

@Test
@DisplayName("스트라이크 점수 증가")
void addStrike() {
// given
BaseballScore score = new BaseballScore();

// when
score.addStrike();

// then
assertEquals(score.getStrike(), 1);
}

@Test
@DisplayName("볼 점수 증가")
void addBall() {
// given
BaseballScore score = new BaseballScore();

// when
score.addBall();

// then
assertEquals(score.getBall(), 1);
}

@Test
@DisplayName("3스트라이크인 경우 true 반환")
void isThreeStrikeTrue(){
// given
BaseballScore score = new BaseballScore();
score.addStrike();
score.addStrike();
score.addStrike();

// when
boolean response = score.isThreeStrike();

// then
assertEquals(response, true);

}

@Test
@DisplayName("3스트라이크가 아닌 경우 false 반환")
void isThreeStrikeFalse(){
// given
BaseballScore score = new BaseballScore();
score.addStrike();
score.addStrike();

// when
boolean response = score.isThreeStrike();

// then
assertEquals(response, false);

}

}
Loading