-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LottoService를 추가하여 LottoGameMachine의 책임을 이동하였고, DTO를 제거하면서 필요한 로직을 수정함. result 클래스 변경 및 테스트를 추가하였음
- Loading branch information
Showing
22 changed files
with
211 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.