-
Notifications
You must be signed in to change notification settings - Fork 2k
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
로또 완성!!! #2105
base: main
Are you sure you want to change the base?
로또 완성!!! #2105
Changes from 1 commit
72ce2e7
cd685bb
7c43480
8610737
5b9329b
d28635a
6cfa1f6
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 |
---|---|---|
|
@@ -7,4 +7,8 @@ | |
|
||
[ ✅ ] 당첨번호를 입력한다. | ||
|
||
[ ✅ ] 보너스번호를 입력한다. | ||
[ ✅ ] 보너스번호를 입력한다. | ||
|
||
[ ✅ ] 당첨통계를 출력한다. | ||
|
||
[ ✅ ] 수익률을 계산한다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package controller; | ||
|
||
import domain.*; | ||
import dto.WinningResultDto; | ||
import service.WinningService; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
public class WinningController { | ||
private final WinningService winningService; | ||
|
||
public WinningController(){ | ||
winningService = new WinningService(); | ||
} | ||
|
||
public void generateWinngingAndBonus(final Lottos lottos, final int purchaseAmount){ | ||
WinningNumbers winningNumbers = generateWinngingNumbers(); | ||
BonusNumber bonusNumber = generateBonusNumber(); | ||
printWinningStaticsAndProfit(winningNumbers, bonusNumber, lottos, purchaseAmount); | ||
} | ||
|
||
private WinningNumbers generateWinngingNumbers(){ | ||
try{ | ||
List<Integer> winningNumbers = InputView.inputWinngingNumbers(); | ||
return createWinngingNumbers(winningNumbers); | ||
} catch (IllegalArgumentException e){ | ||
OutputView.printMessage(e.getMessage()); | ||
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. OutputView도 "private final OutputView outputView" 이렇게 선언해주는게 어떨까요? 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. 음..!! 그렇게 선언하면 좋은 점이 어떤것인가요!! |
||
return generateWinngingNumbers(); | ||
} | ||
} | ||
|
||
private BonusNumber generateBonusNumber(){ | ||
try{ | ||
int number = InputView.inputBonusNumber(); | ||
return createBonusNumber(number); | ||
} catch (IllegalArgumentException e){ | ||
OutputView.printMessage(e.getMessage()); | ||
return generateBonusNumber(); | ||
} | ||
} | ||
|
||
private WinningNumbers createWinngingNumbers(final List<Integer> winningNumbers){ | ||
return winningService.createWinningNumbers(winningNumbers); | ||
} | ||
|
||
private BonusNumber createBonusNumber(final int bonusNumber){ | ||
return winningService.createBonusNumber(bonusNumber); | ||
} | ||
|
||
private void printWinningStaticsAndProfit(final WinningNumbers winningNumbers, final BonusNumber bonusNumber, final Lottos lottos, final int purchaseAmount){ | ||
generateWinningResult(winningNumbers, bonusNumber); | ||
WinningResultDto winningResultDto = winningService.calculateProfitAndRateOfProfit(lottos.getLottos(), purchaseAmount); | ||
OutputView.printWinningStatics(winningResultDto); | ||
} | ||
|
||
private WinningResult generateWinningResult(final WinningNumbers winningNumbers, final BonusNumber bonusNumber){ | ||
return winningService.createWinningResult(winningNumbers, bonusNumber); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package domain; | ||
|
||
import util.constants.Constants; | ||
|
||
public enum Rank { | ||
FIFTH(3, 5_000, "3개 일치 (5,000원) - "), | ||
FOURTH(4, 50_000, "4개 일치 (50,000원) - "), | ||
THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "), | ||
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), | ||
FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "); | ||
|
||
private int countOfMatch; | ||
private int prize; | ||
private String message; | ||
|
||
Rank(int countOfMatch, int prize, String message){ | ||
this.countOfMatch = countOfMatch; | ||
this.prize = prize; | ||
this.message = message; | ||
} | ||
|
||
public int getCountOfMatch(){ | ||
return countOfMatch; | ||
} | ||
|
||
public int getPrize(){ | ||
return prize; | ||
} | ||
|
||
public String getMessage(){ | ||
return message; | ||
} | ||
|
||
public static Rank calculateRank(int countOfMatchNumbers, boolean isMatchBonus){ | ||
for(Rank rank : values()){ | ||
if(rank.matches(countOfMatchNumbers, isMatchBonus)){ | ||
return rank; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private boolean matches(int countOfMatchNumbers, boolean isMatchBonus){ | ||
if(this == SECOND){ | ||
return checkSecond(countOfMatchNumbers, isMatchBonus); | ||
} | ||
return checkRemaining(countOfMatchNumbers, isMatchBonus); | ||
} | ||
|
||
private boolean checkSecond(int countOfMatchNumbers, boolean isMatchBonus){ | ||
return this.countOfMatch == countOfMatchNumbers && isMatchBonus; | ||
} | ||
|
||
private boolean checkRemaining(int countOfMatchNumbers, boolean isMatchBonus){ | ||
return this.countOfMatch == countOfMatchNumbers && !isMatchBonus; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class WinningResult { | ||
private final WinningNumbers winningNumbers; | ||
private final BonusNumber bonusNumber; | ||
private final HashMap<Rank, Integer> winningResult; | ||
private double totalPrize; | ||
private double rateOfProfit; | ||
|
||
private WinningResult(WinningNumbers winningNumbers, BonusNumber bonusNumber){ | ||
this.winningNumbers = winningNumbers; | ||
this.bonusNumber = bonusNumber; | ||
this.winningResult = new HashMap<>(); | ||
initWinningResult(); | ||
} | ||
|
||
public static WinningResult create(WinningNumbers winningNumbers, BonusNumber bonusNumber){ | ||
return new WinningResult(winningNumbers, bonusNumber); | ||
} | ||
|
||
public double getRateOfProfit(){ | ||
return rateOfProfit; | ||
} | ||
|
||
public HashMap<Rank, Integer> getWinningResult(){ | ||
return winningResult; | ||
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.
Map은 이런 방법으로 불변을 정해 줄 수 있어요 👍 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. 오..!! 감사합니다!! |
||
} | ||
|
||
private void initWinningResult(){ | ||
for (Rank rank : Rank.values()) { | ||
winningResult.put(rank, 0); | ||
} | ||
} | ||
|
||
public void calculateProfit(List<Lotto> lottos){ | ||
for(Lotto lotto : lottos){ | ||
calculate(getMatchNumbers(lotto, winningNumbers), isMatchBonusNumber(lotto, bonusNumber)); | ||
} | ||
} | ||
|
||
public void calculateRateOfProfit(int amount){ | ||
rateOfProfit = (totalPrize / (double) amount) * 100; | ||
} | ||
|
||
private void calculate(int countOfMatchNumber, boolean isMatchBonus){ | ||
Rank rank = Rank.calculateRank(countOfMatchNumber, isMatchBonus); | ||
if(rank != null){ | ||
winningResult.put(rank, winningResult.get(rank) + 1); | ||
addTotalPrize(rank.getPrize()); | ||
} | ||
} | ||
|
||
private void addTotalPrize(int prize){ | ||
totalPrize += prize; | ||
} | ||
|
||
private int getMatchNumbers(Lotto lotto, WinningNumbers winningNumbers){ | ||
List<Integer> lottoNumbers = lotto.getNumbers(); | ||
List<Integer> winningNums = winningNumbers.getNumbers(); | ||
long matchedCount = lottoNumbers.stream() | ||
.filter(winningNums::contains) | ||
.count(); | ||
return (int) matchedCount; | ||
} | ||
|
||
private boolean isMatchBonusNumber(Lotto lotto, BonusNumber bonusNumber){ | ||
return lotto.getNumbers().contains(bonusNumber.getBonusNumber()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dto; | ||
|
||
import domain.Rank; | ||
import domain.WinningResult; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class WinningResultDto { | ||
private final Map<Rank, Integer> winningResult; | ||
private final double rateOfProfit; | ||
private static final String COUNT = "개\n"; | ||
|
||
private WinningResultDto(final WinningResult winningResult) { | ||
this.winningResult = winningResult.getWinningResult(); | ||
rateOfProfit = winningResult.getRateOfProfit(); | ||
} | ||
|
||
public static WinningResultDto create(final WinningResult winningResult) { | ||
return new WinningResultDto(winningResult); | ||
} | ||
|
||
public String formatRateOfReturn(){ | ||
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. dto에서 계산해서 format을 만들다니.. 한 수 배웁니다! |
||
double value = Math.round(rateOfProfit * 10.0) / 10.0; | ||
return String.format("%.1f", value); | ||
} | ||
|
||
public String generateResultString() { | ||
StringBuilder resultString = new StringBuilder(); | ||
for (Rank rank : Rank.values()) { | ||
int count = winningResult.getOrDefault(rank, 0); | ||
resultString.append(rank.getMessage()).append(count).append(COUNT); | ||
} | ||
return resultString.toString(); | ||
} | ||
} |
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.
21, 22번 라인을 이렇게 줄일 수 있을거 같아요!
lottos = generateLottos(purchaseAmount);