Skip to content

Commit 06308c1

Browse files
gisungParkHAE\1161893
andauthored
Step3 - 로또(2등) (#3199)
* docs: <3단계> 기능 요구 사항 정리 * feat: <3단계> 로또 2등 당첨 확인 - 보너스 번호 추가 - 로또 2등 당첨 여부 확인 * feat: <3단계> 피드백 반영 - 상수 제거 - HashMap 메서드 활용 * refactor: <3단계> lottoTicket 검증 주체 변경 - 클라이언트 로또 티켓이 당첨 번호와 보너스 번호로 비교 검증 진행 --------- Co-authored-by: HAE\1161893 <[email protected]>
1 parent bfa706d commit 06308c1

File tree

11 files changed

+109
-75
lines changed

11 files changed

+109
-75
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@
2323
- [x] 1 ~ 45 범위의 랜덤한 숫자 6개를 뽑는다.
2424
- [x] 위 숫자 리스트 생산 비용을 1000으로 책정하여, 전달받은 금액만큼 생성한다.
2525
- [x] 당첨 번호와 숫자 일치 개수를 확인하여 당첨 통계를 낸다.
26-
- [ ] 수익률을 계산한다.
26+
- [x] 수익률을 계산한다.
2727
- [x] 해당 결과를 콘솔에 출력한다.
28+
29+
### Step 3
30+
- [x] 보너스 번호를 입력받는다.
31+
- [x] 보너스 번호로 2등 여부를 확인한다.

src/main/java/step2/controller/LottoGameController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ public void playLottoGame() {
2323

2424
List<LottoTicket> lottoTickets = lottoGames.buyLottoGame(gameCount);
2525
ResultView.printLottoTicket(lottoTickets);
26-
LottoTicket winningNumber = lottoGames.readWinningNumber(InputView.readWinningNumbers());
26+
LottoTicket winningTicket = lottoGames.readWinningNumber(InputView.readWinningNumbers());
27+
int bonusNumber = InputView.readBonusNumber();
2728

2829
ResultView.printBlankLine();
2930
ResultView.printMessage("당첨 통계");
3031

3132
LottoResultReport lottoResultReport = new LottoResultReport();
3233
for (LottoTicket lottoTicket : lottoTickets) {
33-
lottoResultReport.recordRank(winningNumber.countMatchingNumbers(lottoTicket));
34+
lottoResultReport.recordRank(lottoTicket.checkLottoTicket(winningTicket, bonusNumber));
3435
}
3536

3637
ResultView.printResultReport(lottoResultReport);
3738
double profit = lottoResultReport.calculateProfit(gameCount);
3839
ResultView.printMessage("총 수익률은 " + profit + "입니다.");
39-
4040
}
41-
4241
}

src/main/java/step2/domain/LottoResultReport.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@
66

