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
f18032d
commit 91f6c78
Showing
7 changed files
with
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,55 @@ | ||
package lotto; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import lotto.domain.Lotto; | ||
import lotto.domain.Money; | ||
import lotto.domain.WinNumbers; | ||
import lotto.domain.lottonumbercreator.AutoLottoNumberCreator; | ||
import lotto.domain.lottonumbercreator.LottoNumberCreator; | ||
import lotto.domain.lottorank.LottoRank; | ||
import lotto.domain.lottorank.LottoRanks; | ||
import lotto.view.InputView; | ||
|
||
public class Application { | ||
private static final LottoNumberCreator CREATOR = new AutoLottoNumberCreator(); | ||
|
||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
System.out.println("구입금액을 입력해 주세요."); | ||
|
||
final Money money = new Money(InputView.requestInteger()); | ||
final int lottoCount = money.getLottoCount(); | ||
System.out.println(String.format("\n%d개를 구매했습니다.", lottoCount)); | ||
|
||
final List<Lotto> autoLottos = getAutoLottos(lottoCount); | ||
autoLottos.forEach(System.out::println); | ||
|
||
System.out.println("\n당첨 번호를 입력해 주세요."); | ||
final Lotto winLotto = new Lotto(InputView.requestWinNumbers()); | ||
|
||
System.out.println("\n보너스 번호를 입력해 주세요."); | ||
int bonusBall = InputView.requestInteger(); | ||
|
||
final WinNumbers winNumbers = new WinNumbers(winLotto, bonusBall); | ||
final LottoRanks lottoRanks = getLottoRanks(autoLottos, winNumbers); | ||
|
||
System.out.println("당첨 통계\n---"); | ||
System.out.println(lottoRanks); | ||
System.out.println(String.format("총 수익률은 %.1f%%입니다.", money.getRateOfProfit(lottoRanks.getProfit()))); | ||
} | ||
|
||
private static List<Lotto> getAutoLottos(int lottoCount) { | ||
return Stream.generate(() -> Lotto.from(CREATOR)) | ||
.limit(lottoCount) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static LottoRanks getLottoRanks(List<Lotto> autoLottos, WinNumbers winNumbers) { | ||
final List<LottoRank> ranks = autoLottos.stream() | ||
.map(lotto -> LottoRank.calculateRank(winNumbers, lotto)) | ||
.collect(Collectors.toList()); | ||
|
||
return LottoRanks.from(ranks); | ||
} | ||
} |
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
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,45 @@ | ||
package lotto.domain.lottorank; | ||
|
||
import static lotto.domain.lottorank.LottoRank.FIFTH; | ||
import static lotto.domain.lottorank.LottoRank.FIRST; | ||
import static lotto.domain.lottorank.LottoRank.FOURTH; | ||
import static lotto.domain.lottorank.LottoRank.SECOND; | ||
import static lotto.domain.lottorank.LottoRank.THIRD; | ||
|
||
import java.util.Arrays; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LottoRanks { | ||
|
||
private final Map<LottoRank, Integer> ranks; | ||
|
||
private LottoRanks(Map<LottoRank, Integer> ranks) { | ||
this.ranks = ranks; | ||
} | ||
|
||
public static LottoRanks from(List<LottoRank> ranks) { | ||
EnumMap<LottoRank, Integer> lottoRankMap = new EnumMap<>(LottoRank.class); | ||
Arrays.stream(LottoRank.values()).forEach(lottoRank -> lottoRankMap.put(lottoRank, 0)); | ||
|
||
ranks.forEach(rank -> lottoRankMap.computeIfPresent(rank, (key, value) -> value + 1)); | ||
return new LottoRanks(lottoRankMap); | ||
} | ||
|
||
public int getProfit() { | ||
return this.ranks.entrySet() | ||
.stream() | ||
.mapToInt(entry -> entry.getKey().getPrize() * entry.getValue()) | ||
.sum(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("3개 일치 (%,d원) - %d개\n", FIFTH.getPrize(), ranks.get(FIFTH)) | ||
+ String.format("4개 일치 (%,d원) - %d개\n", FOURTH.getPrize(), ranks.get(FOURTH)) | ||
+ String.format("5개 일치 (%,d원) - %d개\n", THIRD.getPrize(), ranks.get(THIRD)) | ||
+ String.format("5개 일치, 보너스 볼 일치 (%,d원) - %d개\n", SECOND.getPrize(), ranks.get(SECOND)) | ||
+ String.format("6개 일치 (%,d원) - %d개\n", FIRST.getPrize(), ranks.get(FIRST)); | ||
} | ||
} |
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,31 @@ | ||
package lotto.view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class InputView { | ||
private static final String DELIMITER = ","; | ||
|
||
public static int requestInteger() { | ||
final String input = Console.readLine(); | ||
return parseInt(input); | ||
} | ||
|
||
public static List<Integer> requestWinNumbers() { | ||
final String input = Console.readLine(); | ||
|
||
return Arrays.stream(input.split(DELIMITER)) | ||
.map(InputView::parseInt) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static int parseInt(String value) { | ||
try { | ||
return Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException(String.format("[ERROR] 입력값 %s는 숫자가 아닙니다.", value)); | ||
} | ||
} | ||
} |
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