Skip to content
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
91 changes: 90 additions & 1 deletion src/main/java/lotto/Application.java

Choose a reason for hiding this comment

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

우왕! 직접 작성하셨다니! 정말 대단해요!

Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
package lotto;
import java.util.*;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현

try{
List<Lotto> purchasedLottos = new ArrayList<>();
int[] result;

//구입 금액 입력 받은 후 로또 개수 구하기
int purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요.");
int numLotto = Lotto.getNumberOfLotto(purchaseAmount);
System.out.println(numLotto + "개를 구매했습니다.");

//로또 생성 후 생성한 로또들 출력
Lotto.generateLotto(purchasedLottos, numLotto);
Application.printPurchasedLottos(purchasedLottos);

//당첨번호 입력받기
List<Integer> winningNumbers = new ArrayList<>(getWinningNumbers());

//보너스 번호 입력받기
int bonus = getBonusNumber();
Lotto.checkWinningNumberBonus(winningNumbers, bonus);


//로또 비교하기
result = getResult(purchasedLottos, winningNumbers, bonus);

//결과 출력하기
Application.printWinningResults(result, purchaseAmount);

} catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}

}


//구매한 로또 목록 출력하는 메소드
private static void printPurchasedLottos(List<Lotto> purchased){
for(int i = 0; i<purchased.size(); i++){
System.out.println(purchased.get(i).getLottoNum());
}
}

//당첨 번호 입력받는 메소드
private static List<Integer> getWinningNumbers() {
List<Integer> winningNumbers = new ArrayList<>(InputHelper.getListInput("당첨 번호를 입력해 주세요."));
Collections.sort(winningNumbers);
if(!Lotto.checkWinningNumbersRange(winningNumbers)){
throw new IllegalArgumentException("[ERROR]로또 번호 개수는 6개입니다.");
}
if (!Lotto.checkWinningNumbersCount(winningNumbers)) {
throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다.");
}
if(!Lotto.checkWinningNumbersDuplication(winningNumbers)){
throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다.");
}
return winningNumbers;
}

//보너스 번호 입력 받는 메소드
private static int getBonusNumber(){
int bonusN = InputHelper.getIntInput("보너스 번호를 입력해 주세요.");
if(bonusN < 1 || bonusN > 45){
throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다.");
}
return bonusN;
}

//구매한 모든 로또에 대해 반복문을 돌면서 당첨 확인한 후 결과를 저장하는 메소드
private static int[] getResult(List<Lotto> purchased, List<Integer> winningNumbers, int bonus){
int[] result = {0, 0, 0, 0, 0, 0};
for(int i = 0; i<purchased.size(); i++){
int rank = Lotto.checkWinningResults(winningNumbers, purchased.get(i), bonus);
result[rank] += 1;
}
return result;
}

//결과 출력하는 메소드
private static void printWinningResults(int[] result, int purchaseP) {
double earningRate;
System.out.println("당첨 통계");
System.out.println("---");
System.out.println("3개 일치 (5,000원) - " + result[5] + "개");
System.out.println("4개 일치 (50,000원) - " + result[4] + "개");
System.out.println("5개 일치 (1,500,000원) - " + result[3] + "개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + result[2] + "개");
System.out.println("6개 일치 (2,000,000,000원) - " + result[1] + "개");
earningRate = (double)((result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000)) / (purchaseP / 1000) * 100;
System.out.println("총 수익률은 " + earningRate + "%입니다.");
}
}
35 changes: 35 additions & 0 deletions src/main/java/lotto/InputHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lotto;


import java.util.ArrayList;
import java.util.List;
import org.kokodak.Console;

public class InputHelper {

//정수를 입력받는 메소드
public static int getIntInput(String prompt) {
try{
System.out.println(prompt);
int num = Integer.parseInt(Console.readLine());
return num;
} catch (IllegalArgumentException e){
throw new IllegalArgumentException("[ERROR]유효한 숫자를 입력해주세요");
}
}

//리스트를 입력받는 메소드
public static List<Integer> getListInput(String prompt){
List<Integer> list = new ArrayList<>();
System.out.println(prompt);
String line = Console.readLine();
String[] str = line.split(",");
if(str.length == 6){
for(int i = 0; i < str.length; i++){
list.add(Integer.parseInt(str[i]));
}
return list;
}
throw new IllegalArgumentException("[ERROR]당첨숫자의 개수를 잘못 입력했습니다.");
}
}
78 changes: 76 additions & 2 deletions src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package lotto;

import java.util.List;
import org.kokodak.Randoms;
import java.util.random.*;
import java.util.*;

public class Lotto {
private final List<Integer> numbers;
public static final int LOTTO_PRICE = 1000;

public Lotto(List<Integer> numbers) {
validate(numbers);
Expand All @@ -15,6 +18,77 @@ private void validate(List<Integer> numbers) {
throw new IllegalArgumentException();
}
}

//로또 넘버 반환하는 메소드
public List<Integer> getLottoNum(){
return numbers;
}

//구매한 로또 개수 구하기
public static int getNumberOfLotto(int price){
if(price % Lotto.LOTTO_PRICE == 0)
return price / Lotto.LOTTO_PRICE;
throw new IllegalArgumentException("[ERROR]구입금액이 정수개로 나누어 떨어지지 않습니다!");
}

//로또 개수만큼 로또 생성하기
public static void generateLotto(List<Lotto> lottoList, int num){
for(int i = 0; i<num; i++){
List<Integer> numberList = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6));
Collections.sort(numberList);
Lotto lotto = new Lotto(numberList);
lottoList.add(lotto);
}
}

//입력받은 당첨번호의 범위 확인하기
public static boolean checkWinningNumbersCount(List<Integer> numbers){
return numbers.size() == 6;
}

public static boolean checkWinningNumbersRange(List<Integer> numbers){
for(int i = 0; i < numbers.size(); i++){
if(numbers.get(i) < 1 || numbers.get(i) > 45){
return false;
}
}
return true;
}

public static boolean checkWinningNumbersDuplication(List<Integer> numbers){
Set<Integer> set = new HashSet<>(numbers);
if(set.size() != numbers.size())
return false;
return true;
}

public static void checkWinningNumberBonus(List<Integer> numbers, int bonus){
Set<Integer> s = new HashSet<>(numbers);
List<Integer> list = new ArrayList<>(numbers);
s.add(bonus);
list.add(bonus);
if(s.size() != list.size()){
throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다.");
}
}


//로또 번호 비교하는 메소드
public static int checkWinningResults(List<Integer> winningNums, Lotto lottos, int bonusN){
int numOfWinnings = 0;
for (int number : lottos.numbers) {
if (winningNums.contains(number)) numOfWinnings++;
}
if(numOfWinnings == 0 || numOfWinnings == 1 || numOfWinnings == 2) return 0;
if(numOfWinnings == 3) return 5;
if(numOfWinnings == 4) return 4;
if(numOfWinnings == 5){
if (lottos.numbers.contains(bonusN))
return 2;
return 3;
}
return 1;
}


// TODO: 추가 기능 구현
}