Skip to content
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

Open
wants to merge 10 commits into
base: dradnats1012
Choose a base branch
from
10 changes: 10 additions & 0 deletions src/main/java/lotto/Application.java
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();
}
}
27 changes: 27 additions & 0 deletions src/main/java/lotto/Constant.java
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;
}
Comment on lines +3 to +27

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상금쪽은 한곳에서만 쓰다보니 상금 관련 클래스에 두는게 나아보이네요 👍

42 changes: 42 additions & 0 deletions src/main/java/lotto/Rank.java
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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/controller/Controller.java
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());
}
}
51 changes: 51 additions & 0 deletions src/main/java/lotto/domain/CheckPlace.java
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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/lotto/domain/CustomLotto.java
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();
}
}
41 changes: 41 additions & 0 deletions src/main/java/lotto/domain/Lotto.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요구사항에 함수 하나는 한가지 일만 하게 하라고 나와있기는 했는데 이렇게 까지 함수로 나눌 필요성이 있었나 하는 생각이 저는 드네요
숫자를 검증하는 것과 사이즈를 검증하는 것이 과연 다른 일인가? 하는 생각이 들어요
이것도 답이 있는 것은 아니지만 어디까지 함수를 나눠야 하는가 생각해보면 좋을 것 같아요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검증이라는 역할은 같지만 검증하는 내용이 달라서 나눴는데 저도 이거 나눌때 이렇게 나눌필요까지 있을까 하는 고민이 있었는데 일단 요구사항대로 나눴습니다!
개인적으로는 그냥 묶어도 될것 같아용

}
54 changes: 54 additions & 0 deletions src/main/java/lotto/domain/LottoGame.java
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());
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/domain/Result.java
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;
}
}
35 changes: 35 additions & 0 deletions src/main/java/lotto/domain/Reward.java
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();
}
}
Loading