Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 55 additions & 0 deletions src/main/java/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import domain.LottoMachine;
import domain.Profit;
import domain.WinningStatus;
import domain.lastweekwinninglotto.LastWeekWinningBonusBall;
import domain.lastweekwinninglotto.LastWeekWinningLotto;
import domain.lottoticket.LottoTicket;
import domain.lottoticket.Lottos;
import domain.lottoticket.NumberOfLottoTicket;
import domain.strategy.RandomLottoNumberStrategy;
import ui.Printer;
import ui.Receiver;

import java.util.ArrayList;
import java.util.List;

public class LottoApplication {
private final Printer printer = new Printer();
private final Receiver receiver = new Receiver();
private static LottoMachine lottoMachine = new LottoMachine();
private final RandomLottoNumberStrategy randomLottoNumberStrategy = new RandomLottoNumberStrategy();

public void run() {
printer.requestPurchaseAmount();
NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(receiver.receiveLottoTotalAmount());
Lottos lottos = makeLottos(numberOfLottoTicket);
printer.printAllLotto(lottos);

printer.requestLastWeekLottoWinningNumber();
List<Integer> LastWeekLottoWinningNumbers = receiver.receiveLastWeekLottoWinningNumbers();
LastWeekWinningLotto lastWeekWinningLotto = new LastWeekWinningLotto(LastWeekLottoWinningNumbers);

printer.requestLottoBonusBallNumber();
LastWeekWinningBonusBall lastWeekWinningBonusBall = new LastWeekWinningBonusBall(receiver.receiveLottoBonusBallNumber());
ArrayList<WinningStatus> getLottoPrices = lottoMachine.Discriminator(lottos, lastWeekWinningLotto, lastWeekWinningBonusBall, numberOfLottoTicket);
Profit profit = new Profit(getLottoPrices, numberOfLottoTicket);

printer.printLottoProfit(profit.getProfit());
printer.printIsLottoProfit(profit.isProfit());
}

private Lottos makeLottos(NumberOfLottoTicket numberOfLottoTicket) {
ArrayList<LottoTicket> lottoDummy = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ArrayList<LottoTicket> lottoDummy = new ArrayList<>();
List<LottoTicket> lottoDummy = new ArrayList<>();

Interface!

for (int i = 0; i < numberOfLottoTicket.getNumberOfLottoTicket(); i++) {
LottoTicket lotto = new LottoTicket(randomLottoNumberStrategy.getRandomLottoNumbers());
lottoDummy.add(lotto);
}
Lottos lottos = new Lottos(lottoDummy);
return lottos;
}

public static void main(String[] args) {
LottoApplication app = new LottoApplication();
app.run();
}
}
59 changes: 59 additions & 0 deletions src/main/java/domain/EnumWinningStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package domain;

import domain.lottoticket.NumberOfLottoTicket;
import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;
import ui.Printer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class EnumWinningStatus {
private static final int NOT_MATCH_LOTTO = 0;
private ArrayList<WinningStatus> lottoPrices = new ArrayList<>();
private Map<WinningStatus, Integer> mappingLottoWithBonusBall = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

HashMap을 통해서 관리하셨군요!
좋은 방법인 것 같아요.

private Printer printer = new Printer();

public ArrayList<WinningStatus> getLottoPrices(LottoFactory lottoFactory, NumberOfLottoTicket numberOfLottoTicket){
LottoWinningResult lottoWinningResults = lottoFactory.getLottoWinningResult();
LottoWinningBonusBallResult lottoWinningBonusBallResult = lottoFactory.getLottoWinningBonusBallResult();

for (int i = 0; i < numberOfLottoTicket.getNumberOfLottoTicket(); i++) {
int lottoMatchedNumber = lottoWinningResults.getLottoWinningResult().get(i);
Boolean lottoBonusBallNumber = lottoWinningBonusBallResult.getLottoWinningBonusBallResult().get(i);
getWinningLottoTicketPrices(lottoMatchedNumber, lottoBonusBallNumber);
}
printer.printWinningStatistics();
printer.printAllMatchedLottoResults(getMappingLottoWithBonusBall());
return lottoPrices;
}

private void getWinningLottoTicketPrices(int lottoMatchedNumber, boolean lottoBonusBallNumber) {
for(WinningStatus winningStatus: WinningStatus.values()){
int matchedWinningNumberCount = winningStatus.getMatchCount();
boolean isMatchedBonusBallCount = winningStatus.hasBonusBall();
makeWinningLottoTicket(lottoMatchedNumber, matchedWinningNumberCount, isMatchedBonusBallCount, lottoBonusBallNumber, winningStatus);
}
}

private void makeWinningLottoTicket(
int lottoMatchedNumber,
int matchedWinningNumberCount,
boolean isMatchedBonusBallCount,
boolean lottoBonusBallNumber,
WinningStatus winningStatus
){
if((lottoMatchedNumber == matchedWinningNumberCount) && (isMatchedBonusBallCount == lottoBonusBallNumber)){
lottoPrices.add(winningStatus);
}
}

private Map<WinningStatus, Integer> getMappingLottoWithBonusBall() {
for (WinningStatus key: lottoPrices) {
mappingLottoWithBonusBall.put(key, mappingLottoWithBonusBall.getOrDefault(key, 0)+1);
}
return mappingLottoWithBonusBall;
}

}
23 changes: 23 additions & 0 deletions src/main/java/domain/LottoFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package domain;

