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 new file mode 100644 index 00000000..644940c1 --- /dev/null +++ b/src/main/java/model/Lottos.java @@ -0,0 +1,36 @@ +package model; + +import java.util.List; + +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; + this.count = count; + this.myLottos = myLottos; + } + + public int getBalance() { + return balance; + } + + 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 new file mode 100644 index 00000000..214699a0 --- /dev/null +++ b/src/main/java/model/OneLotto.java @@ -0,0 +1,46 @@ +package model; + +import java.util.List; + +public class OneLotto { + private final List lottoNumbers; + private boolean bonusCheck; + private CorrectNum correctCnt; + private int correctNum; + + public OneLotto(List lottoNumbers) { + this.lottoNumbers = lottoNumbers; + } + + 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 new file mode 100644 index 00000000..eb688864 --- /dev/null +++ b/src/main/java/model/RandomNumGenerator.java @@ -0,0 +1,21 @@ +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..f0fb8b73 --- /dev/null +++ b/src/main/java/model/RankCalculator.java @@ -0,0 +1,49 @@ +package model; + +import java.util.List; + +public class RankCalculator { + + private final List ans; + + public RankCalculator(List ans) { + this.ans = ans; + } + + private void eachLottoRank(OneLotto oneLotto) { + List numbers = oneLotto.getLottoNumbers(); //로또 한장 + long count = numbers.stream() + .filter(ans::contains) + .count(); //몇개맞지? + oneLotto.setCorrectNum((int) count); + } + + public void allLottoRank(Lottos lottos) { + List oneLottoList = lottos.getMyLottos(); + for (OneLotto oneLotto : oneLottoList) { + eachLottoRank(oneLotto); + } + } + + public long calculateTotal(Lottos lottos) { + long total = 0; + 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 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 new file mode 100644 index 00000000..3d28ee64 --- /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.getCorrectNum().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.getBalance())).isEqualTo(40031100); + } +} \ No newline at end of file