forked from knh4437/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebb73f7
commit f18032d
Showing
5 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lotto.domain; | ||
|
||
import java.util.Arrays; | ||
import lotto.domain.rankvalidator.RankValidator; | ||
|
||
public enum LottoRank { | ||
FIRST(2_000_000_000, WinNumbers::isFirstRank), | ||
SECOND(30_000_000, WinNumbers::isSecondRank), | ||
THIRD(1_500_000, WinNumbers::isThirdRank), | ||
FOURTH(50_000, WinNumbers::isFourthRank), | ||
FIFTH(5_000, WinNumbers::isFifthRank), | ||
OTHER(0, WinNumbers::isOutOfRank); | ||
|
||
|
||
private final int prize; | ||
private final RankValidator rankValidator; | ||
|
||
LottoRank(int prize, RankValidator rankValidator) { | ||
this.prize = prize; | ||
this.rankValidator = rankValidator; | ||
} | ||
|
||
public static LottoRank calculateRank(WinNumbers winNumbers, Lotto lotto) { | ||
return Arrays.stream(LottoRank.values()) | ||
.filter(rank -> rank.rankValidator.isRanked(winNumbers, lotto)) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 일치하는 등수가 존재하지 않습니다")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lotto.domain.rankvalidator; | ||
|
||
import lotto.domain.Lotto; | ||
import lotto.domain.WinNumbers; | ||
|
||
public interface RankValidator { | ||
|
||
boolean isRanked(WinNumbers winNumbers, Lotto lotto); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class LottoRankTest { | ||
|
||
public static Stream<Arguments> findRank() { | ||
return Stream.of( | ||
Arguments.of(List.of(1, 2, 3, 4, 5, 6), LottoRank.FIRST), | ||
Arguments.of(List.of(1, 2, 3, 4, 5, 7), LottoRank.SECOND), | ||
Arguments.of(List.of(1, 2, 3, 4, 5, 8), LottoRank.THIRD), | ||
Arguments.of(List.of(1, 2, 3, 4, 7, 8), LottoRank.FOURTH), | ||
Arguments.of(List.of(1, 2, 3, 7, 8, 9), LottoRank.FIFTH), | ||
Arguments.of(List.of(1, 2, 7, 8, 9, 10), LottoRank.OTHER) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("findRank") | ||
void 로또_당첨_등수를_확인한다(List<Integer> numbers, LottoRank expect) { | ||
// given | ||
final Lotto winLotto = new Lotto(List.of(1, 2, 3, 4, 5, 6)); | ||
final int bonusBall = 7; | ||
final WinNumbers winNumbers = new WinNumbers(winLotto, bonusBall); | ||
|
||
final Lotto lotto = new Lotto(numbers); | ||
|
||
// when | ||
final LottoRank actual = LottoRank.calculateRank(winNumbers, lotto); | ||
|
||
// then | ||
Assertions.assertThat(actual).isEqualTo(expect); | ||
} | ||
} |