-
Notifications
You must be signed in to change notification settings - Fork 34
[2주차] 객체지향 코드 연습(seryn6911-hub) #45
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.contorller.Contoller; | ||
| import lotto.view.View; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| try { | ||
| View view = new View(); | ||
| Contoller controller = new Contoller(view); | ||
| controller.startController(); | ||
| } catch (IllegalArgumentException e) { | ||
| // 예외 발생 시 조용히 종료 - 추가 출력 없음 | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
|
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. 컨트롤러가 너무 담당하는 일이 많고, 협력 객체를 직접 생성하고 있기 때문에 결합도가 너무 높습니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package lotto.contorller; | ||
|
|
||
| import lotto.enums.Rank; | ||
| import lotto.model.Lotto_purchaser; | ||
| import lotto.view.View; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class Contoller { | ||
| private final View view; | ||
|
|
||
| public Contoller(View view) { | ||
| this.view = view; | ||
| } | ||
|
|
||
| public void startController(){ | ||
| try { | ||
| //금액 받기 | ||
| int purchasePrice = view.lottoPurchseStart(); | ||
| Lotto_purchaser purchaser = new Lotto_purchaser(purchasePrice); | ||
|
|
||
| //로또 개수 출력 | ||
| view.LottoNum(purchaser.costToTicketNUM()); | ||
|
|
||
| //로또배열 생성 | ||
| List<List<Integer>> tickets = purchaser.randomPick(); | ||
|
|
||
| //로또 출력 | ||
| view.LottoDrow(tickets); | ||
|
|
||
| //당첨번호 입력 | ||
| List<Integer> winningNumber = view.winningNumber(); | ||
| //보너스 번호 입력 | ||
| int bounusNumber = view.bounusNumber(); | ||
|
|
||
| //같은 tickets로 당첨 확인 | ||
| RankSystem rankSystem = new RankSystem( | ||
| tickets, winningNumber, bounusNumber); | ||
|
|
||
| // 통계 계산 실행 | ||
| Map<Rank, Integer> rank = rankSystem.calculateWinningStatistics(); | ||
| YieldCalculation yieldCalculation = new YieldCalculation(rank, purchasePrice); | ||
| double profit = yieldCalculation.getLottoYield(); | ||
| //통계출력 | ||
| view.winningStatistics(rank, profit); | ||
|
|
||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
|
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. |
||
|
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. 순위 시스템에 너무 많은 역할이 있는 것 같습니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package lotto.contorller; | ||
|
|
||
| import lotto.enums.Rank; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class RankSystem { | ||
|
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. 순위 시스템? 클래스 명이 좀 직관적이지 않은 것 같습니다. |
||
| private List<List<Integer>> lottos; | ||
| private List<Integer> winningNumbers; | ||
| private int bounusNumber; // typo: bonusNumber로 수정하는 것을 권장 | ||
|
|
||
|
|
||
|
|
||
|
|
||
| public RankSystem(List<List<Integer>> lottos, List<Integer> winningNumber, int bounusNumber) { | ||
| this.lottos = lottos; | ||
| this.winningNumbers = winningNumber; | ||
| this.bounusNumber = bounusNumber; | ||
| } | ||
|
|
||
|
|
||
| public Map<Rank, Integer> calculateWinningStatistics() { | ||
| Map<Rank, Integer> result = new EnumMap<>(Rank.class); | ||
|
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. EnumMap을 쓰신 이유가 있으실까요? |
||
|
|
||
| // 초기화 | ||
| for (Rank rank : Rank.values()) { | ||
| result.put(rank, 0); | ||
| } | ||
|
|
||
| for (List<Integer> lotto : lottos) { | ||
| int collectNumber = 0; | ||
| boolean hasBonusNumber = false; | ||
|
|
||
| // 각 번호 비교 | ||
| for (int lottoNumber : lotto) { | ||
| boolean isWinningNumber = false; | ||
|
|
||
| for (int win : winningNumbers) { | ||
| if (lottoNumber == win) { | ||
| collectNumber++; | ||
| isWinningNumber = true; | ||
| break; // 중복 방지 | ||
| } | ||
| } | ||
|
|
||
| // 보너스 번호 체크 | ||
| if (!isWinningNumber && lottoNumber == bounusNumber) { | ||
| hasBonusNumber = true; | ||
| } | ||
| } | ||
|
|
||
| // 등수 판정 | ||
| if (collectNumber == 6) { | ||
| result.put(Rank.SIX, result.get(Rank.SIX) + 1); | ||
| } else if (collectNumber == 5 && hasBonusNumber) { | ||
| result.put(Rank.FIVE_AND_BONUS, result.get(Rank.FIVE_AND_BONUS) + 1); | ||
| } else if (collectNumber == 5) { | ||
| result.put(Rank.FIVE, result.get(Rank.FIVE) + 1); | ||
| } else if (collectNumber == 4) { | ||
| result.put(Rank.FOUR, result.get(Rank.FOUR) + 1); | ||
| } else if (collectNumber == 3) { | ||
| result.put(Rank.THREE, result.get(Rank.THREE) + 1); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
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. YieldCalculation이 명사이긴 하지만 책임 분배를 너무 한 것이 아닌가 하는 느낌이 듭니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package lotto.contorller; | ||
|
|
||
| import lotto.enums.Rank; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
|
|
||
| public class YieldCalculation { | ||
|
|
||
|
|
||
| private Map<Rank, Integer> rank; | ||
| private int purchasePrice; | ||
|
|
||
| public YieldCalculation(Map<Rank, Integer> rank,int purchasePrice){ | ||
| this.rank=rank; | ||
| this.purchasePrice=purchasePrice; | ||
| } | ||
|
|
||
|
|
||
| public double getLottoYield() { | ||
| double totalPrize = (double) rank.get(Rank.SIX) * 2000000000.0 + | ||
| (double) rank.get(Rank.FIVE_AND_BONUS) * 30000000.0 + | ||
| (double) rank.get(Rank.FIVE) * 1500000.0 + | ||
| (double) rank.get(Rank.FOUR) * 50000.0 + | ||
| (double) rank.get(Rank.THREE) * 5000.0; | ||
|
|
||
| double yield = (double) totalPrize / purchasePrice * 100.0; | ||
|
|
||
| return Math.round(yield * 100.0) / 100.0; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+31
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. 금액 계산은 어짜피 정수로 나올텐데 반환 타입을 왜 처음에 double로 가져가셨나요??
Comment on lines
+20
to
+31
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. 그리고 계산이 여기서 된다면 get 이 아니라는 생각이 드네용 |
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package lotto.enums; | ||
|
|
||
| public enum ErrorMessage { | ||
| EMPTY_PURCHASE_AMOUNT("[ERROR] 구입 금액을 입력하지 않았습니다."), | ||
| NOT_A_NUMBER("[ERROR] 구입 금액은 숫자로 입력해야 합니다."), | ||
| INVALID_AMOUNT("[ERROR] 구입 금액은 1000원 단위의 양수여야 합니다."), | ||
| PURCHASE_AMOUNT_TOO_LARGE("[ERROR] 구입 금액이 너무 큽니다."), | ||
|
|
||
| EMPTY_WINNING_NUMBERS("[ERROR] 당첨 번호를 입력하지 않았습니다."), | ||
| INVALID_WINNING_NUMBERS_COUNT("[ERROR] 당첨 번호는 6개의 숫자로 입력해야 합니다."), | ||
| NOT_A_VALID_NUMBER("[ERROR] 당첨 번호는 숫자로 입력해야 합니다."), | ||
| NUMBER_OUT_OF_RANGE("[ERROR] 당첨 번호는 1부터 45 사이의 숫자여야 합니다."), | ||
| DUPLICATE_NUMBER("[ERROR] 당첨 번호에 중복된 숫자가 포함되어 있습니다."), | ||
|
|
||
| EMPTY_BONUS_NUMBER("[ERROR] 보너스 번호를 입력하지 않았습니다."), | ||
| INVALID_BONUS_NUMBER("[ERROR] 보너스 번호는 숫자로 입력해야 합니다."), | ||
| BONUS_NUMBER_OUT_OF_RANGE("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다."), | ||
| BONUS_NUMBER_DUPLICATE("[ERROR] 보너스 번호는 당첨 번호와 중복될 수 없습니다."); | ||
|
|
||
| private final String message; | ||
|
|
||
| ErrorMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package lotto.enums; | ||
|
|
||
| public enum Rank { | ||
| SIX, // 1등 | ||
| FIVE_AND_BONUS, // 2등 | ||
| FIVE, // 3등 | ||
| FOUR, // 4등 | ||
| THREE // 5등 | ||
| // 꽝 | ||
|
Comment on lines
+4
to
+9
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. 이렇게 하면 협업하는 입장에서 좋을지 생각해보셔야 할 것 같습니다ㅏ |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package lotto.model; | ||
|
|
||
| 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 = numbers; | ||
| } | ||
|
|
||
| private void validate(List<Integer> numbers) { | ||
| if (numbers.size() != 6) { | ||
| throw new IllegalArgumentException("로또 번호가 많거나 적음"); | ||
| } | ||
|
|
||
| if (numbers.size() != new HashSet<>(numbers).size()) { | ||
| throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); | ||
| } | ||
| } | ||
|
Comment on lines
+14
to
+22
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. 에러 enum을 만드셨으니 이건 수정해주세요! |
||
|
|
||
| public List<Integer> getNumbers() { | ||
| return numbers; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package lotto.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Lotto_purchaser { | ||
|
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. 카멜 케이스로 통일해주세요 |
||
|
|
||
| private final int costOfPurchasing; | ||
| private List<Lotto> lottos; | ||
| private static final int LOTTO_MIN_NUMBER = 1; | ||
| private static final int LOTTO_MAX_NUMBER = 45; | ||
| private static final int LOTTO_NUMBER_COUNT = 6; | ||
|
|
||
|
|
||
| public Lotto_purchaser(int costOfPurchasing) { | ||
| this.costOfPurchasing = costOfPurchasing; | ||
| } | ||
|
|
||
| // 구매 금액에 따른 로또 티켓 수 반환 | ||
| public int costToTicketNUM() { | ||
| return costOfPurchasing / 1000; | ||
|
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. 이것도 매직넘버 필요합니다! |
||
| } | ||
|
|
||
|
|
||
| public List<List<Integer>> randomPick() { | ||
| List<Lotto> lottos = new ArrayList<>(); | ||
| int ticketCount = costToTicketNUM(); | ||
| for (int i = 0; i < ticketCount; i++) { | ||
| List<Integer> numbers = Randoms.pickUniqueNumbersInRange(LOTTO_MIN_NUMBER, LOTTO_MAX_NUMBER, LOTTO_NUMBER_COUNT); | ||
| lottos.add(new Lotto(numbers)); | ||
| } | ||
| this.lottos = lottos; | ||
|
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. 이 코드의 의미는 뭔가요?? |
||
| List<List<Integer>> lottoList = new ArrayList<>(); | ||
|
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. List가 아니라 List<List>로 하신 이유가 뭔가요?? |
||
| for (Lotto lotto : lottos) { | ||
| lottoList.add(lotto.getNumbers()); | ||
| } | ||
| return lottoList; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+41
to
+45
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. 코드 컨벤션에 대해서 알아보세요! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package lotto.view; | ||
|
|
||
| public interface InputReader { | ||
| default String readLine() { | ||
| return null; | ||
| } | ||
|
Comment on lines
+4
to
+6
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. 이 default의 의미는 무엇인가요? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Scan.java | ||
| package lotto.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Scan implements InputReader { | ||
| private Scanner scanner = new Scanner(System.in); | ||
|
|
||
| @Override | ||
| public String readLine() { | ||
| return scanner.nextLine(); | ||
| } | ||
| } |
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.
AppConfig 사용해보세용
"AppConfig 순수자바" 키워드로 검색하신 후 공부하면 좋을 것 같아요