Skip to content

Commit

Permalink
refactor: DTO 제거 및 책임 분리
Browse files Browse the repository at this point in the history
LottoService를 추가하여 LottoGameMachine의 책임을 이동하였고,
DTO를 제거하면서 필요한 로직을 수정함.
result 클래스 변경 및 테스트를 추가하였음
  • Loading branch information
Livenow14 committed Feb 21, 2021
1 parent 89799fc commit 2fac4d6
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 264 deletions.
6 changes: 5 additions & 1 deletion src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import domain.LottoGameMachine;
import service.LottoService;
import view.LottoGameScreen;
import view.MainScreen;

public class Application {
public static void main(String[] args) {
GameManageApplication gameManageApplication = new GameManageApplication(new MainScreen(), new LottoGameScreen());
LottoGameMachine lottoGameMachine = new LottoGameMachine();
LottoService lottoService = new LottoService(lottoGameMachine);
GameManageApplication gameManageApplication = new GameManageApplication(new MainScreen(), new LottoGameScreen(), lottoService);
gameManageApplication.run();
}
}
44 changes: 34 additions & 10 deletions src/main/java/GameManageApplication.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import domain.LottoGameMachine;
import domain.bettingMoney.BettingMoney;
import domain.lotto.LottoTickets;
import domain.lotto.TicketCount;
import domain.lotto.WinningLotto;
import domain.result.Result;
import service.LottoService;
import util.InputUtil;
import view.LottoGameScreen;
import view.MainScreen;
import view.dto.LottoGameResultDto;

import java.util.Set;

