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

[로또] 노민정 미션제출합니다 #5

Open
wants to merge 16 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
27 changes: 27 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- 로또 번호의 숫자 범위는 1~45까지이다
- 1개의 로또를 발행할 때 중복되지 않는 6개의 숫자를 뽑는다
- 당첨 번호 추첨 시 중복되지 않는 숫자 6개와 보너스 번호 1개를 뽑는다
- 당첨은 1등부터 5등까지 있다
- 1등: 6개 번호 일치 / 2,000,000,000원
- 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원
- 3등: 5개 번호 일치 / 1,500,000원
- 4등: 4개 번호 일치 / 50,000원
- 5등: 3개 번호 일치 / 5,000원
- 로또 1장의 가격은 1000원

입력
- 로또 구입 금액을 입력 받는다
- "," 로 구분한다
- 당첨 번호 입력 받는다 (6자리)
- 보너스 번호 입력 받는다
출력
- 로또 번호를 발행한다
- 오름차순 정렬
- 당첨 내역 출력
- 수익률을 출력한다
기능
- 게임을 종료 시킨다
예외
- 잘못된 값 입력 시 IllegalArgumentException 발생. 에러 문구는 "[ERROR]"로 시작
- 1000원 미만
- 1000원으로 나누어 떨어지지 않는 금액
4 changes: 4 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package lotto;

import lotto.controller.LottoController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
LottoController lottoController = new LottoController();
lottoController.run();
}
}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

42 changes: 42 additions & 0 deletions src/main/java/lotto/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lotto.controller;// @ author ninaaano

import lotto.model.*;
import lotto.view.InputView;
import lotto.view.OutputView;

public class LottoController {

public void run(){
// 금액을 입력 받는다
int money = InputValidator.isValidMoney(InputView.requestInputMoney()); // return money를 반환했다

// 입력받은 금액만큼 로또를 발행한다
OutputView.printLottoTicket(buyLotto(money));

// 당첨 번호를 입력한다
WinningNumber winningNumber = createWinningNumber();

// 보너스 번호를 입력한다
BonusNumber bonusNumber = createBonusNumber(winningNumber);


// 당첨 내역을 출력한다

}

private LottoTicket buyLotto(int money){
int count = LotteryCalculator.calculator(money);
OutputView.printLottoNumber(count);
return new LottoTicket(count);
}

private WinningNumber createWinningNumber(){
return new WinningNumber(InputValidator.isValidNumber());
} // retrurn 할때 이미 ,로 구분. 근데 입력받을때 ,안적어도 에러가 안난다???

private BonusNumber createBonusNumber(WinningNumber winningNumber){
return new BonusNumber(InputValidator.isValidBonusNumber(),winningNumber.getLottoNumbers());
}


}
118 changes: 118 additions & 0 deletions src/main/java/lotto/model/InputValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package lotto.model;// @ author ninaaano

import lotto.model.constant.InputValidMessage;
import lotto.view.InputView;
import lotto.model.Lotto;

import java.util.*;
import java.util.stream.Collectors;

import static lotto.model.constant.CommonCostants.IS_NUMBER;
import static lotto.model.constant.CommonCostants.MIN_LOTTO_PRICE;

