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

import lotto.controller.LottoController;
import lotto.domain.Numbers;

import java.util.List;

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

LottoController lottoController = new LottoController();
lottoController.run();
}
}
16 changes: 16 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,7 +17,20 @@ 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() {
return numbers;
}

// TODO: 추가 기능 구현
}



//git 연결 확인
136 changes: 136 additions & 0 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
@@ -0,0 +1,136 @@
package lotto.controller;

import lotto.Lotto;
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.*;

public class LottoController {

private static List<Lotto> lottoList;

public void run() {
try {
start();
} catch (IllegalStateException e) {
System.out.println(e.getMessage());
}
}

public void start() {
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<>();

for (int i = 0; i < ticketCount; i++) {
lottoList.add(makeLotto());
}
return lottoList;
}

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

return new Lotto(lotto);
}

private void lottoResult(List<Lotto> lottoList, List<Integer> winningNumber, int bonusNumber) {
System.out.println("당첨 통계");
System.out.println("---");

Map<Rank, Integer> count = findRank(lottoList, winningNumber, bonusNumber);
printResult(count);
printRate(count, lottoList.size());
}

private Map<Rank, Integer> findRank(List<Lotto> lottoList, List<Integer> winningNumber, int bonusNumber) {
Map<Rank, Integer> count = setCountZero();

for (int i = 0; i < lottoList.size(); i++) {
Rank rank = match(lottoList.get(i), winningNumber, bonusNumber);
count.put(rank, count.get(rank) + 1);
}
return count;
}

private Map<Rank, Integer> setCountZero() {
Map<Rank, Integer> count = new HashMap<>();

for (Rank rank : Rank.values()) {
count.put(rank, 0);
}
return count;
}

private static Rank match(Lotto lotto, List<Integer> winningNumber, int bonusNumber) {
int countMatch;
boolean containBonus;
List<Integer> numbers = lotto.getNumbers();

countMatch = (int) numbers.stream()
.filter(winningNumber::contains) // 이중콜론 -> 메서드 참조
.count();

containBonus = numbers.contains(bonusNumber);

return Rank.findRank(countMatch, containBonus);
}

private static void printResult(Map<Rank, Integer> count) {
for (int i = Rank.values().length - 1; i >= 0; i--) {
Rank.values()[i].printMessage(count.get(Rank.values()[i]));
}
}

private static void printRate(Map<Rank, Integer> count, int ticketCount) {
double rate = 0;
for (Rank rank : count.keySet()) {
rate = rate + ((double) (rank.getWinningPrice()) / (ticketCount * 1000) * count.get(rank) * 100);
}
OutputView.printRate(rate);
}
}
40 changes: 40 additions & 0 deletions src/main/java/lotto/domain/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lotto.domain;

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

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

public class LottoNumber {
public final int count;
public final List<Lotto> lottos = new ArrayList<>();

public LottoNumber(int money) {
moneyValidate(money);
this.count = money / 1000;
}

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

public void createLotto() {
for (int i = 0; i < count ; i++) {
Lotto lotto = createLottoNumber();
lottos.add(lotto);
}
}

public Lotto createLottoNumber() {
List<Integer> integers = Numbers.randomNumbers();
return new Lotto(integers);
}
}
17 changes: 17 additions & 0 deletions src/main/java/lotto/domain/Numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lotto.domain;

import org.kokodak.Randoms;

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

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

return numberList;
}
}
44 changes: 44 additions & 0 deletions src/main/java/lotto/domain/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lotto.domain;

import lotto.view.OutputView;

import java.util.Arrays;

public enum Rank {
FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - ", false), // 1등
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - ", true), // 2등
THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - ", false), // 3등
FOURTH(4, 50_000, "4개 일치 (50,000원) - ", false), // 4등
FIFTH(3, 5_000, "3개 일치 (5,000원) - ", false), // 5등
MISS(0, 0, "", false);

private final int countMatch;
private final int winningPrice;
private final String message;
private final boolean bonus;

Rank(int countMatch, int winningPrice, String message, boolean bonus) {
this.countMatch = countMatch;
this.winningPrice = winningPrice;
this.message = message;
this.bonus = bonus;
}

public static Rank findRank(int countMatch, boolean bonus) {
return Arrays.stream(values())
.filter(rank -> rank.countMatch == countMatch)
.filter(rank -> rank.bonus == bonus)
.findAny()
.orElse(Rank.MISS);
}

public void printMessage(int count) {
if (this != MISS) {
OutputView.printSuccessMessage(message, count);
}
}

public int getWinningPrice() {
return winningPrice;
}
}
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] 입력은 숫자여야 한다.");
}
}
50 changes: 50 additions & 0 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lotto.view;

import org.kokodak.Console;

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

public class InputView {

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

}

public static List<Integer> inputWinningNumber() {
System.out.println("당첨 번호를 입력해 주세요.");
return numberList(Console.readLine());
}

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++) {
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("보너스 번호를 입력해 주세요.");
try {
return Integer.parseInt(Console.readLine());
} catch (NumberFormatException e) {
ExceptionMessage.isNotNumber();
throw new IllegalArgumentException();
}
}

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

public class OutputView {

public static void printTicketCount(int count) {
System.out.println(count + "개를 구매했습니다.");
}

public static void printSuccessMessage(String message, int count) {
System.out.println(message + count + "개");
}

public static void printRate(double rate) {
System.out.println("총 수익률은 " + String.format("%.1f", rate) + "%입니다.");
}
}