Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/controller/Controller.java
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);
}
}
11 changes: 10 additions & 1 deletion src/main/java/lotto/Application.java
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();
}
}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

70 changes: 70 additions & 0 deletions src/main/java/model/Lotto.java
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;
}
Comment on lines +9 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자에서 검증하는 것 좋습니다.


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
Copy link

Choose a reason for hiding this comment

The 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();
}


}
48 changes: 48 additions & 0 deletions src/main/java/model/LottoRank.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미션 구현 유의사항 잘 읽어주세용
인덴트가 4단이나 들어가 계십니다!

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

78 changes: 78 additions & 0 deletions src/main/java/model/LottoService.java
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 {
Copy link

Choose a reason for hiding this comment

The 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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/model/LottoTickets.java
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;
}
}

41 changes: 41 additions & 0 deletions src/main/java/model/Validator.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 검증 전용으로 책임을 분리한건 좋습니다.
하지만 책임 분리가 곧 클래스를 무조건 나눈다는 뜻은 아닙니다.
이 점 생각하셔서 책임을 적절한 곳에 분배해 보세요!!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 에러만 담당하는 클래스도 따로 만들어 관리하는게 좋아보입니다!

42 changes: 42 additions & 0 deletions src/main/java/view/InputView.java
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());
}
}
Loading