-
Notifications
You must be signed in to change notification settings - Fork 48
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
[로또 미션] 허준기 미션 제출합니다 #42
base: dradnats1012
Are you sure you want to change the base?
Changes from all commits
cbc4a74
a4e03ca
da62a3f
8931dd2
332400e
53b05f8
53bad06
4c0b69d
559c853
d7f16dc
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,10 @@ | ||
package lotto; | ||
|
||
import lotto.controller.Controller; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
Controller controller = new Controller(); | ||
controller.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package lotto; | ||
|
||
public final class Constant { | ||
|
||
private Constant() { | ||
} | ||
|
||
public static final int LOTTO_NUM_COUNT = 6; | ||
|
||
public static final int LOTTO_PRICE = 1000; | ||
|
||
public static final int NO_REWARD = 0; | ||
|
||
public static final int FIFTH_REWARD = 5000; | ||
|
||
public static final int FOURTH_REWARD = 50000; | ||
|
||
public static final int THIRD_REWARD = 1500000; | ||
|
||
public static final int SECOND_REWARD = 30000000; | ||
|
||
public static final int FIRST_REWARD = 2000000000; | ||
|
||
public static final int MIN_NUM = 1; | ||
|
||
public static final int MAX_NUM = 45; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package lotto; | ||
|
||
public enum Rank { | ||
_1ST_PLACE(6, false, Constant.FIRST_REWARD), | ||
_2ND_PLACE(5, true, Constant.SECOND_REWARD), | ||
_3RD_PLACE(5, false, Constant.THIRD_REWARD), | ||
_4TH_PLACE(4, false, Constant.FOURTH_REWARD), | ||
_5TH_PLACE(3, false, Constant.FIFTH_REWARD), | ||
_NO_PLACE(0, false, Constant.NO_REWARD); | ||
|
||
private final int match; | ||
private final boolean bonus; | ||
private final int reward; | ||
|
||
Rank(int match, boolean bonus, int reward) { | ||
this.match = match; | ||
this.bonus = bonus; | ||
this.reward = reward; | ||
} | ||
|
||
public static Rank of(int match, boolean bonus) { | ||
for (Rank rank : Rank.values()) { | ||
if (rank.match == match && rank.bonus == bonus) { | ||
return rank; | ||
} | ||
} | ||
|
||
return _NO_PLACE; | ||
} | ||
|
||
public int getMatch() { | ||
return match; | ||
} | ||
|
||
public boolean getBonus() { | ||
return bonus; | ||
} | ||
|
||
public int getReward() { | ||
return reward; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto.controller; | ||
|
||
import lotto.domain.CheckPlace; | ||
import lotto.domain.CustomLotto; | ||
import lotto.domain.Reward; | ||
import lotto.domain.WinNums; | ||
import lotto.generator.NumberGenerator; | ||
import lotto.generator.RandomNumberGenerator; | ||
import lotto.domain.LottoGame; | ||
import lotto.view.InputView; | ||
import lotto.view.OutputView; | ||
|
||
public class Controller { | ||
|
||
private final NumberGenerator numberGenerator = new RandomNumberGenerator(); | ||
|
||
public void run() { | ||
int inputMoney = InputView.getMoney(); | ||
|
||
int customCount = InputView.getCustomLottoCount(); | ||
CustomLotto customLotto = new CustomLotto(InputView.getCustomLotto(customCount)); | ||
|
||
LottoGame lottoGame = new LottoGame(inputMoney, numberGenerator, customLotto); | ||
OutputView.printLottoes(customCount, lottoGame.getLottoList()); | ||
|
||
WinNums winNums = new WinNums(InputView.getWinLotto(), InputView.getBonusBall()); | ||
CheckPlace checkPlace = new CheckPlace(lottoGame, winNums); | ||
OutputView.printStatistics(checkPlace.getResultMap()); | ||
|
||
Reward reward = new Reward(checkPlace.getResult(), inputMoney); | ||
OutputView.printReward(reward.getRate()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lotto.Rank; | ||
|
||
public class CheckPlace { | ||
private final WinNums winNums; | ||
private final Result result; | ||
private final List<Lotto> lottoList; | ||
private final List<Integer> winList; | ||
|
||
public CheckPlace(LottoGame lottoGame, WinNums winNums) { | ||
this.winNums = winNums; | ||
this.lottoList = lottoGame.getLottoList(); | ||
this.winList = winNums.getWinNums(); | ||
result = new Result(); | ||
} | ||
|
||
public Map<Rank, Integer> getResultMap() { | ||
checkNum(); | ||
return result.getResult(); | ||
} | ||
|
||
private void checkNum() { | ||
int match; | ||
boolean isBonus; | ||
|
||
for (Lotto lotto : lottoList) { | ||
match = 0; | ||
isBonus = false; | ||
for (int num : winList) { | ||
if (lotto.getLottoNums().contains(num)) { | ||
match++; | ||
} | ||
} | ||
if (match == 5 && lotto.getLottoNums().contains(winNums.getBonusNum())) { | ||
isBonus = true; | ||
} | ||
|
||
if (match >= 3) { | ||
result.addResult(Rank.of(match, isBonus)); | ||
} | ||
} | ||
} | ||
|
||
public Result getResult() { | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CustomLotto { | ||
private final List<Lotto> lottoList = new ArrayList<>(); | ||
private final List<List<Integer>> customLottoes; | ||
|
||
public CustomLotto(List<List<Integer>> customLottoes) { | ||
this.customLottoes = customLottoes; | ||
makeCustomLottoes(); | ||
} | ||
|
||
private void makeCustomLottoes() { | ||
for (List<Integer> lotto : customLottoes) { | ||
lottoList.add(new Lotto(lotto)); | ||
} | ||
} | ||
|
||
public List<Lotto> getCustomLottoList() { | ||
return lottoList; | ||
} | ||
|
||
public int getCustomLottoCount() { | ||
return lottoList.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package lotto.domain; | ||
|
||
import java.util.List; | ||
|
||
import lotto.Constant; | ||
import lotto.generator.NumberGenerator; | ||
import lotto.message.ErrorMessage; | ||
|
||
public class Lotto { | ||
|
||
private final List<Integer> lottoNums; | ||
|
||
public Lotto(NumberGenerator numberGenerator) { | ||
lottoNums = numberGenerator.generateLottoNum(); // 생성자에서 어디까지의 역할을 해야하는가? | ||
validateLottoSize(); | ||
} | ||
|
||
public Lotto(List<Integer> lottoNums) { | ||
this.lottoNums = lottoNums; | ||
validateLottoSize(); | ||
validateLottoNum(); | ||
} | ||
|
||
public List<Integer> getLottoNums() { | ||
return lottoNums; | ||
} | ||
|
||
private void validateLottoSize() { | ||
if (lottoNums.size() != Constant.LOTTO_NUM_COUNT) { | ||
throw new IllegalArgumentException(ErrorMessage.INVALID_LOTTO_NUM.getMessage()); | ||
} | ||
} | ||
|
||
private void validateLottoNum(){ | ||
for(int num : lottoNums){ | ||
if(!(num >= Constant.MIN_NUM && num <= Constant.MAX_NUM)){ | ||
throw new IllegalArgumentException(ErrorMessage.INVALID_NUM.getMessage()); | ||
} | ||
} | ||
} | ||
Comment on lines
+28
to
+40
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. 요구사항에 함수 하나는 한가지 일만 하게 하라고 나와있기는 했는데 이렇게 까지 함수로 나눌 필요성이 있었나 하는 생각이 저는 드네요 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,54 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lotto.Constant; | ||
import lotto.generator.NumberGenerator; | ||
import lotto.message.ErrorMessage; | ||
|
||
public class LottoGame { | ||
private final int price; | ||
private final List<Lotto> lottoList = new ArrayList<>(); | ||
private final int trial; | ||
private final NumberGenerator numberGenerator; | ||
private final CustomLotto customLotto; | ||
|
||
public LottoGame(int price, NumberGenerator numberGenerator, CustomLotto customLotto) { | ||
validatePrice(price); | ||
this.price = price; | ||
this.trial = getTrial(); | ||
this.numberGenerator = numberGenerator; | ||
this.customLotto = customLotto; | ||
validateTrial(); | ||
makeLottoList(); | ||
} | ||
|
||
public List<Lotto> getLottoList() { | ||
return lottoList; | ||
} | ||
|
||
private void makeLottoList() { | ||
lottoList.addAll(customLotto.getCustomLottoList()); | ||
|
||
for (int j = 0; j < trial - customLotto.getCustomLottoCount(); j++) { | ||
lottoList.add(new Lotto(numberGenerator)); | ||
} | ||
} | ||
|
||
private void validatePrice(int price) { | ||
if ((price % 1000) != 0) { | ||
throw new IllegalArgumentException(ErrorMessage.INVALID_MONEY.getMessage()); | ||
} | ||
} | ||
|
||
private int getTrial() { | ||
return price / Constant.LOTTO_PRICE; | ||
} | ||
|
||
private void validateTrial(){ | ||
if(trial < customLotto.getCustomLottoCount()){ | ||
throw new IllegalArgumentException(ErrorMessage.TOO_MANY_CUSTOM.getMessage()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto.domain; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lotto.Rank; | ||
|
||
public class Result { | ||
Map<Rank, Integer> result = new LinkedHashMap<>(); | ||
|
||
public Result() { | ||
makeEmptyResult(); | ||
} | ||
|
||
private void makeEmptyResult() { | ||
List<Rank> ranks = List.of(Rank.values()); | ||
for (int i = ranks.size() - 1; i >= 0; i--) { | ||
Rank rank = ranks.get(i); | ||
if (rank != Rank._NO_PLACE) { | ||
result.put(rank, 0); | ||
} | ||
} | ||
} | ||
|
||
public void addResult(Rank rank) { | ||
result.put(rank, result.get(rank) + 1); | ||
} | ||
|
||
public Map<Rank, Integer> getResult() { | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lotto.domain; | ||
|
||
import java.util.Map; | ||
|
||
import lotto.Rank; | ||
|
||
public class Reward { | ||
private final int inputMoney; | ||
private int totalReward; | ||
private final Map<Rank, Integer> resultMap; | ||
|
||
public Reward(Result result, int inputMoney) { | ||
this.inputMoney = inputMoney; | ||
this.resultMap = result.getResult(); | ||
} | ||
|
||
private void caculateTotalReward() { | ||
resultMap.forEach((rank, value) -> | ||
totalReward += rank.getReward() * value); | ||
} | ||
|
||
private String calculate() { | ||
caculateTotalReward(); | ||
|
||
if (totalReward == 0) { | ||
return "0"; | ||
} | ||
double rewardRate = (double)totalReward / inputMoney; | ||
return String.format("%.2f", rewardRate); | ||
} | ||
|
||
public String getRate() { | ||
return calculate(); | ||
} | ||
} |
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.
무슨 느낌으로 상수용 클래스를 만들어 하나에 모았는지는 충분히 알겠는데 한 클래스에서만 사용되는 상수들도 이렇게 모아놓는 것이 좋을지 생각해보면 좋을 것 같아요~
물론 정해진 답이 있는 것은 아니지만요
저도 덕분에 더 공부하고 가네요!
참고: https://xxeol.tistory.com/8
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.
상금쪽은 한곳에서만 쓰다보니 상금 관련 클래스에 두는게 나아보이네요 👍