-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step3 - 로또(2등) #4240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Step3 - 로또(2등) #4240
Changes from all commits
95d4388
8f3eb6f
5b24f98
2d03c79
015df32
c5f098f
2f009a1
ca58738
4562f03
13f270f
8adf298
da9e76a
6372c85
7176377
eb64ba1
2fcceec
9ca7513
e397223
b2a6e82
57d3d57
9ceb94b
0b17acb
aef4391
b1aeb06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,30 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.controller.LottoMachine; | ||
| import lotto.controller.WinningLotto; | ||
| import lotto.domain.WinningLotto; | ||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoNumber; | ||
| import lotto.domain.LottoResult; | ||
| import lotto.ui.InputView; | ||
| import lotto.ui.ResultView; | ||
| import lotto.util.LottoNumberParser; | ||
|
|
||
| public class LottoApplication { | ||
| public static void main(String[] args) { | ||
| String purchaseAmount = InputView.getPurchaseAmount(); | ||
|
|
||
| LottoMachine lottoMachine = new LottoMachine(purchaseAmount); | ||
| Lotto lotto = lottoMachine.generate(); | ||
| ResultView.printLottoCount(lotto.count()); | ||
| ResultView.printLottoNumbersList(lotto.toString()); | ||
|
|
||
| ResultView.printLotto(lotto); | ||
|
|
||
| String winningNumbers = InputView.getWinningNumber(); | ||
| WinningLotto winningLotto = new WinningLotto(LottoNumberParser.parse(winningNumbers)); | ||
| String bonusNumber = InputView.getBonusNumber(); | ||
|
|
||
| WinningLotto winningLotto = new WinningLotto(winningNumbers, bonusNumber); | ||
| LottoResult result = lotto.getMatchResult(winningLotto); | ||
|
|
||
| ResultView.printLottoResult(winningLotto.getResult(lotto)); | ||
| ResultView.printProfit(winningLotto.getProfit(lotto)); | ||
| ResultView.printLottoResult(result.toString()); | ||
| ResultView.printProfit(result.getProfit()); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,10 @@ | |
| public class LottoNumber { | ||
| private final int number; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상황
위 요구사항을 서버에서 생성되는 LottoNumber의 인스턴스의 갯수는? 동시에 생성되는 인스턴스 갯수가 너무 많다. 힌트 : Map과 같은 곳에 인스턴스를 생성한 후 재사용하는 방법을 찾아본다.****
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지난 번에 피드백 남긴 이 부분 도전해 보면 어떨까? |
||
|
|
||
| public LottoNumber(String number) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 캐싱을 담당하는 기능을 LottoNumber 외부에서 구현하고 이와 같이 생성자를 public으로 구현하면 LottoNumberCache 존재를 모르는 개발자는 이 생성자를 통해 LottoNumber를 직접 생성할 수 있다. 힌트: LottoNumber에 정적 팩터리 메서드 추가해 해결 |
||
| this(Integer.parseInt(number)); | ||
| } | ||
|
|
||
| public LottoNumber(int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,43 @@ | ||
| package lotto.domain; | ||
|
|
||
| import lotto.util.LottoNumberParser; | ||
|
|
||
| import java.sql.Array; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoNumbers { | ||
| private final List<LottoNumber> numbers; | ||
|
|
||
| public LottoNumbers(List<Integer> numbers) { | ||
| this.numbers = toLottoNumber(numbers); | ||
| public LottoNumbers(int... numbers) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| this(LottoNumberParser.parse(numbers)); | ||
| } | ||
|
|
||
| private static List<LottoNumber> toLottoNumber(List<Integer> numbers) { | ||
| List<LottoNumber> lottoNumbers = new ArrayList<>(); | ||
|
|
||
| for (int number : numbers) { | ||
| lottoNumbers.add(new LottoNumber(number)); | ||
| } | ||
| public LottoNumbers(String numbers) { | ||
| this(LottoNumberParser.parse(numbers)); | ||
| } | ||
|
|
||
| return lottoNumbers; | ||
| public LottoNumbers(List<LottoNumber> numbers) { | ||
| this.numbers = numbers; | ||
| } | ||
|
|
||
| public LottoRank getMatchedRank(LottoNumbers other) { | ||
| public int getMatchCount(LottoNumbers other) { | ||
| int matchCount = 0; | ||
|
|
||
| for (int i = 0; i < numbers.size(); i++) { | ||
| if (matchesAtIndex(i, other)) { | ||
| for (LottoNumber number : numbers) { | ||
| if (other.contains(number)) { | ||
| matchCount++; | ||
| } | ||
| } | ||
|
|
||
| return LottoRank.of(matchCount); | ||
| return matchCount; | ||
| } | ||
|
|
||
| private boolean matchesAtIndex(int index, LottoNumbers other) { | ||
| return numbers.get(index).equals(other.numbers.get(index)); | ||
| public boolean contains(LottoNumber number) { | ||
| return numbers.contains(number); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,56 @@ | ||
| package lotto.domain; | ||
|
|
||
| public enum LottoRank { | ||
| FIRST(6, 2_000_000_000), | ||
| SECOND(5, 30_000_000), | ||
| THIRD(4, 1_500_000), | ||
| FOURTH(3, 50_000), | ||
| NONE(0, 0); | ||
| FIRST(6, 2_000_000_000, false), | ||
| SECOND_BONUS(5, 30_000_000, true), | ||
| SECOND(5, 1_500_000, false), | ||
| THIRD(4, 50_000, false), | ||
| FOURTH(3, 5_000, false), | ||
| NONE(0, 0, false); | ||
|
|
||
|
|
||
| private final int matchCount; | ||
| private final int prize; | ||
| private final boolean requireBonus; | ||
|
|
||
| LottoRank(int matchCount, int prize) { | ||
| LottoRank(int matchCount, int prize, boolean requireBonus) { | ||
| this.matchCount = matchCount; | ||
| this.prize = prize; | ||
| this.requireBonus = requireBonus; | ||
| } | ||
|
|
||
| public static LottoRank of(int matchCount) { | ||
| public static LottoRank of(int matchCount, boolean bonusMatch) { | ||
| for (LottoRank rank : LottoRank.values()) { | ||
| if (isMatch(rank, matchCount)) { | ||
| if (isMatch(rank, matchCount, bonusMatch)) { | ||
| return rank; | ||
| } | ||
| } | ||
|
|
||
| return NONE; | ||
| } | ||
|
|
||
| public static boolean isMatch(LottoRank rank, int matchCount) { | ||
| public static boolean isMatch(LottoRank rank, int matchCount, boolean requireBonus) { | ||
| return isCountMatch(rank, matchCount) && isBonusRequireMatch(rank, requireBonus); | ||
| } | ||
|
|
||
| private static boolean isCountMatch(LottoRank rank, int matchCount) { | ||
| return rank.matchCount == matchCount; | ||
| } | ||
|
|
||
| private static boolean isBonusRequireMatch(LottoRank rank, boolean requireBonus) { | ||
| return rank.requireBonus == requireBonus; | ||
| } | ||
|
|
||
| public int getPrize(LottoRank rank, int count) { | ||
| return rank.prize * count; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("%d개 일치 (%d원)", matchCount, prize); | ||
| return String.format("%d개%s(%d원)", matchCount, matchString(), prize); | ||
| } | ||
|
|
||
| private String matchString() { | ||
| return requireBonus ? ", 보너스 볼 일치" : " 일치"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ public void add(LottoRank rank) { | |
| matchResult.put(rank, matchResult.get(rank) + 1); | ||
| } | ||
|
|
||
| public int prize() { | ||
| private int prize() { | ||
| int prize = 0; | ||
| for (LottoRank rank : LottoRank.values()) { | ||
| prize += rank.getPrize(rank, matchResult.get(rank)); | ||
|
|
@@ -34,6 +34,20 @@ public int prize() { | |
| return prize; | ||
| } | ||
|
|
||
| private LottoPrice getPrice() { | ||
| int count = 0; | ||
| for (LottoRank rank : LottoRank.values()) { | ||
| count += matchResult.get(rank); | ||
| } | ||
|
|
||
| return new LottoPrice(count); | ||
| } | ||
|
|
||
| public String getProfit() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| LottoPrice price = getPrice(); | ||
| return String.valueOf(price.getProfit(prize())); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return Arrays.stream(LottoRank.values()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package lotto.domain; | ||
|
|
||
|
|
||
| import lotto.util.LottoNumberParser; | ||
|
|
||
| public class WinningLotto { | ||
| private final LottoNumbers winningNumbers; | ||
| private final LottoNumber bonusNumber; | ||
|
|
||
| public WinningLotto(String winningNumbers, String bonusNumber) { | ||
| this(new LottoNumbers(LottoNumberParser.parse(winningNumbers)), new LottoNumber(bonusNumber)); | ||
| } | ||
|
|
||
| public WinningLotto(LottoNumbers winningNumbers, LottoNumber bonusNumber) { | ||
| if (winningNumbers.contains(bonusNumber)) { | ||
| throw new IllegalArgumentException(String.format("당첨 숫자와 보너스 숫자가 일치합니다. (%d)", bonusNumber)); | ||
| } | ||
|
|
||
| this.winningNumbers = winningNumbers; | ||
| this.bonusNumber = bonusNumber; | ||
| } | ||
|
|
||
| public LottoRank getMatchedRank(LottoNumbers numbers) { | ||
| int matchCount = winningNumbers.getMatchCount(numbers); | ||
| return LottoRank.of(matchCount, numbers.contains(bonusNumber)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package lotto.util; | ||
|
|
||
| import lotto.domain.LottoNumber; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class LottoNumberCache { | ||
| private static final int MIN = 1; | ||
| private static final int MAX = 45; | ||
| private static final LottoNumber NONE = new LottoNumber(0); | ||
| private static final Map<Integer, LottoNumber> Cache = new HashMap<>(); | ||
|
|
||
| static { | ||
| for (int i = MIN; i <= MAX; i++) { | ||
| Cache.put(i, new LottoNumber(i)); | ||
| } | ||
| } | ||
|
|
||
| public static LottoNumber get(int value) { | ||
| LottoNumber lottoNumber = Cache.getOrDefault(value, NONE); | ||
| if (lottoNumber == NONE) { | ||
| throw new IllegalArgumentException("로또의 숫자는 1에서 45 사이여야합니다."); | ||
| } | ||
| return Cache.get(value); | ||
| } | ||
|
|
||
| public static LottoNumber get(String value) { | ||
| return get(Integer.parseInt(value)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수익률 구하는 부분이 이동했으면 price 필드 값은 Lotto에서 제거해도 되지 않을까?