Skip to content
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

[Step4] 로또(2등) #1676

Merged
merged 6 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,33 @@ LotteryTicket - 로또


# [4단계] 로또(2등)

// TODO
```text
[... 생략 ...]

지난 주 당첨 번호를 입력해 주세요.
1, 2, 3, 4, 5, 6
보너스 볼을 입력해 주세요.
7

당첨 통계
---------
3개 일치 (5000원)- 1개
4개 일치 (50000원)- 0개
5개 일치 (1500000원)- 0개
5개 일치, 보너스 볼 일치(30000000원) - 0개
6개 일치 (2000000000원)- 0개
총 수익률은 0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)
```

- [x] 보너스볼 변수 생성
- [x] 보너스볼 체크 로직 추가
- [x] 보너스볼 당첨시 전체 금액 확인
- [x] 보너스볼을 물어보는 메세지 출력
- [x] 보너스볼이 포함된 메세지 출력

--- 그외 고려해볼만한 리팩토링
- [x] matchCount 의 integer을 통해 matchType 개선
- [x] infoCenter 테스트 코드 리팩토링

# [5단계] 로또(수동)

Expand Down
22 changes: 15 additions & 7 deletions src/main/java/lottery/InfoCenter.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package lottery;

import static java.util.Objects.*;
import static java.util.stream.Collectors.*;

import java.util.stream.Collectors;

public class InfoCenter {

private final int MATCH_THREE_NUMBER = 3;
private Ticket lastWeekWinningTicket;
private WinningTicket winningTicket;
private int bonus;

public void setLastWeekWinningTicket(Ticket lastWeekWinningTicket) {
void setLastWeekWinningTicket(Ticket lastWeekWinningTicket) {
this.lastWeekWinningTicket = lastWeekWinningTicket;
}

void setBonusNumber(int number) {
this.bonus = number;
}

public Result confirmTicket(Tickets buyerTickets) {
requireNonNull(lastWeekWinningTicket, "지난 주 티켓이 없습니다.");
requireNonNull(bonus, "보너스 볼이 없습니다.");

winningTicket = new WinningTicket(lastWeekWinningTicket, bonus);
return new Result(LotteryMatchTypeMap.of(buyerTickets.getValues().stream()
.map(ticket -> ticket.numbers().matchCountWith(lastWeekWinningTicket.numbers()))
.filter(matchCount -> matchCount >= MATCH_THREE_NUMBER)
.collect(Collectors.groupingBy(LotteryMatchType::fromInteger, summingInt(a -> 1)))));
.map(ticket -> winningTicket.getMatchTypeWith(ticket))
.filter(matchType -> LotteryMatchType.MISS_MATCH != matchType)
.collect(groupingBy(a -> a, summingInt(a -> 1)))));
}

