Skip to content

Conversation

ejjunm
Copy link

@ejjunm ejjunm commented Sep 29, 2025

코드 짜는 것도 쉽지 않았지만, 객체 지향 원칙에 맞도록 패키지 나누고, 함수 나누는 과정이 어려웠습니다. 아직 하나하나 나눠서 코드를 짜본 적이 없어서 더 익숙해져야 할 거 같습니다!

@ejjunm ejjunm changed the title [2주차] 객체지향 코드 연습 [2주차] 객체지향 코드 연습(ejjunm) Sep 29, 2025
Copy link

@baaamk baaamk left a comment

Choose a reason for hiding this comment

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

고생하셨습니다!!

고민 많이 하신게 보여서 기쁩니다!!
코드리뷰 확인하시고 AppConfig 사용 적극적으로 검토해주세요!

더 좋은 코드를 위해서 조금 더 고민하시고 하시면 좋을 것 같아요!
고생하셨습니다!!

Comment on lines +27 to +44
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;
}
Copy link

Choose a reason for hiding this comment

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

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

Comment on lines +15 to +31
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 사이의 숫자여야 합니다.");
}
}
Copy link

Choose a reason for hiding this comment

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

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

Comment on lines +33 to +39
public List<Integer> getWinningNum() {
return winningNum;
}

public int getWinningBonusNum(){
return winningBonusNum;
}
Copy link

Choose a reason for hiding this comment

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

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

Comment on lines +9 to +13
public WinningLotto(List<Integer> winningNum, int winningBonusNum) {
validate(winningNum, winningBonusNum);
this.winningNum = winningNum;
this.winningBonusNum = winningBonusNum;
}
Copy link

Choose a reason for hiding this comment

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

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

Comment on lines +25 to +34
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;
}
Copy link

Choose a reason for hiding this comment

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

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

Comment on lines +28 to +33
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;
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.

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

Comment on lines +21 to +31
public void getResult(LottoResult result){
Map<Rank, Integer> ranks = result.getRanks();

System.out.println("당첨 통계\n---");
System.out.println("3개 일치 (5,000원) - " + ranks.getOrDefault(Rank.FIFTH, 0) + "개");
System.out.println("4개 일치 (50,000원) - " + ranks.getOrDefault(Rank.FOURTH, 0) + "개");
System.out.println("5개 일치 (1,500,000원) - " + ranks.getOrDefault(Rank.THIRD, 0) + "개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + ranks.getOrDefault(Rank.SECOND, 0) + "개");
System.out.println("6개 일치 (2,000,000,000원) - " + ranks.getOrDefault(Rank.FIRST, 0) + "개");
}

Copy link

Choose a reason for hiding this comment

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

view가 모델 내부를 알고있으면 안됩니다.
MVC 설계에서 View는 Model을 알면 안 되고, Model은 View를 알면 안 됩니다!

DTO를 활용해서 해보시는건 어떨까요??

System.out.println("총 수익률은 " + String.format("%.1f", result.calculateYield(purchaseAmount)) + "입니다.");
}

} No newline at end of file
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(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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants