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

[Feat] 자신이 구매한 로또 번호 출력기능 #2124

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions src/main/java/lotto/BuyingLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lotto;

import camp.nextstep.edu.missionutils.Console;

public class BuyingLotto {
static int lottoCount;

public BuyingLotto() {
buyLotto();
}

private void buyLotto() {
boolean validInput = false;

while (!validInput) {
try {
System.out.println("구입금액을 입력해 주세요.");
String input = Console.readLine();
int money = Integer.parseInt(input);
validateInput(money);
lottoCount = money / 1000;
System.out.println(lottoCount + "개를 구매했습니다.");
validInput = true;
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] 금액은 1000원 단위여야 합니다. 다시 입력해 주세요.");
}
}
}

public int getLottoCount() {
return lottoCount;
}

private void validateInput(int money) {
if (money % 1000 == 0) return;
throw new IllegalArgumentException();
}

}
8 changes: 8 additions & 0 deletions src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ private void validate(List<Integer> numbers) {
}
}

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

public void printLotto() {
System.out.println(numbers);
}

// TODO: 추가 기능 구현
}
32 changes: 32 additions & 0 deletions src/main/java/lotto/LottoCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lotto;

import camp.nextstep.edu.missionutils.Randoms;

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

public class LottoCreator {
private List<Lotto> lottoList;

public LottoCreator(int count) {
createLotto(count);
}

private void createLotto(int count) {
lottoList = IntStream.range(0, count)
.mapToObj(i -> new Lotto(LottoNumberGenerator.generateLottoNumber()))
.collect(Collectors.toList());

}

public List<Lotto> getLottoList() {
return lottoList;
}

public void printLottoList() {
for (Lotto lotto : lottoList) {
lotto.printLotto();
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/LottoNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class LottoNumberGenerator {
private static final int MAX_NUM = 45;
private static final int MIN_NUM = 1;
private static final int COUNT = 6;

private LottoNumberGenerator() {
throw new UnsupportedOperationException("Utility class");
}

public static List<Integer> generateLottoNumber() {
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(MIN_NUM, MAX_NUM, COUNT);
Collections.sort(numbers);
return numbers;
}
}
60 changes: 60 additions & 0 deletions src/main/java/lotto/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package lotto;

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

public class LottoResult {
public int[] resultArray = {0, 0, 0, 0, 0};
private int attempts;

public LottoResult(List<Lotto> lottoList) {
attempts = lottoList.size();
checkLotto(lottoList);
}

private void checkLotto(List<Lotto> lottoList) {
WinningNumberInput winningNumberInput = new WinningNumberInput();

for (Lotto lotto : lottoList) {
List<Integer> common = new ArrayList<>(winningNumberInput.getWinningNumbers());
common.retainAll(lotto.getNumbers());

checkCondition(common.size(), lotto.getNumbers(), winningNumberInput);
}
}

private void checkCondition(int commonSize, List<Integer> lottoNumbers, WinningNumberInput winningNumberInput) {
if (commonSize == 6) {
resultArray[4]++;
return;
}
if (commonSize == 5 && lottoNumbers.contains(winningNumberInput.getBonusNumber())) {
resultArray[3]++;
return;
}
if (commonSize >= 3 && commonSize <= 5) {
resultArray[commonSize - 3]++;
return;
}
}

private double CalculateReturn() {
int allResult = resultArray[0] * 5000 + resultArray[1] * 50000 + resultArray[2] * 1500000 + resultArray[3] * 30000000 + resultArray[4] * 200000000;
double result = (double) allResult / (attempts * 10);

// 소수점 둘째자리에서 반올림
return Math.round(result * 100) / 100.0;
}

public void printResult() {
System.out.println("당첨 통계");
System.out.println("---");
System.out.println("3개 일치(5000원) - " + resultArray[0] + "개");
System.out.println("4개 일치(50,000원) - " + resultArray[1] + "개");
System.out.println("5개 일치(1,500,000원) - " + resultArray[2] + "개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + resultArray[3] + "개");
System.out.println("6개 일치(2,000,000,000원) - " + resultArray[4] + "개");
System.out.println("총 수익률은 " + CalculateReturn() + "%입니다.");
}

}
33 changes: 33 additions & 0 deletions src/main/java/lotto/WinningNumberInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto;

import camp.nextstep.edu.missionutils.Console;

import java.util.List;

public class WinningNumberInput {

private List<Integer> winningNumbers;
private int bonusNumber;

public WinningNumberInput() {
setWinningNumber();
}

private void setWinningNumber() {
System.out.println("당첨 번호를 입력해 주세요.");
String input = Console.readLine();
winningNumbers = WinningNumberValidator.parseAndValidateWinningNumbers(input);

System.out.println("보너스 번호를 입력해 주세요.");
String bonusInput = Console.readLine();
bonusNumber = WinningNumberValidator.validateBonusNumber(bonusInput, winningNumbers);
}

public List<Integer> getWinningNumbers() {
return winningNumbers;
}

public int getBonusNumber() {
return bonusNumber;
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/WinningNumberValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto;

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

public class WinningNumberValidator {

public static List<Integer> parseAndValidateWinningNumbers(String input) {
List<Integer> numbers = Arrays.stream(input.split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());

if (numbers.size() != 6 || !numbers.stream().allMatch(WinningNumberValidator::isValidNumber)) {
throw new IllegalArgumentException("[ERROR] 당첨 번호 오류");
}

return numbers;
}

public static int validateBonusNumber(String input, List<Integer> winningNumbers) {
int number = Integer.parseInt(input);
if (!isValidNumber(number) || winningNumbers.contains(number)) {
throw new IllegalArgumentException("[ERROR] 보너스 번호 오류");
}
return number;
}

private static boolean isValidNumber(int number) {
return number >= 1 && number <= 45;
}
}