public Ticket lastWeekWinningNumbers() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/lottery/LotteryMatchType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum LotteryMatchType {
THREE_MATCH(3, Money.won(5_000L)),
FOUR_MATCH(4, Money.won(50_000L)),
FIVE_MATCH(5, Money.won(1_500_000L)),
FIVE_MATCH_WITH_BONUS(5, Money.won(30_000_000L)),
SIX_MATCH(6, Money.won(2_000_000_000L));

private final int matchCount;
Expand All @@ -27,7 +28,7 @@ public int matchCount() {

public static LotteryMatchType fromInteger(int matchCount) {
return Arrays.stream(LotteryMatchType.values())
.filter(a -> a.matchCount == matchCount)
.filter(matchType -> matchType.matchCount == matchCount)
.findFirst()
.orElse(MISS_MATCH);
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/lottery/LottoNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public Set<Integer> getValues() {
return Collections.unmodifiableSet(numbers);
}

public int matchCountWith(LottoNumbers numbers){
this.numbers.removeAll(numbers.getValues());
return SIZE_OF_TICKET - this.numbers.size();
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/lottery/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static void main(String[] args) {
.collect(Collectors.toCollection(LinkedHashSet::new));
infoCenter.setLastWeekWinningTicket(Ticket.of(LottoNumbers.from(ints)));

inputView.askForBonusNumber();
int bonus = Integer.parseInt(sc.nextLine().trim());
infoCenter.setBonusNumber(bonus);

Result result = buyer.checkTicket(infoCenter);
float totalYield = result.getTotalYield(initialMoney);
resultView.printResult(result, totalYield);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/lottery/Ticket.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lottery;

import java.util.Objects;
import java.util.Set;

public final class Ticket {

Expand All @@ -19,8 +20,8 @@ private void setNumbers(LottoNumbers lottoNumbers) {
this.lottoNumbers = lottoNumbers;
}

public LottoNumbers numbers() {
return lottoNumbers;
public Set<Integer> numbers() {
return lottoNumbers.getValues();
}

@Override
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/lottery/WinningTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lottery;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

public class WinningTicket {

private final Ticket ticket;
private final int bonus;

Choose a reason for hiding this comment

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

WinningTicket을 정의해주셨네요 👍


public WinningTicket(Ticket ticket, int bonus) {
this.ticket = ticket;
this.bonus = bonus;
}

public Ticket getTicket() {
return ticket;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof WinningTicket))
return false;
WinningTicket that = (WinningTicket)o;
return bonus == that.bonus && Objects.equals(ticket, that.ticket);
}

@Override
public int hashCode() {
return Objects.hash(ticket, bonus);
}

public LotteryMatchType getMatchTypeWith(Ticket targetTicket) {
LinkedHashSet<Integer> targetNumbers = new LinkedHashSet<>(targetTicket.numbers());
targetNumbers.removeAll(this.ticket.numbers());

LotteryMatchType matchType = LotteryMatchType.fromInteger(Ticket.SIZE_OF_TICKET - targetNumbers.size());
if (isBonusMatchCount(bonus, matchType, targetNumbers)) {
return LotteryMatchType.FIVE_MATCH_WITH_BONUS;
}
return matchType;
}

private boolean isBonusMatchCount(int bonus,
LotteryMatchType matchType,
Set<Integer> winningTicketNumbers) {
return matchType == LotteryMatchType.FIVE_MATCH && winningTicketNumbers.contains(bonus);
}
}
16 changes: 11 additions & 5 deletions src/main/java/lottery/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
public class InputView {

public void askForBuyerMoney() {
System.out.println("구입금액을 입력해 주세요");
print("구입금액을 입력해 주세요");
}

public void inputCountOfTickets(int count) {
System.out.println(count + "개를 구매했습니다.");
print(count + "개를 구매했습니다.");
}

public void printTickets(Tickets tickets) {
tickets.getValues().forEach(ticket -> {
Set<Integer> numbers = ticket.numbers().getValues();
System.out.println("[" + numbers.stream()
Set<Integer> numbers = ticket.numbers();
print("[" + numbers.stream()
.map(String::valueOf)
.collect(Collectors.joining(", ")) + "]");
});
}

public void askForLastWeekTickets() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
print("지난 주 당첨 번호를 입력해 주세요.");
}
public void askForBonusNumber(){
print("보너스 볼을 입력해 주세요.");
}

private void print(String x) {
System.out.println(x);
}
}
43 changes: 36 additions & 7 deletions src/main/java/lottery/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
package lottery.view;

import java.util.Arrays;
import java.util.Map;

import lottery.LotteryMatchType;
import lottery.Result;

public class ResultView {

public void printResult(Result result, float yield) {
System.out.println("당첨 통계");
System.out.println("-----------");
result.getResultMap()
.forEach(
(matchType, count) -> System.out.printf("%d개 일치 (%d원) - %d개\n", matchType.matchCount(), matchType.money().amount(), count)
);
System.out.printf("총 수익률은 %.2f 입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)\n", yield);

print("당첨 통계");
print("-----------");
final Map<LotteryMatchType, Integer> resultMap = result.getResultMap();
Arrays.stream(LotteryMatchType.values())
.filter(matchType -> matchType != LotteryMatchType.MISS_MATCH)
.forEachOrdered(
matchType -> {
if (resultMap.containsKey(matchType)) {
makeMatchStringTemplate(matchType, resultMap.get(matchType));
}
makeMatchStringTemplate(matchType, 0);
}
);
System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)\n", yield);
}

private void makeMatchStringTemplate(LotteryMatchType matchType, Integer count) {
StringBuilder sb = new StringBuilder();
StringBuilder append = sb.append(matchType.matchCount())
.append("개 일치");
if (matchType == LotteryMatchType.FIVE_MATCH_WITH_BONUS) {
append.append(", 보너스 볼 일치");
}
append.append(" (")
.append(matchType.money().amount())
.append("원)- " + count + "개");
System.out.println(append);
}

private void print(String 당첨_통계) {
System.out.println(당첨_통계);
}
}