-
Couldn't load subscription status.
- Fork 92
[로또 미션] 신예린 미션 제출합니다. #19
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
base: nyeroni
Are you sure you want to change the base?
Changes from 21 commits
958541e
8627586
c086150
d435b8e
07b7bc7
5ed6fea
2fd7c31
ebbd2b3
2a199f1
2d59e3d
6b2269c
7052bdd
4375301
e2860da
49773c8
9c28320
2fe5105
4595b55
561a3b3
0e387fb
cfe53f4
ef9bdab
3d69130
f1804d5
652c085
7beec8d
48b8f00
04d0f96
2fd040c
4f9977c
2f51b48
36e8947
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,48 @@ | ||
| ## 🔨 구현 기능 목록 | ||
|
|
||
| ### 입력 | ||
|
|
||
| - 구입 금액 입력 | ||
| - 한 장당 1000원 | ||
| - 1000원으로 나누어 떨어지지 않으면 예외 처리 | ||
| - 수동으로 구매할 로또 수 입력 | ||
| - 숫자 외의 입력은 모두 예외처리 | ||
| - 수동으로 구매할 로또 번호 입력 | ||
| - 번호는 쉼표(,)로 구분 | ||
| - 숫자와 쉼표(,) 외의 입력은 모두 예외 처리 | ||
| - 6개가 되지 않는다면 예외 처리 | ||
| - 당첨 번호 입력 | ||
| - 번호는 쉼표(,)로 구분 | ||
| - 숫자와 쉼표(,) 외의 입력은 모두 예외 처리 | ||
| - 6개가 되지 않는다면 예외 처리 | ||
| - 보너스 번호 입력 | ||
| - 숫자 외의 입력은 모두 예외처리 | ||
|
|
||
|
|
||
| ### 출력 | ||
|
|
||
|
|
||
| - 수동 로또와 자동 로또 발행 | ||
| - 오름차순으로 정렬해서 보여줌 | ||
| - 중복되지 않는 6개 숫자 | ||
| - 당첨 내역 출력 | ||
| - `3개 일치 (5000원) - n개` | ||
| - `4개 일치 (50000원) - n개` | ||
| - `5개 일치 (1500000원) - n개` | ||
| - `5개 일치, 보너스 볼 일치 (30000000원) - n개` | ||
| - `6개 일치 (2000000000원) - n개` | ||
| - 수익률 출력 | ||
| - `총 수익률은 n입니다.` | ||
| - 소숫점 둘째자리 밑으론 버림 | ||
| - 예외 상황시 에러 문구 출력 | ||
| - `[ERROR] error message` | ||
|
|
||
| ### 게임 진행 | ||
|
|
||
| - 구입 금액을 입력 | ||
| - 수동으로 구매할 로또 수 입력 | ||
| - 수동 로또 번호 입력 | ||
| - 로또 번호 1~45 사이의 중복되지 않는 정수로 6개 무작위 추첨해서 구입한 개수(수동 로또 개수 제외) 만큼 자동 로또 발행 | ||
| - 당첨 번호와 보너스 번호를 입력받음 | ||
| - 일치하는 내역과 총 수익률을 출력 | ||
| - 예외가 발생할 경우 예외 문구 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import controller.LottoController; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String [] args) throws IOException { | ||
| LottoController lottoController = new LottoController(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package controller; | ||
|
|
||
| import model.AutoLotto; | ||
| import model.Lotto; | ||
| import model.LottoList; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoController { | ||
| private InputView inputView = new InputView(); | ||
| private OutputView outputView = new OutputView(); | ||
| private LottoList lottoList; | ||
|
|
||
| public LottoController(){ | ||
| startLottoGame(); | ||
| } | ||
| public void startLottoGame() { | ||
| int price = receiveMoneyInput(); | ||
| int manualLottoCount = manualLottoCount(); | ||
| displayLottoTickets(price, manualLottoNumbers(manualLottoCount)); | ||
| Lotto winningNumbers = getWinningNumbers(); | ||
| int bonusNum = receiveBonusNumberInput(); | ||
|
|
||
| int[] matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum); | ||
| double winRate = calculateWinningRate(matchCounts, price); | ||
| outputView.printResults(matchCounts, Math.floor(winRate*100)/100 ); | ||
| } | ||
| private int manualLottoCount(){ | ||
| return convertStringToInt(inputView.inputManualLottoCount()); | ||
| } | ||
| private List<Lotto> manualLottoNumbers(int count){ | ||
| List<String> manualLottoNums = inputView.manualLottos(count); | ||
| List<Lotto> manualLottos = new ArrayList<>(); | ||
|
|
||
| for (String manualLotto : manualLottoNums) { | ||
| Lotto lotto = new Lotto(); | ||
| manualLottos.add(lotto.convertToList(manualLotto)); | ||
| } | ||
| return manualLottos; | ||
| } | ||
|
|
||
| private int receiveBonusNumberInput() { | ||
| return convertStringToInt(inputView.inputBonusNumber()); | ||
| } | ||
|
|
||
| private int receiveMoneyInput(){ | ||
| return convertStringToInt(inputView.inputPrice()); | ||
| } | ||
| private int convertStringToInt(String price){ | ||
| return Integer.parseInt(price); | ||
| } | ||
| private void displayLottoTickets(int price, List<Lotto> lottos){ | ||
| int autoCount = price/1000 - lottos.size(); | ||
| int manualCount = lottos.size(); | ||
| outputView.printTicketCount(autoCount, manualCount); | ||
| lottoList = generateLottoList(autoCount, lottos); | ||
| outputView.printLottoNumbers(lottoList); | ||
| } | ||
| private LottoList generateLottoList(int count, List<Lotto> lottos){ | ||
| lottoList = new LottoList(); | ||
|
|
||
| for (Lotto lotto : lottos) { | ||
| lottoList.setLottoList(lotto); | ||
| } | ||
| for(int i=0; i<count; i++){ | ||
| lottoList.setLottoList(generateLotto()); | ||
| } | ||
| return lottoList; | ||
| } | ||
| private Lotto generateLotto(){ | ||
| List<Integer> numbers = new ArrayList<>(); | ||
| AutoLotto autoLotto = new AutoLotto(); | ||
| numbers = autoLotto.getAutoLotto(); | ||
| return new Lotto(numbers); | ||
| } | ||
|
|
||
| private Lotto getWinningNumbers(){ | ||
| String winnerNumbersStr = inputView.inputWinnerNumber(); | ||
| Lotto winningLotto = new Lotto(); | ||
| return winningLotto.convertToList(winnerNumbersStr); | ||
| } | ||
|
|
||
| private int [] calculateMatches(LottoList ticketList, Lotto winningNumbers, int bonusNum){ | ||
| int [] matchCounts = new int[5]; | ||
| for(Lotto ticket : ticketList.getLottoList()) { | ||
| int countMatches = ticket.calculateMatches(winningNumbers); | ||
| if(countMatches == 3){ | ||
| matchCounts[0] ++; | ||
| } | ||
| else if (countMatches == 4){ | ||
| matchCounts[1] ++; | ||
| } | ||
| else if(countMatches == 5){ | ||
| if(ticket.bonusMatches(bonusNum)){ | ||
| matchCounts[3] ++; | ||
| } | ||
| else matchCounts[2] ++; | ||
| } | ||
| else if(countMatches == 6){ | ||
| matchCounts[4] ++; | ||
| } | ||
| } | ||
| return matchCounts; | ||
| } | ||
| private double calculateWinningRate(int [] matchCounts, int price){ | ||
| int totalWinnings = matchCounts[0] * 5000 + matchCounts[1] * 50000 + matchCounts[2] * 1500000 + matchCounts[3] * 30000000 + matchCounts[4] * 2000000000; | ||
| double winningRate = (double) totalWinnings / (double) price; | ||
| return winningRate; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package model; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class AutoLotto { | ||
| private static final int MIN_LOTTO_NUMBER = 1; | ||
| private static final int MAX_LOTTO_NUMBER = 45; | ||
| private static final int CNT_LOTTO_NUMBER = 6; | ||
|
|
||
| private static List<Integer> lottoNums = new ArrayList<>(); | ||
| public AutoLotto(){ | ||
| createAutoLotto(); | ||
| } | ||
| public void createAutoLotto(){ | ||
|
|
||
| List<Integer> numbers = new ArrayList<>(); | ||
| for (int i = MIN_LOTTO_NUMBER; i <= MAX_LOTTO_NUMBER; i++) { | ||
| numbers.add(i); | ||
| } | ||
| Collections.shuffle(numbers); | ||
| lottoNums = new ArrayList<>(); | ||
| for (int i = 0; i < CNT_LOTTO_NUMBER; i++) { | ||
| int uniqueNumber = numbers.get(i); | ||
| while (lottoNums.contains(uniqueNumber)) { | ||
| Collections.shuffle(numbers); | ||
| uniqueNumber = numbers.get(i); | ||
| } | ||
| lottoNums.add(uniqueNumber); | ||
| } | ||
| Collections.sort(lottoNums); | ||
| } | ||
| public List<Integer> getAutoLotto(){ | ||
| return lottoNums; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Lotto { | ||
| private static final int BONUS_NUMBER_INDEX = 6; | ||
| private List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| this.numbers = numbers; | ||
| } | ||
| public Lotto(){ | ||
|
|
||
| } | ||
| public Lotto convertToList(String lottoNumbersStr) { | ||
| String[] str = lottoNumbersStr.split(",\\s*"); | ||
| List<Integer> lottoList = new ArrayList<>(); | ||
| for (String s : str) { | ||
| lottoList.add(Integer.parseInt(s)); | ||
| } | ||
| Lotto lotto = new Lotto(lottoList); | ||
| return lotto; | ||
| } | ||
| public List<Integer>getNumbers(){ | ||
| return numbers; | ||
| } | ||
|
|
||
| public int calculateMatches(Lotto winningLotto) { | ||
| int cnt = 0; | ||
| List<Integer> winningLottoList = winningLotto.getNumbers(); | ||
| for (Integer integer : winningLottoList) { | ||
| if(numbers.contains(integer)){ | ||
| cnt ++; | ||
| } | ||
| } | ||
| return cnt; | ||
| } | ||
| public boolean bonusMatches(int bonusNum) { | ||
| if(numbers.contains(bonusNum)){ | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoList { | ||
| List<Lotto> lottoList = new ArrayList<>(); | ||
| private int totalPrice; | ||
| public LottoList(){ | ||
|
|
||
| } | ||
| public void setLottoList(Lotto lotto){ | ||
| lottoList.add(lotto); | ||
| } | ||
| public List<Lotto> getLottoList(){ | ||
| return lottoList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package model; | ||
|
|
||
| public class UserCount { | ||
|
|
||
| private static final int LOTTO_UNIT = 1000; | ||
|
|
||
| private final int count; | ||
| private final int price; | ||
|
|
||
| public UserCount(String price){ | ||
| int priceNum = convertStringToInt(price); | ||
| validate(priceNum); | ||
| this.price = priceNum; | ||
| this.count = calculateCount(priceNum); | ||
| } | ||
| public int calculateCount(int price){ | ||
| return price/1000; | ||
| } | ||
| public int getCount(){ | ||
| return count; | ||
| } | ||
| public int getPrice(){ | ||
| return price; | ||
| } | ||
| public int convertStringToInt(String price){ | ||
| try { | ||
| return Integer.parseInt(price); | ||
| } catch (NumberFormatException e) { | ||
| System.err.println("[ERROR] 숫자로만 입력해주세요."); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| public void validate(int priceNum){ | ||
| validateRange(priceNum); | ||
| validateUnit(priceNum); | ||
| } | ||
| public void validateRange(int priceNum){ | ||
| if(priceNum<=0){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| public void validateUnit(int priceNum){ | ||
| if(priceNum % LOTTO_UNIT != 0){ | ||
| throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력되어야 합니다."); | ||
| } | ||
| } | ||
|
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. 좋은 예외처리 같습니다! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package model; | ||
|
|
||
| public enum WinningType { | ||
| FIFTH(3, 5000, "3개 일치 (5000원)- "), | ||
| FOURTH(4, 50000, "4개 일치 (50000원)- "), | ||
| THIRD(5, 150000, "5개 일치 (1500000원)- "), | ||
| SECOND(5, 30000000, "5개 일치, 보너스 볼 일치(30000000원)- "), | ||
| FIRTH(6, 2000000000, "6개 일치 (2000000000원)- "); | ||
|
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 활용을 잘 하신 것 같습니다! 같은 의미인 것 끼리 모아서 메시지를 구성할때에도 편리할 것 같아요 |
||
|
|
||
| private static final int WINNING_MIN_COUNT = 3; | ||
| private static final String ERROR_MESSAGE = "[ERROR]"; | ||
|
|
||
| private int countOfCorrect; | ||
| private int reward; | ||
| private String message; | ||
|
|
||
| WinningType(int countOfCorrect, int reward, String message) { | ||
| this.countOfCorrect = countOfCorrect; | ||
| this.reward = reward; | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage(){ | ||
| return message; | ||
| } | ||
|
|
||
| } | ||
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.
궁금한점이 있습니다. numbers에는 이미 유니크한 숫자들이 shuffle되어있는 것 아닌가요? 이미 중복되지 않는 것은 보장된 것 같은데 한번 더 중복을 체크하시는 이유가 궁금합니다.