Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/step2/controller/LottoGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public void playLottoGame() {

List<LottoTicket> lottoTickets = lottoGames.buyLottoGame(gameCount);
ResultView.printLottoTicket(lottoTickets);
LottoTicket winningNumber = lottoGames.readWinningNumber(InputView.readWinningNumbers());
LottoTicket winningTicket = lottoGames.readWinningNumber(InputView.readWinningNumbers());
int bonusNumber = InputView.readBonusNumber();

ResultView.printBlankLine();
ResultView.printMessage("당첨 통계");

LottoResultReport lottoResultReport = new LottoResultReport();
for (LottoTicket lottoTicket : lottoTickets) {
lottoResultReport.recordRank(winningNumber.checkLottoTicket(lottoTicket, bonusNumber));
lottoResultReport.recordRank(lottoTicket.checkLottoTicket(winningTicket, bonusNumber));
}

ResultView.printResultReport(lottoResultReport);
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/step2/domain/LottoResultReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@

public class LottoResultReport {

static final int EMPTY_COUNT = 0;
static final int ONE = 1;

private Map<Rank, Integer> lottoResultReport;

public LottoResultReport() {
lottoResultReport = new HashMap<>();
}

public int recordRank(Rank rank) {
if (lottoResultReport.containsKey(rank)) {
Integer cnt = lottoResultReport.get(rank);
lottoResultReport.put(rank, cnt + 1);
return cnt;
}
lottoResultReport.put(rank, 1);
return ONE;
Integer value = lottoResultReport.getOrDefault(rank, 0);
lottoResultReport.put(rank, value + 1);
return lottoResultReport.get(rank);
Comment on lines +16 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public int findReportByMatchCount(Rank rank) {
if (lottoResultReport.containsKey(rank)) {
return lottoResultReport.get(rank);
}
return EMPTY_COUNT;
return lottoResultReport.getOrDefault(rank, 0);
}

public double calculateProfit(int gameCount) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/step2/domain/LottoTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public boolean isContain(Integer number) {
return this.lottoTicket.contains(number);
}

public Rank checkLottoTicket(LottoTicket compareTarget, int bonusNumber) {
public Rank checkLottoTicket(LottoTicket winningTicket, int bonusNumber) {
int count = (int) lottoTicket.stream()
.filter(i -> compareTarget.isContain(i))
.filter(i -> winningTicket.isContain(i))
.count();

if(count == 5 && compareTarget.isContain(bonusNumber)) {
if(count == 5 && isContain(bonusNumber)) {
return Rank.SECOND;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/step2/domain/LottoTicketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static Stream<Arguments> winningNumbersSample() throws Throwable {
@MethodSource("secondRankSample")
public void 로또_2등_당첨(List<Integer> numbers, List<Integer> winningNumbers, int bonusNumber) throws Exception {
LottoTicket winningTicket = new LottoTicket(winningNumbers);
assertThat(winningTicket.checkLottoTicket(new LottoTicket(numbers), bonusNumber)).isEqualTo(Rank.SECOND);
assertThat(new LottoTicket(numbers).checkLottoTicket(winningTicket, bonusNumber)).isEqualTo(Rank.SECOND);
}

static Stream<Arguments> secondRankSample() throws Throwable {
Expand Down