Skip to content
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
8 changes: 8 additions & 0 deletions src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package lotto;

import lotto.view.ExceptionMessage;

import java.util.HashSet;
import java.util.List;

public class Lotto {
Expand All @@ -14,6 +17,11 @@ private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException();
}
HashSet<Integer> integers = new HashSet<>(numbers);
if (integers.size() != numbers.size()) {
throw new IllegalArgumentException();
}

}

public List<Integer> getNumbers() {
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/lotto/controller/LottoController.java
Copy link

Choose a reason for hiding this comment

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

domain과 view에서 다뤄야 할 메소드를 분리하고 controller에서는 전체적인 흐름을 관리하도록 하는 게 좋을 것 같습니다.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import lotto.domain.LottoNumber;
import lotto.domain.Numbers;
import lotto.domain.Rank;
import lotto.view.ExceptionMessage;
import lotto.view.InputView;
import lotto.view.OutputView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class LottoController {

Expand All @@ -20,22 +18,49 @@ public void run() {
try {
start();
} catch (IllegalStateException e) {

System.out.println(e.getMessage());
}
}

public void start() {
int money = Integer.parseInt(InputView.inputMyMoney());
int money = InputView.inputMyMoney();
int ticketCount = new LottoNumber(money).count;
OutputView.printTicketCount(ticketCount);

lottoList = makeLottoList(ticketCount);
List<Integer> winningNumber = InputView.inputWinningNumber();
validateWinningNumber(winningNumber);
int bonusNumber = InputView.inputBonusNumber(winningNumber);

validateBonusNumber(winningNumber, bonusNumber);
lottoResult(lottoList, winningNumber, bonusNumber);
}

private void validateWinningNumber(List<Integer> winningNumber) {
if (winningNumber.size() != 6) {
ExceptionMessage.isCountNotSix();
throw new IllegalArgumentException();
}
for (int i = 0; i < winningNumber.size(); i++) {
if (winningNumber.get(i) < 1 || winningNumber.get(i) > 45) {
ExceptionMessage.isNotRange();
throw new IllegalArgumentException();
}
}
HashSet<Integer> numbers = new HashSet<>(winningNumber);
if (winningNumber.size() != numbers.size()) {
ExceptionMessage.isDuplicate();
throw new IllegalArgumentException();
}
}

private void validateBonusNumber(List<Integer> winningNumber, int bonusNumber) {
if (winningNumber.contains(bonusNumber)) {
ExceptionMessage.isNotPossibleBonus();
throw new IllegalArgumentException();
}
}


private static List<Lotto> makeLottoList(int ticketCount) {
lottoList = new ArrayList<>();

Expand All @@ -46,7 +71,7 @@ private static List<Lotto> makeLottoList(int ticketCount) {
}

private static Lotto makeLotto() {
List<Integer> lotto = new ArrayList<>();
List<Integer> lotto;
lotto = Numbers.randomNumbers();
System.out.println(lotto);

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/lotto/domain/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lotto.domain;

import lotto.Lotto;
import lotto.view.ExceptionMessage;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,8 +16,13 @@ public LottoNumber(int money) {
}

private void moneyValidate(int money) {
if (money <= 0 || money % 1000 != 0) {
throw new IllegalStateException("error");
if (money <= 0) {
ExceptionMessage.isPositive();
throw new IllegalArgumentException();
}
if (money % 1000 != 0) {
ExceptionMessage.isDivisible();
throw new IllegalArgumentException();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/lotto/domain/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

public class Numbers {
public static List<Integer> randomNumbers() {
List<Integer> numberList = Randoms.pickUniqueNumbersInRange(1, 45, 6);
List<Integer> numberList = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6));
// unsupportedoperationexception 에러 -> ArrayList로 받아서 해결
Collections.sort(numberList);

return numberList;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/lotto/view/ExceptionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lotto.view;

public class ExceptionMessage {

// 1. 구입 금액 양수
public static void isPositive() {
System.out.println("[ERROR] 구입 금액은 양수여야 한다.");
}

// 2. 구입 금액 1000의 배수
public static void isDivisible() {
System.out.println("[ERROR] 구입 금액은 1000의 배수여야 한다.");
}

// 3. 입력 숫자 중복
public static void isDuplicate() {
System.out.println("[ERROR] 로또 번호는 중복되지 않아야 한다.");
}

//4. 입력 숫자 범위
public static void isNotRange() {
System.out.println("[ERROR] 로또 번호는 1-45 사이여야 한다.");
}

// 5. 입력 숫자 개수 6
public static void isCountNotSix() {
System.out.println("[ERROR] 로또 번호는 6개를 선택해야 한다.");
}

// 6. 보너스 숫자 중복
public static void isNotPossibleBonus() {
System.out.println("[ERROR] 보너스 번호는 기존의 번호와 달라야 한다.");
}

// 7. 숫자가 아닌 입력
public static void isNotNumber() {
System.out.println("[ERROR] 입력은 숫자여야 한다.");
}
}
27 changes: 19 additions & 8 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package lotto.view;

import lotto.Lotto;
import org.kokodak.Console;

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

public class InputView {

public static String inputMyMoney() {
public static int inputMyMoney() {
System.out.println("구입금액을 입력해 주세요.");
return Console.readLine();
try {
return Integer.parseInt(Console.readLine());
} catch (NumberFormatException e) {
ExceptionMessage.isNotNumber();
throw new IllegalArgumentException();
}

}

public static List<Integer> inputWinningNumber() {
Expand All @@ -22,18 +27,24 @@ private static List<Integer> numberList(String winningNumber) {
String[] numbers = winningNumber.split(",");
ArrayList<Integer> winningNumberList = new ArrayList<Integer>();
for (int i = 0; i < numbers.length; i++) {
winningNumberList.add(Integer.parseInt(numbers[i]));
try {
winningNumberList.add(Integer.parseInt(numbers[i]));
} catch (NumberFormatException e){
ExceptionMessage.isNotNumber();
throw new IllegalArgumentException();
}
}
return winningNumberList;
}

public static int inputBonusNumber(List<Integer> winningNumber) {
System.out.println("보너스 번호를 입력해 주세요.");
int bonusNumber = Integer.parseInt(Console.readLine());
if (winningNumber.contains(bonusNumber)) {
try {
return Integer.parseInt(Console.readLine());
} catch (NumberFormatException e) {
ExceptionMessage.isNotNumber();
throw new IllegalArgumentException();
}
return bonusNumber;
}

}
}