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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/onboarding.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package com.example.maddaewoole;

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

public class Daewoole {
public int now_anger = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

클래스 내부에 property(필드)를 갖고 있다면, 해당 필드를 갖고 뭘 할 수 있을까? 얘를 갖고 어떤 기능을 넣고, 이용할 수 있을까? 에 대한 고민을 하면 정말 조습니다.
할 수 있는게 없다면, 진짜 필요한 필드일까?에 대해 고민해보아여

public int anger_threshold;
public Map<String, Integer> anger_map = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

자바는 보통 변수명을 카멜케이스로 작성합니다!
nowAnger, angerThreshold 이런 식으로여!


}
public int getAnger_threshold() {
return anger_threshold;
}
Daewoole(List<String> str){
anger_threshold = (int) ((Math.random() * 40) + 80);
Copy link
Contributor

Choose a reason for hiding this comment

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

40, 80, 10000과 같이 직접 수를 작성하기보단, 변수명을 주는건 어떨까요?
다른 사람이 코드를 읽었을 때, 40, 80의 의미가 뭔지 바로 알 수 있도록여!

anger_map.put(str.get(0), (int)(Math.random() * 10000) % 20);
anger_map.put(str.get(1), (int)(Math.random() * 20) + 10);
anger_map.put(str.get(2), (int)(Math.random() * 20) + 30);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

생성자는 뭐하는 애고, 어떻게 써야할까? 에 대해 고민해봅시다!!!
이후에 책임 분리까지 관심이 생기신다면 정적 팩토리 메서드도 한숟가락..

https://sun-22.tistory.com/84

https://tecoble.techcourse.co.kr/post/2020-05-26-static-factory-method/

int beProvinced(String str){
return anger_map.get(str);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.example.maddaewoole;


import java.util.List;

public class Jpark2 {
private List<String> str = List.of("'당신의 지각비, 회식비로 대체되었다'", "'코딩 그렇게 하는 거 아닌데'", "'오늘 저녁은 감탄계'");

String prov(){
int random_num = (int) ((Math.random()*10000)%3);
return str.get(random_num);
}

public List<String> getStr() {
return str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
public class Main {

public static void main(String[] args) {
Jpark2 jiwon = new Jpark2();
Daewoole daewook = new Daewoole(jiwon.getStr());

int attack_cnt = 0;
while (true) {
String attack = jiwon.prov();
int daewookAnger = daewook.beProvinced(attack);
System.out.println("지원은 " + attack + "를 시전하여 대욱의 분노를 " + daewookAnger + " 증가시켰다.");
daewook.now_anger += daewookAnger;
System.out.println("현재 대욱의 분노 수치 : " + daewook.now_anger);
if (daewook.now_anger > daewook.getAnger_threshold()) {
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

각자의 코딩 스타일이 있겠지만, 무한루프보다는 탈출조건까지만 함수를 시행하는건 어떨까요??
탈출 조건 명시도 잘 해두셨으니까여!!

attack_cnt++;
}
System.out.println("대욱을 도발한 횟수 : " + attack_cnt + "회");
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,88 @@
package com.example.bot;

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


public class BotImpl implements JiwonBehavior {

private final Map<String, Integer> product = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Map 등의 Collection류 변수명은 복수형으로 작성해주시면 좋습니다!


@Override
public void takeOrder(String order) {
if (order.equals("얼마야?")) {
result();
return;
}
int loc = order.indexOf('>');
String name = order.substring(1, loc);
order = order.substring(loc + 1);
Comment on lines +17 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 매개변수를 재할당하는 것 보다는 새로운 값을 지정해서 사용하는 편이 좋을 것 같습니다!
    이후의 흐름에서 order라는 매개변수가 원본 값임을 기대하는데 중간에 사이드 이펙트(부작용, 값이 바뀌는 행위)가 생기면 이후 다른 사람이 흐름을 파악하기가 어려울 수 있다고 생각해요.

  • indexOf()와 substring()을 이용하는 부분은 '주어지는 주문 입력에 대해 주문은 이렇게 분리된다'라는 로직을 적용하는 부분인데, 해당하는 로직을 갖는 객체(우리가 정의한 '주문(Order)')로 정의해서 사용하면 좀 더 객체지향적일 것 같습니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

  • 아직 객체가 어색하다면, 하나의 기능을 하는 함수를 빼보는 것부터 시작해도 좋습니다!!

  • 추가적으로, 이부분도 사람마다 다르지만 저는 가독성을 생각한다면 변수명은 상세할 수록 좋다고 생각합니다! loc보다는 prefixPosition 혹은 prefixIndex같은 변수명은 어떨까여

  • 위에 언급드린바와 같이, 파싱의 대상인 prefix, suffix(<, >)는 위에 static으로 선언하는 방식도 고려해보는게 좋을 것 같습니다! 만약 <,>를 여기저기 많이 써놨는데, 이제부턴 {,}로 파싱해주세요 혹은 >,<로 파싱해주세요~ 라고 하면 일일이 찾아서 고쳐야하니까엽


if (order.equals("빼")) {
outProduct(name);
} else if (order.equals("시켰나?")) {
didIOrdered(name);
Copy link
Contributor

Choose a reason for hiding this comment

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

'I'와 같은 표현은 주로 사용하지 않습니다..!
손님이 '시켰나?' 라고 물어본 것에 대해서 'name'이라는 변수에 해당하는 아이템이 있는지 찾는 것이므로 �didProductOrdered와 같은 방식으로 바꿀 수 있을 것 같습니다!

  • 해당 메서드를 호출하는 객체는 주문을 받는 Bot이어서 order를 하는 주체가 아닌 점도 있는 것 같아요!

} else if (order.equals("몇 개야?")) {
howManyOrdered(name);
Copy link
Contributor

Choose a reason for hiding this comment

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

메서드명은 맨 앞에 동사가 오게끔 작성해주세요!

} else {
orderProducts(order, name);
}
}

private void orderProducts(String order, String name) {
int price = name.length() * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

'1000'과 같은 부분은 정책적으로 변경될 여지가 있는 부분인데, 해당하는 부분의 변경에 대해서 상수 값으로 관리하지 않으면 이후에 변경된 시점에 일일히 바꿔주어야 합니다..!

정책적으로 지정된 값들에 대해서는 별도의 상수(static final 혹은 enum)로 관리해주시는게 좋을 것 같습니다!

if (product.containsKey(name)) {
product.put(name, product.get(name) + 1);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Map에서 get을 쓴다는 것.. NullPointException을 감내한다는 것.. input에 대한 예외사항을 견딜 자신이 있다는 것..!!
과제를 진행하면서 개인적으로 고민을 많이 했던 부분인데, 예외사항에 대해서 추가적인 처리를 도입하거나, getOrDefault로 우회하는 방식을 생각해보면 좋을 것 같습니다!! 함께 더 고민해봅시더..

product.put(name, 1);
}

if (order.indexOf('랑') >= 0) {
int loc = order.indexOf('<');
name = order.substring(loc + 1, order.length() - 1);
price += name.length() * 1000;
if (product.containsKey(name)) {
product.put(name, product.get(name) + 1);
} else {
product.put(name, 1);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

책임 분리란 무엇일까여.. 항상 고민되는 부분입니다.
핵심 로직인 값 계산과 출력이 함께 얽혀있는데, 어떻게 둘을 떼어낼 수 있을까요?!


}
System.out.println("네 " + price + "원이요");
}

private void outProduct(String name) {
if (product.containsKey(name)) {
product.remove(name);
System.out.println("네");
} else {
System.out.println("<" + name + ">안 시키셨어요");
}
}

private void didIOrdered(String name) {
if (product.containsKey(name)) {
System.out.println("<" + name + ">시키셨어요");
} else {
System.out.println("<" + name + ">안 시키셨어요");
}
}

private void howManyOrdered(String name) {
if (product.containsKey(name)) {
System.out.println("<" + name + ">" + product.get(name) + "개요");
} else {
System.out.println("<" + name + ">안 시키셨어요");
}
}

@Override
public void result() {
int total = 0;
for (String key : product.keySet()) {
total += key.length() * 1000 * product.get(key);
}
System.out.println("총 " + total + "원 입니다");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

public class BFS implements Algorithm {

boolean isSolved = false;

@Override
public boolean isSolution(Algorithm algorithm) {
return algorithm instanceof BFS;
}

@Override
public String getSolution() {
return "주변부터 둘러보기";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

public class BinarySearch implements Algorithm {

public boolean isSolution(Algorithm algorithm) {
return algorithm instanceof BinarySearch;
}

@Override
public String getSolution() {
return "반띵 ㄱㄱ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

public class DFS implements Algorithm {

public boolean isSolution(Algorithm algorithm) {
return algorithm instanceof DFS;
}

@Override
public String getSolution() {
return "너에게 닿기를...";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

public class DP implements Algorithm {

public boolean isSolution(Algorithm algorithm) {
return algorithm instanceof DP;
}

@Override
public String getSolution() {
return "누가 첫날부터 DP문제를 들고옵니까";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
package com.example.algorithmbot;

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

public class Main {

public static void main(String[] args) {
BFS bfs = new BFS();
DFS dfs = new DFS();
TwoPointer twoPointer = new TwoPointer();
BinarySearch binarySearch = new BinarySearch();
DP dp = new DP();

List<Algorithm> algorithm = new ArrayList<>(
Arrays.asList(bfs, dfs, twoPointer, binarySearch, dp));
Comment on lines +10 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

List<Algorithm> algorithms = List.of(new BFS(), new DFS()...);
와 같은 방식으로도 표현할 수 있습니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

언급해주신바와 같이 JDK 17의 좋은 업데이트 Collections 대신 List.of로 축약하는 기능을 사용하신다면 당신은 자바 멋쟁이

List<String> todayAlgorithm = new ArrayList<>(
Arrays.asList("BFS", "DFS", "TwoPointer", "BinaraySearch", "DP"));
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 매일 진행되는 알고리즘은 두 개인데, todayAlgorithm은 그러한 정보를 담고 있지 않다(모든 알고리즘의 이름을 담고 있다)는 점에서 이름을 AllAlgorithmNames와 같은 식으로 바꿔줄 수 있을 것 같아요!
    더해서, 위에 이미 있는 algorithms라는 변수를 이용해 해당하는 원소들의 이름을 추출해서 새 List로 지정해줘도 좋을 것 같습니다.


Wchae wchae = new Wchae();

for (int i = 0; i < 20; i++) {
int today1 = (int) ((Math.random() * 10) % 5);
int today2 = (int) ((Math.random() * 10) % 5);

Algorithm todayAlgorithm1 = algorithm.get(today1);
Algorithm todayAlgorithm2 = algorithm.get(today2);
Comment on lines +24 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

today1, today2 라는 변수명이 나오게 된 경위가 '풀 수 있는 알고리즘 전체의 이름을 가진 리스트'를 기준으로 '해당하는 리스트의 원소 개수가 끝 값인 난수'를 지정한 후에 '인덱스로 지정해서 두 가지를 뽑아낸다'로 이해했는데,

today1, today2라는 이름은 직관적으로 다른 사람이 이해하기 어려울 수 있으므로

Algorithm todayAlgorithm1 = algorithm.get((int) ((Math.random() * 10) % 5));
Algorithm todayAlgorithm2 = algorithm.get((int) ((Math.random() * 10) % 5));

라는 방식으로 표현할 수 있을 것 같습니다.
좀 더 낫게 만든다면 (int) ((Math.random() * 10) % 5)에 해당하는 부분을 public int getRandomIntByRange(int from, int to)와 같은 방식으로 메서드를 분리해서 다음과 같이 표현해준다면 더 읽기 좋을 것 같아요!

Algorithm todayAlgorithm1 = algorithm.get(getRandomIntByRange(1, 5));


System.out.println(
"오늘의 알고리즘 : " + todayAlgorithm.get(today1) + ", " + todayAlgorithm.get(today2));
Comment on lines +30 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 부분은 todayAlgorithm1, 2를 대입해서 사용할 수 있을 것 같은데 아마도 깜빡하신 부분인 것 같습니다!

System.out.println(
"우주 : " + wchae.solveAlgorithm(todayAlgorithm1) + " / " + wchae.solveAlgorithm(
todayAlgorithm2));
}
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 @@ -2,4 +2,12 @@

public class TwoPointer implements Algorithm {

public boolean isSolution(Algorithm algorithm) {
return algorithm instanceof TwoPointer;
}

@Override
public String getSolution() {
return "너에게 닿기를...";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package com.example.algorithmbot;

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

public class Wchae {

private final Map<Algorithm, Integer> algorithmMap = new HashMap<>();

public String solveAlgorithm(Algorithm haveToSolve) {
Copy link
Contributor

Choose a reason for hiding this comment

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

매개변수 명이 haveToSolve인 것 보다는 Algorithm algorithm으로 적어주시는 것이 가독성에 더 좋을 것 같습니다! (해당 메서드로 들어온 이상 solve를 진행하는 것은 보장되어 있기 때문에)

Optional<Algorithm> today1 = algorithmMap.keySet().stream()
.filter(x -> x.isSolution(haveToSolve)).findFirst();
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

람다식을 사용하실 때 사용되는 변수에 대해서는 변수명을 명기해주시는 것이 좋습니다!
위의 케이스에서는 x라고 적혀있으면 다른 사람이 읽을 때 해당하는 변수가 무엇에 해당하는지 찾아보아야 하는데요,
'algorithm ->' 혹은 'algo ->', 'a ->' 라는 식으로 적어주시면 가늠이 가능합니다!

Optional<Algorithm> today1 = algorithmMap.keySet().stream()
				.filter(algorithm  -> algorithm.isSolution(haveToSolve))
				.findFirst();

Copy link
Contributor

Choose a reason for hiding this comment

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

람다식 마스터 hyowchoi?
이거 못막습니다.. 변수명만 조금 더 신경써봅시더!!


if (today1.isPresent()) {
int solved_cnt = algorithmMap.getOrDefault(haveToSolve, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

스네이크 케이스 -> 카멜 케이스 주의해주세요!

algorithmMap.put(haveToSolve, solved_cnt + 1);
double percentage = Math.random();
if ((double) solved_cnt * 0.1 > percentage) {
return haveToSolve.getSolution();
} else {
return "스트레스 받네...";
}
} else {
algorithmMap.put(haveToSolve, 1);
return "스트레스 받네...";
}
}

}