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

[로또] 구원형 미션 제출합니다. #1356

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# java-lotto-precourse

## 기능 요구 사항
### 간단한 로또 발매기를 구현한다.
- 로또 번호의 숫자 범위는 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장의 가격은 1,000원이다.
- 당첨 번호와 보너스 번호를 입력받는다.
- 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역 및 수익률을 출력하고 로또 게임을 종료한다.
- 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 그 부분부터 입력을 다시 받는다.
- Exception이 아닌 IllegalArgumentException, IllegalStateException 등과 같은 명확한 유형을 처리한다.

## 기능 구현 사항
- 입력 관련 기능
- 사용자로부터 로또 구입 금액을 입력받는다.
- 1000원으로 나누어 떨어지지 않는 경우 예외처리
- 당첨번호 6자리를 ','를 기준으로 구분하여 입력받는다
- 당첨번호가 6자리 미만이거나 초과할 경우 예외처리
- 당첨번호가 1 ~ 45를 벗어나는 경우 예외처리
- 숫자 이외의 문자를 입력받거나, 입력이 없을 시 예외처리
- 중복되는 숫자를 입력받았을 시 예외처리
- 보너스 번호를 입력받는다
- 보너스 번호가 1 ~ 45를 벗어나는 경우 예외 처리
- 숫자 이외의 문자를 입력받거나, 입력이 없을 시 예외처리
- 입력받은 당첨 번호와 중복되는 숫자를 입력받았을 시 예외처리
- 1 ~ 45 사이의 로또 번호를 생성하는 기능
- 생성 후 오름차순으로 저장
- 마찬가지로 숫자의 중복이 불가함
- 각 생성된 로또번호를 통해 당첨 내역을 확인하는 기능
- 각 당첨 순위는 Enum으로 명세해둠
- 당첨금을 기준으로 수익률을 계산하는 기능
- 요구사항에 맞게 결과를 출력하는 기능
3 changes: 3 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package lotto;

import static lotto.Controller.lottoController.start;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
start();
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/Controller/lottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto.Controller;

import lotto.domain.*;
import lotto.service.CheckLotto;
import lotto.view.Input;

import java.util.List;
import java.util.Map;

import static lotto.service.CalculateResult.calculateResult;
import static lotto.service.CheckLotto.checkLottoRank;
import static lotto.view.Output.*;

public class lottoController {
public static void start(){
try {
int putchaseAmount = Input.inputPurchaseAmount();
List<Integer> winningNumber = Input.inputWinnerNumbers();
Lotto winningLotto = new Lotto(winningNumber);
int bonusNumber = Input.inputBonusNumber(winningNumber);
int numberOfPurchase = Input.calculateLottoAmount(putchaseAmount);
List<Lotto> lottos = LottoNumberGenerator.generateLottoNumbers(numberOfPurchase);
WinningLotto winningLottoWithBonusNumber = new WinningLotto(winningLotto, bonusNumber);
List<ResultOfLotto> resultOfLottos = checkLottoRank(winningLottoWithBonusNumber, lottos, bonusNumber);
List<WinningRank> resultList = calculateResult(resultOfLottos);
Map<WinningRank, Integer> resultOfLottoAsEnum = collectResult(resultList);
printPurchasedLottoNumbers(lottos);
printResult(resultOfLottoAsEnum, putchaseAmount);
}catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

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

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

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

public Lotto(List<Integer> numbers) {
validateLottoNumber(numbers);
this.numbers = numbers;
}

private void validateLottoSize(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다.");
}
}

// TODO: 추가 기능 구현
private void validateLottoNumberRange(List<Integer> numbers){
numbers.stream().forEach(element->{
if(element < 1 || element > 45){
throw new IllegalArgumentException("[ERROR] 로또 번호는 1 ~ 45 내의 숫자여야 합니다.");
}
});
}

private void validateLottoNumberIsInteger(List<Integer> numbers){
numbers.stream().forEach(element->{
if(!(element instanceof Integer)){
throw new IllegalArgumentException("[ERROR] 로또 번호는 정수만 입력 가능합니다.");
}
});
}

private void validateLottoNumberIsDuplicated(List<Integer> numbers){
Set<Integer> numbersSet = new HashSet<>(numbers);
if(numbersSet.size() < numbers.size()){
throw new IllegalArgumentException("[ERROR] 중복된 로또 번호는 입력이 불가합니다.");
}
}

private void validateLottoNumber(List<Integer> numbers){
try{
validateLottoSize(numbers);
validateLottoNumberRange(numbers);
validateLottoNumberIsInteger(numbers);
validateLottoNumberIsDuplicated(numbers);
}
catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}

public List<Integer> getLottoNumbers(){
return numbers;
}
}
26 changes: 26 additions & 0 deletions src/main/java/lotto/domain/LottoNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.domain;

import camp.nextstep.edu.missionutils.Randoms;

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

public class LottoNumberGenerator {
public static Lotto generateLotto(){
List<Integer> lottoNumbers = Randoms.pickUniqueNumbersInRange(1,45,6);
Collections.sort(lottoNumbers);

Lotto lotto = new Lotto(lottoNumbers);
return lotto;
}

public static List<Lotto> generateLottoNumbers(int amountOfLotto){
List<Lotto> lottos = new ArrayList<>();
for(int i = 0 ; i < amountOfLotto ; i++){
lottos.add(generateLotto());
}
return lottos;
}
}
27 changes: 27 additions & 0 deletions src/main/java/lotto/domain/ResultOfLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lotto.domain;