77
public class LottoResultReport {
88

9-
static final int ZERO = 0;
10-
11-
private Map<PrizeMoney, Integer> lottoResultReport;
9+
private Map<Rank, Integer> lottoResultReport;
1210

1311
public LottoResultReport() {
1412
lottoResultReport = new HashMap<>();
1513
}
1614

17-
public int recordRank(PrizeMoney prizeMoney) {
18-
if (lottoResultReport.containsKey(prizeMoney)) {
19-
Integer cnt = lottoResultReport.get(prizeMoney);
20-
lottoResultReport.put(prizeMoney, cnt + 1);
21-
return cnt;
22-
}
23-
lottoResultReport.put(prizeMoney, 1);
24-
return 1;
15+
public int recordRank(Rank rank) {
16+
Integer value = lottoResultReport.getOrDefault(rank, 0);
17+
lottoResultReport.put(rank, value + 1);
18+
return lottoResultReport.get(rank);
2519
}
2620

27-
public int findReportByMatchCount(PrizeMoney prizeMoney) {
28-
if (lottoResultReport.containsKey(prizeMoney)) {
29-
return lottoResultReport.get(prizeMoney);
30-
}
31-
return ZERO;
21+
public int findReportByMatchCount(Rank rank) {
22+
return lottoResultReport.getOrDefault(rank, 0);
3223
}
3324

3425
public double calculateProfit(int gameCount) {
@@ -38,7 +29,7 @@ public double calculateProfit(int gameCount) {
3829
}
3930

4031
long sum() {
41-
Set<Map.Entry<PrizeMoney, Integer>> entries = lottoResultReport.entrySet();
32+
Set<Map.Entry<Rank, Integer>> entries = lottoResultReport.entrySet();
4233
return entries.stream().mapToLong(e -> e.getKey().prizeMoney() * e.getValue()).sum();
4334
}
4435

src/main/java/step2/domain/LottoTicket.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ public boolean isContain(Integer number) {
2929
return this.lottoTicket.contains(number);
3030
}
3131

32-
public PrizeMoney countMatchingNumbers(LottoTicket compareTarget) {
32+
public Rank checkLottoTicket(LottoTicket winningTicket, int bonusNumber) {
3333
int count = (int) lottoTicket.stream()
34-
.filter(i -> compareTarget.isContain(i))
34+
.filter(i -> winningTicket.isContain(i))
3535
.count();
36-
return PrizeMoney.toPrizeMoney(count);
36+
37+
if(count == 5 && isContain(bonusNumber)) {
38+
return Rank.SECOND;
39+
}
40+
41+
return Rank.toPrizeMoney(count);
3742
}
3843

3944
public String printTicket() {

src/main/java/step2/domain/PrizeMoney.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package step2.domain;
2+
3+
import java.security.InvalidParameterException;
4+
import java.util.Arrays;
5+
6+
public enum Rank {
7+
MISS(0, 0, "0개 일치 (0)"),
8+
FIFTH(3, 5_000, "3개 일치 (5000)"),
9+
FOURTH(4, 50_000, "4개 일치 (50000)"),
10+
THIRD(5, 1_500_000, "5개 일치 (1500000)"),
11+
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치(30000000원)"),
12+
FIRST(6, 2_000_000_000, "6개 일치 (2000000000)");
13+
14+
private static final int THIRD_COUNT = 5;
15+
16+
private int matchCount;
17+
private long prizeMoney;
18+
19+
private String message;
20+
21+
Rank(int matchCount, long rank, String message) {
22+
this.matchCount = matchCount;
23+
this.prizeMoney = rank;
24+
this.message = message;
25+
}
26+
27+
public static Rank toPrizeMoney(int matchCount) {
28+
if (matchCount == THIRD_COUNT) {
29+
return Rank.THIRD;
30+
}
31+
32+
return Arrays.stream(values())
33+
.filter(prizeMoney -> prizeMoney.matchCount == matchCount)
34+
.findAny()
35+
.orElse(Rank.MISS);
36+
}
37+
38+
public int matchCount() {
39+
return this.matchCount;
40+
}
41+
42+
public long prizeMoney() {
43+
return this.prizeMoney;
44+
}
45+
46+
public String message() {
47+
return this.message;
48+
}
49+
}

src/main/java/step2/view/InputView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public static int readAmountOfPurchase() {
1010
return readInt("구매 금액을 입력해 주세요");
1111
}
1212

13+
public static int readBonusNumber() {
14+
return readInt("보너스 볼을 입력해 주세요.");
15+
}
16+
1317
private static int readInt(String message) {
1418
System.out.println(message);
1519
return toInt(scanner.nextLine());

src/main/java/step2/view/ResultView.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
import step2.domain.LottoResultReport;
44
import step2.domain.LottoTicket;
5-
import step2.domain.PrizeMoney;
5+
import step2.domain.Rank;
66

77
import java.util.List;
88

99
public class ResultView {
1010

11-
private static final int MINIMUM_MATH_COUNT = 3;
12-
private static final int MAXIMUM_MATH_COUNT = 6;
13-
1411
public static void printMessage(String message) {
1512
System.out.println(message);
1613
}
@@ -27,9 +24,16 @@ public static void printLottoTicket(List<LottoTicket> lottoTickets) {
2724
}
2825

2926
public static void printResultReport(LottoResultReport lottoResultReport) {
30-
for (int i = MINIMUM_MATH_COUNT; i < MAXIMUM_MATH_COUNT + 1; i++) {
31-
PrizeMoney prizeMoney = PrizeMoney.toPrizeMoney(i);
32-
printMessage(i + "개 일치 (" + PrizeMoney.toPrizeMoney(i).prizeMoney() + ") - " + lottoResultReport.findReportByMatchCount(prizeMoney) + "개");
27+
Rank[] values = Rank.values();
28+
for (Rank rank : values) {
29+
printEachLottoRank(lottoResultReport, rank);
30+
}
31+
}
32+
33+
private static void printEachLottoRank(LottoResultReport lottoResultReport, Rank rank) {
34+
if (rank == Rank.MISS) {
35+
return;
3336
}
37+
printMessage(rank.message() + " - " + lottoResultReport.findReportByMatchCount(rank) + "개");
3438
}
3539
}

src/test/java/step2/domain/LottoResultReportTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class LottoResultReportTest {
2121

2222
List<Integer> matchCountList = Arrays.asList(3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6);
2323
for (Integer count : matchCountList) {
24-
lottoResultReport.recordRank(PrizeMoney.toPrizeMoney(count));
24+
lottoResultReport.recordRank(Rank.toPrizeMoney(count));
2525
}
2626

2727
for (int i = 3; i < 7; i++) {
28-
assertThat(lottoResultReport.findReportByMatchCount(PrizeMoney.toPrizeMoney(i))).isEqualTo(i);
28+
assertThat(lottoResultReport.findReportByMatchCount(Rank.toPrizeMoney(i))).isEqualTo(i);
2929
}
3030
}
3131

@@ -36,7 +36,7 @@ class LottoResultReportTest {
3636
LottoResultReport lottoResultReport = new LottoResultReport();
3737
for (int i = 1; i <= 6; i++) {
3838
if (matchCounts[i] == 0) continue;
39-
lottoResultReport.recordRank(PrizeMoney.toPrizeMoney(i));
39+
lottoResultReport.recordRank(Rank.toPrizeMoney(i));
4040
}
4141
assertThat(lottoResultReport.sum()).isEqualTo(expected);
4242
}

src/test/java/step2/domain/LottoTicketTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LottoTicketTest {
1818
@MethodSource("winningNumbersSample")
1919
public void 로또_숫자_일치_개수_비교(List<Integer> winningNumbers, int matchCount) throws Exception {
2020
LottoTicket lottoTicket = new LottoTicket(Arrays.asList(1, 12, 22, 23, 34, 44));
21-
assertThat(lottoTicket.countMatchingNumbers(new LottoTicket(winningNumbers))).isEqualTo(PrizeMoney.toPrizeMoney(matchCount));
21+
assertThat(lottoTicket.checkLottoTicket(new LottoTicket(winningNumbers), 0)).isEqualTo(Rank.toPrizeMoney(matchCount));
2222
}
2323

2424
static Stream<Arguments> winningNumbersSample() throws Throwable {
@@ -32,4 +32,19 @@ static Stream<Arguments> winningNumbersSample() throws Throwable {
3232
);
3333
}
3434

35+
@DisplayName("2등 당첨 케이스를 테스트한다.")
36+
@ParameterizedTest
37+
@MethodSource("secondRankSample")
38+
public void 로또_2등_당첨(List<Integer> numbers, List<Integer> winningNumbers, int bonusNumber) throws Exception {
39+
LottoTicket winningTicket = new LottoTicket(winningNumbers);
40+
assertThat(new LottoTicket(numbers).checkLottoTicket(winningTicket, bonusNumber)).isEqualTo(Rank.SECOND);
41+
}
42+
43+
static Stream<Arguments> secondRankSample() throws Throwable {
44+
return Stream.of(
45+
Arguments.of(Arrays.asList(1, 12, 13, 14, 27, 45), Arrays.asList(10, 12, 13, 14, 27, 45), 1),
46+
Arguments.of(Arrays.asList(7, 8, 17, 18, 32, 33), Arrays.asList(7, 8, 17, 29, 32, 33), 18),
47+
Arguments.of(Arrays.asList(10, 16, 21, 24, 39, 40), Arrays.asList(10, 16, 21, 24, 39, 41), 40)
48+
);
49+
}
3550
}

0 commit comments

Comments
 (0)