-
Notifications
You must be signed in to change notification settings - Fork 6
feat: 로또 1단계-자동미션 제출 #4
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
base: jjanggyu
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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<>(); | ||
| 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(); | ||
| } | ||
| } | ||
| 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<>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| } | ||
| 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; | ||
| } | ||
|
|
||
| } |
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discriminator라는 함수가 받는 인자가 너무 많은 것 같아요. |
||
| for (LottoTicket lotto : lottos.getLottos()) | ||
| { | ||
| List<Boolean> lottoMatchedResult = getMatchLottoNumber(lotto, lastWeekWinningLotto); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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(); | ||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interface!