-
Notifications
You must be signed in to change notification settings - Fork 122
[완두콩] 이지인 로또 미션 제출합니다. #206
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
eas-yin
wants to merge
15
commits into
next-step:eas-yin
Choose a base branch
from
eas-yin:step1
base: eas-yin
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
15 commits
Select commit
Hold shift + click to select a range
454c575
feat: 1단계 로또 자동 구매 구현
eas-yin 93aa2d4
refactor: MVC 패턴 구조 적용
eas-yin 549c80d
feat: 2단계 로또 당첨 구현
eas-yin 7d69333
feat: 3단계 로또 2등 당첨 구현
eas-yin d02b955
feat: 4단계 로또 수동 구매 구현
eas-yin 915790b
LottoNumber 생성 팩토리 메서드 추가 및 LottoPick 메서드 수정
eas-yin d8d6548
LottoNumber 유효성 검사 구현 및 타입 변경
eas-yin d405a8e
LottoNumber 객체 비교 및 sort 오류 수정
eas-yin 50395ca
test: LottoNumber 정렬 테스트 추가
eas-yin 0664a34
refactor: LottoNumber 생성자 private으로 변경
eas-yin 27c891f
refactor: Lotto를 일급 컬렉션으로 변경 및 LottoGenerator로 책임 분리
eas-yin 5086f62
fix: 수동과 자동을 합친 후 출력하도록 수정
eas-yin eaf3205
refactor: LottoNumber toString() 추가
eas-yin 92d0254
fix: 불필요한 scanner.nextLine() 제거
eas-yin d178e57
refactor: PurchaseAmount 검증 및 계산 책임 변경
eas-yin 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import domain.*; | ||
| import view.InputView; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| LottoGenerator lottoGenerator = new LottoGenerator(); | ||
| LottoResult lottoResult = new LottoResult(); | ||
| WinningRate winningRate = new WinningRate(); | ||
|
|
||
| int purchasePrice = InputView.inputPrice(); | ||
| PurchaseAmount purchaseAmount = PurchaseAmount.from(purchasePrice); | ||
|
|
||
| int lottoCount = purchaseAmount.calculateCount(); | ||
| int passiveCount = InputView.inputPassiveCount(); | ||
| int autoCount = lottoCount - passiveCount; | ||
|
|
||
| List<List<LottoNumber>> passiveLotto = InputView.inputPassiveLotto(passiveCount); | ||
| List<List<LottoNumber>> autoLotto = lottoGenerator.lottoLists(autoCount); | ||
|
|
||
| passiveLotto.addAll(autoLotto); | ||
| ResultView.printPurchase(passiveLotto, passiveCount, autoCount); | ||
|
|
||
| List<Integer> wins = InputView.inputWinning(); | ||
| int bonusBall = InputView.inputBonusBall(); | ||
| List<Integer> counts = lottoResult.calculateCounts(passiveLotto, wins, bonusBall); | ||
|
|
||
| int winPrice = winningRate.calculateWinPrice(counts); | ||
| double rate = winningRate.calculateRate(winPrice, purchasePrice); | ||
|
|
||
| ResultView.printResult(counts, rate); | ||
| } | ||
| } | ||
|
|
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,36 @@ | ||
| package domain; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class Lotto { | ||
| private final List<LottoNumber> numbers; | ||
|
|
||
| private Lotto(List<LottoNumber> numbers) { | ||
| this.numbers = numbers; | ||
| } | ||
|
|
||
| public static Lotto from(List<LottoNumber> numbers) { | ||
| validateCount(numbers); | ||
| validateDuplicate(numbers); | ||
| return new Lotto(numbers); | ||
| } | ||
|
|
||
| private static void validateCount(List<LottoNumber> numbers) { | ||
| if(numbers.size() != 6) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| private static void validateDuplicate(List<LottoNumber> numbers) { | ||
| Set<LottoNumber> set = new HashSet<>(numbers); | ||
| if (set.size() != numbers.size()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
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,53 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class LottoGenerator { | ||
| private static List<Integer> lottoList() { | ||
| List<Integer> lotto = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < 45; i++) { | ||
| lotto.add(i+1); | ||
| } | ||
| return lotto; | ||
| } | ||
|
|
||
| private void lottoShuffle(List<Integer> lotto) { | ||
| Collections.shuffle(lotto); | ||
| } | ||
|
|
||
| private List<LottoNumber> lottoPick(List<Integer> lotto) { | ||
| List<LottoNumber> lottoSix = new ArrayList<>(); | ||
| for (int i = 0; i < 6; i++) { | ||
| lottoSix.add(LottoNumber.from(lotto.get(i))); | ||
| } | ||
|
|
||
| return lottoSix; | ||
| } | ||
|
|
||
| private void lottoSort(List<LottoNumber> lotto) { | ||
| Collections.sort(lotto); | ||
| } | ||
|
|
||
| public List<LottoNumber> run() { | ||
| List<Integer> lottoList = lottoList(); | ||
|
|
||
| lottoShuffle(lottoList); | ||
| List<LottoNumber> lotto = lottoPick(lottoList); | ||
| lottoSort(lotto); | ||
|
|
||
| return lotto; | ||
| } | ||
|
|
||
| public List<List<LottoNumber>> lottoLists(int count) { | ||
| List<List<LottoNumber>> lottos = new ArrayList<>(); | ||
| for (int i = 0; i < count; i++) { | ||
| lottos.add(run()); | ||
| } | ||
| return lottos; | ||
| } | ||
|
|
||
|
|
||
| } |
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,52 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class LottoNumber implements Comparable<LottoNumber>{ | ||
| private final int number; | ||
|
|
||
| private LottoNumber(int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public static LottoNumber from(int number) { | ||
| validate(number); | ||
| return new LottoNumber(number); | ||
| } | ||
|
|
||
| private static final int MIN = 1; | ||
| private static final int MAX = 45; | ||
|
|
||
| private static void validate(int number) { | ||
| if (number < MIN || number > MAX) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| LottoNumber that = (LottoNumber) o; | ||
| return number == that.number; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(number); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(LottoNumber o) { | ||
| return Integer.compare(number, o.number); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.valueOf(number); | ||
| } | ||
| } |
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,37 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoResult { | ||
|
|
||
| private boolean containsWinningNumber(List<LottoNumber> lottoList, int win) { | ||
| return lottoList.contains(LottoNumber.from(win)); | ||
| } | ||
|
|
||
| private int resultCounting(List<LottoNumber> lottoList, int win, int count) { | ||
| if (containsWinningNumber(lottoList, win)) { | ||
| count++; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| private int checkingWinningNumbers(List<LottoNumber> lottoList, List<Integer> wins, int bonusBall) { | ||
| int count = 0; | ||
| for (int win : wins) { | ||
| count = resultCounting(lottoList, win, count); | ||
| } | ||
|
|
||
| if (count == 5 && containsWinningNumber(lottoList, bonusBall)) return 7; | ||
| return count; | ||
| } | ||
|
|
||
| public List<Integer> calculateCounts(List<List<LottoNumber>> lottos, List<Integer> wins, int bonusBall) { | ||
| List<Integer> counts = new ArrayList<>(); | ||
|
|
||
| for (List<LottoNumber> lotto : lottos) { | ||
| counts.add(checkingWinningNumbers(lotto, wins, bonusBall)); | ||
| } | ||
| return counts; | ||
| } | ||
| } |
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 domain; | ||
|
|
||
| public class PurchaseAmount { | ||
| private final int amount; | ||
|
|
||
| private PurchaseAmount(int amount) { | ||
| validate(amount); | ||
| this.amount = amount; | ||
| } | ||
|
|
||
| public static PurchaseAmount from(int amount) { | ||
| return new PurchaseAmount(amount); | ||
| } | ||
|
|
||
| private static void validate(int amount) { | ||
| if (amount < 1000) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| if (amount % 1000 != 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public int getAmount() { | ||
| return amount; | ||
| } | ||
|
|
||
| public int calculateCount() { | ||
| return amount / 1000; | ||
| } | ||
| } |
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,25 @@ | ||
| package domain; | ||
|
|
||
| public enum WinningPrize { | ||
| THREE(3, 5000), | ||
| FOUR(4, 50000), | ||
| FIVE(5, 1500000), | ||
| BONUS(7, 30000000), | ||
| SIX(6, 2000000000); | ||
|
|
||
| private final int goal; | ||
| private final int prize; | ||
|
|
||
| WinningPrize(int goal, int prize) { | ||
| this.goal = goal; | ||
| this.prize = prize; | ||
| } | ||
|
|
||
| public int getGoal() { | ||
| return goal; | ||
| } | ||
|
|
||
| public int getPrize() { | ||
| return prize; | ||
| } | ||
| } |
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,47 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class WinningRate { | ||
|
|
||
| public int calculateWinPrice(List<Integer> counts) { | ||
| return threeWin(counts) + fourWin(counts) + fiveWin(counts) + secondWin(counts) + sixWin(counts); | ||
| } | ||
|
|
||
| private int threeWin(List<Integer> counts) { | ||
| return countGoal(counts, WinningPrize.THREE.getGoal()) * WinningPrize.THREE.getPrize(); | ||
| } | ||
|
|
||
| private int fourWin(List<Integer> counts) { | ||
| return countGoal(counts, WinningPrize.FOUR.getGoal()) | ||
| * WinningPrize.FOUR.getPrize(); | ||
| } | ||
|
|
||
| private int fiveWin(List<Integer> counts) { | ||
| return countGoal(counts, WinningPrize.FIVE.getGoal()) | ||
| * WinningPrize.FIVE.getPrize(); | ||
| } | ||
|
|
||
| private int secondWin(List<Integer> counts) { | ||
| return countGoal(counts, WinningPrize.BONUS.getGoal()) | ||
| * WinningPrize.BONUS.getPrize(); | ||
| } | ||
|
|
||
| private int sixWin(List<Integer> counts) { | ||
| return countGoal(counts, WinningPrize.SIX.getGoal()) | ||
| * WinningPrize.SIX.getPrize(); | ||
| } | ||
|
|
||
| private int countGoal(List<Integer> counts, int goal) { | ||
| int count = 0; | ||
|
|
||
| for (int i = 0; i < counts.size(); i++) { | ||
| if (goal == counts.get(i)) count++; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| public double calculateRate(int winPrice, int purchasePrice) { | ||
| return (double) winPrice / purchasePrice; | ||
| } | ||
| } |
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,69 @@ | ||
| package view; | ||
|
|
||
| import domain.LottoNumber; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
|
|
||
| private static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static int inputPrice() { | ||
|
|
||
| System.out.println("구입금액을 입력해 주세요."); | ||
| int price = scanner.nextInt(); | ||
|
|
||
| return price; | ||
| } | ||
|
|
||
| public static List<Integer> inputWinning() { | ||
| System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); | ||
| String win = scanner.nextLine(); | ||
|
|
||
| String[] wins = win.split(","); | ||
| List<Integer> nums = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < wins.length; i++) { | ||
| nums.add(Integer.parseInt(wins[i])); | ||
| } return nums; | ||
| } | ||
| public static int inputBonusBall() { | ||
| System.out.println("\n보너스 볼을 입력해 주세요."); | ||
| int bonusBall = scanner.nextInt(); | ||
|
|
||
| return bonusBall; | ||
| } | ||
|
|
||
| public static int inputPassiveCount() { | ||
| System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요."); | ||
| int count = scanner.nextInt(); | ||
| return count; | ||
| } | ||
|
|
||
| public static List<List<LottoNumber>> inputPassiveLotto(int manualCount) { | ||
| scanner.nextLine(); | ||
|
|
||
| System.out.println("\n수동으로 구매할 번호를 입력해 주세요."); | ||
| List<List<LottoNumber>> passiveLottos = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < manualCount; i++) { | ||
| passiveLottos.add(inputManualLotto()); | ||
| } | ||
| return passiveLottos; | ||
| } | ||
|
|
||
| private static List<LottoNumber> inputManualLotto() { | ||
| String input = scanner.nextLine(); | ||
| String[] numbers = input.split(","); | ||
|
|
||
| List<LottoNumber> lotto = new ArrayList<>(); | ||
|
|
||
| for (String number : numbers) { | ||
| lotto.add(LottoNumber.from(Integer.parseInt(number.trim()))); | ||
| } | ||
|
|
||
| return lotto; | ||
| } | ||
| } |
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.
지금은
Lotto가 이름이랑은 다르게 한 장의 로또 번호를 가지는 게 아니고, 구입 장수 계산이랑 자동 번호 생성을 담당하고 있네요.일급 컬렉션은 단순히 List를 클래스로 감싸는 것보다, 컬렉션 전체가 지켜야 하는 규칙을 한곳에서 보장하는 데 의미가 있는데요. 사실 지금
Lotto에서 사용하고 있는List<Integer>는 아래 같은 경우도 모두 포함할 수 있거든요.한 장의 로또를 표현하는
Lotto가List<LottoNumber>를 가지고, 생성될 때 번호 개수와 중복 여부를 검증하도록 만들어보면 어떨까요?그렇게 바꿨을 때 지금의
Lotto가 담당하고 있는 자동 번호 생성과 구입 장수 계산은 각각 어디에 위치하는 게 자연스러울지도 함께 고민해보면 좋을 것 같습니다~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.
현재는 로또가 계산 같은 기능을 하는데 로또 한 장의 상태를 나타내야 합니다. 일급 컬렉션은 하나의 객체가 그 컬렉션에 대한 규칙과 책임을 갖게 합니다.
기존의 Lotto에 있던 기능 메서드들은 LottoGenerator 클래스로 이동하였고, 그에 따라서 Application 클래스도 수정하였습니다.
Lotto 클래스에서 컬렉션이 규칙과 책임을 가지도록 private한 리스트를 만들었고, 생성자와 팩토리 메서드를 구현하였습니다.
번호가 5개 또는 7개인 로또 - 유효성 검증 메서드를 구현했습니다.
같은 번호가 중복된 로또 - 유효성 검증 메서드를 구현했습니다.
유효하지 않은 번호가 포함된 로또 - LottoNumber 클래스에서 이미 검증했다고 생각하여 따로 구현하지 않았습니다.
또한 LottoTest 클래스에서 각각을 테스트 하였습니다.