From 902686020711527ef86281ae7af8ad17e19ec206 Mon Sep 17 00:00:00 2001 From: Sumin Date: Fri, 3 May 2024 14:00:44 +0900 Subject: [PATCH 1/2] level2 --- src/main/java/model/Lottos.java | 25 ++++++++++ src/main/java/model/OneLotto.java | 16 +++++++ src/main/java/model/RandomNumGenerator.java | 22 +++++++++ src/main/java/model/RankCalculator.java | 48 +++++++++++++++++++ src/test/java/model/RankCalculatorTest.java | 53 +++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 src/main/java/model/Lottos.java create mode 100644 src/main/java/model/OneLotto.java create mode 100644 src/main/java/model/RandomNumGenerator.java create mode 100644 src/main/java/model/RankCalculator.java create mode 100644 src/test/java/model/RankCalculatorTest.java diff --git a/src/main/java/model/Lottos.java b/src/main/java/model/Lottos.java new file mode 100644 index 00000000..67d1d716 --- /dev/null +++ b/src/main/java/model/Lottos.java @@ -0,0 +1,25 @@ +package model; + +import java.util.List; + +public class Lottos { + private final int balance; + private final int count; + private final List myLottos; + + public Lottos(int balance, int count, List myLottos) { + this.balance = balance; + this.count = count; + this.myLottos = myLottos; + } + + public int getBalance() { + return balance; + } + + public List getMyLottos() { + return myLottos; + } + + +} diff --git a/src/main/java/model/OneLotto.java b/src/main/java/model/OneLotto.java new file mode 100644 index 00000000..e59fafe2 --- /dev/null +++ b/src/main/java/model/OneLotto.java @@ -0,0 +1,16 @@ +package model; + +import java.util.List; + +public class OneLotto { + private final List lottoNumbers; + + public OneLotto(List lottoNumbers) { + this.lottoNumbers = lottoNumbers; + } + + public List getLottoNumbers() { + return lottoNumbers; + } + +} diff --git a/src/main/java/model/RandomNumGenerator.java b/src/main/java/model/RandomNumGenerator.java new file mode 100644 index 00000000..9686876b --- /dev/null +++ b/src/main/java/model/RandomNumGenerator.java @@ -0,0 +1,22 @@ +package model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class RandomNumGenerator { + + public List randomNumGenerate() { + List numList = new ArrayList<>(); + + for (int i = 1; i <= 45; i++) { + numList.add(i); + } + + Collections.shuffle(numList); + + return numList.subList(0, 6); + } + + +} diff --git a/src/main/java/model/RankCalculator.java b/src/main/java/model/RankCalculator.java new file mode 100644 index 00000000..1e9668d7 --- /dev/null +++ b/src/main/java/model/RankCalculator.java @@ -0,0 +1,48 @@ +package model; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class RankCalculator { + + private final OneLotto ans; + List correctNum = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0)); + + public RankCalculator(OneLotto ans) { + this.ans = ans; + } + + private void eachLottoRank(OneLotto oneLotto) { + List numbers = oneLotto.getLottoNumbers(); //로또 한장 + List ansNumbers = ans.getLottoNumbers(); //정답 로또 번호 + long count = numbers.stream() + .filter(ansNumbers::contains) + .count(); //몇개맞지? + correctNum.set((int) (count - 1), correctNum.get((int) count - 1) + 1); + } + + public void allLottoRank(Lottos lottos) { + List oneLottoList = lottos.getMyLottos(); + for (OneLotto oneLotto : oneLottoList) { + eachLottoRank(oneLotto); + } + } + + private long calculateTotal(List rankList) { + List money = new ArrayList<> + (Arrays.asList(0, 0, 5000, 50000, 1500000, 2000000000)); + long total = 0; + for (int i = 2; i < rankList.size(); i++) { + total += ((long) rankList.get(i) * money.get(i)); + } + return total; + } + + public double rateOfReturn(Lottos lottos) { + int balance = lottos.getBalance(); + long total = calculateTotal(correctNum); + + return (double) (total / balance * 100); + } +} diff --git a/src/test/java/model/RankCalculatorTest.java b/src/test/java/model/RankCalculatorTest.java new file mode 100644 index 00000000..76f1f77c --- /dev/null +++ b/src/test/java/model/RankCalculatorTest.java @@ -0,0 +1,53 @@ +package model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class RankCalculatorTest { + + @DisplayName("한장의 로또의 등수 잘 계산되는지 확인한다.") + @Test + void oneLottoCorrectCountTest() { + List oneLottoList = new ArrayList<>(1); + OneLotto oneLotto = new OneLotto(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6))); + oneLottoList.add(oneLotto); + Lottos lottos = new Lottos(1000, 1000 / 1000, oneLottoList); + + ArrayList ansLottoNumbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 7, 8)); //정답 + + RankCalculator rankCalculator = new RankCalculator(new OneLotto(ansLottoNumbers)); + rankCalculator.allLottoRank(lottos); + + assertThat(rankCalculator.correctNum.get(3)).isEqualTo(1); + } + + @DisplayName("여러 장 로또의 총 수익금과 수익률이 올바른지 확인한다.") + @Test + void allLottoCorrectCountTest() { + List oneLottoList = new ArrayList<>(5); + OneLotto oneLotto1 = new OneLotto(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6))); + OneLotto oneLotto2 = new OneLotto(new ArrayList<>(Arrays.asList(1, 2, 3, 5, 6, 7))); + OneLotto oneLotto3 = new OneLotto(new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7, 8))); + OneLotto oneLotto4 = new OneLotto(new ArrayList<>(Arrays.asList(4, 5, 6, 7, 8, 9))); + OneLotto oneLotto5 = new OneLotto(new ArrayList<>(Arrays.asList(5, 6, 7, 8, 9, 10))); + oneLottoList.add(oneLotto1); + oneLottoList.add(oneLotto2); + oneLottoList.add(oneLotto3); + oneLottoList.add(oneLotto4); + oneLottoList.add(oneLotto5); + + Lottos lottos = new Lottos(5000, 5000 / 1000, oneLottoList); + ArrayList ansLottoNumbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6)); + + RankCalculator rankCalculator = new RankCalculator(new OneLotto(ansLottoNumbers)); + rankCalculator.allLottoRank(lottos); + + assertThat(rankCalculator.rateOfReturn(lottos)).isEqualTo(40031100); + } +} \ No newline at end of file From 15b60031f65a56dab93633651905e0c8ed9dcab3 Mon Sep 17 00:00:00 2001 From: Sumin Date: Mon, 6 May 2024 20:18:20 +0900 Subject: [PATCH 2/2] Final --- src/main/java/controller/LottoMain.java | 52 ++++++++++++++ src/main/java/model/CorrectNum.java | 34 ++++++++++ src/main/java/model/Lottos.java | 11 +++ src/main/java/model/OneLotto.java | 30 +++++++++ src/main/java/model/RandomNumGenerator.java | 1 - src/main/java/model/RankCalculator.java | 37 +++++----- src/main/java/view/LottoInput.java | 53 +++++++++++++++ src/main/java/view/Print.java | 75 +++++++++++++++++++++ src/test/java/model/RankCalculatorTest.java | 4 +- 9 files changed, 276 insertions(+), 21 deletions(-) create mode 100644 src/main/java/controller/LottoMain.java create mode 100644 src/main/java/model/CorrectNum.java create mode 100644 src/main/java/view/LottoInput.java create mode 100644 src/main/java/view/Print.java diff --git a/src/main/java/controller/LottoMain.java b/src/main/java/controller/LottoMain.java new file mode 100644 index 00000000..0a9b835f --- /dev/null +++ b/src/main/java/controller/LottoMain.java @@ -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; + int balance = LottoInput.inputBalance(); + + RandomNumGenerator randomNumGenerator = new RandomNumGenerator(); + List myLottos = new ArrayList<>(); + + manual = LottoInput.manual(); + automatic = balance / 1000 - manual; + + System.out.println("수동으로 구매할 번호를 입력해 주세요."); + for (int i = 0; i < manual; i++) { + List 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 lottoAnswer = LottoInput.lottoAnswer(); + RankCalculator rankCalculator = new RankCalculator(lottoAnswer); + + rankCalculator.allLottoRank(lottos); + + LottoInput.bonusAnswer(lottos); + + Print.winning(rankCalculator, lottos); + } +} diff --git a/src/main/java/model/CorrectNum.java b/src/main/java/model/CorrectNum.java new file mode 100644 index 00000000..7e9bf005 --- /dev/null +++ b/src/main/java/model/CorrectNum.java @@ -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 getList() { + return List.of(CorrectNum.values()); //{ZERO,ONE, ... + } +} diff --git a/src/main/java/model/Lottos.java b/src/main/java/model/Lottos.java index 67d1d716..644940c1 100644 --- a/src/main/java/model/Lottos.java +++ b/src/main/java/model/Lottos.java @@ -6,6 +6,7 @@ public class Lottos { private final int balance; private final int count; private final List myLottos; + private int bonusBall; public Lottos(int balance, int count, List myLottos) { this.balance = balance; @@ -21,5 +22,15 @@ public List getMyLottos() { return myLottos; } + public int getCount() { + return count; + } + + public int getBonusBall() { + return bonusBall; + } + public void setBonusBall(int bonusBall) { + this.bonusBall = bonusBall; + } } diff --git a/src/main/java/model/OneLotto.java b/src/main/java/model/OneLotto.java index e59fafe2..214699a0 100644 --- a/src/main/java/model/OneLotto.java +++ b/src/main/java/model/OneLotto.java @@ -4,6 +4,9 @@ public class OneLotto { private final List lottoNumbers; + private boolean bonusCheck; + private CorrectNum correctCnt; + private int correctNum; public OneLotto(List lottoNumbers) { this.lottoNumbers = lottoNumbers; @@ -13,4 +16,31 @@ public List 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 nums = CorrectNum.getList(); + + for (int i = 0; i < nums.size(); i++) { + if (nums.get(i).getNum() == correctNum && !bonusCheck) { + correctCnt = nums.get(i); //왜 설정이 안되지? + break; + } + } + } } diff --git a/src/main/java/model/RandomNumGenerator.java b/src/main/java/model/RandomNumGenerator.java index 9686876b..eb688864 100644 --- a/src/main/java/model/RandomNumGenerator.java +++ b/src/main/java/model/RandomNumGenerator.java @@ -18,5 +18,4 @@ public List randomNumGenerate() { return numList.subList(0, 6); } - } diff --git a/src/main/java/model/RankCalculator.java b/src/main/java/model/RankCalculator.java index 1e9668d7..f0fb8b73 100644 --- a/src/main/java/model/RankCalculator.java +++ b/src/main/java/model/RankCalculator.java @@ -1,25 +1,21 @@ package model; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class RankCalculator { - private final OneLotto ans; - List correctNum = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0)); + private final List ans; - public RankCalculator(OneLotto ans) { + public RankCalculator(List ans) { this.ans = ans; } private void eachLottoRank(OneLotto oneLotto) { List numbers = oneLotto.getLottoNumbers(); //로또 한장 - List ansNumbers = ans.getLottoNumbers(); //정답 로또 번호 long count = numbers.stream() - .filter(ansNumbers::contains) + .filter(ans::contains) .count(); //몇개맞지? - correctNum.set((int) (count - 1), correctNum.get((int) count - 1) + 1); + oneLotto.setCorrectNum((int) count); } public void allLottoRank(Lottos lottos) { @@ -29,20 +25,25 @@ public void allLottoRank(Lottos lottos) { } } - private long calculateTotal(List rankList) { - List money = new ArrayList<> - (Arrays.asList(0, 0, 5000, 50000, 1500000, 2000000000)); + public long calculateTotal(Lottos lottos) { long total = 0; - for (int i = 2; i < rankList.size(); i++) { - total += ((long) rankList.get(i) * money.get(i)); + List 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 double rateOfReturn(Lottos lottos) { - int balance = lottos.getBalance(); - long total = calculateTotal(correctNum); - - return (double) (total / balance * 100); + public String rateOfReturn(int balance, long total) { + String result = String.format("%.2f", (double) total / (double) balance); + return result; } + } diff --git a/src/main/java/view/LottoInput.java b/src/main/java/view/LottoInput.java new file mode 100644 index 00000000..95994247 --- /dev/null +++ b/src/main/java/view/LottoInput.java @@ -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); + System.out.println("구입금액을 입력해 주세요."); + return sc.nextInt(); + } + + public static List lottoAnswer() { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + Scanner sc = new Scanner(System.in); + String str = sc.nextLine(); + String[] ans = str.split(", "); + List result = new ArrayList<>(); + for (int i = 0; i < ans.length; i++) { + result.add(Integer.parseInt(ans[i])); + } + 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 manualLottoInput() { + Scanner sc = new Scanner(System.in); + String str = sc.nextLine(); + String[] ans = str.split(", "); + List result = new ArrayList<>(); + for (int i = 0; i < ans.length; i++) { + result.add(Integer.parseInt(ans[i])); + } + return result; + } +} diff --git a/src/main/java/view/Print.java b/src/main/java/view/Print.java new file mode 100644 index 00000000..048217c9 --- /dev/null +++ b/src/main/java/view/Print.java @@ -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 oneLottoList = lottos.getMyLottos(); + for (OneLotto oneLotto : oneLottoList) { + System.out.println(makeLottoStr(oneLotto)); + } + } + + private static String makeLottoStr(OneLotto oneLotto) { + StringBuffer sb = new StringBuffer(); + sb.append("["); + + List 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 oneLottos = lottos.getMyLottos(); + List 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 oneLottos, CorrectNum correctNum) { + int cnt = 0; + for (OneLotto oneLotto : oneLottos) { + if (correctNum == oneLotto.getCorrectCnt()) + cnt++; + } + return cnt; + } +} diff --git a/src/test/java/model/RankCalculatorTest.java b/src/test/java/model/RankCalculatorTest.java index 76f1f77c..3d28ee64 100644 --- a/src/test/java/model/RankCalculatorTest.java +++ b/src/test/java/model/RankCalculatorTest.java @@ -24,7 +24,7 @@ void oneLottoCorrectCountTest() { RankCalculator rankCalculator = new RankCalculator(new OneLotto(ansLottoNumbers)); rankCalculator.allLottoRank(lottos); - assertThat(rankCalculator.correctNum.get(3)).isEqualTo(1); + assertThat(rankCalculator.getCorrectNum().get(3)).isEqualTo(1); } @DisplayName("여러 장 로또의 총 수익금과 수익률이 올바른지 확인한다.") @@ -48,6 +48,6 @@ void allLottoCorrectCountTest() { RankCalculator rankCalculator = new RankCalculator(new OneLotto(ansLottoNumbers)); rankCalculator.allLottoRank(lottos); - assertThat(rankCalculator.rateOfReturn(lottos)).isEqualTo(40031100); + assertThat(rankCalculator.rateOfReturn(lottos.getBalance())).isEqualTo(40031100); } } \ No newline at end of file