-
Notifications
You must be signed in to change notification settings - Fork 92
[로또 미션] 김수민 미션 제출합니다. #6
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
base: boyekim
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,52 @@ | ||
| package controller; | ||
|
|
||
| import model.Lottos; | ||
| import model.OneLotto; | ||
| import model.RandomNumGenerator; | ||
| import model.RankCalculator; | ||
| import view.LottoInput; | ||
| import view.Print; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoMain { | ||
|
|
||
| public static void main(String[] args) { | ||
| int manual, automatic; | ||
|
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. 처음부터 선언하지 말고, 생성과 함께 사용하면 어떨까요? |
||
| int balance = LottoInput.inputBalance(); | ||
|
|
||
| RandomNumGenerator randomNumGenerator = new RandomNumGenerator(); | ||
| List<OneLotto> myLottos = new ArrayList<>(); | ||
|
|
||
| manual = LottoInput.manual(); | ||
| automatic = balance / 1000 - manual; | ||
|
|
||
| System.out.println("수동으로 구매할 번호를 입력해 주세요."); | ||
| for (int i = 0; i < manual; i++) { | ||
| List<Integer> manualLotto = LottoInput.manualLottoInput(); | ||
| OneLotto oneLotto = new OneLotto(manualLotto); | ||
| myLottos.add(oneLotto); | ||
| } | ||
|
|
||
| for (int i = 0; i < automatic; i++) { | ||
| OneLotto oneLotto = new OneLotto(randomNumGenerator.randomNumGenerate()); | ||
| myLottos.add(oneLotto); | ||
| } | ||
|
|
||
| Lottos lottos = new Lottos(balance, balance / 1000, myLottos); | ||
| for (OneLotto myLotto : myLottos) { | ||
| System.out.println(myLotto.getLottoNumbers()); | ||
| } | ||
| Print.printing(lottos, manual, automatic); | ||
|
|
||
| List<Integer> lottoAnswer = LottoInput.lottoAnswer(); | ||
| RankCalculator rankCalculator = new RankCalculator(lottoAnswer); | ||
|
|
||
| rankCalculator.allLottoRank(lottos); | ||
|
|
||
| LottoInput.bonusAnswer(lottos); | ||
|
|
||
| Print.winning(rankCalculator, lottos); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| public enum CorrectNum { //개수를 전달받아서 총 수익금 계산 하기? | ||
| ZERO(0, 0), | ||
| ONE(1, 0), | ||
| TWO(2, 0), | ||
| THREE(3, 5000), | ||
| FOUR(4, 50000), | ||
| FIVE_NOT(5, 1500000), | ||
| FIVE(5, 30000000), | ||
| SIX(6, 2000000000); | ||
| private int num; | ||
| private int money; | ||
|
|
||
| CorrectNum(int num, int money) { | ||
| this.num = num; | ||
| this.money = money; | ||
| } | ||
|
|
||
| public int getNum() { | ||
| return num; | ||
| } | ||
|
|
||
| public int getMoney() { | ||
| return money; | ||
| } | ||
|
|
||
| public static List<CorrectNum> getList() { | ||
| return List.of(CorrectNum.values()); //{ZERO,ONE, ... | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Lottos { | ||
|
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 class Lottos {
private final List<OneLotto> myLottos;
// ....
public int calculateMatchNumberCount(Lottos other) {
// ....
return matchNumberCounts;
}
}이렇게 객체가 로직을 포함한다면 필드에 count는 불필요하게 될 것 같아요. |
||
| private final int balance; | ||
| private final int count; | ||
| private final List<OneLotto> myLottos; | ||
| private int bonusBall; | ||
|
|
||
| public Lottos(int balance, int count, List<OneLotto> myLottos) { | ||
| this.balance = balance; | ||
| this.count = count; | ||
| this.myLottos = myLottos; | ||
| } | ||
|
|
||
| public int getBalance() { | ||
| return balance; | ||
| } | ||
|
|
||
| public List<OneLotto> getMyLottos() { | ||
| return myLottos; | ||
| } | ||
|
|
||
| public int getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| public int getBonusBall() { | ||
| return bonusBall; | ||
| } | ||
|
|
||
| public void setBonusBall(int bonusBall) { | ||
| this.bonusBall = bonusBall; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class OneLotto { | ||
|
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. 로또를 설명하는데 일치 개수, 보너스볼 등이 필요할까요?
|
||
| private final List<Integer> lottoNumbers; | ||
| private boolean bonusCheck; | ||
| private CorrectNum correctCnt; | ||
| private int correctNum; | ||
|
Comment on lines
+7
to
+9
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. [보너스 여부]도 포함하여 하나로 관리하면 어떨까요. ex) public class OneLotto {
private final List<Integer> lottoNumbers;
private final Prize prize;
} |
||
|
|
||
| public OneLotto(List<Integer> lottoNumbers) { | ||
| this.lottoNumbers = lottoNumbers; | ||
| } | ||
|
|
||
| public List<Integer> getLottoNumbers() { | ||
| return lottoNumbers; | ||
| } | ||
|
|
||
| public CorrectNum getCorrectCnt() { | ||
| return correctCnt; | ||
| } | ||
|
|
||
| public void setCorrectNum(int correctNum) { | ||
| this.correctNum = correctNum; | ||
| } | ||
|
|
||
| public void setBonusCheck(Lottos lottos) { | ||
| if (correctNum == 4 && lottoNumbers.contains(lottos.getBonusBall())) | ||
| bonusCheck = true; | ||
| } | ||
|
|
||
| public void setCorrectCnt() { | ||
| if (bonusCheck && correctNum == 4) { | ||
| correctCnt = CorrectNum.FIVE_NOT; | ||
| return; | ||
| } | ||
| List<CorrectNum> nums = CorrectNum.getList(); | ||
|
Comment on lines
+27
to
+37
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. setCorrectCnt가 정상적으로 동작하려면 bonusCheck가 이미 지정되어야 하네요. ( 이런 의도대로 항상 사용된다고 보장할 수 있을까요? 추가로 |
||
|
|
||
| for (int i = 0; i < nums.size(); i++) { | ||
| if (nums.get(i).getNum() == correctNum && !bonusCheck) { | ||
| correctCnt = nums.get(i); //왜 설정이 안되지? | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class RandomNumGenerator { | ||
|
|
||
| public List<Integer> randomNumGenerate() { | ||
| List<Integer> numList = new ArrayList<>(); | ||
|
|
||
| for (int i = 1; i <= 45; i++) { | ||
| numList.add(i); | ||
| } | ||
|
|
||
| Collections.shuffle(numList); | ||
|
|
||
| return numList.subList(0, 6); | ||
|
Comment on lines
+15
to
+18
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,49 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class RankCalculator { | ||
|
|
||
| private final List<Integer> ans; | ||
|
|
||
| public RankCalculator(List<Integer> ans) { | ||
| this.ans = ans; | ||
| } | ||
|
|
||
| private void eachLottoRank(OneLotto oneLotto) { | ||
| List<Integer> numbers = oneLotto.getLottoNumbers(); //로또 한장 | ||
| long count = numbers.stream() | ||
| .filter(ans::contains) | ||
| .count(); //몇개맞지? | ||
| oneLotto.setCorrectNum((int) count); | ||
| } | ||
|
|
||
| public void allLottoRank(Lottos lottos) { | ||
| List<OneLotto> oneLottoList = lottos.getMyLottos(); | ||
| for (OneLotto oneLotto : oneLottoList) { | ||
| eachLottoRank(oneLotto); | ||
| } | ||
| } | ||
|
|
||
| public long calculateTotal(Lottos lottos) { | ||
| long total = 0; | ||
| List<OneLotto> oneLottoList = lottos.getMyLottos(); | ||
|
|
||
| for (OneLotto oneLotto : oneLottoList) { | ||
| oneLotto.setBonusCheck(lottos); | ||
| oneLotto.setCorrectCnt(); | ||
| } | ||
|
|
||
| for (OneLotto oneLotto : oneLottoList) { | ||
| CorrectNum correctCnt = oneLotto.getCorrectCnt(); | ||
| total += correctCnt.getMoney(); | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| public String rateOfReturn(int balance, long total) { | ||
| String result = String.format("%.2f", (double) total / (double) balance); | ||
| return result; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package view; | ||
|
|
||
| import model.Lottos; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class LottoInput { | ||
|
|
||
| public static int inputBalance() { | ||
| Scanner sc = new Scanner(System.in); | ||
|
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. 상수로 선언해서 한번만 초기화 할 수 있습니다. |
||
| System.out.println("구입금액을 입력해 주세요."); | ||
| return sc.nextInt(); | ||
| } | ||
|
|
||
| public static List<Integer> lottoAnswer() { | ||
| System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
| Scanner sc = new Scanner(System.in); | ||
| String str = sc.nextLine(); | ||
| String[] ans = str.split(", "); | ||
| List<Integer> result = new ArrayList<>(); | ||
| for (int i = 0; i < ans.length; i++) { | ||
| result.add(Integer.parseInt(ans[i])); | ||
| } | ||
|
Comment on lines
+21
to
+25
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. "1,2,3,4,5,6" |
||
| return result; | ||
| } | ||
|
|
||
| public static int bonusAnswer(Lottos lottos) { | ||
| System.out.println("보너스 볼을 입력해 주세요."); | ||
| Scanner sc = new Scanner(System.in); | ||
| int bonus = sc.nextInt(); | ||
| lottos.setBonusBall(bonus); | ||
| return bonus; | ||
| } | ||
|
|
||
| public static int manual() { | ||
| System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); | ||
| Scanner sc = new Scanner(System.in); | ||
| return sc.nextInt(); | ||
| } | ||
|
|
||
| public static List<Integer> manualLottoInput() { | ||
| Scanner sc = new Scanner(System.in); | ||
| String str = sc.nextLine(); | ||
| String[] ans = str.split(", "); | ||
| List<Integer> result = new ArrayList<>(); | ||
| for (int i = 0; i < ans.length; i++) { | ||
| result.add(Integer.parseInt(ans[i])); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package view; | ||
|
|
||
| import model.CorrectNum; | ||
| import model.Lottos; | ||
| import model.OneLotto; | ||
| import model.RankCalculator; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Print { | ||
| public static void printing(Lottos lottos, int manual, int automatic) { | ||
| System.out.println("수동으로 " + manual + "장, 자동으로 " + automatic + "개를 구매했습니다."); | ||
| List<OneLotto> oneLottoList = lottos.getMyLottos(); | ||
| for (OneLotto oneLotto : oneLottoList) { | ||
| System.out.println(makeLottoStr(oneLotto)); | ||
| } | ||
| } | ||
|
|
||
| private static String makeLottoStr(OneLotto oneLotto) { | ||
| StringBuffer sb = new StringBuffer(); | ||
|
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.
|
||
| sb.append("["); | ||
|
|
||
| List<Integer> tmp = oneLotto.getLottoNumbers(); | ||
|
|
||
| for (Integer i : tmp) { | ||
| sb.append(i.intValue()); | ||
| sb.append(", "); | ||
| } | ||
| sb.delete(sb.length() - 2, sb.length()); | ||
| sb.append("]"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| public static void winning(RankCalculator rankCalculator, Lottos lottos) { | ||
| long total = rankCalculator.calculateTotal(lottos); | ||
| System.out.println("당첨 통계"); | ||
| System.out.println("---------"); | ||
|
|
||
| List<OneLotto> oneLottos = lottos.getMyLottos(); | ||
| List<CorrectNum> cor = CorrectNum.getList(); | ||
| for (int i = 3; i < cor.size(); i++) { | ||
| int cnt = findCorrectNum(oneLottos, cor.get(i)); | ||
| System.out.println(makeWinningStr(cor.get(i), cnt)); | ||
| } | ||
| System.out.println("총 수익률은 " + rankCalculator.rateOfReturn(lottos.getBalance(), total) + "입니다."); | ||
| } | ||
|
|
||
| private static String makeWinningStr(CorrectNum correctNum, int cnt) { | ||
| StringBuffer sb = new StringBuffer(); | ||
| if (correctNum == CorrectNum.FIVE_NOT) { | ||
| sb.append("5개 일치, 보너스 볼 일치("); | ||
| sb.append(correctNum.getMoney()); | ||
| sb.append("원) -"); | ||
| sb.append(cnt); | ||
| sb.append("개"); | ||
| return sb.toString(); | ||
| } | ||
| sb.append(correctNum.getNum()); | ||
| sb.append("개 일치 ("); | ||
| sb.append(correctNum.getMoney()); | ||
| sb.append("원)- "); | ||
| sb.append(cnt); | ||
| sb.append("개"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private static int findCorrectNum(List<OneLotto> oneLottos, CorrectNum correctNum) { | ||
| int cnt = 0; | ||
| for (OneLotto oneLotto : oneLottos) { | ||
| if (correctNum == oneLotto.getCorrectCnt()) | ||
| cnt++; | ||
| } | ||
| return cnt; | ||
| } | ||
| } | ||
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.
하나의 메서드에서 너무 많은 로직이 수행되고 있어요.
메서드의 역할이 가중되면, 해당 메서드의 의존도가 높고 다양한 요구사항 변경에 영향을 받을 수 있습니다. 또, 테스트 하기도 어렵고 무슨 역할을 하는지 파악하기 어려워요.
main에서 꼭 이루어져야 하는 로직은 무엇일까요?
입력을 받고, 로또를 생성하고, 계산이 이루어지는 구체적인 로직들은 해당 역할을 하는 객체에 위임 해도 좋을 것 같아요.