import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;

public class LottoFactory {
private LottoWinningResult lottoWinningResult;
private LottoWinningBonusBallResult lottoWinningBonusBallResult;

public LottoFactory(LottoWinningResult lottoWinningResults, LottoWinningBonusBallResult lottoWinningBonusBallResults){
this.lottoWinningResult = lottoWinningResults;
this.lottoWinningBonusBallResult = lottoWinningBonusBallResults;
}

public LottoWinningResult getLottoWinningResult(){
return this.lottoWinningResult;
}

public LottoWinningBonusBallResult getLottoWinningBonusBallResult(){
return this.lottoWinningBonusBallResult;
}

}
68 changes: 68 additions & 0 deletions src/main/java/domain/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package domain;

import domain.lastweekwinninglotto.LastWeekWinningBonusBall;
import domain.lastweekwinninglotto.LastWeekWinningLotto;
import domain.lottoticket.LottoTicket;
import domain.lottoticket.Lottos;
import domain.lottoticket.NumberOfLottoTicket;
import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;

import java.util.ArrayList;
import java.util.List;

public class LottoMachine {
EnumWinningStatus enumWinningStatus = new EnumWinningStatus();
ArrayList<Integer> lottoWinningResults = new ArrayList<>();
ArrayList<Boolean> lottoWinningBonusBallResults = new ArrayList<>();

public ArrayList<WinningStatus> Discriminator(
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

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

여기도 숨어있군요

Lottos lottos,
LastWeekWinningLotto lastWeekWinningLotto,
LastWeekWinningBonusBall lastWeekWinningBonusBall,
NumberOfLottoTicket numberOfLottoTicket
){
Comment on lines +20 to +24
Copy link
Member

Choose a reason for hiding this comment

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

Discriminator라는 함수가 받는 인자가 너무 많은 것 같아요.
numberOfLottoTicket의 경우 lottos에 포함되어 있을 수 있지 않을까요?

for (LottoTicket lotto : lottos.getLottos())
{
List<Boolean> lottoMatchedResult = getMatchLottoNumber(lotto, lastWeekWinningLotto);
Copy link
Member

Choose a reason for hiding this comment

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

로또 매칭 결과값을 List로 따로 관리하셨군요!

사실 저도 이번 미션 진행하면서 결과 값을 관리하는 것에 대해 고민을 많이 했는데 결국 추가적으로 관리하는 것은 이번 미션 목적에 비해 오버 엔지니어링이라는 생각에 결과값 리스트를 따로 관리하지 않았습니다.

이렇게 결과값 리스트 따로 관리하면 이후에 추가적인 로직으로 판단하는 데에 있어서 장점이 있을까요?

Boolean lottoMatchedBonusBallResult = getMatchLottoBonusBallNumber(lotto, lastWeekWinningBonusBall);
int matchLottoNumberCount = getMatchLottoNumberCount(lottoMatchedResult);
lottoWinningResults.add(matchLottoNumberCount);
lottoWinningBonusBallResults.add(lottoMatchedBonusBallResult);
}
LottoFactory lottoFactory = createLottoFactory(lottoWinningResults, lottoWinningBonusBallResults);
return enumWinningStatus.getLottoPrices(lottoFactory, numberOfLottoTicket);
}

private LottoFactory createLottoFactory(ArrayList<Integer> lottoWinningResults, ArrayList<Boolean> lottoWinningBonusBallResults) {
LottoWinningBonusBallResult lottoWinningBonusBallResult = new LottoWinningBonusBallResult(lottoWinningBonusBallResults);
LottoWinningResult winningResult = new LottoWinningResult(lottoWinningResults);

LottoFactory lottoFactory = new LottoFactory(winningResult, lottoWinningBonusBallResult);
return lottoFactory;
}

private int getMatchLottoNumberCount(List<Boolean> lottoMatchedResult){
return (int)lottoMatchedResult.stream().filter(index-> (index)).count();
}

private List<Boolean> getMatchLottoNumber(LottoTicket lotto, LastWeekWinningLotto lastWeekWinningLotto){
List<Integer> lottoList = lotto.getLotto();
List<Integer> lastWeekWinningLottos = lastWeekWinningLotto.getLotto();
ArrayList<Boolean> lottoMatchedResult = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

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

Interface!


for (int winningLottoNumber = 0; winningLottoNumber < lastWeekWinningLottos.size(); winningLottoNumber++) {
Boolean isMatchLottoNumber = lottoList.contains(lastWeekWinningLottos.get(winningLottoNumber));
lottoMatchedResult.add(isMatchLottoNumber);
}
return lottoMatchedResult;
}

private Boolean getMatchLottoBonusBallNumber(LottoTicket lotto, LastWeekWinningBonusBall lastWeekWinningBonusBall) {
List<Integer> lottoBonusBalls = lotto.getLotto();
int lastWeekWinningBonusBalls = lastWeekWinningBonusBall.getLastWeekWinningBonusBall();
Boolean isMatchLottoNumber = lottoBonusBalls.contains(lastWeekWinningBonusBalls);
return isMatchLottoNumber;

}
}
36 changes: 36 additions & 0 deletions src/main/java/domain/Profit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain;

