-
Notifications
You must be signed in to change notification settings - Fork 8
[로또] 김난슬 미션 제출합니다. #4
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
Open
seulnan
wants to merge
20
commits into
Java-JavaScript-Language-Stuty:main
Choose a base branch
from
seulnan:seulnan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5ae3864
docs(README): 요구사항 리드미에 정리
seulnan 16bc3ee
feat(view): inputview 및 outputview 구현
seulnan fc805fb
feat(controller): 게임 진행 흐름 관리
seulnan 46963f5
feat(service): 게임 로직 및 로또 번호 생성기능 추가
seulnan 69e8af2
feat(entity): 로또 및 당첨 로직
seulnan 096c11c
feat(validator): 입력값 검증 기능 추가
seulnan da703b7
feat(enums): 로또 등수 판별 로직 추가
seulnan 910f32f
feat(util): 로또 결과 계산
seulnan 31ec78e
feat(dto): 당첨결과 데이터구조
seulnan 43a22da
chore(config): AppConfig추가하여 의존성 관리
seulnan 2264b65
feat(test): 로또 서비스 및 검증 테스트추가
seulnan d8c7f00
fix(view): 당첨 결과 출력 순서 변경 및 코드 스타일 개선
seulnan f0f7978
refactor(view): 입력 검증 로직을 재사용 가능하도록 리팩토링
seulnan 9f588a6
refactor(validator): 로또 입력값 검증 로직 최적화 및 중복 제거
seulnan 313a3fd
refactor(entity): `WinningLotto` 생성자 변경 (컨트롤러와 일치하도록 수정)
seulnan 329dfa7
refactor(domain): `WinningLotto`를 `entity`에서 `domain` 패키지로 변경
seulnan 20b94d0
remove(validator): `LottoValidator` 삭제 및 `InputValidator`로 검증 일원화
seulnan 4f12033
refactor(enum,domain): 상수화 적용
seulnan 00980d2
refactor(view): `OutputView` 최적화 및 코드 가독성 향상
seulnan 86eac65
fix(view): `OutputView` 출력 순서 수정 (3개 → 6개)
seulnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,160 @@ | ||
| # 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` 등과 같은 명확한 유형을 처리한다. | ||
|
|
||
| ## 입출력 요구 사항 | ||
|
|
||
| ### 입력 | ||
|
|
||
| 로또 구입 금액을 입력 받는다. 구입 금액은 1,000원 단위로 입력 받으며 1,000원으로 나누어 떨어지지 않는 경우 예외 처리한다. | ||
|
|
||
| ``` | ||
| 14000 | ||
| ``` | ||
|
|
||
| 당첨 번호를 입력 받는다. 번호는 쉼표(,)를 기준으로 구분한다. | ||
| ``` | ||
| 1,2,3,4,5,6 | ||
| ``` | ||
|
|
||
| 보너스 번호를 입력 받는다. | ||
| ``` | ||
| 7 | ||
| ``` | ||
|
|
||
| ### 출력 | ||
|
|
||
| 발행한 로또 수량 및 번호를 출력한다. 로또 번호는 오름차순으로 정렬하여 보여준다. | ||
|
|
||
| ``` | ||
| 8개를 구매했습니다. | ||
| [8, 21, 23, 41, 42, 43] | ||
| [3, 5, 11, 16, 32, 38] | ||
| [7, 11, 16, 35, 36, 44] | ||
| [1, 8, 11, 31, 41, 42] | ||
| [13, 14, 16, 38, 42, 45] | ||
| [7, 11, 30, 40, 42, 43] | ||
| [2, 13, 22, 32, 38, 45] | ||
| [1, 3, 5, 14, 22, 45] | ||
| ``` | ||
|
|
||
| 당첨 내역을 출력한다. | ||
|
|
||
| ``` | ||
| 3개 일치 (5,000원) - 1개 | ||
| 4개 일치 (50,000원) - 0개 | ||
| 5개 일치 (1,500,000원) - 0개 | ||
| 5개 일치, 보너스 볼 일치 (30,000,000원) - 0개 | ||
| 6개 일치 (2,000,000,000원) - 0개 | ||
| ``` | ||
|
|
||
| 수익률은 소수점 둘째 자리에서 반올림한다. (ex. 100.0%, 51.5%, 1,000,000.0%) | ||
| ``` | ||
| 총 수익률은 62.5%입니다. | ||
| ``` | ||
|
|
||
| 예외 상황 시 에러 문구를 출력해야 한다. 단, 에러 문구는 "[ERROR]"로 시작해야 한다. | ||
| ``` | ||
| [ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다. | ||
| ``` | ||
|
|
||
| ### 실행 결과 예시 | ||
|
|
||
| ``` | ||
| 구입금액을 입력해 주세요. | ||
| 8000 | ||
|
|
||
| 8개를 구매했습니다. | ||
| [8, 21, 23, 41, 42, 43] | ||
| [3, 5, 11, 16, 32, 38] | ||
| [7, 11, 16, 35, 36, 44] | ||
| [1, 8, 11, 31, 41, 42] | ||
| [13, 14, 16, 38, 42, 45] | ||
| [7, 11, 30, 40, 42, 43] | ||
| [2, 13, 22, 32, 38, 45] | ||
| [1, 3, 5, 14, 22, 45] | ||
|
|
||
| 당첨 번호를 입력해 주세요. | ||
| 1,2,3,4,5,6 | ||
|
|
||
| 보너스 번호를 입력해 주세요. | ||
| 7 | ||
|
|
||
| 당첨 통계 | ||
|
|
||
| 3개 일치 (5,000원) - 1개 | ||
| 4개 일치 (50,000원) - 0개 | ||
| 5개 일치 (1,500,000원) - 0개 | ||
| 5개 일치, 보너스 볼 일치 (30,000,000원) - 0개 | ||
| 6개 일치 (2,000,000,000원) - 0개 | ||
| 총 수익률은 62.5%입니다. | ||
| ``` | ||
|
|
||
| ## 🎯 프로그래밍 요구 사항 | ||
|
|
||
| - JDK 17 버전에서 실행 가능해야 한다. JDK 17에서 정상적으로 동작하지 않을 경우 0점 처리한다. | ||
| - 프로그램 실행의 시작점은 `Application`의 `main()`이다. | ||
| - `build.gradle` 파일을 변경할 수 없고, 외부 라이브러리를 사용하지 않는다. | ||
| - Java 코드 컨벤션 가이드를 준수하며 프로그래밍한다. | ||
| - 프로그램 종료 시 `System.exit()`를 호출하지 않는다. | ||
| - 프로그램 구현이 완료되면 `ApplicationTest`의 모든 테스트가 성공해야 한다. 테스트가 실패할 경우 0점 처리한다. | ||
| - 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 이름을 수정하거나 이동하지 않는다. | ||
| - indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다. | ||
| - 함수(또는 메서드)의 길이가 15라인을 넘어가지 않도록 구현한다. | ||
| - `else` 예약어를 쓰지 않는다. | ||
| - Java Enum을 적용한다. | ||
| - 도메인 로직에 단위 테스트를 구현해야 한다. 단, UI(`System.out`, `System.in`, `Scanner`) 로직은 제외한다. | ||
| - 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 분리해 구현한다. | ||
|
|
||
| ## 라이브러리 | ||
|
|
||
| - `camp.nextstep.edu.missionutils`에서 제공하는 `Randoms` 및 `Console` API를 사용하여 구현해야 한다. | ||
|
|
||
| List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); | ||
|
|
||
| ## Lotto 클래스 | ||
|
|
||
| 제공된 `Lotto` 클래스를 활용해 구현해야 한다. | ||
|
|
||
| - `numbers`의 접근 제어자인 `private`을 변경할 수 없다. | ||
| - `Lotto`에 필드(인스턴스 변수)를 추가할 수 없다. | ||
| - `Lotto`의 패키지 변경은 가능하다. | ||
|
|
||
| ```java | ||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| validate(numbers); | ||
| this.numbers = numbers; | ||
| } | ||
|
|
||
| private void validate(List<Integer> numbers) { | ||
| if (numbers.size() != 6) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| // TODO: 추가 기능 구현 | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.config.AppConfig; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| AppConfig.getLottoController().startGame(); | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package lotto.config; | ||
|
|
||
| import lotto.controller.LottoController; | ||
| import lotto.service.LottoService; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| public class AppConfig { | ||
| private static final InputView INPUT_VIEW = new InputView(); | ||
| private static final OutputView OUTPUT_VIEW = new OutputView(); | ||
| private static final LottoService LOTTO_SERVICE = new LottoService(); | ||
|
|
||
| private static final LottoController LOTTO_CONTROLLER = | ||
| new LottoController(LOTTO_SERVICE, INPUT_VIEW, OUTPUT_VIEW); | ||
|
|
||
| public static LottoController getLottoController() { | ||
| return LOTTO_CONTROLLER; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package lotto.controller; | ||
|
|
||
| import java.util.List; | ||
| import lotto.model.dto.LottoResultDto; | ||
| import lotto.model.domain.LottoTicket; | ||
| import lotto.model.domain.WinningLotto; | ||
| import lotto.service.LottoService; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| public class LottoController { | ||
| private final LottoService lottoService; | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
|
|
||
| public LottoController(LottoService lottoService, InputView inputView, OutputView outputView) { | ||
| this.lottoService = lottoService; | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void startGame() { | ||
| int purchaseAmount = inputView.readPurchaseAmount(); | ||
| LottoTicket lottoTicket = lottoService.purchaseLottoTickets(purchaseAmount); | ||
| outputView.printLottoTickets(lottoTicket); | ||
|
|
||
| WinningLotto winningLotto = getWinningLotto(); | ||
| LottoResultDto result = lottoService.calculateResults(lottoTicket, winningLotto); | ||
| outputView.printResults(result, purchaseAmount); | ||
| } | ||
|
|
||
| private WinningLotto getWinningLotto() { | ||
| List<Integer> winningNumbers = inputView.readWinningNumbers(); // 당첨 번호 입력 | ||
| int bonusNumber = inputView.readBonusNumber(winningNumbers); // 당첨 번호를 넘겨줌 | ||
| return new WinningLotto(winningNumbers, bonusNumber); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package lotto.enums; | ||
|
|
||
| public enum LottoPrice { | ||
| PRICE_PER_TICKET(1000); | ||
|
|
||
| private final int price; | ||
|
|
||
| LottoPrice(int price) { | ||
| this.price = price; | ||
| } | ||
|
|
||
| public int getPrice() { | ||
| return price; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package lotto.enums; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public enum LottoRank { | ||
| FIRST(6, false, 2_000_000_000), | ||
| SECOND(5, true, 30_000_000), | ||
| THIRD(5, false, 1_500_000), | ||
| FOURTH(4, false, 50_000), | ||
| FIFTH(3, false, 5_000), | ||
| NONE(0, false, 0); | ||
|
|
||
| private static final int BONUS_CRITERIA = 5; | ||
|
|
||
| private final int matchCount; | ||
| private final boolean requiresBonus; | ||
| private final int prize; | ||
| private static final Map<Integer, LottoRank> RANK_MAP = Arrays.stream(values()) | ||
| .filter(rank -> !rank.requiresBonus) | ||
| .collect(Collectors.toMap(LottoRank::getMatchCount, rank -> rank)); | ||
|
|
||
| LottoRank(int matchCount, boolean requiresBonus, int prize) { | ||
| this.matchCount = matchCount; | ||
| this.requiresBonus = requiresBonus; | ||
| this.prize = prize; | ||
| } | ||
|
|
||
| public int getPrize() { | ||
| return prize; | ||
| } | ||
|
|
||
| public int getMatchCount() { | ||
| return matchCount; | ||
| } | ||
|
|
||
| public static LottoRank findRank(int matchCount, boolean hasBonus) { | ||
| if (hasBonus && matchCount == BONUS_CRITERIA) { | ||
| return SECOND; | ||
| } | ||
| return RANK_MAP.getOrDefault(matchCount, NONE); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package lotto.model.domain; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| validate(numbers); | ||
| this.numbers = List.copyOf(numbers); // 불변 리스트로 변환 | ||
| } | ||
|
|
||
| private void validate(List<Integer> numbers) { | ||
| if (numbers.size() != 6 || new HashSet<>(numbers).size() != 6) { | ||
| throw new IllegalArgumentException("[ERROR] 로또 번호는 6개의 서로 다른 숫자여야 합니다."); | ||
| } | ||
| if (numbers.stream().anyMatch(num -> num < 1 || num > 45)) { | ||
| throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| public List<Integer> getNumbers() { | ||
| return numbers; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package lotto.model.domain; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoTicket { | ||
| private final List<Lotto> tickets; | ||
| private static final int LOTTO_MIN_NUMBER = 1; // 🎯 로또 번호 최소값 | ||
| private static final int LOTTO_MAX_NUMBER = 45; // 🎯 로또 번호 최대값 | ||
| private static final int LOTTO_SIZE = 6; | ||
|
|
||
| public LottoTicket(int count) { | ||
| this.tickets = IntStream.range(0, count) | ||
| .mapToObj(i -> generateLotto()) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Lotto generateLotto() { | ||
| List<Integer> numbers = new ArrayList<>(Randoms.pickUniqueNumbersInRange(LOTTO_MIN_NUMBER, LOTTO_MAX_NUMBER, LOTTO_SIZE)); | ||
| Collections.sort(numbers); | ||
| return new Lotto(numbers); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LottoTicket에서 Lotto를 생성하면 두 도메인이 분리되기 어렵다보니 Lotto 내부에서 팩토리 패턴으로 생성하는 방안을 써보는걸 추천드려용 만약 저렇게 바꾸신다면 static 변수들도 lotto로 옮기셔도 될듯? static이 붙은 클래스 내 변수는 인스턴스 변수가 아니라 조건에 위배안되더라구요 |
||
|
|
||
| public List<Lotto> getTickets() { | ||
| return tickets; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package lotto.model.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class WinningLotto { | ||
| private final Set<Integer> winningNumbers; | ||
| private final int bonusNumber; | ||
|
|
||
| public WinningLotto(List<Integer> numbers, int bonusNumber) { | ||
| this.winningNumbers = Set.copyOf(numbers); | ||
| this.bonusNumber = bonusNumber; | ||
| } | ||
|
|
||
| public int countMatchingNumbers(Lotto lotto) { | ||
| return (int) lotto.getNumbers().stream() | ||
| .filter(winningNumbers::contains) | ||
| .count(); | ||
| } | ||
|
|
||
| public boolean hasBonusNumber(Lotto lotto) { | ||
| return lotto.getNumbers().contains(bonusNumber); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이미 만들어놓은 validator를 재사용해도 좋을 것 같아요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 이게 기존코드에 에러를 던지는 코드가 있더라구요 저도 이걸 validator로 따로 빼려다가 그냥 기존코드를 유지하는 방식이 더 좋을것같아서 남겨놨었어요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근데 지금 생각해보니까 중복코드라 그냥 validator를 재사용하는 것이 좋을듯합니다! 좋은 의견 감사해요 @daeGULLL