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
31 changes: 31 additions & 0 deletions src/main/java/baseball/domain/Judgement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package baseball.domain;


import baseball.domain.ball.Ball;
import baseball.domain.ball.BallList;
import baseball.domain.count.Count;
import java.util.List;
import java.util.stream.Collectors;

public class Judgement {

public List<Count> judgeAllBalls(BallList userBalls, BallList comBalls) {
return userBalls.getBalls().stream()
.map(userBall -> judgeOneBall(userBall, comBalls))
.collect(Collectors.toList());
}

public Count judgeOneBall(Ball userBall, BallList comBalls) {
Ball comBall = comBalls.getBalls().get(userBall.getPosition());

if (comBall.equals(userBall)) {
return Count.STRIKE;
}

if (comBalls.containsBall(userBall)) {
return Count.BALL;
}

return Count.NOTHING;
}
}
42 changes: 42 additions & 0 deletions src/main/java/baseball/domain/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package baseball.domain;

import baseball.domain.count.Count;
import baseball.domain.count.Counts;

public class Score {

private final Counts counts;

private Score(Counts counts) {
this.counts = counts;
}

public static Score from(Counts counts) {
return new Score(counts);
}

public String getScreenMessage() {
if (isStrikeCountGreaterThanZero() && isBallCountGreaterThanZero()) {
return counts.getStrikeCount() + Count.STRIKE.description + " " + counts.getBallCount() + Count.BALL.description;
}

if (isStrikeCountGreaterThanZero()) {
return counts.getStrikeCount() + Count.STRIKE.description;
}

if (isBallCountGreaterThanZero()) {
return counts.getBallCount() + Count.BALL.description;
}

return Count.NOTHING.description;
}

private boolean isBallCountGreaterThanZero() {
return counts.getBallCount() > 0;
}

private boolean isStrikeCountGreaterThanZero() {
return counts.getStrikeCount() > 0;
}

}
41 changes: 41 additions & 0 deletions src/main/java/baseball/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package baseball.domain;

import baseball.domain.ball.Ball;
import baseball.domain.ball.BallList;
import baseball.shared.MESSAGES;
import baseball.util.BaseballUtils;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class User {

private final BallList balls;

private User(List<Integer> balls) {
this.balls = convertStringsToBallList(balls);
}

public static User of(String ballNumbers) {
if (ballNumbers.length() != 3) {
throw new IllegalArgumentException(MESSAGES.USER_INSTANCE_ERROR_MESSAGE.getMessage());
}

List<String> ballStringNumbers = BaseballUtils.splitByBlank(ballNumbers);
List<Integer> ballIntegerNumbers = BaseballUtils.convertDataTypesIntoListData(ballStringNumbers);

return new User(ballIntegerNumbers);
}

public BallList toBallList() {
return balls;
}

private BallList convertStringsToBallList(List<Integer> ballNumbers) {
List<Ball> ballList = IntStream.range(0, ballNumbers.size())
.mapToObj(position -> Ball.of(position, ballNumbers.get(position)))
.collect(Collectors.toList());
return BallList.of(ballList);

}
}
62 changes: 62 additions & 0 deletions src/main/java/baseball/domain/ball/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package baseball.domain.ball;

import baseball.shared.MESSAGES;
import java.util.Objects;

public class Ball {

private static final int MIN = 1;
private static final int MAX = 9;
private final int number;
private final int position;

private Ball(int position, int number) {
if (ballPositionValidation(position) || ballNumberValidation(number)) {
throw new IllegalArgumentException(MESSAGES.BALL_INSTANCE_ERROR.getMessage());
}

this.position = position;
this.number = number;
}

public static Ball of(int position, int number) {
return new Ball(position, number);
}

public boolean isAnotherPositionSameNumber(Ball targetBall) {
return (this.position != targetBall.getPosition()) && (this.number == targetBall.getNumber());
}

public int getNumber() {
return number;
}

public int getPosition() {
return position;
}

private boolean ballNumberValidation(int inputNumber) {
return inputNumber < MIN || inputNumber > MAX;
}

private boolean ballPositionValidation(int inputPosition) {
return inputPosition < 0;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Ball ball = (Ball) o;
return number == ball.number && position == ball.position;
}

@Override
public int hashCode() {
return Objects.hash(number, position);
}
}
52 changes: 52 additions & 0 deletions src/main/java/baseball/domain/ball/BallList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package baseball.domain.ball;

