-
Notifications
You must be signed in to change notification settings - Fork 10
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
[로또 게임] 서예원 2차 과제 제출합니다. #14
base: main
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,10 @@ | ||
#수정 사항 | ||
1. LottoController에 run()을 만들어서 main에서 기능을 직접 실행하지 않도록 수정해보았습니다 | ||
2. UserView,LottoCalculator등의 생성만 Application에서 해보려고 했습니다. | ||
3. 구매수량만큼 로또를 생성하는 부분을 더 자세하게 함수로 분리했습니다. | ||
4. 필요한 객체 선언,생성을 미리 한번에 작성하였습니다. | ||
5. 사용자 입출력관리 클래스 이름을 UserService -> UserView로 수정하였습니다. | ||
6. LottoCalculator에서 들여쓰기 3개이상인 곳의 메소드를 더 자세하게 분리하고,count매개변수와 enum을 쓰도록 해보았습니다 | ||
7. 상금값도 enum을 쓰도록 고쳐보았습니다. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Lotto> 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); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package service; | ||
|
||
public class Enum { | ||
public enum MatchingResult{ | ||
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. ENUM 맵이라는 자료구조를 한번 공부해보세요~~ |
||
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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> numbers = lotto.getNumber(); | ||
boolean matchingBonusNumber = false; | ||
for (int j = 0; j < numbers.size(); j++) { | ||
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. contains() 함수를 공부해보세요~~ |
||
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<Lotto> 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) { | ||
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 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> generateLottoNumber(){ | ||
List<Integer>numbers = Randoms.pickUniqueNumbersInRange(1,45,6); | ||
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. 저라면 randomNumber도 한번 검증 해줄듯~~ㅋㅋ |
||
return numbers; | ||
} | ||
|
||
public List<Lotto> generateLottos(int quantity) { | ||
List<Lotto> lottos = new ArrayList<>(); | ||
for (int i = 0; i < quantity; i++) { | ||
List<Integer> numbers = generateLottoNumber(); // generateLottoNumber 메서드 호출 | ||
Lotto lotto = new Lotto(numbers); | ||
lottos.add(lotto); | ||
} | ||
return lottos; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package view; | ||
import model.Lotto; | ||
import java.util.List; | ||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
//사용자 입력 출력 관련된 거 | ||
public class UserView { | ||
|
||
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 void printStartMessage(){ | ||
System.out.println("구입금액을 입력해주세요."); | ||
} | ||
|
||
//사용자로부터 구입할 로또 개수를 입력받는다 | ||
public int getLottoQuantity(){ | ||
String input = Console.readLine(); | ||
return Integer.parseInt(input)/1000; //인트로 변환, 1000원에 1장 | ||
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 void printGeneratedLotto(List<Lotto>lottos){ | ||
int lottoQuantity= lottos.size(); | ||
System.out.println(lottoQuantity+"개를 구매했습니다."); | ||
for(int i=0;i<lottos.size();i++){ | ||
//로또 한개로 풀기 | ||
Lotto lotto= lottos.get(i); | ||
//로또한개의 번호들 | ||
List<Integer>numbers= lotto.getNumber(); | ||
|
||
//각 번호들 문자열로 변환하기 | ||
String string = "["; | ||
for(int j=0;j<numbers.size();j++){ | ||
string += numbers.get(j); | ||
if(j<numbers.size()-1 ) | ||
string += ", "; | ||
|
||
} | ||
string+="]"; | ||
|
||
System.out.println(string); | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
|
||
//당첨번호 입력 받을거임 문자열->숫자를..? 입력: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<stringNumbers.length;i++){ | ||
numbers[i]= Integer.parseInt(stringNumbers[i]); | ||
} | ||
return numbers; //숫자 1 2 3 4 5 6 | ||
} | ||
|
||
public int getBonusNumber(){ | ||
System.out.println("보너스 번호를 입력해 주세요."); | ||
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원) - "+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]+"개"); | ||
System.out.println("총 수익률은 "+profitRate+"%입니다."); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package lotto; | ||
|
||
import model.Lotto; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
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.
의존성 주입이군요!! 이게 왜 의존성 주입인지 공부해봅시다!