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
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ repositories {
}

dependencies {
implementation 'com.github.woowacourse-projects:mission-utils:1.0.0'
implementation 'com.github.kokodak:mission-utils:1.0.0'

implementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'org.mockito:mockito-inline:3.12.4'
implementation 'org.assertj:assertj-core:3.21.0'
implementation 'org.junit.jupiter:junit-jupiter:5.8.1'
}

java {
Expand All @@ -19,4 +25,4 @@ java {

test {
useJUnitPlatform()
}
}
32 changes: 32 additions & 0 deletions docs/README.md

Choose a reason for hiding this comment

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

리드미에 구현해야 할 코드를 세분화해서 정리해놓으면 코드 작성시 훨씬 수월할 것 같다는 생각이 드네요 저도 참고하겠습니다!!

Copy link
Author

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
@@ -0,0 +1,32 @@
# 로또
Copy link

Choose a reason for hiding this comment

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

리드미를 이용하여 개발 방향을 잡는 방식은 굉장히 배울만하다고 생각합니다! 다음부터는 저도 리드미 적극 활용하겠습니다 ^~^

Copy link
Author

Choose a reason for hiding this comment

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

근데 이거 요구사항에 있어요!!!!!

Copy link

Choose a reason for hiding this comment

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

저도 알아요

1. 입력
1. 금액 입력
- 정수인지 검증
- 0인지 검증
- 1000원 단위인지 검증
2. 당첨번호 입력
- 정수인지 검증
- 1~45사이 정수인지 검증
- 중복된 로또 번호가 존재하는지 검증
- 개수가 6개인지 검증
3. 보너스번호 입력
- 정수인지 검증
- 1~45사이 정수인지 검증
- 정규 당첨번호에 있는 수인지 검증
2. 로또 생성
1. 금액//1000 만큼 로또 생성
2. 검증
- 1~45사이 정수인지 검증
- 중복된 로또 번호가 존재하는지 검증
- 개수가 6개인지 검증
- 오름차순인지 검증
3. 로또 출력
1. 로또 수 출력
2. 로또 번호 출력
4. 당첨번호와 로또 번호확인
1. 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등
2. 당첨 수 계산
5. 당첨결과 출력
1. 당첨 내역 출력
2. 수익률 출력

14 changes: 14 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package lotto;

import lotto.controller.LottoController;
import lotto.view.InputView;
import lotto.view.OutputView;

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

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
InputView inputView = new InputView();
OutputView outputView = new OutputView();
LottoController lottoController = new LottoController(inputView,outputView);
lottoController.start();

}

}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

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

import lotto.domain.Lotto;
import lotto.domain.Ranking;
import lotto.domain.WinnigResult;
import lotto.util.Constants;
import lotto.util.ExceptionMessage;
import lotto.util.LottoGenerator;
import lotto.util.MoneyValidator;
import lotto.view.InputView;
import lotto.view.OutputView;

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

public class LottoController {
private final InputView inputView;
private final OutputView outputView;

public LottoController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void start(){
try {
int money=inputView.readMoney();
LottoGenerator lottoGenerator = new LottoGenerator();

List<Lotto> lottos = lottoGenerator.generate(money);
List<Integer> winningNumbers = inputView.readWinningNumbers();
int bonusNum = inputView.readBonusNumber(winningNumbers);
WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum);
lottoResult(lottos, winnigResult, money / 1000);
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
private void lottoResult(List<Lotto> lottoList, WinnigResult winnigResult, int ticketNum){
Map<Ranking, Integer> result = setResult();
Ranking rank;

OutputView.printSuccessResult();
for (int i = 0; i < lottoList.size(); i++) {
rank = winnigResult.match(lottoList.get(i));

result.put(rank, result.get(rank) + 1);
}
printResult(result);
printEarningRate(result, ticketNum);
}

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

private void printEarningRate(Map<Ranking, Integer> result, int lottoAmount) {
double EarningRate = 0;
for (Ranking rank : result.keySet()) {
EarningRate =
EarningRate + ((double) (rank.getWinningAmount()) / (lottoAmount * Constants.LOTTO_PRICE) * (result.get(
rank)) * (100));

}
OutputView.printRevenueRate(EarningRate);
}

private Map<Ranking, Integer> setResult() {
Map<Ranking, Integer> result = new LinkedHashMap<>();

for (Ranking rank : Ranking.values()) {
result.put(rank, 0);
}
return result;
}
}
64 changes: 64 additions & 0 deletions src/main/java/lotto/domain/Lotto.java

Choose a reason for hiding this comment

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

도메인이 너무 세분화 되어 있는 것 보다는 핵심 내용 3개로 나누어서 코드를 작성하신 점이 인상 깊네요~ 가독성이 높은 것 같아요. 그런데 validate 함수와 validateDuplicateNum의 기능 차이가 뭔지 설명해주실 수 있을까요? 제가 보기에는 역할이 비슷해보여서요!

Copy link
Author

Choose a reason for hiding this comment

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

어쩔

Copy link
Author

Choose a reason for hiding this comment

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

뻥이에요

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
@@ -0,0 +1,64 @@
package lotto.domain;

import lotto.util.Constants;
import lotto.util.ExceptionMessage;

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

import static lotto.util.ExceptionMessage.INVALID_WINNING_NUMBER_DUPLICATE;
import static lotto.util.ExceptionMessage.INVALID_WINNING_SIZE;

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

public Lotto(List<Integer> numbers) {
validate(numbers);
validateDuplicateNum(numbers);
validateNumRange(numbers);
this.numbers = numbers;
Collections.sort(numbers);
}


private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException(INVALID_WINNING_SIZE.getMessage());
}
Set<Integer> set = new HashSet<>(numbers);
if (set.size()!=6){
throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage());
}
}
private void validateDuplicateNum(List<Integer> numbers) {
Set<Integer> set = new HashSet<>(numbers);
if (set.size()!=6){
throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage());
}
}
private void validateNumRange(List<Integer> numbers) {

for (Integer number : numbers) {
if (number<=0 || number>45){
throw new IllegalArgumentException(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage());
}
}
}

