-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Step4] 로또(2등) #1676
Changes from 2 commits
7fc89fa
728cfb1
3fd2490
0243810
b196404
fb32db9
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,23 +1,29 @@ | ||||||
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 int bonus; | ||||||
|
||||||
public void setLastWeekWinningTicket(Ticket lastWeekWinningTicket) { | ||||||
this.lastWeekWinningTicket = lastWeekWinningTicket; | ||||||
} | ||||||
|
||||||
void setBonusNumber(int number) { | ||||||
this.bonus = number; | ||||||
} | ||||||
|
||||||
public Result confirmTicket(Tickets buyerTickets) { | ||||||
requireNonNull(lastWeekWinningTicket, "지난 주 티켓이 없습니다."); | ||||||
requireNonNull(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 -> ticket.numbers().getMatchTypeWith(lastWeekWinningTicket.numbers(), bonus)) | ||||||
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. 규칙 4: 한 줄에 점을 하나만 찍는다. 위 객체지향 생활 체조 원칙 관점에서 봤을 때 아래와 같이 호출되는건 어떨까요?
Suggested change
|
||||||
.filter(matchType -> LotteryMatchType.MISS_MATCH != matchType) | ||||||
.collect(groupingBy(a -> a, summingInt(a -> 1))))); | ||||||
} | ||||||
|
||||||
public Ticket lastWeekWinningNumbers() { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,11 +27,30 @@ public Set<Integer> getValues() { | |||||||||||||||||||||||||||
return Collections.unmodifiableSet(numbers); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public int matchCountWith(LottoNumbers numbers){ | ||||||||||||||||||||||||||||
public LotteryMatchType getMatchTypeWith(LottoNumbers numbers, int bonus) { | ||||||||||||||||||||||||||||
LotteryMatchType matchType = LotteryMatchType.fromInteger(getSameNumberCount(numbers)); | ||||||||||||||||||||||||||||
if (isBonusMatchCount(bonus, matchType)) { | ||||||||||||||||||||||||||||
return LotteryMatchType.FIVE_MATCH_WITH_BONUS; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
if (isNotContainsBonus(bonus, matchType)) { | ||||||||||||||||||||||||||||
return LotteryMatchType.FIVE_MATCH; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
return matchType; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
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.
if를 2개 사용한다고 위 원칙이 지켜진다고 보긴 어려울 것 같습니다. enum의 values()는 순서를 보장했던 것 같은데 그렇다고 하면 FIVE_MATCH가 우선적으로 반환되어 아래와 같이도 사용할 수 있을 것 같아요. 다만 조금 혼란을 줄 순 있으니 여러 방법들을 한번 고민해보시면 좋을 것 같습니다.
Suggested change
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. 저도 이부분에 대해서 고민이 많았습니다. values()가 순서를 보장하지만, enum의 순서를 개발자가 조금이라도 변경하면- 사이드 이펙트가 발견될 수 있을 확률이 높은 것 같아, 위와같이 수정했습니다. 조금 더 고민해보겠습니다! |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
private int getSameNumberCount(LottoNumbers numbers) { | ||||||||||||||||||||||||||||
this.numbers.removeAll(numbers.getValues()); | ||||||||||||||||||||||||||||
return SIZE_OF_TICKET - this.numbers.size(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
private boolean isBonusMatchCount(int bonus, LotteryMatchType matchType) { | ||||||||||||||||||||||||||||
return matchType == LotteryMatchType.FIVE_MATCH && this.numbers.contains(bonus); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
private boolean isNotContainsBonus(int bonus, LotteryMatchType matchType) { | ||||||||||||||||||||||||||||
return matchType == LotteryMatchType.FIVE_MATCH && (this.numbers.contains(bonus) == false); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||
public boolean equals(Object o) { | ||||||||||||||||||||||||||||
if (this == o) | ||||||||||||||||||||||||||||
|
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(당첨_통계); | ||
} | ||
} |
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.
Ticket을 상속 혹은 위임해서 winningTicket에 보너스 번호를 추가하고 로또의 제약이나 기능을 그대로 사용할 수 있지 않을까요?
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.
생각해보니, 그렇게 할수 있겠네요. 한번 시도해보겠습니다!