diff --git a/docs/README.md b/docs/README.md index e69de29bb2d..a5e32df93b2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,10 @@ +#수정 사항 +1. LottoController에 run()을 만들어서 main에서 기능을 직접 실행하지 않도록 수정해보았습니다 +2. UserView,LottoCalculator등의 생성만 Application에서 해보려고 했습니다. +3. 구매수량만큼 로또를 생성하는 부분을 더 자세하게 함수로 분리했습니다. +4. 필요한 객체 선언,생성을 미리 한번에 작성하였습니다. +5. 사용자 입출력관리 클래스 이름을 UserService -> UserView로 수정하였습니다. +6. LottoCalculator에서 들여쓰기 3개이상인 곳의 메소드를 더 자세하게 분리하고,count매개변수와 enum을 쓰도록 해보았습니다 +7. 상금값도 enum을 쓰도록 고쳐보았습니다. + + diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java new file mode 100644 index 00000000000..5a1aadcf639 --- /dev/null +++ b/src/main/java/controller/LottoController.java @@ -0,0 +1,50 @@ +package controller; + +import model.Lotto; +import service.LottoCalculator; +import service.LottoGenerator; +import view.UserView; + +import java.util.ArrayList; +import java.util.List; + +public class LottoController { + private final UserView userView; + private final LottoCalculator lottoCalculator; + private final LottoGenerator lottoGenerator; + + public LottoController(UserView userView, LottoCalculator lottoCalculator, LottoGenerator lottoGenerator) { + this.userView = userView; + this.lottoCalculator = lottoCalculator; + this.lottoGenerator = lottoGenerator; + } + + public void run() { + userView.printStartMessage(); // 시작 메시지 출력 + + // 사용자로부터 구매할 로또 개수 입력받기 + int lottoQuantity = userView.getLottoQuantity(); + + // 구매한 로또 리스트 생성 + List lottos = lottoGenerator.generateLottos(lottoQuantity); + userView.printGeneratedLotto(lottos); // 생성된 로또 번호 출력 + + // 당첨 번호와 보너스 번호 입력 받기 + int[] winningNumbers = userView.getLotteryWinningNumber(); + int bonusNumber = userView.getBonusNumber(); + + // 맞춘 각 등수별 개수 계산 + int[] matchingCounts = lottoCalculator.matchingWinningNumber(winningNumbers, bonusNumber, lottos); + + // 총 상금 계산 + int totalPrize = lottoCalculator.calculateTotalPrizeMoney(matchingCounts); + + // 수익률 계산 + double profitRate = lottoCalculator.calculateProfitRate(lottoQuantity, totalPrize); + + // 결과 출력 + userView.printResult(matchingCounts, profitRate); + } + + +} diff --git a/src/main/java/game/Application.java b/src/main/java/game/Application.java new file mode 100644 index 00000000000..d0e17da8416 --- /dev/null +++ b/src/main/java/game/Application.java @@ -0,0 +1,17 @@ +package game; + +import controller.LottoController; +import service.LottoCalculator; +import service.LottoGenerator; +import view.UserView; + +public class Application { + public static void main(String[] args) { + UserView userView = new UserView(); + LottoCalculator lottoCalculator = new LottoCalculator(); + LottoGenerator lottoGenerator = new LottoGenerator(); + LottoController lottoController = new LottoController(userView, lottoCalculator, lottoGenerator); + + lottoController.run(); + } +} diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java deleted file mode 100644 index d190922ba44..00000000000 --- a/src/main/java/lotto/Application.java +++ /dev/null @@ -1,7 +0,0 @@ -package lotto; - -public class Application { - public static void main(String[] args) { - // TODO: 프로그램 구현 - } -} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/model/Lotto.java similarity index 81% rename from src/main/java/lotto/Lotto.java rename to src/main/java/model/Lotto.java index 519793d1f73..ce9a646b9b3 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -1,7 +1,8 @@ -package lotto; +package model; import java.util.List; + public class Lotto { private final List numbers; @@ -16,5 +17,9 @@ private void validate(List numbers) { } } + public List getNumber(){ + return numbers; + } + // TODO: 추가 기능 구현 } diff --git a/src/main/java/service/Enum.java b/src/main/java/service/Enum.java new file mode 100644 index 00000000000..994da839525 --- /dev/null +++ b/src/main/java/service/Enum.java @@ -0,0 +1,38 @@ +package service; + +public class Enum { + public enum MatchingResult{ + FIRST(0), + SECOND(1), + THIRD(2), + FOURTH(3), + FIFTH(4); + + private final int index; + MatchingResult(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + } + + public enum Prize { + THIRD(5000), + FOURTH(50000), + FIFTH(1500000), + FIFTH_BONUS(30000000), + FIRST(2000000000); + + private final int amount; + + Prize(int amount) { + this.amount = amount; + } + + public int getAmount() { + return amount; + } + } +} diff --git a/src/main/java/service/LottoCalculator.java b/src/main/java/service/LottoCalculator.java new file mode 100644 index 00000000000..a2611ed9bab --- /dev/null +++ b/src/main/java/service/LottoCalculator.java @@ -0,0 +1,90 @@ +package service; + +import model.Lotto; + +import java.util.List; + +public class LottoCalculator { + + public int compareWinningNumber(int[] winningNumber, int bonusNumber, Lotto lotto) { + int count = 0; + List numbers = lotto.getNumber(); + boolean matchingBonusNumber = false; + for (int j = 0; j < numbers.size(); j++) { + int number = numbers.get(j); + + for (int k = 0; k < winningNumber.length; k++) { + if (number == winningNumber[k]) { + count++; + } + } + + if (number == bonusNumber) { + matchingBonusNumber= true; + } + } + + return count; + } + + public void updateMatchingResult(int[] matchingResult, int count, boolean matchingBonusNumber) { + if (count == 6) { + matchingResult[Enum.MatchingResult.FIRST.getIndex()]++; + } + if (count == 5 && matchingBonusNumber) { + matchingResult[Enum.MatchingResult.SECOND.getIndex()]++; + } + if (count == 5 && !matchingBonusNumber) { + matchingResult[Enum.MatchingResult.THIRD.getIndex()]++; + } + if (count == 4) { + matchingResult[Enum.MatchingResult.FOURTH.getIndex()]++; + } + if (count == 3) { + matchingResult[Enum.MatchingResult.FIFTH.getIndex()]++; + } + } + + public int[] matchingWinningNumber(int[] winningNumber, int bonusNumber, List lottos) { + int[] matchingResult = new int[5]; + + for (int i = 0; i < lottos.size(); i++) { + Lotto lotto = lottos.get(i); + int count = compareWinningNumber(winningNumber, bonusNumber, lotto); + boolean matchingBonusNumber = lotto.getNumber().contains(bonusNumber); + + updateMatchingResult(matchingResult, count, matchingBonusNumber); + } + + return matchingResult; + } + + public int calculateTotalPrizeMoney(int[] matchingResult) { + int totalPrizeMoney = 0; + + if (matchingResult[Enum.MatchingResult.FIRST.getIndex()] != 0) { // 6개 일치 + totalPrizeMoney += matchingResult[Enum.MatchingResult.FIRST.getIndex()] * Enum.Prize.FIRST.getAmount(); + } + if (matchingResult[Enum.MatchingResult.SECOND.getIndex()] != 0) { // 5개 + 보너스 + totalPrizeMoney += matchingResult[Enum.MatchingResult.SECOND.getIndex()] * Enum.Prize.FIFTH_BONUS.getAmount(); + } + if (matchingResult[Enum.MatchingResult.THIRD.getIndex()] != 0) { // 5개 일치 + totalPrizeMoney += matchingResult[Enum.MatchingResult.THIRD.getIndex()] * Enum.Prize.FIFTH.getAmount(); + } + if (matchingResult[Enum.MatchingResult.FOURTH.getIndex()] != 0) { // 4개 일치 + totalPrizeMoney += matchingResult[Enum.MatchingResult.FOURTH.getIndex()] * Enum.Prize.FOURTH.getAmount(); + } + if (matchingResult[Enum.MatchingResult.FIFTH.getIndex()] != 0) { // 3개 일치 + totalPrizeMoney += matchingResult[Enum.MatchingResult.FIFTH.getIndex()] * Enum.Prize.THIRD.getAmount(); + } + + return totalPrizeMoney; + } + + public double calculateProfitRate(int lottoQuantity, int totalPrizeMoney) { + double cost = lottoQuantity * 1000.0; + double profitRate = (totalPrizeMoney - cost) / cost * 100.0; + + return profitRate; + } +} diff --git a/src/main/java/service/LottoGenerator.java b/src/main/java/service/LottoGenerator.java new file mode 100644 index 00000000000..47731aa91e3 --- /dev/null +++ b/src/main/java/service/LottoGenerator.java @@ -0,0 +1,26 @@ +package service; + +import camp.nextstep.edu.missionutils.Randoms; +import model.Lotto; + +import java.util.ArrayList; +import java.util.List; + +public class LottoGenerator { + public List generateLottoNumber(){ + Listnumbers = Randoms.pickUniqueNumbersInRange(1,45,6); + return numbers; + } + + public List generateLottos(int quantity) { + List lottos = new ArrayList<>(); + for (int i = 0; i < quantity; i++) { + List numbers = generateLottoNumber(); // generateLottoNumber 메서드 호출 + Lotto lotto = new Lotto(numbers); + lottos.add(lotto); + } + return lottos; + } + + +} diff --git a/src/main/java/view/UserView.java b/src/main/java/view/UserView.java new file mode 100644 index 00000000000..296be771b07 --- /dev/null +++ b/src/main/java/view/UserView.java @@ -0,0 +1,78 @@ +package view; +import model.Lotto; +import java.util.List; +import camp.nextstep.edu.missionutils.Console; + +//사용자 입력 출력 관련된 거 +public class UserView { + + //게임 시작 메세지를 출력한다 + public void printStartMessage(){ + System.out.println("구입금액을 입력해주세요."); + } + + //사용자로부터 구입할 로또 개수를 입력받는다 + public int getLottoQuantity(){ + String input = Console.readLine(); + return Integer.parseInt(input)/1000; //인트로 변환, 1000원에 1장 + } + + //생성된 로또 번호 리스트를 출력한다 + public void printGeneratedLotto(Listlottos){ + int lottoQuantity= lottos.size(); + System.out.println(lottoQuantity+"개를 구매했습니다."); + for(int i=0;inumbers= lotto.getNumber(); + + //각 번호들 문자열로 변환하기 + String string = "["; + for(int j=0;j숫자를..? 입력:1,2,3,4,5,6 + public int[] getLotteryWinningNumber(){ + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + String[]stringNumbers = input.split(","); + int[]numbers =new int[stringNumbers.length]; + for(int i=0;i