// TODO: 추가 기능 구현
//public void printResult
public int countMatch(List<Integer> winningNum){
return (int) numbers.stream()
.filter(winningNum::contains).count();
}
public boolean containNum(int num){
return numbers.contains(num);
}

public List<Integer> getNumbers() {
return numbers;
}

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

import lotto.view.OutputView;

import java.util.Random;

public enum Ranking {
FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "), // 1등
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), // 2등
THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "), // 3등
FOURTH(4, 50_000, "4개 일치 (50,000원) - "), // 4등
FIFTH(3, 5_000, "3개 일치 (5,000원) - "), // 5등
MISS(0, 0, "");
Ranking(int countOfMatch, int winningAmount, String message) {
this.countOfMatch = countOfMatch;
this.winningAmount = winningAmount;
this.message = message;
}
private int countOfMatch;
private int winningAmount;
private String message;

private static final int WINNG_MIN_COUNT=3;
public static Ranking valueOf(int countOfMatch, boolean matchBonus){
if (countOfMatch<WINNG_MIN_COUNT){
return MISS;
}
if (SECOND.getCountOfMatch()==countOfMatch && matchBonus){
return SECOND;
}
for (Ranking rank : values()) {
if (rank.getCountOfMatch()==countOfMatch && rank!=SECOND){
return rank;
}
}
throw new IllegalArgumentException("[ERROR]");
}


public int getCountOfMatch() {
return countOfMatch;
}

public int getWinningAmount() {
return winningAmount;
}

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

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

public class WinnigResult {
private final Lotto lotto;
private final int bonusball;

public WinnigResult(Lotto lotto, int bonusball) {
this.lotto = lotto;
this.bonusball = bonusball;
}

public Ranking match(Lotto playerNumber) {
int countOfMatch = playerNumber.countMatch(lotto.getNumbers());
boolean bonusCheck = playerNumber.containNum(bonusball);
return Ranking.valueOf(countOfMatch, bonusCheck);
}

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

import java.util.List;

public class BonusNumValidator extends Validator{
@Override
public void validate(String input) throws IllegalStateException {
validateInt(input);
validateLottoNumberRange(input);
}

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

public class Constants {
static final int NUM_COUNT=6;
static final int MAX_NUM=45;
static final int MIN_NUM=1;
public static final int LOTTO_PRICE=1000;
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/util/ExceptionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto.util;

import static lotto.util.Constants.LOTTO_PRICE;

public enum ExceptionMessage {
NOT_INT("정수를 입력해주세요"),
INVALID_LOTTO_NUMBER_RANGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."),
INVALID_MONEY_UNIT(String.format("%d원 단위로 구매해야합니다.", LOTTO_PRICE)),
ZERO_EXCEPTION("0보다 큰 금액을 입력해주세요"),
INVALID_WINNING_SIZE("6개의 번호를 입력해주세요."),
INVALID_WINNING_NUMBER_DUPLICATE("중복되지 않은 6개의 번호를 입력해주세요."),
INVALID_BONUS_NUM("보너스가 번호가 정규번호와 중복되었습니다.");

public static final String BASE_MESSAGE="[ERROR] %s";
private final String message;

ExceptionMessage(String message){
this.message= String.format(BASE_MESSAGE, message);
}

public String getMessage() {
return message;
}
}
26 changes: 26 additions & 0 deletions src/main/java/lotto/util/LottoGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.util;

import lotto.domain.Lotto;
import lotto.view.OutputView;
import org.kokodak.Randoms;

import java.util.*;

import static lotto.util.Constants.*;

public class LottoGenerator {
public List<Lotto> generate(int money){
int ticketNum=money/1000;

OutputView.printTicketCount(ticketNum);
List<Lotto> lottos=new ArrayList<>();
for (int i=0; ticketNum>i; i++){
List<Integer> lottoNum= new ArrayList<>(Randoms.pickUniqueNumbersInRange(MIN_NUM,MAX_NUM,NUM_COUNT));
Lotto lotto = new Lotto(lottoNum);
lottos.add(lotto);
System.out.println(lotto.getNumbers());

}
return lottos;
}
}
Loading