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
5 changes: 3 additions & 2 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package lotto;
import Controller.Controller;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Controller lottoController = new Controller();
lottoController.run();
Comment on lines +5 to +6
Copy link

Choose a reason for hiding this comment

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

AppConfig 에 대해서 공부하시면 좋을 것 같습니다!
"AppConfig 순수자바" 키워드로 검색 하시면 좋은 자료가 많아서 도움될거같아요!!

}
}
57 changes: 57 additions & 0 deletions src/main/java/lotto/Controller/Controller.java
Copy link

Choose a reason for hiding this comment

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

컨트롤러가 너무 담당하는 일이 많고, 협력 객체를 직접 생성하고 있기 때문에 결합도가 너무 높습니다.
컨트롤러는 단순히 view와 model을 연결해 주는 곳이라고 생각하시면 됩니다. 유효성 검사가 컨트롤러에 있을 필요가 있을지 생각해 보시는게 좋을 것 같습니다!
AppConfig를 사용하셔서 담당하는 일을 줄이고, DI(생성자 주입)를 사용하셔보시기 바랍니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lotto.Controller;

import Domain.LottoResult;
import Domain.LottoTicketMachine;
import Domain.WinningLotto;
import View.InputView;
import View.OutputView;
import java.util.List;
import java.util.ArrayList;
import Domain.Lottos;

public class Controller {
private final OutputView outputView;
private final InputView inputView;

public Controller() {
this.outputView = new OutputView();
this.inputView = new InputView();
}

private int setMoney(){
return inputView.inputMoney();
//유효성 검사
}

private List<Integer> setNum(){
//유효성 검사
String[] arr = inputView.inputNum();
List<Integer> arr2 = new ArrayList<>();
for(int i = 0; i < arr.length; i++){
arr2.add(Integer.parseInt(arr[i]));
}
return arr2;
Comment on lines +28 to +33
Copy link

Choose a reason for hiding this comment

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

이렇게 의미 없는 변수명은 진짜로 지양하셔야 합니다.

}

private int setBonus(){
return inputView.inputBonusNum();
}


public void run(){
int money = setMoney(); //구입 금액 입력
LottoTicketMachine lottoTicketMachine = new LottoTicketMachine(money);
outputView.getHowMany(money); //구매 개수 출력
Lottos lottos= lottoTicketMachine.execute(); //로또 번호 생성
outputView.getLottoNum(lottos.getLottos()); //로또 번호 출력
List<Integer> winningNum = setNum(); //당첨 번호 입력
int bonusNum = setBonus(); //보너스 번호 입력
WinningLotto winningLotto = new WinningLotto(winningNum, bonusNum);
LottoResult lottoResult = lottos.rankAll(winningLotto);
outputView.getResult(lottoResult);
outputView.getYield(lottoResult, money);

}
Comment on lines +41 to +54
Copy link

Choose a reason for hiding this comment

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

public 메서드가 주요 메서드기 때문에 상단으로 올려주세요!



}
48 changes: 48 additions & 0 deletions src/main/java/lotto/Domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lotto.Domain;

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("[ERROR] 로또 번호는 6개여야 합니다.");
}
if(numbers.stream().distinct().count() != 6){
throw new IllegalArgumentException("[ERROR] 로또 번호에 중복된 숫자가 있습니다.");
}
if (numbers.stream().anyMatch(num -> num < 1 || num > 45)) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
}

public Rank determineRank(WinningLotto winningLotto) {
int matchCount = 0;
boolean bonus = false;

for(int i : this.numbers) {
if(winningLotto.getWinningNum().contains(i)){
matchCount++;
}
}

for(int i : this.numbers) {
if(i == winningLotto.getWinningBonusNum()){
bonus = true;
}
}

return Rank.valueOf(matchCount, bonus);
}
Comment on lines +25 to +42
Copy link

Choose a reason for hiding this comment

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

이건 WinningLotto가 해야 할 일을 Lotto가 하고 있다고 보입니다.
위치를 변경하는게 어떨까요??


public String toString(){
return numbers.toString();
}

}
33 changes: 33 additions & 0 deletions src/main/java/lotto/Domain/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto.Domain;

import java.util.Map;
import java.util.HashMap;

