Skip to content
Merged
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
14 changes: 8 additions & 6 deletions src/main/java/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import domain.LottoGame;
import domain.LottoResult;
import domain.Price;
import domain.Money;
import domain.WinningLotto;
import view.InputView;
import view.ResultView;

Expand All @@ -9,13 +10,14 @@ public class MainApplication {
public static void main(String[] args) {
int moneyAmount = InputView.getAmount();

Price price = new Price(moneyAmount);
Money money = new Money(moneyAmount);

LottoGame lottoGame = new LottoGame(price.ticketCount());
LottoGame lottoGame = new LottoGame(money.ticketCount());
ResultView.printLottos(lottoGame);

String winningNumbers = InputView.getWinningNumbers();
LottoResult result = lottoGame.playingLotto(winningNumbers, price);
ResultView.printLottoResult(result);
WinningLotto winningLotto = new WinningLotto(InputView.getWinningNumbers(), InputView.getBonusNumber());
LottoResult result = lottoGame.playingLotto(winningLotto);

ResultView.printLottoResult(result, money);
}
}
25 changes: 5 additions & 20 deletions src/main/java/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package domain;

import java.util.List;

public class Lotto {
private Numbers numbers;
private LottoNumbers lottoNumbers;

public static Lotto generateLotto(List<Integer> integers) {
Lotto lotto = new Lotto();
lotto.numbers = new Numbers(integers);
return lotto;
public Lotto (LottoNumbers numbers) {
this.lottoNumbers = numbers;
}

public LottoResultStatus getLottoResultStatus(int[] winningNumbers) {
int containsWinningNumberCount = 0;
for (int number : winningNumbers) {
if(numbers.contains(number))
containsWinningNumberCount ++;
}

return LottoResultStatus.findByMatchCount(containsWinningNumberCount);
public LottoNumbers getLottoNumbers() {
return this.lottoNumbers;
}

public Numbers getNumbers() {
return this.numbers;
}

}
16 changes: 4 additions & 12 deletions src/main/java/domain/LottoGame.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package domain;

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

public class LottoGame {

private List<Lotto> lottos;

public LottoGame(int lottoCount) {
LottoGenerator generator = new LottoGenerator();
LottoNumberGenerator generator = new LottoNumberGenerator();
this.lottos = new ArrayList<>();

int i=0;
while (i < lottoCount) {
Lotto lotto = Lotto.generateLotto(generator.generateRandomNumber());
Lotto lotto = new Lotto(generator.generateRandomNumber());
lottos.add(lotto);
i ++;
}
}

public LottoResult playingLotto(String inputWinningNumbers, Price price) {
int[] intWinningNumbers = convertingWinningNumbers(inputWinningNumbers);
LottoResult lottoGameResult = new LottoResult(lottos, intWinningNumbers);
lottoGameResult.calculateBenefit(price);
public LottoResult playingLotto(WinningLotto winningLotto) {
LottoResult lottoGameResult = new LottoResult(lottos, winningLotto);
return lottoGameResult;
}

public int[] convertingWinningNumbers(String winningNumbers) {
String[] stringWinningNumbers = winningNumbers.replace(" ","").split(",");
return Arrays.stream(stringWinningNumbers).mapToInt(str-> Integer.parseInt(str)).toArray();
}

public List<Lotto> lottos() {
return lottos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
import java.util.Collections;
import java.util.List;

public class LottoGenerator {
private static final int LOTTO_NUMBER_LENTH = 6;
public class LottoNumberGenerator {
private static final int LOTTO_RANGE_MIN_NUM = 1;
private static final int LOTTO_MAX_NUMBER = 46;
private List<Integer> lottoRangeNumbers;

public LottoGenerator() {
public LottoNumberGenerator() {
lottoRangeNumbers = makeLottoNumberRange();
}

public List<Integer> generateRandomNumber() {
public LottoNumbers generateRandomNumber() {
Collections.shuffle(this.lottoRangeNumbers);

List<Integer> lottoNum = new ArrayList<Integer>();
for (int i=0; i < LOTTO_NUMBER_LENTH; i++) {
for (int i = 0; i < LottoNumbers.LOTTO_NUMBER_SIZE; i++) {
lottoNum.add(this.lottoRangeNumbers.get(i));
}

return lottoNum;
return LottoNumbers.createInstance(lottoNum);
}

private static List<Integer> makeLottoNumberRange() {
List<Integer> lottoNumberRange = new ArrayList<>();

int i = 1;
int i = LOTTO_RANGE_MIN_NUM;
while(i <= LOTTO_MAX_NUMBER) {
lottoNumberRange.add(i);
i ++;
}

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

import java.util.List;
import java.util.stream.Collectors;

public class LottoNumbers {
public static final int LOTTO_NUMBER_SIZE = 6;
private List<Number> numbers;

private LottoNumbers(List<Integer> inputNumbers) {
List<Number> list = inputNumbers.stream().map(Number::createInstance).collect(Collectors.toList());
this.numbers = list;
}

public static LottoNumbers createInstance(List<Integer> inputNumbers) {
if (inputNumbers.size() != LOTTO_NUMBER_SIZE) {
throw new IllegalArgumentException();
}

return new LottoNumbers(inputNumbers);
}

public boolean contains(Number number) {
return numbers.stream().map(num -> num.equals(number)).anyMatch(num -> num.equals(true));
}

public String toString() {
StringBuffer resultString = new StringBuffer();
resultString.append("[");
numbers.forEach(number -> resultString.append(number.toString()+" "));
resultString.append("]");
return resultString.toString();
}

public List<Number> getNumbers() {
return numbers;
}
}
30 changes: 13 additions & 17 deletions src/main/java/domain/LottoResult.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package domain;


import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LottoResult {

private List<Lotto> lottos;
private Map<LottoResultStatus, Integer> lottoResults;
private BigDecimal benefitRate;

public LottoResult(List<Lotto> lottos, int[] winningNumbers) {
public LottoResult(List<Lotto> lottos, WinningLotto winningNumbers) {
this.lottos = lottos;
this.benefitRate = BigDecimal.valueOf(0);
this.lottoResults = new HashMap<>();
initMap();
lottos.stream()
.map(lotto -> lotto.getLottoResultStatus(winningNumbers))
.forEach(this::updateLottoResult);

for (Lotto lotto : lottos) {
updateLottoResult(winningNumbers.result(lotto));
}
}

public List<Lotto> getLottos(){
Expand All @@ -37,20 +33,20 @@ private void updateLottoResult(LottoResultStatus status) {
lottoResults.put(status, lottoResults.get(status) + 1);
}

public Map<LottoResultStatus, Integer> getLottoResults() {
return lottoResults;
public Integer getLottoResult(LottoResultStatus resultStatus) {
if (lottoResults.get(resultStatus) == null) {
return 0;
}

return lottoResults.get(resultStatus);
}

public void calculateBenefit(Price price) {
public BigDecimal calculateBenefit(Money money) {
BigDecimal totalAmountByWinning = new BigDecimal(0);

for(LottoResultStatus status : LottoResultStatus.values()){
totalAmountByWinning = totalAmountByWinning.add(LottoResultStatus.getWinnersPriceByStatus(lottoResults.get(status), status));
}
benefitRate = price.totalRateByWinning(totalAmountByWinning);
}

public BigDecimal getBenefitRate() {
return benefitRate;
return money.totalRateByWinning(totalAmountByWinning);
}
}
33 changes: 21 additions & 12 deletions src/main/java/domain/LottoResultStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,43 @@
import java.util.List;

public enum LottoResultStatus {
NONE(Arrays.asList(0,1,2), 0L),
WINNING_NUM_3(Arrays.asList(3), 5_000L),
WINNING_NUM_4(Arrays.asList(4), 50_000L),
WINNING_NUM_5(Arrays.asList(5), 150_000L),
WINNING_NUM_6(Arrays.asList(6), 2_000_000_000L);
NONE(Arrays.asList(0,1,2), false, 0L, ""),
WINNING_NUM_3(Arrays.asList(3), false, 5_000L, "3개 일치"),
WINNING_NUM_4(Arrays.asList(4), false, 50_000L, "4개 일치"),
WINNING_NUM_5(Arrays.asList(5), false, 1_500_000L, "5개 일치"),
WINNING_NUM_5_BONUS(Arrays.asList(5), true, 1_500_000L, "5개 일치, 보너스 볼 일치"),
WINNING_NUM_6(Arrays.asList(6), false, 2_000_000_000L, "6개 일치");


private List<Integer> matchCounts;
private boolean bonusMatchYn;
private Long winnersPrice;
private String description;

LottoResultStatus(List<Integer> matchCount, Long winnersPrice) {
LottoResultStatus(List<Integer> matchCount, boolean bonusMatchYn, Long winnersPrice, String description) {
this.matchCounts = matchCount;
this.bonusMatchYn = bonusMatchYn;
this.winnersPrice = winnersPrice;
this.description = description;
}

public static LottoResultStatus findByMatchCount(int matchCount) {
public static LottoResultStatus findByMatchCount(int matchCount, boolean bonusYn) {
return Arrays.stream(LottoResultStatus.values())
.filter(matchCountGroup -> matchCountGroup.hasMatchCount(matchCount))
.filter(matchCountGroup -> matchCountGroup.hasMatchCount(matchCount, bonusYn))
.findAny()
.orElse(NONE);
}

public boolean hasMatchCount(Integer integer) {
return matchCounts.contains(integer);
public boolean hasMatchCount(Integer matchCount, boolean bonusYn) {
return this.matchCounts.contains(matchCount) && this.bonusMatchYn == bonusYn;
}

public static BigDecimal getWinnersPriceByStatus(int matchCount, LottoResultStatus status) {
return BigDecimal.valueOf(matchCount * status.winnersPrice);
public static BigDecimal getWinnersPriceByStatus(int lottoCount, LottoResultStatus status) {
return BigDecimal.valueOf(lottoCount * status.winnersPrice);
}

public String findDescription() {
return this.description;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Price {
public class Money {
private static final int ONE_TICKET_PRICE = 1_000;

private int totalAmount;

public Price(int amount) {
public Money(int amount) {
if (amount < ONE_TICKET_PRICE) {
throw new IllegalArgumentException();
}

totalAmount = amount;
}

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/domain/Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package domain;

import java.util.Objects;

public class Number {
private static final int MININUM_NUMBER = 1;
private static final int MAXIMUM_NUMBER = 46;
private Integer number;

private Number(Integer number) {
if (number < MININUM_NUMBER || number > MAXIMUM_NUMBER) {
throw new IllegalArgumentException();
}
this.number = number;
}

public static Number createInstance(Integer integerNumber) {
return new Number(integerNumber);
}

@Override
public String toString() {
return Integer.toString(number);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Number)) return false;
Number number1 = (Number) o;
return Objects.equals(number, number1.number);
}
}
20 changes: 0 additions & 20 deletions src/main/java/domain/Numbers.java

This file was deleted.

Loading