Skip to content

Commit a5ea67c

Browse files
authored
๐Ÿš€ 3๋‹จ๊ณ„ - ๋กœ๋˜(2๋“ฑ) (#4209)
* ๋ถˆ๋ณ€ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋ณ€๊ฒฝ * ๐Ÿš€ 3๋‹จ๊ณ„ - ๋กœ๋˜(2๋“ฑ) : README ์—…๋ฐ์ดํŠธ * 2๋“ฑ, 3๋“ฑ ๊ตฌ๋ถ„ ์œ„ํ•ด ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ํ™•์ธ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ * ์ฝ”๋“œ ์ •๋ฆฌ * 3๋‹จ๊ณ„ ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ * ํ”ผ๋“œ๋ฐฑ : ์ผ๊ธ‰์ฝœ๋ ‰์…˜ ์ ์šฉ, ๊ฐ์ฒด ํ˜‘๋ ฅ
1 parent 5805982 commit a5ea67c

20 files changed

+263
-158
lines changed

โ€Žsrc/main/java/lotto/LottoGameApplication.javaโ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import lotto.domain.game.LottoGame;
44
import lotto.domain.game.LottoMachine;
5-
import lotto.domain.lotto.LottoTicket;
65
import lotto.domain.lotto.Money;
7-
import lotto.domain.result.GameResult;
86
import lotto.domain.lotto.Purchase;
7+
import lotto.domain.lotto.WinningTicket;
8+
import lotto.domain.result.GameResult;
99
import lotto.ui.InputView;
1010
import lotto.ui.ResultView;
1111

@@ -19,11 +19,12 @@ public static void main(String[] args) {
1919
ResultView.printPurchase(purchase);
2020

2121
String winningLottoNumbers = InputView.inputWinningLottoNumbers();
22-
LottoTicket winning = LottoTicket.from(winningLottoNumbers);
22+
int bonusNumber = InputView.inputBonusNumber();
23+
WinningTicket winning = new WinningTicket(winningLottoNumbers, bonusNumber);
2324

2425
LottoGame game = new LottoGame(winning);
2526
GameResult gameResult = game.check(purchase);
2627

27-
ResultView.printResult(gameResult);
28+
ResultView.printResult(gameResult, purchase.getPurchaseAmount());
2829
}
2930
}

โ€Žsrc/main/java/lotto/README.mdโ€Ž

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
55
1. ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ ๊ธฐ๋Šฅ
66
2. N๊ฐœ์˜ ๋กœ๋˜ ๊ตฌ๋งค ๊ธฐ๋Šฅ (์ž๋™)
7-
3. ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ธฐ๋Šฅ
7+
3. ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ธฐ๋Šฅ (๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ + ๋ณด๋„ˆ์Šค๋ณผ 1๊ฐœ)
88
4. ๋‹น์ฒจ ํ†ต๊ณ„ ์กฐํšŒ ๊ธฐ๋Šฅ
99
- ๋ช‡ ๊ฐœ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€, ๋ช‡ ๊ฐœ์ธ์ง€
10+
- ๋ณด๋„ˆ์Šค๋ณผ ์ผ์น˜ ์—ฌ๋ถ€ (5๊ฐœ ์ผ์น˜ ์‹œ)
1011
- ์ˆ˜์ต๋ฅ 
1112

1213
# ์ฃผ์š” ๊ฐ์ฒด
@@ -15,7 +16,8 @@
1516

1617
- ๋ฌด์—‡์ธ๊ฐ€? : ๊ตฌ๋งค๊ธˆ์— ๋”ฐ๋ผ ๋กœ๋˜๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋จธ์‹ 
1718
- ์—ญํ• 
18-
- ๋กœ๋˜ ๋ฐœ๊ธ‰ ๊ฐœ์ˆ˜
19+
- ๋กœ๋˜ ๋ฐœ๊ธ‰ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
20+
- ๋žœ๋ค ๋กœ๋˜ ํ‹ฐ์ผ“ ์ƒ์„ฑ
1921

