From 5fe6c97d5ff687f6237f570e3c96da206661a105 Mon Sep 17 00:00:00 2001 From: Yewon2ee Date: Sun, 14 Jul 2024 17:51:18 +0900 Subject: [PATCH 1/2] upload --- src/main/java/game/Application.java | 67 ++++++++++++++ src/main/java/game/UserService.java | 76 ++++++++++++++++ src/main/java/lotto/Application.java | 7 -- src/main/java/lotto/Lotto.java | 5 ++ src/main/java/lotto/LottoCalculator.java | 108 +++++++++++++++++++++++ src/main/java/lotto/LottoGenerator.java | 13 +++ src/test/java/lotto/ApplicationTest.java | 1 + 7 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 src/main/java/game/Application.java create mode 100644 src/main/java/game/UserService.java delete mode 100644 src/main/java/lotto/Application.java create mode 100644 src/main/java/lotto/LottoCalculator.java create mode 100644 src/main/java/lotto/LottoGenerator.java diff --git a/src/main/java/game/Application.java b/src/main/java/game/Application.java new file mode 100644 index 00000000000..6ae77f9bf2f --- /dev/null +++ b/src/main/java/game/Application.java @@ -0,0 +1,67 @@ +package game; + +import lotto.Lotto; +import lotto.LottoCalculator; +import lotto.LottoGenerator; +import java.util.List; +import java.util.ArrayList; + + +//게임 실행해야하는 곳 +public class Application { + public static void main(String[] args) { + UserService userService = new UserService(); + //구입금액 입력해달라고 프린트 + userService.printStartMessage(); + + //구매수량 입력받기 + int lottoQuantity= userService.getLottoQuantity(); + + + //lottos리스트는 lotto를 객체로 가지고 + //각 lotto는 객체들은 여섯개 숫자 가지고 있는 로또번호(numbers)리스트를 가짐 + + + //List<>는 동적이고 객체를 저장하는 느낌.. + //int[]는 크기 고정이고 숫자를 저장 + + + Listlottos = new ArrayList<>();//각 로또 번호 저장할 배열만들기 + //구매수량 만큼 로또 생성 + LottoGenerator lottoGenerator = new LottoGenerator(); + for(int i=0;i numbers = lottoGenerator.generateLottoNumber(); + Lotto lotto = new Lotto(numbers); + lottos.add(lotto); + } + + + + //만들어진 로또번호 출력 + userService.printGeneratedLotto(lottos); + + //로또 당첨 번호 입력 받기 + int[] LotteryWinningNumber = userService.getLotteryWinningNumber(); + + + + + + + //보너스 번호 입력 받기 + int bonusNumber= userService.getBonusNumber(); + + //당첨결과 계산 + LottoCalculator lottoCalculator= new LottoCalculator(); + int[] matchingResult= lottoCalculator.matchingWinningNumber(LotteryWinningNumber,bonusNumber,lottos); + int totalPrizeMoney = lottoCalculator.calculateTotalPrizeMoney(matchingResult); + double profitRate = lottoCalculator.calculateProfitRate(lottoQuantity,totalPrizeMoney); + + + + + //당첨 결과 출력 + userService.printResult(matchingResult,profitRate); + + } +} diff --git a/src/main/java/game/UserService.java b/src/main/java/game/UserService.java new file mode 100644 index 00000000000..dc246cb5e7d --- /dev/null +++ b/src/main/java/game/UserService.java @@ -0,0 +1,76 @@ +package game; +import lotto.Lotto; +import lotto.LottoCalculator; +import java.util.ArrayList; +import java.util.List; +import java.util.ArrayList; +import java.sql.SQLOutput; +import camp.nextstep.edu.missionutils.Console; + +//사용자 입력 출력 관련된 거 +public class UserService { + 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 numbers; @@ -16,5 +17,9 @@ private void validate(List numbers) { } } + public List getNumber(){ + return numbers; + } + // TODO: 추가 기능 구현 } diff --git a/src/main/java/lotto/LottoCalculator.java b/src/main/java/lotto/LottoCalculator.java new file mode 100644 index 00000000000..36c519b2696 --- /dev/null +++ b/src/main/java/lotto/LottoCalculator.java @@ -0,0 +1,108 @@ +package lotto; +import game.Application; +import game.UserService; +import java.util.List; + + +public class LottoCalculator { + + 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 = 0; + boolean matchingBonusNumber = false; + List numbers = lotto.getNumber(); + + 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; + } + } + + // 6개 일치 + if (count == 6) { + matchingResult[0]++; + continue; + } + + // 5개 + 보너스 + if (count == 5 && matchingBonusNumber) { + matchingResult[1]++; + continue; + } + + // 5개 일치 + if (count == 5) { + matchingResult[2]++; + continue; + } + + // 4개 일치 + if (count == 4) { + matchingResult[3]++; + continue; + } + + // 3개 일치 + if (count == 3) { + matchingResult[4]++; + } + } + + return matchingResult; + } + + public int calculateTotalPrizeMoney(int[]matchingResult){ + int totalPrizeMoney=0; + if(matchingResult[4]!=0){ //3개 일치 오천원 + for(int i=0;igenerateLottoNumber(){ + Listnumbers = Randoms.pickUniqueNumbersInRange(1,45,6); + return numbers; + } + +} diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f522..07183a5f5f9 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -1,6 +1,7 @@ package lotto; import camp.nextstep.edu.missionutils.test.NsTest; +import game.Application; import org.junit.jupiter.api.Test; import java.util.List; From 5a916fcb13ca7f376d2f4317c6df14fef74ffed2 Mon Sep 17 00:00:00 2001 From: Yewon2ee Date: Wed, 17 Jul 2024 01:35:39 +0900 Subject: [PATCH 2/2] upload --- docs/README.md | 10 ++ src/main/java/controller/LottoController.java | 50 ++++++++ src/main/java/game/Application.java | 66 ++--------- src/main/java/lotto/LottoCalculator.java | 108 ------------------ src/main/java/lotto/LottoGenerator.java | 13 --- src/main/java/{lotto => model}/Lotto.java | 2 +- src/main/java/service/Enum.java | 38 ++++++ src/main/java/service/LottoCalculator.java | 90 +++++++++++++++ src/main/java/service/LottoGenerator.java | 26 +++++ .../UserService.java => view/UserView.java} | 20 ++-- src/test/java/lotto/LottoTest.java | 1 + 11 files changed, 235 insertions(+), 189 deletions(-) create mode 100644 src/main/java/controller/LottoController.java delete mode 100644 src/main/java/lotto/LottoCalculator.java delete mode 100644 src/main/java/lotto/LottoGenerator.java rename src/main/java/{lotto => model}/Lotto.java (96%) create mode 100644 src/main/java/service/Enum.java create mode 100644 src/main/java/service/LottoCalculator.java create mode 100644 src/main/java/service/LottoGenerator.java rename src/main/java/{game/UserService.java => view/UserView.java} (87%) 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 index 6ae77f9bf2f..d0e17da8416 100644 --- a/src/main/java/game/Application.java +++ b/src/main/java/game/Application.java @@ -1,67 +1,17 @@ package game; -import lotto.Lotto; -import lotto.LottoCalculator; -import lotto.LottoGenerator; -import java.util.List; -import java.util.ArrayList; +import controller.LottoController; +import service.LottoCalculator; +import service.LottoGenerator; +import view.UserView; - -//게임 실행해야하는 곳 public class Application { public static void main(String[] args) { - UserService userService = new UserService(); - //구입금액 입력해달라고 프린트 - userService.printStartMessage(); - - //구매수량 입력받기 - int lottoQuantity= userService.getLottoQuantity(); - - - //lottos리스트는 lotto를 객체로 가지고 - //각 lotto는 객체들은 여섯개 숫자 가지고 있는 로또번호(numbers)리스트를 가짐 - - - //List<>는 동적이고 객체를 저장하는 느낌.. - //int[]는 크기 고정이고 숫자를 저장 - - - Listlottos = new ArrayList<>();//각 로또 번호 저장할 배열만들기 - //구매수량 만큼 로또 생성 + UserView userView = new UserView(); + LottoCalculator lottoCalculator = new LottoCalculator(); LottoGenerator lottoGenerator = new LottoGenerator(); - for(int i=0;i numbers = lottoGenerator.generateLottoNumber(); - Lotto lotto = new Lotto(numbers); - lottos.add(lotto); - } - - - - //만들어진 로또번호 출력 - userService.printGeneratedLotto(lottos); - - //로또 당첨 번호 입력 받기 - int[] LotteryWinningNumber = userService.getLotteryWinningNumber(); - - - - - - - //보너스 번호 입력 받기 - int bonusNumber= userService.getBonusNumber(); - - //당첨결과 계산 - LottoCalculator lottoCalculator= new LottoCalculator(); - int[] matchingResult= lottoCalculator.matchingWinningNumber(LotteryWinningNumber,bonusNumber,lottos); - int totalPrizeMoney = lottoCalculator.calculateTotalPrizeMoney(matchingResult); - double profitRate = lottoCalculator.calculateProfitRate(lottoQuantity,totalPrizeMoney); - - - - - //당첨 결과 출력 - userService.printResult(matchingResult,profitRate); + LottoController lottoController = new LottoController(userView, lottoCalculator, lottoGenerator); + lottoController.run(); } } diff --git a/src/main/java/lotto/LottoCalculator.java b/src/main/java/lotto/LottoCalculator.java deleted file mode 100644 index 36c519b2696..00000000000 --- a/src/main/java/lotto/LottoCalculator.java +++ /dev/null @@ -1,108 +0,0 @@ -package lotto; -import game.Application; -import game.UserService; -import java.util.List; - - -public class LottoCalculator { - - 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 = 0; - boolean matchingBonusNumber = false; - List numbers = lotto.getNumber(); - - 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; - } - } - - // 6개 일치 - if (count == 6) { - matchingResult[0]++; - continue; - } - - // 5개 + 보너스 - if (count == 5 && matchingBonusNumber) { - matchingResult[1]++; - continue; - } - - // 5개 일치 - if (count == 5) { - matchingResult[2]++; - continue; - } - - // 4개 일치 - if (count == 4) { - matchingResult[3]++; - continue; - } - - // 3개 일치 - if (count == 3) { - matchingResult[4]++; - } - } - - return matchingResult; - } - - public int calculateTotalPrizeMoney(int[]matchingResult){ - int totalPrizeMoney=0; - if(matchingResult[4]!=0){ //3개 일치 오천원 - for(int i=0;igenerateLottoNumber(){ - Listnumbers = Randoms.pickUniqueNumbersInRange(1,45,6); - return numbers; - } - -} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/model/Lotto.java similarity index 96% rename from src/main/java/lotto/Lotto.java rename to src/main/java/model/Lotto.java index 05617daad37..ce9a646b9b3 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -1,4 +1,4 @@ -package lotto; +package model; import java.util.List; 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/game/UserService.java b/src/main/java/view/UserView.java similarity index 87% rename from src/main/java/game/UserService.java rename to src/main/java/view/UserView.java index dc246cb5e7d..296be771b07 100644 --- a/src/main/java/game/UserService.java +++ b/src/main/java/view/UserView.java @@ -1,24 +1,23 @@ -package game; -import lotto.Lotto; -import lotto.LottoCalculator; -import java.util.ArrayList; +package view; +import model.Lotto; import java.util.List; -import java.util.ArrayList; -import java.sql.SQLOutput; import camp.nextstep.edu.missionutils.Console; //사용자 입력 출력 관련된 거 -public class UserService { +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+"개를 구매했습니다."); @@ -45,6 +44,7 @@ public void printGeneratedLotto(Listlottos){ } + //당첨번호 입력 받을거임 문자열->숫자를..? 입력:1,2,3,4,5,6 public int[] getLotteryWinningNumber(){ System.out.println("당첨 번호를 입력해 주세요."); @@ -62,10 +62,12 @@ public int getBonusNumber(){ String input = Console.readLine(); return Integer.parseInt(input); } + + //게임의 최종 결과를 출력함 public void printResult(int []matchingResult,double profitRate){ System.out.println("당첨 통계\n----"); System.out.println("3개 일치 (5,000원) - "+matchingResult[4]+"개"); - System.out.println("4개 일치 (50,000원) -5개 일치 (1,500,000원) - "+matchingResult[3]+"개"); + System.out.println("4개 일치 (50,000원) - "+matchingResult[3]+"개"); System.out.println("5개 일치 (1,500,000원) - "+matchingResult[2]+"개"); System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+matchingResult[1]+"개"); System.out.println("6개 일치 (2,000,000,000원) - "+matchingResult[0]+"개"); diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 9f5dfe7eb83..1ec8b8c6fdd 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import model.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test;