-
Notifications
You must be signed in to change notification settings - Fork 1.9k
로또 완성!!! #2105
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?
로또 완성!!! #2105
Changes from all commits
72ce2e7
cd685bb
7c43480
8610737
5b9329b
d28635a
6cfa1f6
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,14 @@ | ||
| ### 기능 요구사항 | ||
|
|
||
| [ ✅ ] 사용자는 구입금액을 입력한다. | ||
| - 구입금액은 1,000원 단위로 입력 받으며 1,000원으로 나누어 떨어지지 않는 경우 예외 처리한다. | ||
|
|
||
| [ ✅ ] 구입금액을 입력하면 구입 금액에 해당하는 만큼 로또를 발행해야 한다. | ||
|
|
||
| [ ✅ ] 당첨번호를 입력한다. | ||
|
|
||
| [ ✅ ] 보너스번호를 입력한다. | ||
|
|
||
| [ ✅ ] 당첨통계를 출력한다. | ||
|
|
||
| [ ✅ ] 수익률을 계산한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package controller; | ||
|
|
||
| import domain.*; | ||
| import dto.LottoDto; | ||
| import service.LottosService; | ||
| import service.PurchaseService; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LottosController { | ||
| private final LottosService lottosService; | ||
| private Lottos lottos; | ||
|
|
||
| public LottosController(){ | ||
| lottosService = new LottosService(); | ||
| } | ||
|
|
||
| public Lottos start(final PurchaseAmount purchaseAmount){ | ||
| Lottos purchaseLottos = generateLottos(purchaseAmount); | ||
| lottos = purchaseLottos; | ||
| printPurchaseLottos(purchaseLottos, purchaseAmount.getQuantity()); | ||
| return lottos; | ||
| } | ||
|
|
||
| private Lottos generateLottos(final PurchaseAmount purchaseAmount){ | ||
| return lottosService.createLottos(purchaseAmount); | ||
| } | ||
|
|
||
| private void printPurchaseLottos(final Lottos purchaseLottos, final int numberOfPurchased){ | ||
| List<LottoDto> printPurchaseLottos = lottosService.getPurchaseLottos(purchaseLottos); | ||
| OutputView.printPurchaseLottos(printPurchaseLottos, numberOfPurchased); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package controller; | ||
|
|
||
| import domain.PurchaseAmount; | ||
| import service.PurchaseService; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class PurchaseController { | ||
| private final PurchaseService purchaseService; | ||
|
|
||
| public PurchaseController(){ | ||
| purchaseService = new PurchaseService(); | ||
| } | ||
|
|
||
| public PurchaseAmount generatePurchaseAmount(){ | ||
| try{ | ||
| int purchaseAmount = InputView.inputPurchaseAmount(); | ||
| return createPurchaseAmount(purchaseAmount); | ||
| } catch (IllegalArgumentException e){ | ||
| OutputView.printMessage(e.getMessage()); | ||
| return generatePurchaseAmount(); | ||
| } | ||
| } | ||
|
|
||
| private PurchaseAmount createPurchaseAmount(final int purchaseAmount){ | ||
| return purchaseService.createPurchaseAmount(purchaseAmount); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package controller; | ||
|
|
||
| import domain.*; | ||
| import dto.WinningResultDto; | ||
| import service.WinningService; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class WinningController { | ||
| private final WinningService winningService; | ||
|
|
||
| public WinningController(){ | ||
| winningService = new WinningService(); | ||
| } | ||
|
|
||
| public void generateWinngingAndBonus(final Lottos lottos, final int purchaseAmount){ | ||
| WinningNumbers winningNumbers = generateWinngingNumbers(); | ||
| BonusNumber bonusNumber = generateBonusNumber(); | ||
| printWinningStaticsAndProfit(winningNumbers, bonusNumber, lottos, purchaseAmount); | ||
| } | ||
|
|
||
| private WinningNumbers generateWinngingNumbers(){ | ||
| try{ | ||
| List<Integer> winningNumbers = InputView.inputWinngingNumbers(); | ||
| return createWinngingNumbers(winningNumbers); | ||
| } catch (IllegalArgumentException e){ | ||
| OutputView.printMessage(e.getMessage()); | ||
|
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. OutputView도 "private final OutputView outputView" 이렇게 선언해주는게 어떨까요?
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. 음..!! 그렇게 선언하면 좋은 점이 어떤것인가요!! |
||
| return generateWinngingNumbers(); | ||
| } | ||
| } | ||
|
|
||
| private BonusNumber generateBonusNumber(){ | ||
| try{ | ||
| int number = InputView.inputBonusNumber(); | ||
| return createBonusNumber(number); | ||
| } catch (IllegalArgumentException e){ | ||
| OutputView.printMessage(e.getMessage()); | ||
| return generateBonusNumber(); | ||
| } | ||
| } | ||
|
|
||
| private WinningNumbers createWinngingNumbers(final List<Integer> winningNumbers){ | ||
| return winningService.createWinningNumbers(winningNumbers); | ||
| } | ||
|
|
||
| private BonusNumber createBonusNumber(final int bonusNumber){ | ||
| return winningService.createBonusNumber(bonusNumber); | ||
| } | ||
|
|
||
| private void printWinningStaticsAndProfit(final WinningNumbers winningNumbers, final BonusNumber bonusNumber, final Lottos lottos, final int purchaseAmount){ | ||
| generateWinningResult(winningNumbers, bonusNumber); | ||
| WinningResultDto winningResultDto = winningService.calculateProfitAndRateOfProfit(lottos.getLottos(), purchaseAmount); | ||
| OutputView.printWinningStatics(winningResultDto); | ||
| } | ||
|
|
||
| private WinningResult generateWinningResult(final WinningNumbers winningNumbers, final BonusNumber bonusNumber){ | ||
| return winningService.createWinningResult(winningNumbers, bonusNumber); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package domain; | ||
|
|
||
| public class BonusNumber { | ||
| private final int bonusNumber; | ||
|
|
||
| private BonusNumber(final int number) { | ||
| bonusNumber = number; | ||
| } | ||
|
|
||
| public static BonusNumber create(final int number){ | ||
| return new BonusNumber(number); | ||
| } | ||
|
|
||
| public int getBonusNumber(){ | ||
| return bonusNumber; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package domain; | ||
|
|
||
| import util.exception.DuplicateException; | ||
| import util.exception.SizeOverException; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import static util.message.ExceptionMessage.DUPLICATE_MESSAGE; | ||
| import static util.message.ExceptionMessage.SIZE_OVER_MESSAGE; | ||
|
|
||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| validate(numbers); | ||
| List<Integer> numberList = sort(numbers); | ||
| this.numbers = numberList; | ||
| } | ||
|
|
||
| public List<Integer> getNumbers(){ | ||
| return numbers; | ||
| } | ||
|
|
||
| private void validate(List<Integer> numbers) { | ||
| validateSize(numbers); | ||
| validateDuplicate(numbers); | ||
| } | ||
|
|
||
| private void validateSize(List<Integer> numbers){ | ||
| if (numbers.size() != 6) { | ||
| throw new SizeOverException(String.format(SIZE_OVER_MESSAGE.getValue(), "로또")); | ||
| } | ||
| } | ||
|
|
||
| private void validateDuplicate(List<Integer> numbers){ | ||
| Set<Integer> lottos = new HashSet<>(numbers); | ||
| if(numbers.size() != lottos.size()){ | ||
| throw new DuplicateException(String.format(DUPLICATE_MESSAGE.getValue())); | ||
| } | ||
| } | ||
|
|
||
| private List<Integer> sort(List<Integer> numbers){ | ||
| List<Integer> numberList = new ArrayList<>(numbers); | ||
| Collections.sort(numberList); | ||
| return numberList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package domain; | ||
|
|
||
| import util.constants.LottosConstants; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Lottos { | ||
| private final List<Lotto> lottos; | ||
|
|
||
| public Lottos(int numberOfPurchased){ | ||
| lottos = generateLottos(numberOfPurchased); | ||
| } | ||
|
|
||
| public static Lottos create(final int numberOfPurchased){ | ||
| return new Lottos(numberOfPurchased); | ||
| } | ||
|
|
||
| public List<Lotto> getLottos(){ | ||
| return lottos; | ||
|
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.
참고 : https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/ |
||
| } | ||
|
|
||
| private List<Lotto> generateLottos(int numberOfPurchased){ | ||
| List<Lotto> lottos = new ArrayList<>(); | ||
| while(numberOfPurchased-- > 0){ | ||
| lottos.add(generateRandomNumbers()); | ||
| } | ||
| return lottos; | ||
| } | ||
|
|
||
| private Lotto generateRandomNumbers(){ | ||
| List<Integer> numbers = Randoms.pickUniqueNumbersInRange( | ||
| LottosConstants.MIN_VALUE.getValue(), | ||
| LottosConstants.MAX_VALUE.getValue(), | ||
| LottosConstants.LOTTO_SIZE.getValue()); | ||
|
|
||
| return new Lotto(numbers); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package domain; | ||
|
|
||
| import util.constants.Constants; | ||
|
|
||
| import static util.message.ExceptionMessage.UNIT_MESSAGE; | ||
|
|
||
| public class PurchaseAmount { | ||
| private final int amount; | ||
| private final int quantity; | ||
|
|
||
| private PurchaseAmount(final int purchaseAmount) { | ||
| validateDivisibleBy1000(purchaseAmount); | ||
| this.amount = purchaseAmount; | ||
| this.quantity = calculatequantity(); | ||
| } | ||
|
|
||
| public static PurchaseAmount create(final int purchaseAmount){ | ||
| return new PurchaseAmount(purchaseAmount); | ||
| } | ||
|
|
||
| public int getQuantity(){ | ||
| return quantity; | ||
| } | ||
|
|
||
| public int getAmount(){ | ||
| return amount; | ||
| } | ||
|
|
||
| private int calculatequantity(){ | ||
| return amount / Constants.ONE_THOUSAND.getValue(); | ||
| } | ||
|
|
||
| private static int validateDivisibleBy1000(final int amount){ | ||
| if(amount % Constants.ONE_THOUSAND.getValue() != Constants.ZERO.getValue()){ | ||
| throw new IllegalArgumentException(String.format(UNIT_MESSAGE.getValue(), Constants.ONE_THOUSAND.getValue())); | ||
| } | ||
| return amount; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package domain; | ||
|
|
||
| import util.constants.Constants; | ||
|
|
||
| public enum Rank { | ||
| FIFTH(3, 5_000, "3개 일치 (5,000원) - "), | ||
| FOURTH(4, 50_000, "4개 일치 (50,000원) - "), | ||
| THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "), | ||
| SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), | ||
| FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "); | ||
|
|
||
| private int countOfMatch; | ||
| private int prize; | ||
| private String message; | ||
|
|
||
| Rank(int countOfMatch, int prize, String message){ | ||
| this.countOfMatch = countOfMatch; | ||
| this.prize = prize; | ||
| this.message = message; | ||
| } | ||
|
|
||
| public int getCountOfMatch(){ | ||
| return countOfMatch; | ||
| } | ||
|
|
||
| public int getPrize(){ | ||
| return prize; | ||
| } | ||
|
|
||
| public String getMessage(){ | ||
| return message; | ||
| } | ||
|
|
||
| public static Rank calculateRank(int countOfMatchNumbers, boolean isMatchBonus){ | ||
| for(Rank rank : values()){ | ||
| if(rank.matches(countOfMatchNumbers, isMatchBonus)){ | ||
| return rank; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private boolean matches(int countOfMatchNumbers, boolean isMatchBonus){ | ||
| if(this == SECOND){ | ||
| return checkSecond(countOfMatchNumbers, isMatchBonus); | ||
| } | ||
| return checkRemaining(countOfMatchNumbers, isMatchBonus); | ||
| } | ||
|
|
||
| private boolean checkSecond(int countOfMatchNumbers, boolean isMatchBonus){ | ||
| return this.countOfMatch == countOfMatchNumbers && isMatchBonus; | ||
| } | ||
|
|
||
| private boolean checkRemaining(int countOfMatchNumbers, boolean isMatchBonus){ | ||
| return this.countOfMatch == countOfMatchNumbers && !isMatchBonus; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package domain; | ||
|
|
||
| import util.exception.DuplicateException; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static util.message.ExceptionMessage.DUPLICATE_MESSAGE; | ||
|
|
||
| public class WinningNumbers { | ||
| private final List<Integer> numbers; | ||
|
|
||
| private WinningNumbers(final List<Integer> inputWinningNumbers) { | ||
| validateDuplicate(inputWinningNumbers); | ||
| numbers = inputWinningNumbers; | ||
| } | ||
|
|
||
| public static WinningNumbers create(final List<Integer> inputWinningNumbers){ | ||
| return new WinningNumbers(inputWinningNumbers); | ||
| } | ||
|
|
||
| public List<Integer> getNumbers(){ | ||
| return numbers; | ||
| } | ||
|
|
||
| private void validateDuplicate(final List<Integer> inputWinningNumbers){ | ||
| Set<Integer> numbers = new HashSet<>(inputWinningNumbers); | ||
| if(numbers.size() != inputWinningNumbers.size()){ | ||
| throw new DuplicateException(String.format(DUPLICATE_MESSAGE.getValue())); | ||
| } | ||
| } | ||
| } |
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.
21, 22번 라인을 이렇게 줄일 수 있을거 같아요!
lottos = generateLottos(purchaseAmount);