2022
## ๋กœ๋˜ (LottoTicket)
2123

@@ -31,32 +33,54 @@
3133
- ๋ฒˆํ˜ธ ์œ ํšจ์„ฑ ๋ณด์žฅ (1 ~ 45)
3234
- ์ˆซ์ž ๊ฐ์ฒด ๋น„๊ต
3335

34-
## ๋‹น์ฒจ๋œ ๋กœ๋˜ (WinningLotto)
36+
## ๊ธˆ์•ก (Money)
3537

36-
- ๋ฌด์—‡์ธ๊ฐ€? : ๋‹น์ฒจ ๊ฒฐ๊ณผ ์ •๋ณด (6๊ฐœ์˜ ์ˆซ์ž)
38+
- ๋ฌด์—‡์ธ๊ฐ€? : ๊ธˆ์•ก์„ ํ‘œํ˜„ํ•˜๋Š” ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜
3739
- ์—ญํ• 
38-
- ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋น„๊ต
40+
- ๊ธˆ์•ก ์œ ํšจ์„ฑ ๋ณด์žฅ (0 ์ด์ƒ)
41+
- ๊ธˆ์•ก ์—ฐ์‚ฐ (๋ง์…ˆ, ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ)
42+
- BigDecimal์„ ์‚ฌ์šฉํ•œ ์ •ํ™•ํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐ
3943

40-
## ๋‹น์ฒจ ๋“ฑ์ˆ˜ (Rank)
44+
## ๊ตฌ๋งค (Purchase)
4145

42-
- ๋ฌด์—‡์ธ๊ฐ€? : ๋‹น์ฒจ ๊ฒฐ๊ณผ ์œ ํ˜• (๋ช‡ ๊ฐœ์˜ ๋ฒˆํ˜ธ ์ผ์น˜, ๋‹น์ฒจ๊ธˆ)
46+
- ๋ฌด์—‡์ธ๊ฐ€? : ๋กœ๋˜ ๊ตฌ๋งค ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฐ์ฒด
47+
- ์—ญํ• 
48+
- ๊ตฌ๋งคํ•œ ๋กœ๋˜ ํ‹ฐ์ผ“ ๋ชฉ๋ก ๊ด€๋ฆฌ
49+
- ํ‹ฐ์ผ“ ๊ฐœ์ˆ˜ ์กฐํšŒ
50+
- ์ด ๊ตฌ๋งค ๊ธˆ์•ก ๊ณ„์‚ฐ
51+
52+
## ๋‹น์ฒจ ํ‹ฐ์ผ“ (WinningTicket)
53+
54+
- ๋ฌด์—‡์ธ๊ฐ€? : ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค๋ณผ 1๊ฐœ๋ฅผ ๋‹ด์€ ํ‹ฐ์ผ“
55+
- ์—ญํ• 
56+
- ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ ๋ณด๊ด€
57+
- ๋ณด๋„ˆ์Šค๋ณผ ๋ณด๊ด€
58+
- ๊ตฌ๋งค ํ‹ฐ์ผ“์˜ ๋ณด๋„ˆ์Šค๋ณผ ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ
4359

44-
## ๋กœ๋˜ ๊ฒฐ๊ณผ (LottoResult)
60+
## ๋กœ๋˜ ๊ฒŒ์ž„ (LottoGame)
4561

46-
- ๋ฌด์—‡์ธ๊ฐ€? : ์–ด๋–ค Rank์— ํ•ด๋‹น๋˜๋Š”์ง€ ์ •๋ณด
62+
- ๋ฌด์—‡์ธ๊ฐ€? : ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๊ตฌ๋งคํ•œ ํ‹ฐ์ผ“๋“ค์„ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด
63+
- ์—ญํ• 
64+
- ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋ณด๊ด€
65+
- ๊ตฌ๋งคํ•œ ํ‹ฐ์ผ“๋“ค์˜ ๋‹น์ฒจ ๊ฒฐ๊ณผ ๊ณ„์‚ฐ
66+
- ๊ฒŒ์ž„ ๊ฒฐ๊ณผ ์ƒ์„ฑ
4767

