-
Notifications
You must be signed in to change notification settings - Fork 34
[2주차] 객체지향 코드 제출(JongHyeok03 ) #43
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controller; | ||
|
||
import model.Validator; | ||
import model.Lotto; | ||
import model.LottoService; | ||
import model.LottoTickets; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
public class Controller { | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
private final LottoService lottoService = new LottoService(); | ||
private final Validator validator = new Validator(); | ||
|
||
public Controller(InputView inputView, OutputView outputView) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
|
||
} | ||
public void run(){ | ||
int price = inputView.readPurchasePrice(); | ||
validator.validatePurchasedAmount(price); | ||
LottoTickets tickets = lottoService.purchaseLottos(price); | ||
outputView.printLottos(tickets); | ||
List<Integer> winningNumber = inputView.readWinningNumberInput(); | ||
validator.validateWinningNumber(winningNumber); | ||
Lotto winningLotto = new Lotto(winningNumber); | ||
int bonusNumber = inputView.readBonusNumber(); | ||
validator.validateBonusNumber(winningLotto, bonusNumber); | ||
int[] result = LottoService.checkResult(tickets, winningLotto, bonusNumber); | ||
outputView.printResult(result, price); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
package lotto; | ||
|
||
import controller.Controller; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
|
||
Controller controller = new Controller(inputView, outputView); | ||
controller.run(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Lotto { | ||
private 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: 추가 기능 구현 | ||
|
||
public void sortNumbers() { | ||
this.numbers = mergeSort(this.numbers); | ||
} | ||
private List<Integer> mergeSort(List<Integer> list) { | ||
if (list.size() <= 1) { | ||
return list; | ||
} | ||
|
||
int mid = list.size() / 2; | ||
List<Integer> left = new ArrayList<>(list.subList(0, mid)); | ||
List<Integer> right = new ArrayList<>(list.subList(mid, list.size())); | ||
|
||
List<Integer> sortedLeft = mergeSort(left); | ||
List<Integer> sortedRight = mergeSort(right); | ||
|
||
return merge(sortedLeft, sortedRight); | ||
} | ||
Comment on lines
+24
to
+37
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<Integer> merge(List<Integer> left, List<Integer> right) { | ||
List<Integer> mergedList = new ArrayList<>(); | ||
int leftIndex = 0; | ||
int rightIndex = 0; | ||
|
||
while (leftIndex < left.size() && rightIndex < right.size()) { | ||
if (left.get(leftIndex) < right.get(rightIndex)) { | ||
mergedList.add(left.get(leftIndex)); | ||
leftIndex++; | ||
continue; | ||
} | ||
if (left.get(leftIndex) >= right.get(rightIndex)) { | ||
mergedList.add(right.get(rightIndex)); | ||
rightIndex++; | ||
} | ||
} | ||
mergedList.addAll(left.subList(leftIndex, left.size())); | ||
mergedList.addAll(right.subList(rightIndex, right.size())); | ||
return mergedList; | ||
} | ||
|
||
public List<Integer> getNumbers() { | ||
return numbers; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return numbers.toString(); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package model; | ||
|
||
public enum LottoRank { | ||
FIRST(6, 2_000_000_000, false), // 6개 일치 | ||
SECOND(5, 30_000_000, true), // 5개 일치 + 보너스 | ||
THIRD(5, 1_500_000, false), // 5개 일치 | ||
FOURTH(4, 50_000, false), // 4개 일치 | ||
FIFTH(3, 5_000, false), // 3개 일치 | ||
MISS(0, 0, false); // 낙첨 (0개 일치) | ||
|
||
private final int matchCount; | ||
private final int prize; | ||
private final boolean matchBonus; | ||
|
||
LottoRank(int matchCount, int prize, boolean matchBonus) { | ||
this.matchCount = matchCount; | ||
this.prize = prize; | ||
this.matchBonus = matchBonus; | ||
} | ||
|
||
public int getMatchCount() { | ||
return matchCount; | ||
} | ||
|
||
public int getPrize() { | ||
return prize; | ||
} | ||
|
||
public boolean isMatchBonus() { | ||
return matchBonus; | ||
} | ||
|
||
public static LottoRank findRank(int matchCount, boolean matchBonus) { | ||
for (LottoRank rank : values()) { | ||
if (rank.matchCount == matchCount) { | ||
// 5개 일치일 경우, 보너스 번호 일치 여부로 2등과 3등을 구분합니다. | ||
if (matchCount == 5) { | ||
if (rank.isMatchBonus() == matchBonus) { | ||
return rank; | ||
} | ||
continue; | ||
} | ||
return rank; | ||
} | ||
} | ||
return MISS; | ||
} | ||
Comment on lines
+33
to
+47
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,78 @@ | ||
package model; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
|
||
import java.util.List; | ||
|
||
public class LottoService { | ||
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 Lotto makeLotto(int purchasedAmount){ | ||
List<Integer> numbers = null; | ||
for (int i = 0; i < purchasedAmount; i++){ | ||
numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); | ||
} | ||
return new Lotto(numbers); | ||
} | ||
|
||
public LottoTickets purchaseLottos(int purchasedPrice) { | ||
LottoTickets purchasedTickets = new LottoTickets(); | ||
int lottoCount = purchasedPrice / 1000; | ||
|
||
for(int i = 0; i < lottoCount; i++){ | ||
Lotto lotto = makeLotto(lottoCount); | ||
lotto.sortNumbers(); | ||
purchasedTickets.add(lotto); | ||
} | ||
|
||
return purchasedTickets; | ||
} | ||
|
||
public static int[] checkResult(LottoTickets hadLottos, Lotto winningLotto, int bonusNum) { | ||
int[] resultCounts = new int[LottoRank.values().length]; | ||
|
||
List<Lotto> purchasedLottos = hadLottos.getLottos(); | ||
|
||
for (Lotto purchasedLotto : purchasedLottos) { | ||
int matchCount = countMatchingNumbers(winningLotto.getNumbers(), purchasedLotto); | ||
boolean isBonusMatch = countMatchingNumbers(bonusNum, purchasedLotto); | ||
|
||
LottoRank rank = LottoRank.findRank(matchCount, isBonusMatch); | ||
|
||
if (rank != LottoRank.MISS) { | ||
int rankIndex = rank.ordinal(); | ||
resultCounts[rankIndex]++; | ||
} | ||
} | ||
return resultCounts; | ||
} | ||
|
||
public static int countMatchingNumbers(List<Integer> winningLotto, Lotto purchasedLotto) { | ||
List<Integer> myNumbers = purchasedLotto.getNumbers(); // 내 로또 번호 | ||
|
||
int matchCount = 0; | ||
int pointerIndex = 0; | ||
int winningIndex = 0; | ||
|
||
while (pointerIndex < 6 && winningIndex < 6) { | ||
int myNum = myNumbers.get(pointerIndex); | ||
int winningNum = winningLotto.get(winningIndex); | ||
|
||
if (myNum == winningNum) { | ||
// 1. 번호가 같으면 일치 개수 증가시키고 두 포인터 모두 이동 | ||
matchCount++; | ||
pointerIndex++; | ||
winningIndex++; | ||
} else if (myNum < winningNum) { | ||
// 2. 내 번호가 작으면 내 포인터만 이동 | ||
pointerIndex++; | ||
} else { // myNum > winningNum | ||
// 3. 당첨 번호가 작으면 당첨 포인터만 이동 | ||
winningIndex++; | ||
} | ||
} | ||
return matchCount; | ||
} | ||
public static boolean countMatchingNumbers(int bonusNum, Lotto purchasedLotto) { | ||
return purchasedLotto.getNumbers().contains(bonusNum); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoTickets { | ||
private final List<Lotto> tickets = new ArrayList<>(); | ||
|
||
public void add(Lotto lotto) { | ||
tickets.add(lotto); | ||
} | ||
public int size() { | ||
return tickets.size(); | ||
} | ||
public String toString() { | ||
StringBuilder output = new StringBuilder(); | ||
for (Lotto ticket : tickets) output.append(ticket).append("\n"); | ||
return output.toString(); | ||
} | ||
|
||
public List<Lotto> getLottos() { | ||
return tickets; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package model; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class Validator { | ||
|
||
public void validatePurchasedAmount(int purchasedAmount) { | ||
if (purchasedAmount < 1000 || purchasedAmount % 1000 != 0) { | ||
throw new IllegalArgumentException("[ERROR] 구매 금액은 1000원 이상, 1000원 단위로 입력해주세요"); | ||
} | ||
} | ||
|
||
public void validateWinningNumber(List<Integer> winningNumbers) { | ||
if (winningNumbers.size() != 6) { | ||
throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다."); | ||
} | ||
validateDuplicateNumbers(winningNumbers); | ||
|
||
for (Integer num : winningNumbers) { | ||
if (num < 1 || num > 45) { | ||
throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); | ||
} | ||
} | ||
} | ||
|
||
private void validateDuplicateNumbers(List<Integer> winningNumbers) { | ||
Set<Integer> uniqueNumbers = new HashSet<>(winningNumbers); | ||
|
||
if (uniqueNumbers.size() != winningNumbers.size()) { | ||
throw new IllegalArgumentException("[ERROR] 로또 번호는 중복될 수 없습니다."); | ||
} | ||
} | ||
|
||
public void validateBonusNumber(Lotto winningLotto, int bonusNum) { | ||
if(winningLotto.getNumbers().contains(bonusNum)){ | ||
throw new IllegalArgumentException("[ERROR] 로또 번호는 중복될 수 없습니다."); | ||
} | ||
} | ||
} | ||
Comment on lines
+7
to
+41
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,42 @@ | ||
package view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class InputView { | ||
|
||
public int readPurchasePrice() { | ||
while (true) { | ||
try { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
String purchaseAmount = Console.readLine(); | ||
return Integer.parseInt(purchaseAmount); | ||
} | ||
catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
public List<Integer> readWinningNumberInput() { | ||
while (true) { | ||
try { | ||
System.out.println("당첨 번호를 입력해 주세요."); | ||
String winningNum = Console.readLine(); | ||
String[] winningNums = winningNum.split(","); | ||
List<Integer> winningNumList = new ArrayList<>(); | ||
for (String num : winningNums) winningNumList.add(Integer.parseInt(num)); | ||
return winningNumList; | ||
} | ||
catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
public int readBonusNumber() { | ||
System.out.println("보너스 번호를 입력해 주세요."); | ||
return Integer.parseInt(Console.readLine()); | ||
} | ||
} |
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.
생성자에서 검증하는 것 좋습니다.