-
Notifications
You must be signed in to change notification settings - Fork 52
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
[로또 미션] 최준호 미션 제출합니다. #45
base: choi-jjunho
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lotto; | ||
|
||
import lotto.controller.LottoController; | ||
|
||
public class LottoApplication { | ||
|
||
public static void main(String[] args) { | ||
LottoController controller = new LottoController(); | ||
controller.start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package lotto.controller; | ||
|
||
import lotto.domain.LottoMachine; | ||
import lotto.view.InputView; | ||
|
||
public class LottoController { | ||
|
||
public void start() { | ||
var money = InputView.inputMoney(); | ||
var manualCount = InputView.inputManualBuyCount(); | ||
var manualNumbers = InputView.inputManualNumbers(manualCount); | ||
LottoMachine machine = new LottoMachine(); | ||
machine.buyLotto(money, manualNumbers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package lotto.domain; | ||
|
||
import static lotto.domain.LottoRate.NONE; | ||
|
||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AnswerLotto { | ||
|
||
private final List<Integer> numbers; | ||
private final int bonusNumber; | ||
|
||
public AnswerLotto(List<Integer> numbers, int bonusNumber) { | ||
this.numbers = numbers; | ||
this.bonusNumber = bonusNumber; | ||
} | ||
|
||
public Map<LottoRate, Integer> calculateResult(List<Lotto> lottos) { | ||
Map<LottoRate, Integer> result = new EnumMap<>(LottoRate.class); | ||
for (Lotto lotto : lottos) { | ||
AnswerMatchModel matchModel = getMatchModel(lotto); | ||
LottoRate rate = LottoRate.from(matchModel); | ||
if (rate != NONE) { | ||
result.put(rate, result.getOrDefault(rate, 0) + 1); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private AnswerMatchModel getMatchModel(Lotto lotto) { | ||
List<Integer> lottoNumbers = lotto.getNumbers(); | ||
int count = (int)lottoNumbers.stream() | ||
.filter(numbers::contains) | ||
.count(); | ||
boolean isBonus = lottoNumbers.contains(bonusNumber); | ||
if (isBonus) { | ||
return new AnswerMatchModel(count + 1, true); | ||
} | ||
return new AnswerMatchModel(count, false); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lotto.domain; | ||
|
||
public record AnswerMatchModel( | ||
int count, | ||
boolean isBonus | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
|
||
public class Lotto { | ||
|
||
private final List<LottoNumber> numbers; | ||
|
||
public Lotto(List<Integer> numbers) { | ||
this.numbers = numbers.stream() | ||
.map(LottoNumber::new) | ||
.toList(); | ||
} | ||
|
||
public static Lotto create(NumberGenerator numberGenerator) { | ||
List<Integer> generate = numberGenerator.generate(); | ||
return new Lotto(generate); | ||
} | ||
|
||
public List<Integer> getNumbers() { | ||
return numbers.stream() | ||
.map(LottoNumber::getNumber) | ||
.toList(); | ||
} | ||
Comment on lines
+20
to
+24
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. List로 반환하는 이유가 있나요? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoGroup { | ||
|
||
private final List<Lotto> lottos; | ||
|
||
public LottoGroup() { | ||
this.lottos = new ArrayList<>(); | ||
} | ||
|
||
public LottoGroup(List<List<Integer>> lottos) { | ||
this.lottos = lottos.stream() | ||
.map(Lotto::new) | ||
.toList(); | ||
} | ||
|
||
public void add(Lotto lotto) { | ||
this.lottos.add(lotto); | ||
} | ||
|
||
public List<Lotto> getLottos() { | ||
return lottos; | ||
} | ||
|
||
public List<List<Integer>> getNumbers() { | ||
return lottos.stream() | ||
.map(Lotto::getNumbers) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import lotto.view.InputView; | ||
import lotto.view.OutputView; | ||
|
||
public class LottoMachine { | ||
|
||
private static final int LOTTO_PRICE = 1000; | ||
|
||
public void buyLotto(int money, List<List<Integer>> manualNumbers) { | ||
money -= manualNumbers.size() * LOTTO_PRICE; | ||
LottoGroup lottos = new LottoGroup(manualNumbers); | ||
for (int i = 0; i < money; i += LOTTO_PRICE) { | ||
Lotto lotto = Lotto.create(new LottoNumberGenerator()); | ||
lottos.add(lotto); | ||
} | ||
OutputView.printLotto(lottos.getNumbers()); | ||
Map<LottoRate, Integer> rate = matchAnswer(lottos); | ||
double rateOfReturn = getRateOfReturn(money, rate); | ||
OutputView.printStatics(rate, rateOfReturn); | ||
} | ||
|
||
public Map<LottoRate, Integer> matchAnswer(LottoGroup lottos) { | ||
List<Integer> answerNumber = InputView.inputAnswerNumber(); | ||
int bonusNumber = InputView.inputBonusNumber(); | ||
AnswerLotto answerLotto = new AnswerLotto(answerNumber, bonusNumber); | ||
return answerLotto.calculateResult(lottos.getLottos()); | ||
} | ||
|
||
private double getRateOfReturn(int money, Map<LottoRate, Integer> statics) { | ||
int earned = 0; | ||
for (Entry<LottoRate, Integer> entry : statics.entrySet()) { | ||
LottoRate rate = entry.getKey(); | ||
int earnMoney = rate.getPrice() * entry.getValue(); | ||
earned += earnMoney; | ||
} | ||
return (double)earned / money; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lotto.domain; | ||
|
||
public class LottoNumber { | ||
|
||
private final int number; | ||
|
||
public LottoNumber(int number) { | ||
this.number = number; | ||
} | ||
|
||
public int getNumber() { | ||
return number; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LottoNumberGenerator implements NumberGenerator { | ||
|
||
private final List<Integer> baseNumbers; | ||
|
||
public LottoNumberGenerator() { | ||
this.baseNumbers = IntStream.rangeClosed(1, 45).boxed() | ||
.collect(Collectors.toCollection(ArrayList::new)); | ||
} | ||
|
||
@Override | ||
public List<Integer> generate() { | ||
Collections.shuffle(baseNumbers); | ||
return baseNumbers.subList(0, 6).stream() | ||
.sorted() | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lotto.domain; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum LottoRate { | ||
THREE_MATCHED(5000, 3, LottoRate::defaultViewMessage), | ||
FOUR_MATCHED(50000, 4, LottoRate::defaultViewMessage), | ||
FIVE_MATCHED(1500000, 5, LottoRate::defaultViewMessage), | ||
FIVE_MATCHED_WITH_BONUS(30000000, 5, LottoRate::bonusViewMessage), | ||
SIX_MATCHED(2000000000, 6, LottoRate::defaultViewMessage), | ||
NONE(0, 0, LottoRate::defaultViewMessage); | ||
|
||
private final int price; | ||
private final int matchCount; | ||
private final ViewMessageFormatter viewMessage; | ||
|
||
LottoRate(int price, int matchCount, ViewMessageFormatter viewMessage) { | ||
this.price = price; | ||
this.matchCount = matchCount; | ||
this.viewMessage = viewMessage; | ||
} | ||
|
||
public static LottoRate from(AnswerMatchModel match) { | ||
if (match.isBonus() && match.count() == 5) { | ||
return FIVE_MATCHED_WITH_BONUS; | ||
} | ||
return Arrays.stream(values()) | ||
.filter(it -> it.matchCount == match.count()) | ||
.findAny() | ||
.orElse(NONE); | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public int getMatchCount() { | ||
return matchCount; | ||
} | ||
|
||
public String getViewMessage(int count) { | ||
return viewMessage.format(this, count); | ||
} | ||
|
||
private static String defaultViewMessage(LottoRate rate, int count) { | ||
return String.format("%d개 일치 (%d)- %d개", rate.matchCount, rate.price, count); | ||
} | ||
|
||
private static String bonusViewMessage(LottoRate rate, int count) { | ||
return String.format("%d개 일치, 보너스 볼 일치(%d)- %d개", rate.matchCount, rate.price, count); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface ViewMessageFormatter { | ||
String format(LottoRate rate, int count); | ||
} | ||
Comment on lines
+45
to
+56
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. 우와... 이런게 있었다니 ㄷㄷ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
|
||
public interface NumberGenerator { | ||
|
||
List<Integer> generate(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package lotto.view; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.StringTokenizer; | ||
|
||
public class InputView { | ||
|
||
private static Scanner scanner = new Scanner(System.in); | ||
|
||
private InputView() { | ||
} | ||
|
||
public static int inputMoney() { | ||
System.out.println("구입금액을 입력해주세요."); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
|
||
public static int inputManualBuyCount() { | ||
System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
|
||
public static List<List<Integer>> inputManualNumbers(int count) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
for (int i = 0; i < count; i++) { | ||
StringTokenizer stringTokenizer = new StringTokenizer(scanner.nextLine(), ", "); | ||
List<Integer> numbers = new ArrayList<>(); | ||
while (stringTokenizer.hasMoreTokens()) { | ||
int number = Integer.parseInt(stringTokenizer.nextToken()); | ||
numbers.add(number); | ||
} | ||
result.add(numbers); | ||
} | ||
Comment on lines
+30
to
+35
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. |
||
return result; | ||
} | ||
|
||
public static List<Integer> inputAnswerNumber() { | ||
System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
StringTokenizer stringTokenizer = new StringTokenizer(scanner.nextLine(), ", "); | ||
List<Integer> result = new ArrayList<>(); | ||
while (stringTokenizer.hasMoreTokens()) { | ||
int number = Integer.parseInt(stringTokenizer.nextToken()); | ||
result.add(number); | ||
} | ||
return result; | ||
} | ||
|
||
public static int inputBonusNumber() { | ||
System.out.println("보너스 볼을 입력해 주세요."); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package lotto.view; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import lotto.domain.LottoRate; | ||
|
||
public class OutputView { | ||
|
||
private OutputView() { | ||
} | ||
|
||
public static void printLotto(List<List<Integer>> numbers) { | ||
System.out.printf("%d개를 구매했습니다.%n", numbers.size()); | ||
for (List<Integer> number : numbers) { | ||
String result = number.stream() | ||
.map(String::valueOf) | ||
.collect(Collectors.joining(", ")); | ||
System.out.printf("[%s]%n", result); | ||
} | ||
} | ||
|
||
public static void printStatics(Map<LottoRate, Integer> rate, double rateOfReturn) { | ||
System.out.println(""" | ||
당첨 통계 | ||
--------- | ||
"""); | ||
for (LottoRate detail : LottoRate.values()) { | ||
if (detail == LottoRate.NONE) { | ||
continue; | ||
} | ||
Integer count = rate.getOrDefault(detail, 0); | ||
System.out.println(detail.getViewMessage(count)); | ||
} | ||
System.out.printf("총 수익률은 %.2f입니다.%n", rateOfReturn); | ||
} | ||
} |
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.
depth 지켜주세용