-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Step3 #3251
base: geatrigger
Are you sure you want to change the base?
Step3 #3251
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,13 @@ | ||
package lottery.domain; | ||
|
||
public class BonusBall { | ||
private final LotteryNumber value; | ||
|
||
public BonusBall(LotteryNumber value) { | ||
this.value = value; | ||
} | ||
|
||
public LotteryNumber value() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
package lottery.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class Lotteries { | ||
public static final Integer BONUS_BALL_CHANCE_NUMBER = 5; | ||
public static final Integer BONUS_BALL_CHANCE_NUMBER_RANK = 6; | ||
public static final Integer RANK_LENGTH = 8; | ||
public static final List<Integer> matchNumberToRank = Arrays.asList(0, 1, 2, 3, 4, 5, 7); | ||
|
||
public static List<Lottery> buy(Integer price, LotteryStrategy lotteryStrategy) { | ||
Comment on lines
+14
to
15
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. 로또금액을 VO 로 포장하면 더 신뢰할 수 있는 값이 되겠네요. |
||
int number = calculateNumberOfLottery(price); | ||
List<Lottery> lotteries = new ArrayList<>(); | ||
|
@@ -18,19 +25,36 @@ private static int calculateNumberOfLottery(Integer price) { | |
return price / LotteryPrice.VALUE; | ||
} | ||
|
||
public static LotteryResult calculateResult(List<Lottery> lotteries, Lottery winLottery) { | ||
public static LotteryResult calculateResult(List<Lottery> lotteries, WinLottery winLottery) { | ||
List<Integer> numberOfMatchNumbers = lotteries.stream().map((lottery) -> calculateMatchCount(lottery, winLottery)).collect(Collectors.toList()); | ||
List<Boolean> hasBonusBalls = lotteries.stream().map((lottery) -> calculateHasBonusBall(lottery, winLottery)).collect(Collectors.toList()); | ||
List<Integer> winLotteryNumbers = new ArrayList<>(); | ||
for (int i = 0; i <= Lottery.LENGTH; i++) { | ||
for (int i = 0; i < RANK_LENGTH; i++) { | ||
winLotteryNumbers.add(0); | ||
} | ||
for (int number : numberOfMatchNumbers) { | ||
winLotteryNumbers.set(number, winLotteryNumbers.get(number) + 1); | ||
for (int i = 0; i < numberOfMatchNumbers.size(); i++) { | ||
checkBonusBallAndAdd(hasBonusBalls.get(i), numberOfMatchNumbers.get(i), winLotteryNumbers); | ||
} | ||
Comment on lines
31
to
37
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 new LotteryResult(winLotteryNumbers, lotteries.size()); | ||
} | ||
|
||
private static int calculateMatchCount(Lottery lottery, Lottery winLottery) { | ||
private static void checkBonusBallAndAdd(Boolean hasBonusBall, int number, List<Integer> winLotteryNumbers) { | ||
if (hasBonusBall && number == BONUS_BALL_CHANCE_NUMBER) { | ||
winLotteryNumbers.set(BONUS_BALL_CHANCE_NUMBER_RANK, winLotteryNumbers.get(BONUS_BALL_CHANCE_NUMBER_RANK) + 1); | ||
return; | ||
} | ||
winLotteryNumbers.set(matchNumberToRank.get(number), winLotteryNumbers.get(matchNumberToRank.get(number)) + 1); | ||
} | ||
|
||
private static Boolean calculateHasBonusBall(Lottery lottery, WinLottery winLottery) { | ||
List<LotteryNumber> lotteryNumbers = lottery.numbers(); | ||
LotteryNumber bonusBallValue = winLottery.bonusBall().value(); | ||
|
||
return lotteryNumbers.contains(bonusBallValue); | ||
} | ||
|
||
|
||
private static int calculateMatchCount(Lottery lottery, WinLottery winLottery) { | ||
List<LotteryNumber> lotteryNumbers = lottery.numbers(); | ||
List<LotteryNumber> winLotteryNumbers = winLottery.numbers(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package lottery.domain; | ||
|
||
import java.util.List; | ||
|
||
public class WinLottery { | ||
private final Lottery lottery; | ||
private final BonusBall bonusBall; | ||
|
||
public WinLottery(Lottery lottery, BonusBall bonusBall) { | ||
this.lottery = lottery; | ||
this.bonusBall = bonusBall; | ||
} | ||
|
||
public List<LotteryNumber> numbers() { | ||
return lottery.numbers(); | ||
} | ||
|
||
public BonusBall bonusBall() { | ||
return bonusBall; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ | |
|
||
public enum WinPrize { | ||
FIRST_PLACE(2000000000), | ||
SECOND_PLACE(1500000), | ||
THIRD_PLACE(50000), | ||
FOURTH_PLACE(5000), | ||
SECOND_PLACE(30000000), | ||
THIRD_PLACE(1500000), | ||
FOURTH_PLACE(50000), | ||
FIFTH_PLACE(5000), | ||
LOST(0); | ||
Comment on lines
3
to
9
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. enum 을 통해서 상금과 당첨 개수, 보너스볼 여부도 관리해볼 수 있을까요? 🤔 |
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package lottery.view; | ||
|
||
import lottery.domain.BonusBall; | ||
import lottery.domain.Lottery; | ||
import lottery.domain.LotteryNumber; | ||
|
||
import java.util.Scanner; | ||
|
||
public class BonusBallInputView implements InputView<BonusBall> { | ||
Scanner scanner = new Scanner(System.in); | ||
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. 기찬님 견해가 궁금합니다! 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. 매번 새로운 인스턴스를 생성하는 건 비효율적인 것 같네요 |
||
@Override | ||
public BonusBall receive() { | ||
System.out.println("보너스 볼을 입력해 주세요."); | ||
return new BonusBall(new LotteryNumber(scanner.nextInt())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,42 @@ | ||
package lottery.view; | ||
|
||
import jdk.nashorn.api.scripting.ScriptObjectMirror; | ||
import lottery.domain.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class LotteryResultOutputView implements OutputView<LotteryResult> { | ||
private final List<Integer> winPrizes = Arrays.asList(WinPrize.LOST.value(), WinPrize.LOST.value(), WinPrize.LOST.value(), WinPrize.FOURTH_PLACE.value(), WinPrize.THIRD_PLACE.value(), WinPrize.SECOND_PLACE.value(), WinPrize.FIRST_PLACE.value()); | ||
private final List<Integer> winPrizes = Arrays.asList(WinPrize.LOST.value(), WinPrize.LOST.value(), WinPrize.LOST.value(), WinPrize.FIFTH_PLACE.value(), WinPrize.FOURTH_PLACE.value(), WinPrize.THIRD_PLACE.value(), WinPrize.SECOND_PLACE.value(), WinPrize.FIRST_PLACE.value()); | ||
private final List<Integer> rankToMatchNumber; | ||
|
||
public LotteryResultOutputView() { | ||
rankToMatchNumber = new ArrayList<>(); | ||
for (int i = 0; i <= Lotteries.RANK_LENGTH; i++) { | ||
rankToMatchNumber.add(0); | ||
} | ||
for (int i = 0; i < Lotteries.matchNumberToRank.size(); i++) { | ||
rankToMatchNumber.set(Lotteries.matchNumberToRank.get(i), i); | ||
} | ||
} | ||
|
||
@Override | ||
public void print(LotteryResult output) { | ||
final int MIN_WIN_NUMBER = 3; | ||
List<Integer> winNumbers = output.winNumbers(); | ||
int totalPrize = 0; | ||
for (int i = MIN_WIN_NUMBER; i <= Lottery.LENGTH; i++) { | ||
for (int i = MIN_WIN_NUMBER; i < Lotteries.RANK_LENGTH; i++) { | ||
totalPrize += winNumbers.get(i) * winPrizes.get(i); | ||
System.out.printf("%d개 일치 (%d원)- %d개\n", i, winPrizes.get(i), winNumbers.get(i)); | ||
System.out.printf("%s (%d원)- %d개\n", rankToDetail(i), winPrizes.get(i), winNumbers.get(i)); | ||
} | ||
System.out.printf("총 수익률은 %.2f입니다", (float) totalPrize / (output.numberOfLottery() * LotteryPrice.VALUE)); | ||
} | ||
|
||
private String rankToDetail(int i) { | ||
if (i == Lotteries.BONUS_BALL_CHANCE_NUMBER_RANK) { | ||
return String.format("%d개 일치, 보너스 볼 일치", Lotteries.BONUS_BALL_CHANCE_NUMBER); | ||
} | ||
return String.format("%d개 일치", rankToMatchNumber.get(i)); | ||
} | ||
} |
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.
더욱 더 VO 스럽게 사용하기 위해 동등성을 부여해주면 어떨까요? (equals & hash code)