-
Notifications
You must be signed in to change notification settings - Fork 40
[스프링 화요일 1팀](박철) 미션 제출합니다. #28
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: main
Are you sure you want to change the base?
Changes from all commits
6996f88
7078add
4081a5e
93baef3
3f9ecef
d018c7c
eb2df15
92d91fc
74e11b0
64cee95
b41e47e
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,32 @@ | ||
| # 로또 | ||
|
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. 리드미를 이용하여 개발 방향을 잡는 방식은 굉장히 배울만하다고 생각합니다! 다음부터는 저도 리드미 적극 활용하겠습니다 ^~^
Author
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. 근데 이거 요구사항에 있어요!!!!! 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. 저도 알아요 |
||
| 1. 입력 | ||
| 1. 금액 입력 | ||
| - 정수인지 검증 | ||
| - 0인지 검증 | ||
| - 1000원 단위인지 검증 | ||
| 2. 당첨번호 입력 | ||
| - 정수인지 검증 | ||
| - 1~45사이 정수인지 검증 | ||
| - 중복된 로또 번호가 존재하는지 검증 | ||
| - 개수가 6개인지 검증 | ||
| 3. 보너스번호 입력 | ||
| - 정수인지 검증 | ||
| - 1~45사이 정수인지 검증 | ||
| - 정규 당첨번호에 있는 수인지 검증 | ||
| 2. 로또 생성 | ||
| 1. 금액//1000 만큼 로또 생성 | ||
| 2. 검증 | ||
| - 1~45사이 정수인지 검증 | ||
| - 중복된 로또 번호가 존재하는지 검증 | ||
| - 개수가 6개인지 검증 | ||
| - 오름차순인지 검증 | ||
| 3. 로또 출력 | ||
| 1. 로또 수 출력 | ||
| 2. 로또 번호 출력 | ||
| 4. 당첨번호와 로또 번호확인 | ||
| 1. 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 | ||
| 2. 당첨 수 계산 | ||
| 5. 당첨결과 출력 | ||
| 1. 당첨 내역 출력 | ||
| 2. 수익률 출력 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.controller.LottoController; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| LottoController lottoController = new LottoController(inputView,outputView); | ||
| lottoController.start(); | ||
|
|
||
| } | ||
|
|
||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.domain.Ranking; | ||
| import lotto.domain.WinnigResult; | ||
| import lotto.util.Constants; | ||
| import lotto.util.ExceptionMessage; | ||
| import lotto.util.LottoGenerator; | ||
| import lotto.util.MoneyValidator; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class LottoController { | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
|
|
||
| public LottoController(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void start(){ | ||
| try { | ||
| int money=inputView.readMoney(); | ||
| LottoGenerator lottoGenerator = new LottoGenerator(); | ||
|
|
||
| List<Lotto> lottos = lottoGenerator.generate(money); | ||
| List<Integer> winningNumbers = inputView.readWinningNumbers(); | ||
| int bonusNum = inputView.readBonusNumber(winningNumbers); | ||
| WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum); | ||
| lottoResult(lottos, winnigResult, money / 1000); | ||
| }catch (IllegalArgumentException e){ | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| private void lottoResult(List<Lotto> lottoList, WinnigResult winnigResult, int ticketNum){ | ||
| Map<Ranking, Integer> result = setResult(); | ||
| Ranking rank; | ||
|
|
||
| OutputView.printSuccessResult(); | ||
| for (int i = 0; i < lottoList.size(); i++) { | ||
| rank = winnigResult.match(lottoList.get(i)); | ||
|
|
||
| result.put(rank, result.get(rank) + 1); | ||
| } | ||
| printResult(result); | ||
| printEarningRate(result, ticketNum); | ||
| } | ||
|
|
||
| private void printResult(Map<Ranking, Integer> result) { | ||
| for (int i = Ranking.values().length - 1; i >= 0; i--) { | ||
| Ranking.values()[i].printMessage(result.get(Ranking.values()[i])); | ||
| } | ||
| } | ||
|
|
||
| private void printEarningRate(Map<Ranking, Integer> result, int lottoAmount) { | ||
| double EarningRate = 0; | ||
| for (Ranking rank : result.keySet()) { | ||
| EarningRate = | ||
| EarningRate + ((double) (rank.getWinningAmount()) / (lottoAmount * Constants.LOTTO_PRICE) * (result.get( | ||
| rank)) * (100)); | ||
|
|
||
| } | ||
| OutputView.printRevenueRate(EarningRate); | ||
| } | ||
|
|
||
| private Map<Ranking, Integer> setResult() { | ||
| Map<Ranking, Integer> result = new LinkedHashMap<>(); | ||
|
|
||
| for (Ranking rank : Ranking.values()) { | ||
| result.put(rank, 0); | ||
| } | ||
| return result; | ||
| } | ||
| } |
|
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. 도메인이 너무 세분화 되어 있는 것 보다는 핵심 내용 3개로 나누어서 코드를 작성하신 점이 인상 깊네요~ 가독성이 높은 것 같아요. 그런데 validate 함수와 validateDuplicateNum의 기능 차이가 뭔지 설명해주실 수 있을까요? 제가 보기에는 역할이 비슷해보여서요!
Author
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. 어쩔
Author
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. 뻥이에요 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,64 @@ | ||
| package lotto.domain; | ||
|
|
||
| import lotto.util.Constants; | ||
| import lotto.util.ExceptionMessage; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static lotto.util.ExceptionMessage.INVALID_WINNING_NUMBER_DUPLICATE; | ||
| import static lotto.util.ExceptionMessage.INVALID_WINNING_SIZE; | ||
|
|
||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| validate(numbers); | ||
| validateDuplicateNum(numbers); | ||
| validateNumRange(numbers); | ||
| this.numbers = numbers; | ||
| Collections.sort(numbers); | ||
| } | ||
|
|
||
|
|
||
| private void validate(List<Integer> numbers) { | ||
| if (numbers.size() != 6) { | ||
| throw new IllegalArgumentException(INVALID_WINNING_SIZE.getMessage()); | ||
| } | ||
| Set<Integer> set = new HashSet<>(numbers); | ||
| if (set.size()!=6){ | ||
| throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); | ||
| } | ||
| } | ||
| private void validateDuplicateNum(List<Integer> numbers) { | ||
| Set<Integer> set = new HashSet<>(numbers); | ||
| if (set.size()!=6){ | ||
| throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); | ||
| } | ||
| } | ||
| private void validateNumRange(List<Integer> numbers) { | ||
|
|
||
| for (Integer number : numbers) { | ||
| if (number<=0 || number>45){ | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: 추가 기능 구현 | ||
| //public void printResult | ||
| public int countMatch(List<Integer> winningNum){ | ||
| return (int) numbers.stream() | ||
| .filter(winningNum::contains).count(); | ||
| } | ||
| public boolean containNum(int num){ | ||
| return numbers.contains(num); | ||
| } | ||
|
|
||
| public List<Integer> getNumbers() { | ||
| return numbers; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package lotto.domain; | ||
|
|
||
| import lotto.view.OutputView; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public enum Ranking { | ||
| FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "), // 1등 | ||
| SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), // 2등 | ||
| THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "), // 3등 | ||
| FOURTH(4, 50_000, "4개 일치 (50,000원) - "), // 4등 | ||
| FIFTH(3, 5_000, "3개 일치 (5,000원) - "), // 5등 | ||
| MISS(0, 0, ""); | ||
| Ranking(int countOfMatch, int winningAmount, String message) { | ||
| this.countOfMatch = countOfMatch; | ||
| this.winningAmount = winningAmount; | ||
| this.message = message; | ||
| } | ||
| private int countOfMatch; | ||
| private int winningAmount; | ||
| private String message; | ||
|
|
||
| private static final int WINNG_MIN_COUNT=3; | ||
| public static Ranking valueOf(int countOfMatch, boolean matchBonus){ | ||
| if (countOfMatch<WINNG_MIN_COUNT){ | ||
| return MISS; | ||
| } | ||
| if (SECOND.getCountOfMatch()==countOfMatch && matchBonus){ | ||
| return SECOND; | ||
| } | ||
| for (Ranking rank : values()) { | ||
| if (rank.getCountOfMatch()==countOfMatch && rank!=SECOND){ | ||
| return rank; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("[ERROR]"); | ||
| } | ||
|
|
||
|
|
||
| public int getCountOfMatch() { | ||
| return countOfMatch; | ||
| } | ||
|
|
||
| public int getWinningAmount() { | ||
| return winningAmount; | ||
| } | ||
|
|
||
| public void printMessage(int count) { | ||
| if (this!=MISS){ | ||
| OutputView.printSuccessMessage(message,count); | ||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class WinnigResult { | ||
| private final Lotto lotto; | ||
| private final int bonusball; | ||
|
|
||
| public WinnigResult(Lotto lotto, int bonusball) { | ||
| this.lotto = lotto; | ||
| this.bonusball = bonusball; | ||
| } | ||
|
|
||
| public Ranking match(Lotto playerNumber) { | ||
| int countOfMatch = playerNumber.countMatch(lotto.getNumbers()); | ||
| boolean bonusCheck = playerNumber.containNum(bonusball); | ||
| return Ranking.valueOf(countOfMatch, bonusCheck); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package lotto.util; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class BonusNumValidator extends Validator{ | ||
| @Override | ||
| public void validate(String input) throws IllegalStateException { | ||
| validateInt(input); | ||
| validateLottoNumberRange(input); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package lotto.util; | ||
|
|
||
| public class Constants { | ||
| static final int NUM_COUNT=6; | ||
| static final int MAX_NUM=45; | ||
| static final int MIN_NUM=1; | ||
| public static final int LOTTO_PRICE=1000; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package lotto.util; | ||
|
|
||
| import static lotto.util.Constants.LOTTO_PRICE; | ||
|
|
||
| public enum ExceptionMessage { | ||
| NOT_INT("정수를 입력해주세요"), | ||
| INVALID_LOTTO_NUMBER_RANGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."), | ||
| INVALID_MONEY_UNIT(String.format("%d원 단위로 구매해야합니다.", LOTTO_PRICE)), | ||
| ZERO_EXCEPTION("0보다 큰 금액을 입력해주세요"), | ||
| INVALID_WINNING_SIZE("6개의 번호를 입력해주세요."), | ||
| INVALID_WINNING_NUMBER_DUPLICATE("중복되지 않은 6개의 번호를 입력해주세요."), | ||
| INVALID_BONUS_NUM("보너스가 번호가 정규번호와 중복되었습니다."); | ||
|
|
||
| public static final String BASE_MESSAGE="[ERROR] %s"; | ||
| private final String message; | ||
|
|
||
| ExceptionMessage(String message){ | ||
| this.message= String.format(BASE_MESSAGE, message); | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package lotto.util; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.view.OutputView; | ||
| import org.kokodak.Randoms; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import static lotto.util.Constants.*; | ||
|
|
||
| public class LottoGenerator { | ||
| public List<Lotto> generate(int money){ | ||
| int ticketNum=money/1000; | ||
|
|
||
| OutputView.printTicketCount(ticketNum); | ||
| List<Lotto> lottos=new ArrayList<>(); | ||
| for (int i=0; ticketNum>i; i++){ | ||
| List<Integer> lottoNum= new ArrayList<>(Randoms.pickUniqueNumbersInRange(MIN_NUM,MAX_NUM,NUM_COUNT)); | ||
| Lotto lotto = new Lotto(lottoNum); | ||
| lottos.add(lotto); | ||
| System.out.println(lotto.getNumbers()); | ||
|
|
||
| } | ||
| return lottos; | ||
| } | ||
| } |
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.
리드미에 구현해야 할 코드를 세분화해서 정리해놓으면 코드 작성시 훨씬 수월할 것 같다는 생각이 드네요 저도 참고하겠습니다!!
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.
좋은 말씀 감사합니다.