Skip to content
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주차] 객체지향 코드 연습 (Seooooo24) #32

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d6d489e
Feat: 당첨번호 배열로 변환
Seooooo24 Sep 27, 2024
597b2b6
Feat: 당첨번호 배열로 변환
Seooooo24 Sep 27, 2024
f54b598
Feat: 당첨번호 배열로 변환
Seooooo24 Sep 27, 2024
f128525
Feat: 입력받은 금액별로 로또 매수 반환
Seooooo24 Sep 27, 2024
6343aac
style: MVC 패턴 적용
Seooooo24 Sep 28, 2024
927c289
feat: 로또 숫자 뽑기
Seooooo24 Sep 28, 2024
e27d52c
feat: 뽑은 로또 숫자 보여주기
Seooooo24 Sep 28, 2024
4c41108
feat: 당첨 번호와 보너스 번호 입력받기
Seooooo24 Sep 29, 2024
4e4e218
feat: 당첨 번호와 로또 번호 비교하기
Seooooo24 Sep 30, 2024
dc33e18
Merge remote-tracking branch 'origin/main'
Seooooo24 Sep 30, 2024
57d005b
feat: 보너스 번호 매치하기
Seooooo24 Oct 4, 2024
93ab4bb
refactor: 당첨 통계 MVC 분리
Seooooo24 Oct 4, 2024
b22b652
feat: 수익률 계산 및 보여주기
Seooooo24 Oct 4, 2024
962bb59
refactor: 로또 번호 생성 방식 변경
Seooooo24 Oct 4, 2024
bc1c595
docs: README.md 수정
Seooooo24 Oct 4, 2024
0b543fa
refactor: 메서드명 변경
Seooooo24 Oct 4, 2024
c85416a
docs: README.md에 예외 처리 부분 추가
Seooooo24 Oct 4, 2024
45be801
feat: 로또 번호 리스트 오름차순 정렬
Seooooo24 Oct 6, 2024
3467293
refactor: Lotto 클래스 생성자 수정
Seooooo24 Oct 7, 2024
0272de2
refactor: Comment 반영
Seooooo24 Oct 7, 2024
1bad3d8
refactor: interface 생성 및 구조 변경
Seooooo24 Oct 9, 2024
a6fb766
refactor: interface 생성 및 구조 변경
Seooooo24 Oct 10, 2024
2fd8fa5
refactor: interface 생성 및 구조 변경
Seooooo24 Oct 10, 2024
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 docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

## Controller

### 입력

구입 금액을 입력받기

당첨 번호를 입력받기

보너스 번호를 입력받기
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved

### 처리


## View

안내 메시지 출력하기

로또 수만큼 로또 번호 출력하기



## Model

구입 금액에 따라 로또 몇 장을 발행해줄지 정하는 기능

랜덤으로 숫자를 뽑는 기능

한 로또에서 이미 뽑힌 번호를 제외하는 기능

번호 몇 개가 일치하는지 확인하는 기능

수익률을 계산하는 기능

에러 문구를 출력하는 기능

54 changes: 54 additions & 0 deletions src/main/java/controller/CLotto.java
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package controller;

import model.LottoNum;
import model.Lotto;
import model.WinCheck;
import valueObject.ErrorMessage;
import view.VLotto;

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

public class CLotto {
private LottoNum lottoNum;
private Lotto mLotto;
private WinCheck winCheck;
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
private VLotto vLotto;
private ErrorMessage errorMessage;
Copy link
Contributor

Choose a reason for hiding this comment

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

초기화 시점이 언제인가요?


public CLotto(LottoNum lottoNum, VLotto vLotto) {
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
this.lottoNum = lottoNum;
this.vLotto = vLotto;
}

public void run() {
int money = vLotto.getMoneyInput();
int lottoCount = lottoNum.buyLotto(money);
vLotto.displayLottoCount(lottoCount);

List<List<Integer>> userLottos = new ArrayList<>();

for (int i = 0; i < lottoCount; i++) {
List<Integer> lottoNumbers = lottoNum.lottoNumbers();
vLotto.displayLottoNumber(lottoNumbers);
userLottos.add(lottoNumbers);
Copy link
Contributor

Choose a reason for hiding this comment

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

Lotto클래스 내부에 List 타입의 numbers가 있는데, Lotto클래스를 활용해보는 것이 더 좋아보입니다.

}


try {
// 당첨 번호 및 보너스 번호 입력받기
String winningNumber = vLotto.getWinningNumber();
List<Integer> winningNumberList = lottoNum.splitNum(winningNumber);
Copy link
Contributor

Choose a reason for hiding this comment

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

List와 같이 자료형의 이름을 변수명에 쓴다면, 나중에 자료형이 변경되었을 때 해당 변수명도 같이 변경되어야 하고 그 변수를 사용하는 다른 곳에도 영향이 끼치기에 자료형은 복수형으로 사용하는 것이 좋습니다.

Suggested change
List<Integer> winningNumberList = lottoNum.splitNum(winningNumber);
List<Integer> winningNumbers = lottoNum.splitNum(winningNumber);

mLotto = new Lotto(winningNumberList);
Copy link
Contributor

Choose a reason for hiding this comment

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

mLotto가 사용되고 있지 않은 것 같습니다. 무슨 목적으로 생성했던 것인지 의도가 궁금합니다!

int bonusNumber = vLotto.getBonusNumber();
lottoNum.validate(bonusNumber); // 보너스 번호 범위 확인
winCheck = new WinCheck(winningNumberList, userLottos);
} catch (NumberFormatException e) {
System.err.println(errorMessage.ERROR_NUM);
} catch (IllegalArgumentException e) {
System.err.println(errorMessage.ERROR_COUNT);
}


}
}
15 changes: 15 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package lotto;

