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

[디디] 로또 미션 제출합니다. #129

Merged
merged 18 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/main/java/lotto/domain/controller/LottoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ private WinningLotto createWinningLotto() {
private void createResult(PurchaseMoney money, Lottos lottos, WinningLotto winningLotto) {
LottoGame game = new LottoGame(lottos, winningLotto, money);
GameResult gameResult = game.getResult();
OutputView.printResult(gameResult);
OutputView.printResult(gameResult, gameResult.calculateProfit(money));
}
}
3 changes: 1 addition & 2 deletions src/main/java/lotto/domain/model/LottoGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ public LottoGame(Lottos lottos, WinningLotto winningLotto, PurchaseMoney money)

public GameResult getResult() {
List<Rank> ranks = Arrays.asList(Rank.values());
GameResult gameResult = new GameResult(ranks);
GameResult gameResult = new GameResult();

for (Lotto lotto : lottos) {
Rank rank = winningLotto.isWinningLotto(lotto);
gameResult.count(rank);
}
gameResult.calculateProfit(money);
return gameResult;
}
}
18 changes: 6 additions & 12 deletions src/main/java/lotto/domain/result/GameResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

import lotto.domain.PurchaseMoney;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GameResult {
public static final int PERCENTAGE = 100;
private final Map<Rank, Count> statistic;
private double profit;

public GameResult(List<Rank> ranks) {
public GameResult() {
statistic = new ConcurrentHashMap<>();
for (Rank rank : ranks) {
for (Rank rank : Rank.values()) {
statistic.put(rank, new Count());
}
this.profit = 0;
}

public void calculateProfit(PurchaseMoney money) {
profit = statistic.keySet().stream()
public double calculateProfit(PurchaseMoney money) {
double profit = statistic.keySet().stream()
.mapToDouble(rank -> rank.getPrize() * getCountByRank(rank).getCount())
.sum();
profit = profit / money.getPurchaseMoney() * 100;
return profit / money.getPurchaseMoney() * PERCENTAGE;
}

public void count(Rank rank) {
Expand All @@ -38,8 +36,4 @@ public Map<Rank, Count> getStatistic() {
public Count getCountByRank(Rank rank) {
return statistic.get(rank);
}

public double getProfit() {
return profit;
}
}
29 changes: 15 additions & 14 deletions src/main/java/lotto/domain/result/Rank.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lotto.domain.result;

import java.util.Arrays;
import java.util.stream.Stream;

public enum Rank {
DEFAULT(0, 0, false),
Expand All @@ -11,7 +10,6 @@ public enum Rank {
BONUS(5, 30_000_000, true),
SIX(6, 2_000_000_000, false);

public static final int FIVE_MATCH = 5;
private final int matchingNumbers;
private final int prize;
private final boolean bonusMatching;
Expand All @@ -23,16 +21,9 @@ public enum Rank {
}

public static Rank getRank(int numberOfMatch, boolean isBonus) {
Stream<Rank> rankStream = Arrays.stream(values())
.filter(statistic -> statistic.matchingNumbers == numberOfMatch);

if (numberOfMatch != FIVE_MATCH) {
return rankStream
.findFirst()
.orElse(DEFAULT);
}
return rankStream
.filter(statistic -> statistic.bonusMatching == isBonus)
return Arrays.stream(values())
.filter(rank -> rank.matchingNumbers == numberOfMatch)
.filter(rank->rank.checkBonus(isBonus))
Copy link

Choose a reason for hiding this comment

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

띄어쓰기 컨벤션 위반입니다.

.findFirst()
.orElse(DEFAULT);
}
Expand All @@ -49,7 +40,17 @@ public boolean isNotDefault() {
return matchingNumbers != 0;
}

public boolean isBonusMatching() {
return matchingNumbers == FIVE_MATCH && bonusMatching;
public boolean checkBonus(boolean isBonus) {
if (this == BONUS || this == FIVE) {
return bonusMatching == isBonus;
}
return true;
}

public boolean isBonus(){
if(this==BONUS){
Copy link

Choose a reason for hiding this comment

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

띄어쓰기 컨벤션 위반입니다. 그리고 if분기문을 축약해 쓸 수 있겠네요~

return true;
}
return false;
}
}
6 changes: 3 additions & 3 deletions src/main/java/lotto/view/OutputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public static void printLottos(Lottos lottos) {
System.out.println();
}

public static void printResult(GameResult gameResult) {
public static void printResult(GameResult gameResult, double profit) {
System.out.println("\n당첨 통계 \n ==============");

printRank(gameResult.getStatistic());
printProfit(gameResult.getProfit());
printProfit(profit);
}

private static void printRank(Map<Rank, Count> statistic) {
Expand Down Expand Up @@ -59,7 +59,7 @@ private static void printSecond(Rank rank, Count count) {
}

private static boolean checkBonus(Rank rank, Count count) {
if (rank.isBonusMatching()) {
if (rank.isBonus()) {
printSecond(rank, count);
return true;
}
Expand Down
9 changes: 2 additions & 7 deletions src/test/java/lotto/domain/gameResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import lotto.domain.result.GameResult;
import lotto.domain.result.Rank;
import org.assertj.core.api.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -18,7 +14,7 @@ public class gameResultTest {

@BeforeEach
void initGameResult() {
gameResult = new GameResult(Arrays.asList(Rank.values()));
gameResult = new GameResult();
gameResult.count(Rank.THREE);
gameResult.count(Rank.THREE);
gameResult.count(Rank.THREE);
Expand All @@ -39,7 +35,6 @@ void countTest() {
@DisplayName("수익률을 잘 계산하는지 테스트")
void calculateProfitTest() {
PurchaseMoney money = new PurchaseMoney("6000");
gameResult.calculateProfit(money);
assertThat(gameResult.getProfit()).isEqualTo(PROFIT / 6000 * 100);
assertThat(gameResult.calculateProfit(money)).isEqualTo(PROFIT / 6000 * 100);
}
}