public class ResultOfLotto {
private int numberOfMatching;
private boolean isBonusNumberCorrect;

public ResultOfLotto(int numberOfMatching, boolean isBonusNumberCorrect) {
this.numberOfMatching = numberOfMatching;
this.isBonusNumberCorrect = isBonusNumberCorrect;
}

public int getNumberOfMatching() {
return numberOfMatching;
}

public void setNumberOfMatching(int numberOfMatching) {
this.numberOfMatching = numberOfMatching;
}

public boolean isBonusNumberCorrect() {
return isBonusNumberCorrect;
}

public void setBonusNumberCorrect(boolean bonusNumberCorrect) {
isBonusNumberCorrect = bonusNumberCorrect;
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/domain/WinningLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.domain;

import java.util.HashMap;
import java.util.Map;

public class WinningLotto {
Map<Lotto, Integer> winningLottoWithBonusNumber = new HashMap<>();

public WinningLotto(Lotto lotto, int bonusNumber) {
winningLottoWithBonusNumber.put(lotto,bonusNumber);
}

public Map<Lotto, Integer> getWinningLotto() {
return winningLottoWithBonusNumber;
}

public void setWinningLotto(Map<Lotto, Integer> winningLotto) {
this.winningLottoWithBonusNumber = winningLotto;
}
}
47 changes: 47 additions & 0 deletions src/main/java/lotto/domain/WinningRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lotto.domain;

public enum WinningRank {
LOSE(0,0,false),
FIFTHPLACE(3,5000,false),
FOURTHPLACE(4,50000,false),
THIRDPLACE(5,1500000,false),
SECONDPLACE(5,30000000,true),
FIRSTPLACE(6,2000000000,false);

private final int numberOfCorrectNumber;
private final int prizeMoneyAmount;

public boolean isBonusNumberCorrect() {
return isBonusNumberCorrect;
}

private final boolean isBonusNumberCorrect;

WinningRank(int numberOfCorrectNumber, int prizeMoneyAmount, boolean isBonusNumberCorrect){
this.numberOfCorrectNumber = numberOfCorrectNumber;
this.prizeMoneyAmount = prizeMoneyAmount;
this.isBonusNumberCorrect = isBonusNumberCorrect;
}

public static WinningRank calculateLottoRank(int numberOfCorrectNumber, boolean isBonusNumberCorrect){
WinningRank lottoWinningRank = LOSE;
for(WinningRank winningRank: WinningRank.values()){
if(winningRank.getnumberOfCorrectNumber() == numberOfCorrectNumber){
if(winningRank.getnumberOfCorrectNumber() == 5 && winningRank.isBonusNumberCorrect() == isBonusNumberCorrect)
lottoWinningRank = winningRank;
else
lottoWinningRank = winningRank;
}
}
return lottoWinningRank;
}


public int getnumberOfCorrectNumber() {
return numberOfCorrectNumber;
}

public int getPrizeMoneyAmount(){
return prizeMoneyAmount;
}
}
25 changes: 25 additions & 0 deletions src/main/java/lotto/service/CalculateResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lotto.service;

import lotto.domain.Lotto;
import lotto.domain.ResultOfLotto;
import lotto.domain.WinningRank;

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

import static lotto.domain.WinningRank.calculateLottoRank;

public class CalculateResult {
public static List<WinningRank> calculateResult(List<ResultOfLotto> resultOfLottos){
List<WinningRank> resultList = new ArrayList<>();
for (ResultOfLotto resultOfLotto: resultOfLottos) {
int matchingNumber = resultOfLotto.getNumberOfMatching();
boolean isBonusNumberCorrect = resultOfLotto.isBonusNumberCorrect();
WinningRank winningRank = calculateLottoRank(matchingNumber, isBonusNumberCorrect);
System.out.println(winningRank);
resultList.add(winningRank);
}
return resultList;
}
}
46 changes: 46 additions & 0 deletions src/main/java/lotto/service/CheckLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lotto.service;

import lotto.domain.Lotto;
import lotto.domain.ResultOfLotto;
import lotto.domain.WinningLotto;

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

public class CheckLotto {
public static List<ResultOfLotto> checkLottoRank(WinningLotto winningLotto, List<Lotto> purchasedLottoNumbers, int bonusNumber){
List<ResultOfLotto> resultOfLottos = new ArrayList<>();

List<Integer> winningLottoNumber = new ArrayList<>();
int winningBonusNumber = 0;
int numberOfMatching = 0;
boolean isBonusNumberCorrect = false;
for (Map.Entry<Lotto, Integer> entry : winningLotto.getWinningLotto().entrySet()) {
winningLottoNumber = entry.getKey().getLottoNumbers();
winningBonusNumber = entry.getValue();
}
for(Lotto purchasedLotto : purchasedLottoNumbers){
List<Integer> purchasedLottoNumber = purchasedLotto.getLottoNumbers();
numberOfMatching = checkLottoNumbers(winningLottoNumber, purchasedLottoNumber);
isBonusNumberCorrect = checkBonusNumber(winningBonusNumber, bonusNumber);
ResultOfLotto resultOfLotto = new ResultOfLotto(numberOfMatching, isBonusNumberCorrect);
resultOfLottos.add(resultOfLotto);
}

return resultOfLottos;
}

public static boolean checkBonusNumber(int winningBonusNumber, int bonusNumber){
return winningBonusNumber == bonusNumber;
}

public static int checkLottoNumbers(List<Integer> winningLottoNumber, List<Integer> purchasedLottoNumber){
long numberOfMatchNumber = purchasedLottoNumber.stream()
.filter(winningLottoNumber::contains)
.count();

return (int)numberOfMatchNumber;
}
}
Loading