import domain.lottoticket.NumberOfLottoTicket;

import java.util.List;

public class Profit {
private static final int PROFIT_THRESHOLD = 1;
private static int sumLottoPrices;
private static NumberOfLottoTicket numberOfLottoTicket;
private static float calculatedProfit;
public Profit(List<WinningStatus> lottoPrices, NumberOfLottoTicket numberOfLottoTicket) {
this.sumLottoPrices = sumLottoPrices(lottoPrices);
this.numberOfLottoTicket = numberOfLottoTicket;
this.calculatedProfit = CalculatedProfit();
}

private int sumLottoPrices(List<WinningStatus> lottoPrices){
return lottoPrices.stream().mapToInt(WinningStatus::getWinningMoney).sum();
}

private float CalculatedProfit() {
return (float)sumLottoPrices / numberOfLottoTicket.getMoney();
}

public boolean isProfit() {
if (calculatedProfit<PROFIT_THRESHOLD) {
return false;
}
return true;
}

public float getProfit() {
return this.calculatedProfit;
}
}
31 changes: 31 additions & 0 deletions src/main/java/domain/WinningStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain;

public enum WinningStatus {
SIX_MATCH(6, false, 2000000000),
FIVE_MATCH_WITH_BONUS_BALL(5, true, 30000000),
FIVE_MATCH(5, false, 1500000),
FOUR_MATCH(4, false, 50000),
THREE_MATCH(3, false, 5000);

private final int matchCount;
private final int winningMoney;
private final boolean BonusBall;

WinningStatus(int matchCount, boolean BonusBall, int winningMoney){
this.matchCount = matchCount;
this.winningMoney = winningMoney;
this.BonusBall = BonusBall;
}

public int getMatchCount() {
return this.matchCount;
}

public boolean hasBonusBall() {
return this.BonusBall;
}

public int getWinningMoney() {
return this.winningMoney;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain.exception;

public class NotValidLottoLengthException extends RuntimeException {
private static final String MESSAGE = "번호는 6개를 입력해야 합니다.";

public NotValidLottoLengthException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package domain.lastweekwinninglotto;

public class LastWeekWinningBonusBall {

private final int bonusBall;
public LastWeekWinningBonusBall(int bonusBall){
this.bonusBall = bonusBall;
}

public int getLastWeekWinningBonusBall(){
return this.bonusBall;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package domain.lastweekwinninglotto;

import domain.lottoticket.Lotto;

import java.util.List;

public class LastWeekWinningLotto implements Lotto {
private final List<Integer> winningLotto;

public LastWeekWinningLotto(List<Integer> winningLotto){
this.winningLotto = winningLotto;
}

@Override
public List<Integer> getLotto(){
return this.winningLotto;
}
}
7 changes: 7 additions & 0 deletions src/main/java/domain/lottoticket/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package domain.lottoticket;

import java.util.List;

public interface Lotto {
public List<Integer> getLotto();
}
25 changes: 25 additions & 0 deletions src/main/java/domain/lottoticket/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package domain.lottoticket;

import domain.exception.*;
import java.util.List;

public class LottoTicket implements Lotto{
private static final int LOTTO_NUMBER_COUNT = 6;
private final List<Integer> numbers;

public LottoTicket(List<Integer> numbers) {
checkLottoLength(numbers);
this.numbers = numbers;
}

private void checkLottoLength(List<Integer> numbers){
if(numbers.size() !=LOTTO_NUMBER_COUNT){
throw new NotValidLottoLengthException();
}
}
@Override
public List<Integer> getLotto(){
return this.numbers;
}

}
15 changes: 15 additions & 0 deletions src/main/java/domain/lottoticket/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain.lottoticket;

import java.util.List;

public class Lottos {
private final List<LottoTicket> lotto;

public Lottos(List<LottoTicket> lotto ) {
this.lotto = lotto;
}

public List<LottoTicket> getLottos() {
return this.lotto;
}
}
Loading