public class GameManageApplication {
private final MainScreen mainScreen;
private final LottoGameScreen lottoGameScreen;
private final LottoService lottoService;

public GameManageApplication(final MainScreen mainScreen, final LottoGameScreen lottoGameScreen) {
public GameManageApplication(final MainScreen mainScreen, final LottoGameScreen lottoGameScreen, LottoService lottoService) {
this.mainScreen = mainScreen;
this.lottoGameScreen = lottoGameScreen;
this.lottoService = lottoService;
}

public void run() {
LottoGameMachine lottoGameMachine = lottoGameManageInitialize();
LottoTickets lottoTickets = lottoGameMachine.makeLottos();
WinningLotto winnings = lottoGameMachine.findWinnings();
lottoGameMachine.lottoDraw(lottoTickets, winnings);
BettingMoney bettingMoney = getBettingMoney();
TicketCount ticketCount = getTicketCount(bettingMoney);
mainScreen.showTicketCount(ticketCount);
LottoTickets lottoTickets = lottoService.getLottoTickets(bettingMoney);
lottoGameScreen.showAllLottoStatus(lottoTickets.getLottoTickets());
WinningLotto winningLotto = getWinningLotto();

Result result = new Result(lottoTickets, winningLotto);
lottoGameScreen.showGameResult(new LottoGameResultDto(result.getResults()));
lottoGameScreen.showRevenueResult(result.findEarningsRate(bettingMoney));
}

private WinningLotto getWinningLotto() {
lottoGameScreen.confirmWinningLotto();
Set<Integer> winningNumbers = InputUtil.inputWinningNumbers();
lottoGameScreen.confirmBonusLotto();
int bonusNumber = InputUtil.inputBonusNumber();
return new WinningLotto(winningNumbers, bonusNumber);
}

private TicketCount getTicketCount(final BettingMoney bettingMoney) {
int ticketCount = bettingMoney.getTicketCount();
return TicketCount.of(ticketCount);
}

private LottoGameMachine lottoGameManageInitialize() {
private BettingMoney getBettingMoney() {
mainScreen.showInputMoney();
int input = InputUtil.nextInt();
BettingMoney bettingMoney = BettingMoney.of(input);
LottoGameMachine lottoGameMachine = new LottoGameMachine(bettingMoney, lottoGameScreen);
return lottoGameMachine;
return BettingMoney.of(input);
}
}
90 changes: 10 additions & 80 deletions src/main/java/domain/LottoGameMachine.java
Original file line number Diff line number Diff line change
@@ -1,94 +1,24 @@
package domain;

import domain.ball.LottoBall;
import domain.ball.LottoBalls;
import domain.bettingMoney.BettingMoney;
import domain.lotto.LottoTicket;
import domain.lotto.LottoTickets;
import domain.lotto.TicketCount;
import domain.lotto.WinningLotto;
import domain.result.LottoRank;
import domain.result.Result;
import util.InputUtil;
import util.RandomLottoUtil;
import view.LottoGameScreen;
import view.dto.DrawResultDto;
import view.dto.LottoCountResponseDto;
import view.dto.LottoResponseDto;
import view.dto.RevenueDto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoGameMachine {
private final BettingMoney bettingMoney;
private final LottoGameScreen lottoGameScreen;

public LottoGameMachine(final BettingMoney bettingMoney, LottoGameScreen lottoGameScreen) {
this.bettingMoney = bettingMoney;
this.lottoGameScreen = lottoGameScreen;
}

public LottoTickets makeLottos() {
TicketCount ticketCount = getTicketCount();
LottoGameScreen lottoGameScreen = new LottoGameScreen();
lottoGameScreen.showLottoCount(new LottoCountResponseDto(ticketCount.getTicketCount()));

LottoTickets lottoTickets = makeLottos(ticketCount);
List<LottoTicket> lottoTicketGroup = lottoTickets.getLottos();

List<LottoResponseDto> lottoResponseDtos = makeLottoResponseDtos(lottoTicketGroup);
lottoGameScreen.showAllLottoStatus(lottoResponseDtos);
return lottoTickets;
}

private List<LottoResponseDto> makeLottoResponseDtos(final List<LottoTicket> lottoTicketGroup) {
List<LottoResponseDto> lottoResponseDtos = lottoTicketGroup.stream()
.map(lotto -> (new LottoResponseDto(lotto)))
public List<LottoTicket> buyTickets(BettingMoney bettingMoney) {
int ticketCount = bettingMoney.getTicketCount();
return IntStream.range(0, ticketCount)
.mapToObj(count -> makeTicket())
.collect(Collectors.toList());
return lottoResponseDtos;
}

private LottoTickets makeLottos(final TicketCount ticketCount) {
List<LottoTicket> lottoTickets = new ArrayList<>();
for (int i = 0; i < ticketCount.getTicketCount(); i++) {
lottoTickets.add(new LottoTicket(RandomLottoUtil.generateLottoNumbers()));
}
return new LottoTickets(lottoTickets);
}

private TicketCount getTicketCount() {
int lottoCount = bettingMoney.getTicketCount();
return TicketCount.of(lottoCount);
}

public WinningLotto findWinnings() {
lottoGameScreen.confirmWinningLotto();
Set<Integer> winningNumbers = InputUtil.inputWinningNumbers();

lottoGameScreen.confirmBonusLotto();
int bonusNumber = InputUtil.inputBonusNumber();

return new WinningLotto(winningNumbers, bonusNumber);
}

public void lottoDraw(LottoTickets lottoTickets, WinningLotto winnings) {
Result result = new Result(lottoTickets);
Map<LottoRank, Integer> matches = result.findMatches(winnings);
lottoGameScreen.showDrawResult(new DrawResultDto(matches));
int prizeMoney = 0;

List<Integer> prizes = matches.entrySet().stream()
.map(Map.Entry::getKey)
.map(LottoRank::getPrize)
.collect(Collectors.toList());
for (int prize : prizes) {
prizeMoney += prize;
}

BigDecimal revenue = bettingMoney.getRevenue(prizeMoney);
lottoGameScreen.showRevenueResult(new RevenueDto(revenue));
private LottoTicket makeTicket() {
List<LottoBall> lottoBalls = LottoBalls.getRandomLottoBalls();
return new LottoTicket(new LottoBalls(lottoBalls));
}
}
14 changes: 11 additions & 3 deletions src/main/java/domain/ball/LottoBall.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Objects;

public class LottoBall {
private static final int MIN_LOTTO_VALUE = 1;
private static final int MAX_LOTTO_VALUE = 45;
public class LottoBall implements Comparable<LottoBall> {
public static final int MIN_LOTTO_VALUE = 1;
public static final int MAX_LOTTO_VALUE = 45;
public static final String PERMIT_LOTTO_NUMBER = "%d~%d 사이의 번호만 허용합니다.";

private final int value;
Expand Down Expand Up @@ -40,5 +40,13 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(value);
}

@Override
public int compareTo(LottoBall o) {
if (this.value > o.value) {
return 1;
}
return -1;
}
}

22 changes: 19 additions & 3 deletions src/main/java/domain/ball/LottoBalls.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static domain.ball.LottoBall.MAX_LOTTO_VALUE;
import static domain.ball.LottoBall.MIN_LOTTO_VALUE;

public class LottoBalls {
private static final int LOTTO_NUMBERS_SIZE = 6;
private static final int LOTTO_BALL_SIZE = 6;

private final List<LottoBall> lottoBalls;

Expand All @@ -18,6 +23,17 @@ public LottoBalls(final List<LottoBall> lottoBalls) {
this.lottoBalls = copy;
}

public static List<LottoBall> getRandomLottoBalls() {
List<LottoBall> lottoBalls = IntStream.rangeClosed(MIN_LOTTO_VALUE, MAX_LOTTO_VALUE)
.mapToObj(LottoBall::new)
.collect(Collectors.toList());
Collections.shuffle(lottoBalls);
return lottoBalls.stream()
.limit(LOTTO_BALL_SIZE)
.sorted()
.collect(Collectors.toList());
}

private void validateLottoNumbers(final List<LottoBall> lottoBalls) {
validateDuplicate(lottoBalls);
validateSize(lottoBalls);
Expand All @@ -32,7 +48,7 @@ private void validateDuplicate(final List<LottoBall> lottoBalls) {
}

private void validateSize(final List<LottoBall> lottoNumbers) {
if (lottoNumbers.size() != LOTTO_NUMBERS_SIZE) {
if (lottoNumbers.size() != LOTTO_BALL_SIZE) {
throw new IllegalArgumentException("6개의 로또 번호가 필요합니다.");
}
}
Expand All @@ -42,7 +58,7 @@ public boolean containNumber(final LottoBall targetLottoBall) {
.anyMatch(targetLottoBall::equals);
}

public List<LottoBall> getLottoNumbers() {
public List<LottoBall> getLottoBalls() {
List<LottoBall> copy = new ArrayList<>(this.lottoBalls);
return Collections.unmodifiableList(copy);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/domain/bettingMoney/BettingMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public int getTicketCount() {
return this.bettingMoney / TICKET_PRICE;
}

public BigDecimal getRevenue(final int prizeMoney) {
public BigDecimal getEarningRate(final int prizeMoney) {
BigDecimal bettingMoney = BigDecimal.valueOf(this.bettingMoney);
BigDecimal prize = BigDecimal.valueOf(prizeMoney);
return bettingMoney.divide(prize);
return prize.divide(bettingMoney);
}
}
4 changes: 2 additions & 2 deletions src/main/java/domain/lotto/LottoTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public LottoTicket(final LottoBalls lottoBalls) {
this.lottoBalls = lottoBalls;
}

public List<LottoBall> findLottoNumbers() {
List<LottoBall> lottoBalls = this.lottoBalls.getLottoNumbers();
public List<LottoBall> getLottoBalls() {
List<LottoBall> lottoBalls = this.lottoBalls.getLottoBalls();
return Collections.unmodifiableList(lottoBalls);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/domain/lotto/LottoTickets.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public LottoTickets(List<LottoTicket> lottoTickets) {
this.lottoTickets = new ArrayList<>(lottoTickets);
}

public List<LottoTicket> getLottos() {
public List<LottoTicket> getLottoTickets() {
ArrayList<LottoTicket> copy = new ArrayList<>(this.lottoTickets);
return Collections.unmodifiableList(copy);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/domain/lotto/TicketCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class TicketCount {
private static final int ZERO = 0;
private static final String TICKET_MINIMUM_SIZE_EXCEPTION_MESSAGE = "최소 한 개의 로또를 구매해야 합니다. 현재 갯수: %d";

private final int ticketCount;

Expand All @@ -16,7 +17,7 @@ public static TicketCount of(final int lottoCount) {

private void validateLottoCount(final int lottoCount) {
if (lottoCount <= ZERO) {
throw new IllegalArgumentException("최소 한 개의 로또를 구매해야 합니다.");
throw new IllegalArgumentException(String.format(TICKET_MINIMUM_SIZE_EXCEPTION_MESSAGE, lottoCount));
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/domain/result/LottoRank.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public static LottoRank findByBonusWithMatches(final boolean containBonus, final
return FIVE_AND_BONUS_MATCHES;
}

public int getMatches() {
return matches;
}

private boolean isSameMatches(final int matches) {
return this.matches == matches;
}
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/domain/result/Result.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package domain.result;

import domain.bettingMoney.BettingMoney;
import domain.lotto.LottoTickets;
import domain.lotto.WinningLotto;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
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) {
public Result(LottoTickets lottoTickets, WinningLotto winningLotto) {
this.lottoTickets = lottoTickets;
this.winningLotto = winningLotto;
this.results = new HashMap<>();
setResult();
}

public Map<LottoRank, Integer> findMatches(WinningLotto winningLotto) {
private Map<LottoRank, Integer> setResult() {
List<LottoRank> lottoRanks = lottoTickets.findMatches(winningLotto);
for (LottoRank lottoRank : lottoRanks) {
putResult(lottoRank);
}
lottoRanks.forEach(this::putResult);
return results;
}

public BigDecimal findEarningsRate(BettingMoney bettingMoney) {
int prize = results.entrySet().stream()
.map(Map.Entry::getKey)
.mapToInt(lottoRank -> lottoRank.getPrize() * results.get(lottoRank))
.sum();
return bettingMoney.getEarningRate(prize);
}

private void putResult(final LottoRank lottoRank) {
if (!results.containsKey(lottoRank)) {
results.put(lottoRank, 1);
return;
}
results.put(lottoRank, results.get(lottoRank) + 1);
}

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

0 comments on commit 2fac4d6

Please sign in to comment.