-
Notifications
You must be signed in to change notification settings - Fork 21
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
[2주차]객체지향 코드 연습(usernamejunhyuk) #28
base: main
Are you sure you want to change the base?
Changes from all commits
79504a4
3d2ac86
5346f99
78eeca4
50d11b5
3814cd1
3d84f92
46a0f56
4f09ea5
3e889ad
84d30bd
477ba33
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package constant; | ||
|
||
public class ErrorMessage { | ||
public static final String MULTIPLES_PURCHASE_AMOUNT_ERROR = "[ERROR] 구입 금액은 1,000원 단위로 입력해야 합니다."; | ||
public static final String NEGATIVE_PURCHASE_AMOUNT_ERROR = "[ERROR] 구입 금액은 음수가 될 수 없습니다."; | ||
|
||
public static final String NUMBERS_SIZE_ERROR = "[ERROR] 로또 번호는 6개여야 합니다."; | ||
public static final String UNIQUE_NUMBERS_ERROR = "[ERROR] 로또 번호는 중복되지 않아야 합니다."; | ||
public static final String RANGE_NUMBER_ERROR = "[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package constant; | ||
|
||
public class LottoInformation { | ||
public static final int PRICE_PER_LOTTO = 1000; | ||
|
||
public final static int MINIMUM_NUMBER = 1; | ||
public final static int MAXIMUM_NUMBER = 45; | ||
public final static int COUNT_NUMBER = 6; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package constant; | ||
|
||
public class LottoMessage { | ||
public static final String INPUT_PURCHASE_AMOUNT = "구입금액을 입력해 주세요."; | ||
public static final String PURCHASED_NUMBER_OF_LOTTO = "개를 구매했습니다."; | ||
|
||
public static final String INPUT_WINNING_NUMBERS = "당첨 번호를 입력해주세요."; | ||
public static final String INPUT_BONUS_NUMBER = "보너스 번호를 입력해주세요."; | ||
public static final String PRINT_RANK_RESULTS_HEADER = "당첨 통계\n---"; | ||
public static final String PRINT_RANK_RESULT_FORMAT = "%s - %d개"; | ||
public static final String PRINT_RESULTS = "총 수익률은 %.2f%%입니다."; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 3주차 코드입니다 2주차로 잘못올려서 다시올립니다 ㅜㅜ | ||
|
||
package constant; | ||
|
||
public enum LottoRank { | ||
FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원)"), | ||
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원)"), | ||
THIRD(5, 1_500_000, "5개 일치 (1,500,000원)"), | ||
FOURTH(4, 50_000, "4개 일치 (50,000원)"), | ||
FIFTH(3, 5_000, "3개 일치 (5,000원)"), | ||
NONE(0, 0, ""); | ||
|
||
private final int matchCount; | ||
private final int prizeMoney; | ||
private final String resultMessage; | ||
|
||
LottoRank(int matchCount, int prizeMoney, String resultMessage) { | ||
this.matchCount = matchCount; | ||
this.prizeMoney = prizeMoney; | ||
this.resultMessage = resultMessage; | ||
} | ||
|
||
public int getMatchCount() { | ||
return matchCount; | ||
} | ||
|
||
public int getPrizeMoney() { | ||
return prizeMoney; | ||
} | ||
|
||
public String getResultMessage() { | ||
return resultMessage; | ||
} | ||
|
||
public static int[] getRankNumbers() { | ||
return new int[LottoRank.values().length]; | ||
} | ||
|
||
public static LottoRank valueOf(int matchCount, boolean matchBonus) { | ||
if (matchCount == FIRST.getMatchCount()) { | ||
return FIRST; | ||
} | ||
if (matchCount == SECOND.getMatchCount() && matchBonus) { | ||
return SECOND; | ||
} | ||
if (matchCount == THIRD.getMatchCount()) { | ||
return THIRD; | ||
} | ||
if (matchCount == FOURTH.getMatchCount()) { | ||
return FOURTH; | ||
} | ||
if (matchCount == FIFTH.getMatchCount()) { | ||
return FIFTH; | ||
} | ||
return NONE; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package constant; | ||
|
||
public class ResultInformation { | ||
public static final int PERCENT_CONVERSION = 100; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
// 3주차 코드입니다 2주차로 잘못올려서 다시올립니다 ㅜㅜ | ||
|
||
package lotto; | ||
|
||
import constant.LottoRank; | ||
import view.BonusLottoView; | ||
import view.LottoResultView; | ||
import view.LottoView; | ||
import view.WinLottoView; | ||
|
||
import java.util.List; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
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. main 클래스는 어플리케이션을 실행하는 용도로만 사용하는 것이 좋습니다. |
||
// TODO: 프로그램 구현 | ||
try { | ||
LottoView lottoView = new LottoView(); | ||
LottoService lottoService = new LottoService(); | ||
WinLottoView winLottoView = new WinLottoView(); | ||
BonusLottoView bonusLottoView = new BonusLottoView(); | ||
LottoCalculator lottoCalculator = new LottoCalculator(); | ||
LottoResultView lottoResultView = new LottoResultView(); | ||
|
||
int purchaseAmount = lottoView.inputPurchaseAmount(); | ||
List<Lotto> purchasedLotto = lottoService.generateLotto(purchaseAmount); | ||
lottoView.printPurchaseMessage(purchasedLotto.size()); | ||
lottoView.printPurchasedLotto(purchasedLotto); | ||
|
||
Lotto winningNumbers = winLottoView.inputWinningNumbers(); | ||
int bonusNumber = bonusLottoView.inputBonusNumber(); | ||
WinLotto winLotto = new WinLotto(winningNumbers.getNumbers(), bonusNumber); | ||
List<LottoRank> results = lottoCalculator.calculateResults(purchasedLotto, winLotto); | ||
|
||
lottoResultView.printResults(results, purchaseAmount); | ||
|
||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,56 @@ | ||
// 3주차 코드입니다 2주차로 잘못올려서 다시올립니다 ㅜㅜ | ||
|
||
package lotto; | ||
|
||
import java.util.List; | ||
import camp.nextstep.edu.missionutils.Randoms; | ||
import constant.ErrorMessage; | ||
import constant.LottoInformation; | ||
|
||
import java.util.*; | ||
|
||
public class Lotto { | ||
private final List<Integer> numbers; | ||
|
||
public static Lotto generate() { | ||
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. Lotto 팩토리 메소드를 사용한 것이 정말 좋은 것 같습니다. |
||
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(LottoInformation.MINIMUM_NUMBER, LottoInformation.MAXIMUM_NUMBER, LottoInformation.COUNT_NUMBER); | ||
Collections.sort(numbers); | ||
return new Lotto(numbers); | ||
} | ||
|
||
public Lotto(List<Integer> numbers) { | ||
validate(numbers); | ||
validateNumber(numbers); | ||
validateRedundancy(numbers); | ||
validateRange(numbers); | ||
this.numbers = numbers; | ||
} | ||
|
||
private void validate(List<Integer> numbers) { | ||
if (numbers.size() != 6) { | ||
throw new IllegalArgumentException(); | ||
private void validateNumber(List<Integer> numbers) { | ||
if (numbers.size() != LottoInformation.COUNT_NUMBER) { | ||
throw new IllegalArgumentException(ErrorMessage.NUMBERS_SIZE_ERROR); | ||
} | ||
} | ||
|
||
private void validateRedundancy(List<Integer> numbers) { | ||
Set<Integer> uniqueNumbers = new HashSet<>(numbers); | ||
if (uniqueNumbers.size() != LottoInformation.COUNT_NUMBER) { | ||
throw new IllegalArgumentException(ErrorMessage.UNIQUE_NUMBERS_ERROR); | ||
} | ||
} | ||
|
||
// TODO: 추가 기능 구현 | ||
private void validateRange(List<Integer> numbers) { | ||
for (int number : numbers) { | ||
validateRange(number); | ||
} | ||
} | ||
|
||
// 범위 검증 메서드를 static으로 변경 (단일 숫자 검증) | ||
public static void validateRange(int number) { | ||
if (number < LottoInformation.MINIMUM_NUMBER || number > LottoInformation.MAXIMUM_NUMBER) { | ||
throw new IllegalArgumentException(ErrorMessage.RANGE_NUMBER_ERROR); | ||
} | ||
} | ||
|
||
public List<Integer> getNumbers() { | ||
return numbers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 3주차 코드입니다 2주차로 잘못올려서 다시올립니다 ㅜㅜ | ||
|
||
package lotto; | ||
|
||
import constant.LottoRank; | ||
import constant.ResultInformation; | ||
import view.BonusLottoView; | ||
import view.WinLottoView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoCalculator { | ||
|
||
WinLottoView winLottoView = new WinLottoView(); | ||
BonusLottoView bonusLottoView = new BonusLottoView(); | ||
private List<LottoRank> results; | ||
|
||
public static LottoRank calculateRank(List<Integer> lottoNumbers, List<Integer> winningNumbers, int bonusNumber) { | ||
int matchCount = (int) lottoNumbers.stream().filter(winningNumbers::contains).count(); | ||
boolean matchBonus = lottoNumbers.contains(bonusNumber); | ||
return LottoRank.valueOf(matchCount, matchBonus); | ||
} | ||
|
||
public static double calculateYield(List<LottoRank> results, int purchaseAmount) { | ||
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. stream 사용법을 모르고 GPT를 쓰셨다면 직접 다시 공부해보셔야합니다. 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. 네 알겠습니다 다시 공부하고 작성하겠습니다! |
||
int totalPrize = results.stream() | ||
.mapToInt(LottoRank::getPrizeMoney) | ||
.sum(); | ||
return (totalPrize / (double) purchaseAmount) * ResultInformation.PERCENT_CONVERSION; | ||
} | ||
|
||
public List<LottoRank> calculateResults(List<Lotto> purchasedLotto, WinLotto winLotto) { | ||
List<LottoRank> results = new ArrayList<>(); | ||
List<Integer> winningNumbers = winLotto.getWinningNumbers(); | ||
int bonusNumber = winLotto.getBonusNumber(); | ||
for (Lotto lotto : purchasedLotto) { | ||
LottoRank lottoRank = LottoCalculator.calculateRank(lotto.getNumbers(), winningNumbers, bonusNumber); | ||
results.add(lottoRank); | ||
} | ||
return results; | ||
} | ||
|
||
public int[] countRanks(List<LottoRank> results) { | ||
int[] rankCount = LottoRank.getRankNumbers(); | ||
for (LottoRank rank : results) { | ||
rankCount[rank.getMatchCount()]++; | ||
} | ||
return rankCount; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package lotto; | ||
|
||
import constant.LottoInformation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoService { | ||
|
||
public List<Lotto> generateLotto(int purchaseAmount) { | ||
List<Lotto> purchasedLotto = new ArrayList<>(); | ||
int numberOfLotto = purchaseAmount / LottoInformation.PRICE_PER_LOTTO; | ||
for (int i = 0; i < numberOfLotto; i++) { | ||
purchasedLotto.add(Lotto.generate()); | ||
} | ||
return purchasedLotto; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package lotto; | ||
|
||
import java.util.List; | ||
|
||
public class WinLotto { | ||
|
||
private Lotto lotto; | ||
private int bonusNumber; | ||
|
||
public WinLotto(List<Integer> lottoNumbers, int bonusNumber) { | ||
lotto = new Lotto(lottoNumbers); | ||
this.bonusNumber = bonusNumber; | ||
} | ||
|
||
public List<Integer> getWinningNumbers() { | ||
return lotto.getNumbers(); | ||
} | ||
|
||
public int getBonusNumber() { | ||
return bonusNumber; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import constant.LottoMessage; | ||
import lotto.Lotto; | ||
|
||
public class BonusLottoView { | ||
public int inputBonusNumber() { | ||
System.out.println(LottoMessage.INPUT_BONUS_NUMBER); | ||
int bonusNumber = Integer.parseInt(Console.readLine()); | ||
Lotto.validateRange(bonusNumber); | ||
return bonusNumber; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package view; | ||
|
||
import constant.LottoMessage; | ||
import constant.LottoRank; | ||
import lotto.LottoCalculator; | ||
|
||
import java.util.List; | ||
|
||
public class LottoResultView { | ||
// 생성자 지우기, 왜 만듦? 이거 만들면 안됨 | ||
// int[] rankCount = lottoCalculator.countRanks(results);가 계산을 하는거 같아 | ||
// View는 단순히 출력만 하기로 했는데 | ||
LottoCalculator lottoCalculator = new LottoCalculator(); | ||
|
||
private void printRankResults(int[] rankCount) { | ||
System.out.println(LottoMessage.PRINT_RANK_RESULTS_HEADER); | ||
for (LottoRank rank : LottoRank.values()) { | ||
if (rank != LottoRank.NONE) { | ||
String printRankResults = String.format(LottoMessage.PRINT_RANK_RESULT_FORMAT, rank.getResultMessage(), rankCount[rank.ordinal()]); | ||
System.out.println(printRankResults); | ||
} | ||
} | ||
} | ||
|
||
public void printResults(List<LottoRank> results, int purchaseAmount) { | ||
int[] rankCount = lottoCalculator.countRanks(results); | ||
printRankResults(rankCount); | ||
double yield = LottoCalculator.calculateYield(results, purchaseAmount); | ||
String printResults = String.format(LottoMessage.PRINT_RESULTS, yield); | ||
System.out.println(printResults); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import constant.ErrorMessage; | ||
import constant.LottoInformation; | ||
import constant.LottoMessage; | ||
import lotto.Lotto; | ||
|
||
import java.util.List; | ||
|
||
// 목적 : 출력과 입력과 검증 | ||
public class LottoView { | ||
|
||
public int inputPurchaseAmount() { | ||
System.out.println(LottoMessage.INPUT_PURCHASE_AMOUNT); | ||
int price = Integer.parseInt(Console.readLine()); | ||
validatePurchaseAmount(price); | ||
return price; | ||
} | ||
|
||
public void printPurchaseMessage(int numberOfLotto) { | ||
System.out.println(numberOfLotto + LottoMessage.PURCHASED_NUMBER_OF_LOTTO); | ||
} | ||
|
||
private static void validatePurchaseAmount(int purchaseAmount) { | ||
if (purchaseAmount <= 0) { | ||
throw new IllegalArgumentException(ErrorMessage.NEGATIVE_PURCHASE_AMOUNT_ERROR); | ||
} | ||
if (purchaseAmount % LottoInformation.PRICE_PER_LOTTO != 0) { | ||
throw new IllegalArgumentException(ErrorMessage.MULTIPLES_PURCHASE_AMOUNT_ERROR); | ||
} | ||
} | ||
|
||
public void printPurchasedLotto(List<Lotto> purchasedLotto) { | ||
for (Lotto lotto : purchasedLotto) { | ||
System.out.println(lotto.getNumbers()); | ||
} | ||
} | ||
} |
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.
�책임을 전부 분리해주세요. 객체 분리 고민이 너무 부족한 것 같습니다.