import controller.CLotto;
import model.Lotto;
import model.LottoNum;
import view.VLotto;

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

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
LottoNum lottoNum = new LottoNum();
VLotto vLotto = new VLotto();
CLotto cLotto = new CLotto(lottoNum, vLotto);
Copy link
Member

Choose a reason for hiding this comment

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

이 연관 관계를 Application이 관리하는게 맞을까?를 고민해보시면 좋을거 같아요!!


cLotto.run();
}


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package lotto;
package model;

import valueObject.ErrorMessage;

import java.util.List;

//
public class Lotto {
private final List<Integer> numbers;
private ErrorMessage errorMessage = new ErrorMessage();
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved

public Lotto(List<Integer> numbers) {
validate(numbers);
Expand All @@ -17,4 +21,7 @@ private void validate(List<Integer> numbers) {
}

// TODO: 추가 기능 구현



}
50 changes: 50 additions & 0 deletions src/main/java/model/LottoNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package model;

import valueObject.ErrorMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LottoNum {
private static final int LOTTO_PRICE = 1000;
private static final int LOTTO_SIZE = 6;

private ErrorMessage errorMessage = new ErrorMessage();
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
// 금액을 입력받고 몇 장을 샀는지 확인하는 메서드
public int buyLotto(int money) {
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
if (money%1000 != 0) {
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException(errorMessage.ERROR_PRICE);
}
return money/LOTTO_PRICE;
}

// 랜덤으로 숫자를 뽑아 리스트에 넣어주는 메서드
public List<Integer> lottoNumbers() {
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
List<Integer> randNum = new ArrayList<Integer>();
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
Random random = new Random();
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved

for (int j = 0; j < LOTTO_SIZE; j++) {
randNum.add(random.nextInt(45)+1);
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
}
return randNum;
}

// String 분리하여 배열에 저장하고 배열을 리턴하는 메서드
public List<Integer> splitNum(String numbers) {
List<Integer> result = new ArrayList<>();

String[] number = numbers.split(",");
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
for (String num : number) {
result.add(Integer.parseInt(num));
}
return result;
}

public void validate(int bonusNum) {
if (1 > bonusNum || bonusNum > 45)
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
throw new NumberFormatException();
}


}
43 changes: 43 additions & 0 deletions src/main/java/model/WinCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package model;

import java.util.List;

public class WinCheck {
private final List<Integer> winNumber;
private final List<List<Integer>> userNumber;

private int threeNum;
private int fourNum;
private int fiveNum;
private int sixNum;
Copy link
Contributor

Choose a reason for hiding this comment

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

enum 메소드로 관리해도 좋을 것 같아요. 막연하지만?? 한번 생각해보면 좋을 것 같습니다


public WinCheck(List<Integer> winNumber, List<List<Integer>> userNumber) {
this.winNumber = winNumber;
this.userNumber = userNumber;

winChecker(winNumber, userNumber);
winStats();
Copy link
Contributor

Choose a reason for hiding this comment

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

생성자 내에 비즈니스 로직과 관련된 메소드를 수행하는 게 적절하지 않은 것 같습니다. 필드를 정의하는 코드만 작성해주는 것이 좋습니다.

}
private void winChecker(List<Integer> winNumber, List<List<Integer>> userNumber) {
for (List<Integer> userNum : userNumber) {
userNum.retainAll(winNumber);
if (userNum.size() == 3) {
threeNum++;
} else if (userNum.size() == 4) {
Copy link
Contributor

Choose a reason for hiding this comment

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

else if 사용을 자제해주세요.
early return 방법 사용해주시길 바랍니다.

fourNum++;
} else if (userNum.size() == 5) {
fiveNum++;
} else if (userNum.size() == 6) {
sixNum++;
}
}
}
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
private void winStats() {
System.out.println("당첨 통계");
System.out.println("---");
System.out.println("3개 일치 (5,000원) - "+threeNum+"개");
System.out.println("4개 일치 (50,000원) - "+fourNum+"개");
System.out.println("5개 일치 (1,500,000원) - "+fiveNum+"개");
System.out.println("6개 일치 (2,000,000,000원) - "+sixNum+"개");
}
}
7 changes: 7 additions & 0 deletions src/main/java/valueObject/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package valueObject;

public class ErrorMessage {
public static final String ERROR_PRICE = "[ERROR] 구입금액은 1000원 단위로만 입력 가능합니다.";
public static final String ERROR_NUM = "[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.";
public static final String ERROR_COUNT = "[ERROR] 당첨 번호는 6개입니다.";
}
39 changes: 39 additions & 0 deletions src/main/java/view/VLotto.java
Seooooo24 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package view;

import java.util.List;
import java.util.Scanner;

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

// 금액 입력받기
public int getMoneyInput() {
System.out.println("구입금액을 입력해 주세요.");
return sc.nextInt();
}

// 당첨 번호 입력받기
public String getWinningNumber() {
System.out.println("당첨 번호를 입력해 주세요.");
if (sc.hasNextLine()) {
sc.nextLine(); // getMoneyInput()의 nextInt() 개행문자를 비워주기 위해 호출
}
return sc.nextLine();
}

// 보너스 번호 입력받기
public int getBonusNumber() {
System.out.println("보너스 번호를 입력해 주세요.");
return sc.nextInt();
}

// 구매한 로또 개수 출력
public void displayLottoCount(int lottoCount) {
System.out.println(lottoCount+"개를 구매했습니다.");
}

// 구매한 로또 번호 리스트 출력
public void displayLottoNumber(List<Integer> numbers) {
System.out.println(numbers);
}
}
1 change: 1 addition & 0 deletions src/test/java/lotto/LottoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lotto;

import model.Lotto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down