Skip to content

Commit

Permalink
refactor: Result 인스턴스 변수 제거
Browse files Browse the repository at this point in the history
생성시에만 사용되는 변수 제거 및 반환값이 필요없는 매소드 변경
  • Loading branch information
Livenow14 committed Feb 24, 2021
1 parent 763ca2c commit 18a1327
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/main/java/domain/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
import java.util.Map;

public class Result {
private final LottoTickets lottoTickets;
private final WinningLotto winningLotto;
private final Map<LottoRank, Integer> results;

public Result(LottoTickets lottoTickets, WinningLotto winningLotto) {
this.lottoTickets = lottoTickets;
this.winningLotto = winningLotto;
this.results = new HashMap<>();
setResult();
this.results = makeResult(lottoTickets, winningLotto);
}

public BigDecimal findEarningsRate(BettingMoney bettingMoney) {
Expand All @@ -29,18 +24,17 @@ public BigDecimal findEarningsRate(BettingMoney bettingMoney) {
}

public Map<LottoRank, Integer> getResults() {
return results;
return new HashMap<>(results);
}

private Map<LottoRank, Integer> setResult() {
private Map<LottoRank, Integer> makeResult(LottoTickets lottoTickets, WinningLotto winningLotto) {
Map<LottoRank, Integer> copy = new HashMap<>();
List<LottoRank> lottoRanks = lottoTickets.findMatches(winningLotto);
lottoRanks.forEach(this::putResult);
return results;
}

private void putResult(final LottoRank lottoRank) {
results.computeIfPresent(lottoRank, (key, value) -> value + 1);
results.putIfAbsent(lottoRank, 1);
lottoRanks.forEach(lottoRank -> {
copy.computeIfPresent(lottoRank, (key, value) -> value + 1);
copy.putIfAbsent(lottoRank, 1);
});
return copy;
}

private int getTotalPrize(LottoRank lottoRank) {
Expand Down

0 comments on commit 18a1327

Please sign in to comment.