-
Notifications
You must be signed in to change notification settings - Fork 92
[그리디] 이고은 로또 미션 3, 4, 5단계 제출합니다. #144
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: ke-62
Are you sure you want to change the base?
Changes from all commits
01a15d2
b74016a
c56ec71
e7fdac1
7c0f366
8d25d65
aa93828
712355d
5483100
a950c6d
32e4236
db12651
3ee3b9e
a7f1d52
c1a8a28
684a177
bb099d0
c0a617d
0a3d25d
a195cdd
31cf72e
448f371
461131a
58cf0ec
fff7970
7995e01
ebf2453
f157862
050a96b
b2ca2bc
43244b9
0e25574
9326c01
cea3d80
0f760a5
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 |
|---|---|---|
| @@ -1,56 +1,137 @@ | ||
| package controller; | ||
|
|
||
| import domain.*; | ||
| import domain.Lotto; | ||
| import domain.LottoNumber; | ||
| import domain.LottoService; | ||
| import domain.LottoTicketCount; | ||
| import domain.Lottos; | ||
| import domain.LottoTotalPrice; | ||
| import domain.Money; | ||
| import domain.MatchCount; | ||
| import domain.ProfitRate; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class LottoController { | ||
|
|
||
| OutputView outputView = new OutputView(); | ||
| InputView inputView = new InputView(); | ||
| ResultView resultView = new ResultView(); | ||
|
|
||
| public void run() { | ||
| outputView.printWonMessage(); | ||
| Money money = new Money(inputView.inputMoney()); | ||
| Money money; | ||
| while (true) { | ||
| try { | ||
| money = new Money(inputView.inputMoney()); | ||
| break; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("다시 입력해주세요."); | ||
| } | ||
| } | ||
|
|
||
| LottoTickets lottoTickets = buyLotto(money); | ||
| Lottos lottoTickets = buyLotto(money); | ||
| checkLotto(lottoTickets, money); | ||
| } | ||
|
|
||
|
|
||
| public LottoTickets buyLotto(Money money) { | ||
|
|
||
| LottoTicketCount ticketNumber = Money.getTicketCount(money); | ||
| public Lottos buyLotto(Money money) { | ||
| LottoTicketCount ticketNumber = money.getTicketCount(); | ||
| resultView.printTicketNumbers(ticketNumber.getCount()); | ||
|
|
||
| outputView.printManualCount(); | ||
| int manualCount; | ||
|
|
||
| while (true) { | ||
| try { | ||
| manualCount = inputView.inputManualCount(); | ||
| ticketNumber.validateManualCount(manualCount); | ||
| break; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("다시 입력해주세요."); | ||
| } | ||
| } | ||
|
|
||
| List<Lotto> manualLottos = new ArrayList<>(); | ||
| outputView.printManualNumbers(); | ||
|
|
||
| LottoService lottoService = new LottoService(); | ||
| for (int i = 0; i < manualCount; i++) { | ||
| while (true) { | ||
| try { | ||
| String manualNumbers = inputView.inputManualNumbers(); | ||
| Lotto lotto = lottoService.parseLottoAnswer(manualNumbers); | ||
| manualLottos.add(lotto); | ||
| break; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("다시 입력해주세요."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int autoCount = ticketNumber.getCount() - manualCount; | ||
|
|
||
| resultView.printManualAuto(manualCount, autoCount); | ||
| outputView.lottoResult(); | ||
| Lottos lottoTickets = Lottos.createMixedTickets(manualLottos, autoCount); | ||
|
|
||
| LottoTickets lottoTickets = new LottoTickets(ticketNumber); | ||
| for (Lotto lotto : lottoTickets.getTickets()) { | ||
| System.out.println(lotto); | ||
| } | ||
|
|
||
| return lottoTickets; | ||
| } | ||
|
|
||
| public void checkLotto(LottoTickets lottoTickets, Money money) { | ||
|
|
||
| public void checkLotto(Lottos ticketAutoCount, Money money) { | ||
| outputView.printLottoAnswer(); | ||
|
|
||
| LottoService lottoService = new LottoService(); | ||
| String lottoAnswer = inputView.inputLottoAnswer(); | ||
|
|
||
| Lotto lottoAnswerObj = null; | ||
| while (lottoAnswerObj == null) { | ||
| try { | ||
| String lottoAnswer = inputView.inputLottoAnswer(); | ||
| lottoAnswerObj = lottoService.parseLottoAnswer(lottoAnswer); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("다시 입력해주세요."); | ||
| } | ||
| } | ||
|
|
||
| outputView.printBonusMessage(); | ||
| int bonusBallNumber = inputView.inputBonusNumber(); | ||
| LottoNumber bonuseBall = new LottoNumber(bonusBallNumber); | ||
|
|
||
| Lotto lottoAnswerObj = lottoService.parseLottoAnswer(lottoAnswer); | ||
| MatchCount matchCount = lottoService.calculateMatchCount(lottoTickets.getTickets(), lottoAnswerObj, bonuseBall); | ||
| LottoNumber bonusBall; | ||
| while (true) { | ||
| try { | ||
| int bonusBallNumber = inputView.inputBonusNumber(); | ||
| bonusBall = new LottoNumber(bonusBallNumber); | ||
| lottoService.validateBonusBall(lottoAnswerObj, bonusBall); | ||
| break; | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("다시 입력해주세요."); | ||
| } | ||
| } | ||
|
|
||
| MatchCount matchCount = lottoService.calculateMatchCount( | ||
| ticketAutoCount.getTickets(), | ||
| lottoAnswerObj, | ||
| bonusBall); | ||
|
|
||
| int totalSum = LottoProfit.LottoSum(matchCount); | ||
| // ProfitRate profitRate = new ProfitRate(money, new LottoTotalPrice(totalSum)); | ||
| ProfitRate profitRate = new ProfitRate(money, new LottoTotalPrice(matchCount)); | ||
|
|
||
| outputView.lottoResult(); | ||
| resultView.printLottoMatch(matchCount); | ||
| resultView.printLottoProfit(profitRate.getProfitRate()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,5 @@ public enum LottoPrice { | |
| public int getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| package domain; | ||
|
|
||
| public class LottoTicketCount { | ||
|
|
||
| private final int count; | ||
|
|
||
| public LottoTicketCount(int count) { | ||
| if(count < 0) { | ||
| throw new IllegalArgumentException("로또 티켓 수는 0장 이상이어야 합니다."); | ||
| } | ||
| this.count = count; | ||
| } | ||
|
|
||
| public int getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| public void validateManualCount(int manualCount) { | ||
| if (manualCount < 0 || manualCount > count) { | ||
| throw new IllegalArgumentException("수동으로 구매할 로또 수는 0 이상이여야 하며, 구매 가능한 티켓 수를 초과할 수 없습니다."); | ||
| } | ||
| } | ||
|
Comment on lines
+18
to
+22
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. 처음에 검증 로직 넣으면서, 수동 구매 수는 0이상에 총 로또 수 보다 작아야 해서 총 로또 수는 변수로 가지고 있는 클래스들을 살펴보다가 클래스 이름도 TicketCount로 포괄적인 것 같아 여기에 넣는게 자연스러울 것 같아서 LottoTicketCount에 넣어두었습니다! |
||
|
|
||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,20 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class LottoTotalPrice { | ||
|
|
||
| private final long totalSum; | ||
|
|
||
| public LottoTotalPrice(MatchCount matchCount) { | ||
| this.totalSum = calculateTotalSum(matchCount); | ||
| } | ||
|
|
||
| private long calculateTotalSum(MatchCount matchCount) { | ||
| return Arrays.stream(LottoPrice.values()) | ||
| .mapToLong(price -> (long) matchCount.getCount(price) * price.getPrice()) | ||
| .sum(); | ||
| this.totalSum = | ||
| ((long) matchCount.getCount(LottoPrice.MATCH_3) * LottoPrice.MATCH_3.getPrice()) + | ||
| ((long) matchCount.getCount(LottoPrice.MATCH_4) * LottoPrice.MATCH_4.getPrice()) + | ||
| ((long) matchCount.getCount(LottoPrice.MATCH_5) * LottoPrice.MATCH_5.getPrice()) + | ||
| ((long) matchCount.getCount(LottoPrice.MATCH_5_BONUS) * LottoPrice.MATCH_5_BONUS.getPrice()) + | ||
| ((long) matchCount.getCount(LottoPrice.MATCH_6) * LottoPrice.MATCH_6.getPrice()); | ||
| } | ||
|
|
||
| public long getTotalSum() { | ||
| return totalSum; | ||
| } | ||
|
|
||
| } |
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.
5단계 요구사항이 없어 보이는데 확인 부탁드려요!
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.
5단계는 리팩토링이라 크게 적은게 없긴 한데, 추가해 두겠습니다!!