public class LottoResult {
private Map<Rank, Integer> ranks;


public LottoResult(Map<Rank, Integer> ranks) {
this.ranks = ranks;
}

public int getYield() {
int totalMoney = 0;
for(Rank rank : ranks.keySet()) {
totalMoney += rank.getPrizeMoney() * ranks.get(rank);
}
return totalMoney;
}

public Map<Rank, Integer> getRanks() {
return ranks;
}

public double calculateYield(int purchaseAmount) {
if (purchaseAmount == 0) {
return 0.0;
}
return (double) getYield() / purchaseAmount * 100;
}

Comment on lines +14 to +32
Copy link

Choose a reason for hiding this comment

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

이 클래스가 get으로 가져와서 만들지 말고, Rank 클래스가 직접 결과를 관리하게 하면 어떨까요?

}
48 changes: 48 additions & 0 deletions src/main/java/lotto/Domain/LottoTicketMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lotto.Domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class LottoTicketMachine {
private int lottoCount;

public LottoTicketMachine(int money) {
validate(money);
this.lottoCount = money / 1000;
}

private void validate(int money) {
if (money % 1000 != 0) {
Copy link

Choose a reason for hiding this comment

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

이런 숫자들은 매직 넘버로 관리해주시면 좋을 것 같습니다!

throw new IllegalArgumentException("[ERROR] 구입 금액은 1,000원 단위여야 합니다.");
}
if (money <= 0) {
throw new IllegalArgumentException("[ERROR] 구입 금액은 0보다 커야 합니다.");
}
}


public List<Integer> getRandomNum(){
List<Integer> numbers = new ArrayList<>();
for(int i = 0; i < 45; i++){
numbers.add(i+1);
}
Collections.shuffle(numbers); //1~45까지 무작위 섞기
List<Integer> lottoNumbers = new ArrayList<>(numbers.subList(0, 6));
Collections.sort(lottoNumbers);
return lottoNumbers;
}
Comment on lines +25 to +34
Copy link

Choose a reason for hiding this comment

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

이걸 만드신 이유가 무엇일까요??


public Lottos execute(){
List<Lotto> lottos = new ArrayList<>();

for(int i = 0; i < lottoCount; i++){
List<Integer> numbers = getRandomNum();
Lotto lotto = new Lotto(numbers);
lottos.add(lotto);
}

return new Lottos(lottos);
}

}
29 changes: 29 additions & 0 deletions src/main/java/lotto/Domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto.Domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class Lottos {
private List<Lotto> lottos = new ArrayList<>();

public Lottos(List<Lotto> lottos) {
this.lottos = lottos;
}

public List<Lotto> getLottos() {
return lottos;
}

public LottoResult rankAll(WinningLotto winningLotto) {
Map<Rank, Integer> rankMap = new HashMap<>();
for (Lotto lotto : lottos) {
Rank rank = lotto.determineRank(winningLotto);
//해당 rank에 누적 더하기
rankMap.put(rank, rankMap.getOrDefault(rank, 0) + 1);
}
return new LottoResult(rankMap);
}

}
45 changes: 45 additions & 0 deletions src/main/java/lotto/Domain/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lotto.Domain;

public enum Rank {
FIRST(6, 2_000_000_000),
SECOND(5, 30_000_000),
THIRD(5, 1_500_000),
FOURTH(4, 50_000),
FIFTH(3, 5_000),
NONE(0, 0);

private final int matchNum;
private final int prizeMoney;

private Rank(int matchNum, int prizeMoney) {
this.matchNum = matchNum;
this.prizeMoney = prizeMoney;
}

public int getMatchNum() {
return matchNum;
}

public int getPrizeMoney() {
return prizeMoney;
}

public static Rank valueOf(int matchNum, boolean matchBonus){
if(matchNum == 6){
return FIRST;
}
if(matchNum == 5 && matchBonus){
return SECOND;
}
if(matchNum == 5){
return THIRD;
}
if(matchNum == 4){
return FOURTH;
}
if(matchNum == 3){
return FIFTH;
}
return NONE;
}
Comment on lines +27 to +44
Copy link

Choose a reason for hiding this comment

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

오 팩토리 매서드를 알고 계셨나요??

}
40 changes: 40 additions & 0 deletions src/main/java/lotto/Domain/WinningLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lotto.Domain;

import java.util.List;

public class WinningLotto {
private List<Integer> winningNum;
private int winningBonusNum;

public WinningLotto(List<Integer> winningNum, int winningBonusNum) {
validate(winningNum, winningBonusNum);
this.winningNum = winningNum;
this.winningBonusNum = winningBonusNum;
}
Comment on lines +9 to +13
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> winningNum, int winningBonusNum) {
if (winningNum.size() != 6) {
throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다.");
}
if (winningNum.stream().distinct().count() != 6) {
throw new IllegalArgumentException("[ERROR] 당첨 번호에 중복된 숫자가 있습니다.");
}
if (winningNum.stream().anyMatch(num -> num < 1 || num > 45)) {
throw new IllegalArgumentException("[ERROR] 당첨 번호는 1부터 45 사이의 숫자여야 합니다.");
}
if (winningNum.contains(winningBonusNum)) {
throw new IllegalArgumentException("[ERROR] 보너스 번호는 당첨 번호와 중복될 수 없습니다.");
}
if (winningBonusNum < 1 || winningBonusNum > 45) {
throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다.");
}
}
Comment on lines +15 to +31
Copy link

Choose a reason for hiding this comment

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

에러 메세지를 이렇게 하기 보단, 한 곳에서 관리하는게 좋아보입니다.


public List<Integer> getWinningNum() {
return winningNum;
}

public int getWinningBonusNum(){
return winningBonusNum;
}
Comment on lines +33 to +39
Copy link

Choose a reason for hiding this comment

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

Num과 같이 줄여서 쓰는 것은 지양해주세요!

}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/lotto/View/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lotto.View;

import java.util.Scanner;

public class InputView {
public final Scanner sc = new Scanner(System.in);

public int inputMoney(){
System.out.println("구입 금액을 입력해 주세요.");
int money = Integer.parseInt(sc.nextLine());
return money;

}

public String[] inputNum(){
System.out.println("당첨 번호를 입력해 주세요.");
return sc.nextLine().split(",");
}

public int inputBonusNum(){
System.out.println("보너스 번호를 입력해 주세요.");
return Integer.parseInt(sc.nextLine());
}

}
Loading