Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@

[ ✅ ] 당첨번호를 입력한다.

[ ✅ ] 보너스번호를 입력한다.
[ ✅ ] 보너스번호를 입력한다.

[ ✅ ] 당첨통계를 출력한다.

[ ✅ ] 수익률을 계산한다.
44 changes: 5 additions & 39 deletions src/main/java/controller/LottosController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package controller;

import domain.BonusNumber;
import domain.PurchaseAmount;
import domain.Lottos;
import domain.WinningNumbers;
import domain.*;
import dto.LottoDto;
import service.LottosService;
import service.PurchaseService;
Expand All @@ -14,15 +11,17 @@

public class LottosController {
private final LottosService lottosService;
private Lottos lottos;

public LottosController(){
lottosService = new LottosService();
}

public void start(final PurchaseAmount purchaseAmount){
public Lottos start(final PurchaseAmount purchaseAmount){
Lottos purchaseLottos = generateLottos(purchaseAmount);
lottos = purchaseLottos;

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);

printPurchaseLottos(purchaseLottos, purchaseAmount.getQuantity());
generateWinngingAndBonus();
return lottos;
}

private Lottos generateLottos(final PurchaseAmount purchaseAmount){
Expand All @@ -33,37 +32,4 @@ private void printPurchaseLottos(final Lottos purchaseLottos, final int numberOf
List<LottoDto> printPurchaseLottos = lottosService.getPurchaseLottos(purchaseLottos);
OutputView.printPurchaseLottos(printPurchaseLottos, numberOfPurchased);
}

private void generateWinngingAndBonus(){
WinningNumbers winningNumbers = generateWinngingNumbers();
BonusNumber bonusNumber = generateBonusNumber();
}

private WinningNumbers generateWinngingNumbers(){
try{
List<Integer> winningNumbers = InputView.inputWinngingNumbers();
return createWinngingNumbers(winningNumbers);
} catch (IllegalArgumentException e){
OutputView.printMessage(e.getMessage());
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 lottosService.createWinningNumbers(winningNumbers);
}

private BonusNumber createBonusNumber(final int bonusNumber){
return lottosService.createBonusNumber(bonusNumber);
}
}
61 changes: 61 additions & 0 deletions src/main/java/controller/WinningController.java
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());

Choose a reason for hiding this comment

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

OutputView도 "private final OutputView outputView" 이렇게 선언해주는게 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

The 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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/domain/BonusNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ private BonusNumber(final int number) {
public static BonusNumber create(final int number){
return new BonusNumber(number);
}

public int getBonusNumber(){
return bonusNumber;
}
}
15 changes: 7 additions & 8 deletions src/main/java/domain/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import util.exception.DuplicateException;
import util.exception.SizeOverException;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import static util.message.ExceptionMessage.DUPLICATE_MESSAGE;
import static util.message.ExceptionMessage.SIZE_OVER_MESSAGE;
Expand All @@ -16,8 +13,8 @@ public class Lotto {

public Lotto(List<Integer> numbers) {
validate(numbers);
sort(numbers);
this.numbers = numbers;
List<Integer> numberList = sort(numbers);
this.numbers = numberList;
}

public List<Integer> getNumbers(){
Expand All @@ -42,7 +39,9 @@ private void validateDuplicate(List<Integer> numbers){
}
}

private void sort(List<Integer> numbers){
Collections.sort(numbers);
private List<Integer> sort(List<Integer> numbers){
List<Integer> numberList = new ArrayList<>(numbers);
Collections.sort(numberList);
return numberList;
}
}
4 changes: 4 additions & 0 deletions src/main/java/domain/PurchaseAmount.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public int getQuantity(){
return quantity;
}

public int getAmount(){
return amount;
}

private int calculatequantity(){
return amount / Constants.ONE_THOUSAND.getValue();
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/domain/Rank.java
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;
}


}
4 changes: 4 additions & 0 deletions src/main/java/domain/WinningNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static WinningNumbers create(final List<Integer> inputWinningNumbers){
return new WinningNumbers(inputWinningNumbers);
}

public List<Integer> getNumbers(){
return numbers;
}

private void validateDuplicate(final List<Integer> inputWinningNumbers){
Set<Integer> numbers = new HashSet<>(inputWinningNumbers);
if(numbers.size() != inputWinningNumbers.size()){
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/domain/WinningResult.java
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;

Choose a reason for hiding this comment

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

Collections.unmodifiableMap(winningResult);

Map은 이런 방법으로 불변을 정해 줄 수 있어요 👍

Copy link
Author

Choose a reason for hiding this comment

The 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());
}
}
36 changes: 36 additions & 0 deletions src/main/java/dto/WinningResultDto.java
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(){

Choose a reason for hiding this comment

The 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();
}
}
Loading