public class InputValidator {

public InputValidator() {
}

/**
* 입력받은 로또 번호 유효성 검사
*/
public static List<Integer> isValidNumber() {
List<Integer> winningNumber = new ArrayList<>();
try {
//isCheckNumber(inputNumber);
//int number = Integer.parseInt(inputNumber);
//validateLottoNumber(number);
String input = InputView.requestInputLottoNumber();
winningNumber = Arrays.stream(input.split(","))
.map(Integer::parseInt).collect(Collectors.toList());
} catch (IllegalArgumentException e) {
//System.out.println(InputValidMessage.INPUT_NUMBER_ERROR.get());
throw new IllegalArgumentException(InputValidMessage.AMOUNT_RANGE.get());
}
return winningNumber;
}

public static void validateLottoNumber(int number) {
if (!checkRange(number)) {
System.out.println("로또 번호는 1 ~ 45 사이의 번호여야 합니다.");
throw new IllegalArgumentException(InputValidMessage.AMOUNT_RANGE.get());
}
}

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

public static int isValidBonusNumber() {
int bonusNumber = 0;
try {
bonusNumber = Integer.parseInt(InputView.requestInputBonusNumber());
// validateDuplicates(Lotto lotto,bonusNumber);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(InputValidMessage.REDUPLICATION.get());
}
return bonusNumber;
}

public static void validateDuplicates(Lotto lotteryNumbers, int bonusNumber) {
if (lotteryNumbers.contains(bonusNumber)) {
throw new IllegalArgumentException(InputValidMessage.REDUPLICATION.get());
}
}






/**
* 로또 번호 검사
* @param numbers
*/
public static void validLottoNumberSize(List<Integer> numbers) {
if(numbers.size() != 6){
throw new IllegalArgumentException(InputValidMessage.REDUPLICATION.get());
}
}

public static void validDuplicate(List<Integer> number){
Set<Integer> lottoNo = new HashSet<>(number);

if(lottoNo.size() != number.size()){
throw new IllegalArgumentException(InputValidMessage.REDUPLICATION.get());
}
}


/**
* 입력받은 금액 유효성 검사
* @param inputMoney
* @return
*/

public static int isValidMoney(String inputMoney){
int money = Integer.parseInt(inputMoney);
try {
isCheckNumber(inputMoney);
amoutRange(money);
return money;
}catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return Integer.parseInt(InputView.requestInputMoney());
}

private static void amoutRange(int money) throws IllegalArgumentException{
if(money < MIN_LOTTO_PRICE || (money % MIN_LOTTO_PRICE) != 0) {
throw new IllegalArgumentException(InputValidMessage.INPUT_MONEY_ERROR.get());
}
}

private static void isCheckNumber(String input) throws IllegalArgumentException{
if(input == null || input.isBlank() || !input.matches(IS_NUMBER)){
throw new IllegalArgumentException(InputValidMessage.INPUT_NUMBER_ERROR.get());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/model/LotteryCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.model;// @ author ninaaano

import static lotto.model.constant.CommonCostants.MIN_LOTTO_PRICE;

/**
* 입력받은 금액을 계산하는 클래스
*/
public class LotteryCalculator {
private final int purchaseMoney;

public LotteryCalculator(int money) {
this.purchaseMoney = money;
}


public static int calculator(int money){
return money / MIN_LOTTO_PRICE;
}

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

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

public class Lotto {
private final List<Integer> numbers;

public Lotto(List<Integer> numbers) {
numbers.forEach(InputValidator::validateLottoNumber);
InputValidator.validLottoNumberSize(numbers);
InputValidator.validDuplicate(numbers);
this.numbers = numbers;
}


public boolean contains(int lottoNumber) {
return getLottoNumber().contains(lottoNumber);
}

public List<Integer> getLottoNumber() {
return new ArrayList<>(numbers);
}

public int countMatchedNumbers(List<Integer> winningNumbers) {
return (int) this.numbers.stream()
.filter(winningNumbers::contains)
.count();
}

// 내보내기
// public LottoNumbersDTO export() {
// return new LottoNumbersDTO(numbers);
// }

}
30 changes: 30 additions & 0 deletions src/main/java/lotto/model/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lotto.model;// @ author ninaaano

import camp.nextstep.edu.missionutils.Randoms;

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

public class LottoTicket {
private final List<Lotto> lottoTickets = new ArrayList<>();
public LottoTicket(int count) {
createLottoTickets(count);
}

private void createLottoTickets(int ticketNumber) {
for (int i = 0; i < ticketNumber; i++) {
List<Integer> lottoNumber = new ArrayList<>(
Randoms.pickUniqueNumbersInRange(1, 45, 6));
Collections.sort(lottoNumber);
Lotto lotto = new Lotto(lottoNumber);
lottoTickets.add(lotto);
}
}

public List<Lotto> getLottoTickets(){
return lottoTickets;
}


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

import java.util.List;

public class WinningNumber {

private final List<Integer> lottoNumbers;

public WinningNumber(List<Integer> lottoNumbers) {
this.lottoNumbers = lottoNumbers;
}

public List<Integer> getLottoNumbers() {
return lottoNumbers;
}
}
10 changes: 10 additions & 0 deletions src/main/java/lotto/model/constant/CommonCostants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lotto.model.constant;// @ author ninaaano

public class CommonCostants {

public static final int MIN_LOTTO_PRICE = 1000;
public static final String IS_NUMBER = "^[1-9][0-9]+$";

private CommonCostants() {
}
}
18 changes: 18 additions & 0 deletions src/main/java/lotto/model/constant/InputMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lotto.model.constant;// @ author ninaaano

public enum InputMessage {

REQUEST_INPUT_MONEY("구입금액을 입력해 주세요."),
REQUEST_INPUT_LOTTO_NUMBER("당첨 번호를 입력해주세요."),
REQUEST_BONUS_NUMBER("보너스 번호를 입력해 주세요.");

private final String message;

private InputMessage (String message) {
this.message = message;
}

public String get() {
return message;
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/model/constant/InputValidMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.model.constant;// @ author ninaaano

public enum InputValidMessage {
INPUT_MONEY_ERROR("[ERROR] 정확한 금액을 입력해주세요"),
INPUT_NUMBER_ERROR("[ERROR] 숫자만 입력할 수 있습니다"),
REDUPLICATION("[ERROR] 중복된 숫자는 입력할 수 없습니다"),
AMOUNT_RANGE("[ERROR] 1-45사이의 숫자만 입력할 수 있습니다")
;

private final String message;

private InputValidMessage (String message) {
this.message = message;
}

public String get() {
return message;
}

}
17 changes: 17 additions & 0 deletions src/main/java/lotto/model/constant/OutputMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lotto.model.constant;// @ author ninaaano

public enum OutputMessage {

RESPONSE_LOTTO_TICKET("%d개를 구매했습니다.");

private final String message;

private OutputMessage (String message) {
this.message = message;
}

public String get() {
return message;
}

}
Loading