import baseball.shared.MESSAGES;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BallList {

public static final int MAX_SIZE = 3;

private final List<Ball> balls;

public List<Ball> getBalls() {
return new ArrayList<>(balls);
}

private BallList(List<Ball> balls) {
if (balls.size() != MAX_SIZE) {
throw new IllegalArgumentException(MESSAGES.BALL_LIST_INSTANCE_ERROR.getMessage());
}

this.balls = balls;
}

public static BallList of(List<Ball> balls) {
return new BallList(balls);
}

public boolean containsBall(Ball ball) {
return balls.stream()
.anyMatch(b -> b.isAnotherPositionSameNumber(ball));

}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BallList ballList = (BallList) o;
return Objects.equals(balls, ballList.balls);
}

@Override
public int hashCode() {
return Objects.hashCode(balls);
}
}
14 changes: 14 additions & 0 deletions src/main/java/baseball/domain/count/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package baseball.domain.count;

public enum Count {

BALL("볼"),
STRIKE("스트라이크"),
NOTHING("낫띵");

public String description;

Count(String description) {
this.description = description;
}
}
44 changes: 44 additions & 0 deletions src/main/java/baseball/domain/count/Counts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package baseball.domain.count;

import java.util.List;
import java.util.Objects;

public class Counts {

private final long strikeCount;
private final long ballCount;

private Counts(List<Count> counts) {
this.strikeCount = counts.stream().filter(count -> count == Count.STRIKE).count();
this.ballCount = counts.stream().filter(count -> count == Count.BALL).count();
}

public static Counts of(Count first, Count second, Count third) {
return new Counts(List.of(first, second, third));
}

public long getStrikeCount() {
return strikeCount;
}

public long getBallCount() {
return ballCount;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Counts counts = (Counts) o;
return strikeCount == counts.strikeCount && ballCount == counts.ballCount;
}

@Override
public int hashCode() {
return Objects.hash(strikeCount, ballCount);
}
}
17 changes: 17 additions & 0 deletions src/main/java/baseball/shared/MESSAGES.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package baseball.shared;

public enum MESSAGES {
USER_INSTANCE_ERROR_MESSAGE("3개의 숫자를 입력하세요"),
BALL_INSTANCE_ERROR("올바르지 않은 입력입니다."),
BALL_LIST_INSTANCE_ERROR("BallNumber는 3개가 최대 입니다.");

private String message;

MESSAGES(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
19 changes: 19 additions & 0 deletions src/main/java/baseball/util/BaseballUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package baseball.util;

import java.util.List;
import java.util.stream.Collectors;

public class BaseballUtils {

public static List<String> splitByBlank(String number) {

return List.of(number.split(""));
}


public static List<Integer> convertDataTypesIntoListData(List<String> strings) {
return strings.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
}
}
40 changes: 40 additions & 0 deletions src/test/java/baseball/domain/BallListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package baseball.domain;

import static org.assertj.core.api.Assertions.*;

import baseball.domain.ball.Ball;
import baseball.domain.ball.BallList;
import baseball.shared.MESSAGES;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;


public class BallListTest {

@Test
@DisplayName("BallList 컬렉션의 요소는 3개를 넘을 수 없다")
void BallList_요소_갯수_초과() {
assertThatThrownBy(() -> BallList.of(List.of(
Ball.of(0, 1),
Ball.of(1, 2),
Ball.of(2, 3),
Ball.of(3, 4)
)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(MESSAGES.BALL_LIST_INSTANCE_ERROR.getMessage());

}

@Test
@DisplayName("BallList 컬렉션의 요소는 3개 보다 적을 수 없다")
void BallList_요소_갯수_미만() {
assertThatThrownBy(() -> BallList.of(List.of(
Ball.of(0, 1),
Ball.of(1, 2)
)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(MESSAGES.BALL_LIST_INSTANCE_ERROR.getMessage());

}
}
Loading