-
Notifications
You must be signed in to change notification settings - Fork 12
Step2 - 로또 게임 2단계 #15
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: pageprologue
Are you sure you want to change the base?
Changes from 14 commits
ceb411e
250b0a0
5079c3d
76114b5
7889a2c
3ce0976
0f9d2a0
7f7ae6c
4f4b103
1fe76e0
6983ea5
2094b29
4089514
ab32b7c
b2792af
2a6e9d8
af61ec9
75f5df1
491f190
a01d40a
db7c747
7bb61e0
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,40 +1,45 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.*; | ||
| import lotto.domain.lotto.LottoTickets; | ||
| import lotto.domain.util.StringUtil; | ||
| import lotto.domain.vending.BuyingPrice; | ||
| import lotto.domain.vending.LottoTicketVendingMachine; | ||
| import lotto.domain.vending.TicketAmount; | ||
| import lotto.domain.winning.WinningLottoRank; | ||
| import lotto.domain.winning.WinningNumbers; | ||
| import lotto.domain.winning.WinningStatistics; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static lotto.domain.LottoTicketVendingMachine.TICKET_PRICE; | ||
|
|
||
| public class LottoController { | ||
| public void run() { | ||
|
|
||
| private static final LottoTicketVendingMachine lottoTicketVendingMachine = new LottoTicketVendingMachine(); | ||
|
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. 클래스 내부에서 new로 선언하며 의존하는 것과 주입받아서 사용하는 것에 대해서 고민해보면 좋을 것 같아요.
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. 넵~! LottoService 객체를 새로 만들었고, 여기에서 LottoTicketVendingMachine을 생성자로 주입받도록 변경하였습니다. |
||
|
|
||
| public static void run() { | ||
| String inputPrice = InputView.getBuyingPrice(); | ||
| BuyingPrice buyingPrice = new BuyingPrice(inputPrice); | ||
| int ticketAmount = buyingPrice.divide(TICKET_PRICE); | ||
| OutputView.printTicketAmount(ticketAmount); | ||
|
|
||
| LottoTicketVendingMachine lottoTicketVendingMachine = new LottoTicketVendingMachine(); | ||
| List<LottoTicket> lottoTickets = lottoTicketVendingMachine.issueTickets(buyingPrice); | ||
| String inputManualCount = InputView.getManualCount(); | ||
| TicketAmount ticketAmount = new TicketAmount(buyingPrice, inputManualCount); | ||
|
|
||
| List<String> inputManualNumbers = InputView.getManualNumbers(ticketAmount); | ||
| LottoTickets lottoTickets = lottoTicketVendingMachine.issueTickets(ticketAmount, inputManualNumbers); | ||
|
|
||
| OutputView.printTicketAmount(ticketAmount); | ||
| OutputView.printLottoTickets(lottoTickets); | ||
|
|
||
| String inputWinningNumbers = InputView.getWinningNumber(); | ||
| List<Integer> splitWinningNumbers = InputView.split(inputWinningNumbers) | ||
| .stream() | ||
| .map(Integer::parseInt) | ||
| .collect(Collectors.toList()); | ||
| List<Integer> splitWinningNumbers = StringUtil.splitParseInt(inputWinningNumbers); | ||
|
|
||
| int bonusNumber = Integer.parseInt(InputView.getBonusNumber()); | ||
| WinningNumbers winningNumbers = new WinningNumbers(splitWinningNumbers, bonusNumber); | ||
| WinningStatistics winningStatistics = new WinningStatistics(winningNumbers); | ||
|
|
||
| Map<LottoPrize, Integer> ranks = winningStatistics.groupByWinningNumber(lottoTickets); | ||
| OutputView.printWinningStatistics(ranks); | ||
| WinningLottoRank winningLottoRank = new WinningLottoRank(lottoTickets, winningNumbers); | ||
| OutputView.printWinningStatistics(winningLottoRank); | ||
|
|
||
| float profitRate = winningStatistics.profitRate(ticketAmount, ranks); | ||
| WinningStatistics winningStatistics = new WinningStatistics(winningLottoRank); | ||
| float profitRate = winningStatistics.profitRate(ticketAmount); | ||
| OutputView.printProfitRate(profitRate); | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,17 @@ | ||
| package lotto.domain; | ||
| package lotto.domain.lotto; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static lotto.domain.LottoNumber.MAX_LOTTO_BOUND; | ||
| import static lotto.domain.LottoNumber.MIN_LOTTO_BOUND; | ||
| import static lotto.domain.lotto.LottoNumber.MAX_LOTTO_BOUND; | ||
| import static lotto.domain.lotto.LottoNumber.MIN_LOTTO_BOUND; | ||
|
|
||
| public class LottoGenerator { | ||
|
|
||
| private static final List<LottoNumber> lottoNumberContainer = create(); | ||
| private static final List<LottoNumber> LOTTO_NUMBER_CONTAINER = create(); | ||
|
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. LottoNumber에 대한 cache가 이곳에 위치해야 하는지 고민해보면 좋을 것 같아요.
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. LottoGenerator 의 역할을 LottoTicketVendingMachine 로 옮겼습니다~! 그리고 로또 번호 인스턴스를 생성하는 부분은 로또 티켓이 자동 생성인지, 수동 생성인지에 따라 차이가 생기게 되었습니다.
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. 넵 이 부분 다시 수정하였습니다~! |
||
|
|
||
| private static List<LottoNumber> create() { | ||
| return IntStream.rangeClosed(MIN_LOTTO_BOUND, MAX_LOTTO_BOUND) | ||
|
|
@@ -20,8 +20,8 @@ private static List<LottoNumber> create() { | |
| } | ||
|
|
||
| public List<LottoNumber> issueAutoLottoNumbers() { | ||
| Collections.shuffle(lottoNumberContainer); | ||
| return lottoNumberContainer.subList(0, 6).stream() | ||
| Collections.shuffle(LOTTO_NUMBER_CONTAINER); | ||
| return LOTTO_NUMBER_CONTAINER.subList(0, 6).stream() | ||
|
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과 6이 의미하는 바가 무엇일까요? |
||
| .sorted(Comparator.comparing(LottoNumber::value)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package lotto.domain; | ||
| package lotto.domain.lotto; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
|
|
@@ -13,6 +13,10 @@ public LottoNumber(final int lottoNumber) { | |
| this.lottoNumber = lottoNumber; | ||
| } | ||
|
|
||
| public LottoNumber(String lottoNumber) { | ||
| this(Integer.parseInt(lottoNumber)); | ||
| } | ||
|
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. Integer.parseInt(lottoNumber)를 했는데 lottoNumber가 숫자가 아닌 문자열이면 어떤 일이 벌어질까요?
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. LOTTO_NUMBER_CONTAINER 에서 6개의 볼을 꺼내기 위해 ArrayList.subList 를 사용하였습니다. 파라미터로 받는 fromIndex와 toIndex를 따로 상수화 하지 않았었는데, 6이라는 숫자는 LOTTO_NUMBER_SIZE 상수로 대체 하였습니다. 0도 상수로 빼는 것이 좋을까요? 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. 엇 제가 남긴 코멘트는 IntegerParseInt에 대해서 valid하지 않은 String이 들어오면 어떻게 되는지에 대한 물음이었는데,
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. 아 이 부분 바로 위에 남긴 코멘트였는데 잘못 달았네요..ㅎ |
||
|
|
||
| private void validateBound(int lottoNumber) { | ||
| if (lottoNumber < MIN_LOTTO_BOUND || lottoNumber > MAX_LOTTO_BOUND) { | ||
| throw new IllegalArgumentException("로또 번호는 1 ~ 45 까지 입니다."); | ||
|
|
||
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.
LottoController에서 run하는 것도 instance 생성에서 static으로 변경한 이유가 있을까요?
이전 리뷰에서 제가 질문을 드린건 무조건 변경하라는 것이 아니라 어떤 차이가 있을까에 대해 고민해보시면 좋을 것 같다는 부분이었어요!
저는 둘 다 괜찮다고 생각하는데 '왜 그렇게 선택했는가'에 대한 기준은 중요하다고 생각합니다!
Uh oh!
There was an error while loading. Please reload this page.
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.
이 부분에 대해서 static 키워드와 정적 팩터리 메서드 부분을 다시 살펴보았습니다.
static 영역에 할당된 메모리는 모든 객체가 공유하고, 프로그램의 종료시까지 메모리가 할당된 채로 존재하게 됩니다. 모든 객체가 공유한다는 점에서 사이드 이펙트 영향이 커 질 수 있습니다. 또, 프로그램이 종료되기 전에 항상 메모리에 상주하고 있어서 자주 사용하지 않는 메서드가 누적된다면 GC에 수거되지 못하므로 메모리 낭비가 발생할 수 있습니다.
인스턴스 생성과 비교하여 정적 팩터리 메서드의 장점에 대해 정리해 보았습니다.
1. 생성자는 의미있는 명명을 하기 어렵지만, 정적 팩토리 메소드는 의미있는 명명이 가능하다.
2. 정적 팩토리 메소드는 메소드 리턴 타입 범위 내에서 다양한 리턴 타입의 정의가 가능하다.
3. 정적 팩토리 메소드는 인스턴스를 생성하기 위한 로직을 캡슐화 할 수 있다.
여기에서는 LottoController 에서 메소드를 작게 분리하고, 의미 있는 명명을 하기 위해 인스턴스를 생성하여 메서드를 호출하는 방식으로 변경하였습니다~!