48-
## ๋กœ๋˜ ๊ฒŒ์ž„ ๊ฒฐ๊ณผ (Result)
68+
## ๋‹น์ฒจ ๋“ฑ์ˆ˜ (Rank)
4969

50-
- ๋ฌด์—‡์ธ๊ฐ€? : ์ „์ฒด ๋กœ๋˜ ๊ตฌ๋งค ๊ฒฐ๊ณผ (๋‹น์ฒจ ํ†ต๊ณ„, ์ˆ˜์ต๋ฅ )์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด
70+
- ๋ฌด์—‡์ธ๊ฐ€? : ๋‹น์ฒจ ๊ฒฐ๊ณผ ์œ ํ˜• (๋ช‡ ๊ฐœ์˜ ๋ฒˆํ˜ธ ์ผ์น˜, ๋‹น์ฒจ๊ธˆ)
5171
- ์—ญํ• 
52-
- ๋‹น์ฒจ ํ†ต๊ณ„์™€ ์ˆ˜์ต๋ฅ ์„ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด
72+
- ์ผ์น˜ ๊ฐœ์ˆ˜์™€ ๋ณด๋„ˆ์Šค๋ณผ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ๋“ฑ์ˆ˜ ํŒ์ •
73+
- ๋“ฑ์ˆ˜๋ณ„ ๋‹น์ฒจ๊ธˆ ๊ด€๋ฆฌ
74+
- 5๊ฐœ ์ผ์น˜ ์‹œ ๋ณด๋„ˆ์Šค๋ณผ๋กœ 2๋“ฑ/3๋“ฑ ๊ตฌ๋ถ„
5375

54-
### ๋‹น์ฒจ ํ†ต๊ณ„ Statistics
76+
## ๋กœ๋˜ ๊ฒŒ์ž„ ๊ฒฐ๊ณผ (GameResult)
5577

56-
- ์ผ์น˜ํ•˜๋Š” ๋ฒˆํ˜ธ ๊ฐฏ์ˆ˜, ๋ช‡ ๊ฐœ
78+
- ๋ฌด์—‡์ธ๊ฐ€? : ์ „์ฒด ๋กœ๋˜ ๊ตฌ๋งค ๊ฒฐ๊ณผ (๋‹น์ฒจ ํ†ต๊ณ„, ์ˆ˜์ต๋ฅ )๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด
79+
- ์—ญํ• 
80+
- ๋“ฑ์ˆ˜๋ณ„ ๋‹น์ฒจ ํ†ต๊ณ„ ๊ด€๋ฆฌ
81+
- ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ (๋‹น์ฒจ ๊ธˆ์•ก / ๊ตฌ์ž… ๊ธˆ์•ก)
82+
- ์†์ต ํŒ์ • (๊ธฐ์ค€: 1.0)
5783

58-
### ์ˆ˜์ต๋ฅ  ProfitRate
5984

60-
- ์ˆ˜์ต๋ฅ  = ๋‹น์ฒจ ๊ธˆ์•ก / ๊ตฌ์ž… ๊ธˆ์•ก * 100
6185

6286

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
package lotto.domain.game;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import java.util.List;
54
import lotto.domain.lotto.LottoTicket;
6-
import lotto.domain.lotto.Money;
7-
import lotto.domain.result.GameResult;
5+
import lotto.domain.lotto.LottoTickets;
86
import lotto.domain.lotto.Purchase;
7+
import lotto.domain.lotto.WinningTicket;
8+
import lotto.domain.result.GameResult;
99

