Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
package com.example.maddaewoole;

import static com.example.maddaewoole.Utils.MENT1;
import static com.example.maddaewoole.Utils.MENT2;
import static com.example.maddaewoole.Utils.MENT3;

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

public class Daewoole {

private final int angerLimit;
private final Map<String, Integer> angerMap;
private int angerLevel;

public Daewoole() {
angerLevel = 0;
angerLimit = Utils.getRandomNumber(80, 121);
Copy link
Contributor

Choose a reason for hiding this comment

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

매직넘버를 쓰는 습관을 들여봅시다!!

  1. 뉴비가 코드를 봤을 때, 80, 121이 먼의미지? 하는 의문이 들 수 있음
  2. 지금은 여기서 한번밖에 안쓰는데, 같은 숫자가 클래스 내에 100개가 있게 될 경우, 이제부턴 80이 아니라 79로 바꿔주세요 하면 100줄의 코드를 모두 79로 바꿔야하는 노가다쑈 시작(단축키가 있지만 그래도..)

80, 121, 0, 21, 10, 31... 수가 너무 많은데 매직넘버로 다 어케바꿔요? 라는 생각이 든다면
범위 설정을 잘못했나.. 스스로 의심을 해보기!

Copy link
Author

@lamodadite lamodadite Jan 11, 2025

Choose a reason for hiding this comment

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

매직넘버를 상수로 바꾸니 가독성이 훨씬 좋아졌습니다!!

System.out.printf("이번 대욱의 분노 임계값은 %d입니다.\n\n", angerLimit);
Copy link
Contributor

Choose a reason for hiding this comment

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

생성자는 뭐하는애일까요?? 왜 필요한거지..?
또, 넥스트 스탭으로 이제 모든 결과가 끝난 시점에서 출력해주세요~ 하면 생성자를 손대서 바꿔야할 것 같은데, 출력부를 분리해보는건 어때여?

Copy link
Author

Choose a reason for hiding this comment

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

출력부를 분리할 생각을 왜 못했을까요.. 확실히 저 부분은 생성자에서 할 일이 아닌것 같네요!

angerMap = new HashMap<>();
angerMap.put(MENT1, Utils.getRandomNumber(0, 21));
angerMap.put(MENT2, Utils.getRandomNumber(10, 31));
angerMap.put(MENT3, Utils.getRandomNumber(30, 51));
}

public int provoked(String ment) {
if (ment.equals(MENT1) || ment.equals(MENT2) || ment.equals(MENT3)) {
angerLevel += angerMap.get(ment);
return angerMap.get(ment);
}
System.out.println(ment + " 이라는 도발 멘트는 없습니다");
return 0;
}

public boolean isOverAngerLimit() {
Copy link
Contributor

Choose a reason for hiding this comment

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

angerLimit이라는 멤버 변수를 대욱이 갖고 있으니, 멤버 변수에 대한 임계점 판별 메서드를 스스로 판단하도록 하는 책임 분리 좋아여~!

return this.angerLevel >= this.angerLimit;
}

public int getAngerLevel() {
return this.angerLevel;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package com.example.maddaewoole;

import static com.example.maddaewoole.Utils.MENT1;
import static com.example.maddaewoole.Utils.MENT2;
import static com.example.maddaewoole.Utils.MENT3;

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

public class Jpark2 {

private final List<String> provocations = new ArrayList<>();

public Jpark2() {
provocations.add(MENT1);
provocations.add(MENT2);
provocations.add(MENT3);
}
Comment on lines +14 to +18
Copy link

Choose a reason for hiding this comment

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

MENT가 바뀌지 않는다면, 생성자가 아닌 provocations에 List.of()로 직접 값을 불변 객체로 넣어도 될 것 같아요


public String provocate() {
return provocations.get(Utils.getRandomNumber(0, provocations.size()));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

하나의 클래스는 머로 구성될까여??
간단히 생각하면 변수 + 메서드라고 생각하는데, 쪠이팍 클래스가 좀 쓸쓸해보여요
쪠이팍 클래스는 어디까지 책임져야 하는것인가..

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
public class Main {

public static void main(String[] args) {

Jpark2 jpark2 = new Jpark2();
Daewoole daewoole = new Daewoole();
int cnt = 0;
while (true) {
String ment = jpark2.provocate();
int anger = daewoole.provoked(ment);
System.out.printf("지원은 '%s'를 시전하여 대욱의 분노를 %d 증가시켰다.\n현재 대욱의 분노 수치 : %d\n\n", ment,
Copy link
Contributor

Choose a reason for hiding this comment

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

다른 클래스에서 출력, 메인 클래스에서도 출력이면 유지보수를 할 때 이리저리왔다갔따 하면서 헷갈릴 것 같아여
출력부 메서드를 만들고, 유틸 클래스에 모아둬도 좋을 것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

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

출력 부분 Util 클래스로 분리했습니다! 확실히 책임분리를 하니 보기도 좋고 확장성도 좋은것 같습니다!

anger,
daewoole.getAngerLevel());
cnt++;
if (daewoole.isOverAngerLimit()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

탈출 조건을 잘 명시해뒀으니, 무한루프 대신 while(daewoole.isOverAngerLimit())
아예 이렇게 걸어두면 좀 더 가독성이 좋을 것 같슴다!

Copy link
Author

Choose a reason for hiding this comment

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

수정햇습니다! 가독성 업!

System.out.printf("참지 못한 대욱은 결국 지원에게 잼민 펀치를 날렸다.\n대욱을 도발한 횟수 : %d회\n", cnt);
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.maddaewoole;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Utils {

static final String MENT1 = "당신의 지각비, 회식비로 대체되었다";
static final String MENT2 = "코딩 그렇게 하는거 아닌데";
static final String MENT3 = "오늘 저녁은 감탄계";

public static int getRandomNumber(int min, int max) {
Random random = ThreadLocalRandom.current();
return random.nextInt(min, max);
}
}