1010
public class LottoGame {
1111

12-
private final LottoTicket winningTicket;
12+
private final WinningTicket winningTicket;
1313

14-
public LottoGame(LottoTicket winningTicket) {
14+
public LottoGame(WinningTicket winningTicket) {
1515
this.winningTicket = winningTicket;
1616
}
1717

1818
public GameResult check(Purchase purchase) {
19-
Money winningPrize = new Money(0);
20-
Map<Rank, Integer> ranks = new HashMap<>();
21-
for (LottoTicket ticket : purchase.getTickets()) {
22-
Rank rank = Rank.valueOf(winningTicket.countMatchingNumbers(ticket));
23-
ranks.compute(rank, (k, v) -> v == null ? 1 : v + 1);
24-
winningPrize = winningPrize.add(rank.getPrize());
25-
}
26-
27-
return new GameResult(ranks, winningPrize, purchase.getPurchaseAmount());
19+
LottoTickets tickets = purchase.getTickets();
20+
return tickets.updateRank(winningTicket);
2821
}
22+
2923
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package lotto.domain.game;
22

3-
import java.math.BigDecimal;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
import lotto.domain.lotto.LottoTicket;
3+
import lotto.domain.lotto.LottoTickets;
74
import lotto.domain.lotto.Money;
85
import lotto.domain.lotto.Purchase;
96

@@ -22,19 +19,15 @@ public LottoMachine(Money lottoTicketPrice) {
2219
}
2320

2421
public Purchase purchase(Money money) {
25-
return new Purchase(money, generateTickets(calculate(money)));
22+
return new Purchase(lottoTicketPrice, generateTickets(calculate(money)));
2623
}
2724

2825
private int calculate(Money money) {
2926
return money.divideForCount(lottoTicketPrice);
3027
}
3128

32-
List<LottoTicket> generateTickets(int cnt) {
33-
List<LottoTicket> tickets = new ArrayList<>();
34-
for (int i = 0; i < cnt; i++) {
35-
tickets.add(LottoTicket.of(Random.generate()));
36-
}
37-
return tickets;
29+
LottoTickets generateTickets(int cnt) {
30+
return new LottoTickets(cnt);
3831
}
3932

4033
}

โ€Žsrc/main/java/lotto/domain/game/Random.javaโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Random {
1313
private static List<Integer> numbers = IntStream.rangeClosed(MIN, MAX)
1414
.boxed()
1515
.collect(Collectors.toList());
16+
1617
public static List<LottoNumber> generate() {
1718
Collections.shuffle(numbers);
1819
return numbers.subList(0, LOTTO_NUMBER_COUNT).stream()
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
package lotto.domain.game;
22

3-
import lotto.domain.lotto.Money;
3+
import java.util.Arrays;
44

55
public enum Rank {
66

77
FIRST(2_000_000_000, 6),
8-
SECOND(1_500_000, 5),
9-
THIRD(50_000, 4),
10-
FOURTH(5000, 3),
8+
SECOND(30_000_000, 5, true),
9+
THIRD(1_500_000, 5),
10+
FOURTH(50_000, 4),
11+
FIFTH(5000, 3),
1112
NONE(0, -1);
1213

1314
private final int prize;
1415
private final int matched;
16+
private final boolean bonusMatched;
1517

1618
Rank(int prize, int matched) {
19+
this(prize, matched, false);
20+
}
21+
22+
Rank(int prize, int matched, boolean bonusMatched) {
1723
this.prize = prize;
1824
this.matched = matched;
25+
this.bonusMatched = bonusMatched;
1926
}
2027

21-
public static Rank valueOf(int matched) {
22-
for (Rank rank : values()) {
23-
if (rank.matched == matched) {
24-
return rank;
25-
}
26-
}
27-
return NONE;
28+
public static Rank valueOf(int matched, boolean bonusMatched) {
29+
return Arrays.stream(values())
30+
.filter(x -> isMatch(x, matched, bonusMatched))
31+
.findFirst()
32+
.orElse(NONE);
33+
}
34+
35+
public int getPrize() {
36+
return prize;
2837
}
2938

30-
public Money getPrize() {
31-
return new Money(this.prize);
39+
private static boolean isMatch(Rank rank, int matched,
40+
boolean bonusMatched) {
41+
if (rank.matched != matched) {
42+
return false;
43+
}
44+
if (matched != 5) {
45+
return true;
46+
}
47+
return rank.bonusMatched == bonusMatched;
3248
}
3349

3450
@Override
3551
public String toString() {
52+
if (this == SECOND) {
53+
return this.matched + "๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (" + this.prize + "์›)";
54+
}
3655
return this.matched + "๊ฐœ ์ผ์น˜ (" + this.prize + "์›)";
3756
}
3857
}

โ€Žsrc/main/java/lotto/domain/lotto/LottoNumber.javaโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Objects;
66

77
public class LottoNumber {
8+
89
private static final Map<Integer, LottoNumber> CACHE = new HashMap<>();
910

1011
static {
@@ -19,7 +20,7 @@ private LottoNumber(int value) {
1920
this.value = value;
2021
}
2122

22-
public static LottoNumber of(int value){
23+
public static LottoNumber of(int value) {
2324
validate(value);
2425
return CACHE.get(value);
2526
}

โ€Žsrc/main/java/lotto/domain/lotto/LottoTicket.javaโ€Ž

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,32 @@
33
import java.util.Arrays;
44
import java.util.HashSet;
55
import java.util.List;
6-
import java.util.Set;
76
import java.util.stream.Collectors;
8-
import lotto.domain.game.LottoMachine;
9-
import lotto.domain.game.Rank;
107

118
public class LottoTicket {
9+
1210
private static final int LOTTO_CNT = 6;
1311

1412
private final List<LottoNumber> numbers;
1513

16-
private LottoTicket(List<LottoNumber> numbers) {
17-
validate(numbers);
18-
this.numbers = List.copyOf(numbers);
14+
public LottoTicket(String numbers) {
15+
this(convert(parse(numbers)));
1916
}
2017

21-
public static LottoTicket of(List<LottoNumber> numbers) {
22-
return new LottoTicket(numbers);
18+
public LottoTicket (Integer... numbers) {
19+
this(convert(List.of(numbers)));
2320
}
2421

25-
public static LottoTicket from(String numbers) {
26-
return new LottoTicket(convert(parse(numbers)));
27-
}
28-
29-
public static LottoTicket of(Integer... numbers) {
30-
return new LottoTicket(convert(List.of(numbers)));
22+
public LottoTicket(List<LottoNumber> numbers) {
23+
validate(numbers);
24+
this.numbers = List.copyOf(numbers);
3125
}
3226

3327
private static List<Integer> parse(String numbers) {
3428
return Arrays.stream(numbers.split(", "))
3529
.map(Integer::parseInt).toList();
3630
}
3731

38-
private static List<LottoNumber> convert(Integer... numbers) {
39-
return Arrays.stream(numbers)
40-
.map(LottoNumber::of)
41-
.collect(Collectors.toList());
42-
}
4332

4433
private static List<LottoNumber> convert(List<Integer> numbers) {
4534
return numbers.stream()
@@ -53,15 +42,19 @@ private void validate(List<LottoNumber> numbers) {
5342
}
5443
}
5544

56-
public int countMatchingNumbers(LottoTicket winningNumbers) {
57-
Set<LottoNumber> winnings = new HashSet<>(winningNumbers.numbers);
58-
return (int) numbers.stream()
59-
.filter(winnings::contains)
45+
public int matchCount(LottoTicket ticket) {
46+
return (int) this.numbers.stream()
47+
.filter(ticket::contains)
6048
.count();
6149
}
6250

51+
public boolean contains(LottoNumber number) {
52+
return this.numbers.contains(number);
53+
}
54+
6355
@Override
6456
public String toString() {
6557
return numbers.toString();
6658
}
59+
6760
}

0 commit comments

Comments
ย (0)