Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,91 @@
package com.example.bot;

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

public class BotImpl implements JiwonBehavior {

List<String> orders = new ArrayList<>();

Comment on lines +9 to +10
Copy link

Choose a reason for hiding this comment

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

상품을 뺄 때 해당하는 상품을 모두 빼야 하니까 상품과 개수를 한 번에 관리할 수 있는 Map같은 자료구조를 사용하는 것도 좋을 것 같아요

@Override
public void takeOrder(String order) {
if (order.equals("얼마야?")) {
result();
return;
}

String[] words = order.split("<|>");

if (isAddOrder(words)) {
addOrder(words);
} else if (isRemoveOrder(words)) {
removeOrder(words);
} else if (isCheckOrder(words)) {
checkOrder(words);
} else if (isCountOrder(words)) {
countOrder(words);
} else {
throw new RuntimeException("알 수 없는 명령입니다: " + order);
}
}

@Override
public void result() {
int totalPrice = orders.stream().mapToInt(String::length).sum();
System.out.printf("총 %d원 입니다\n", (totalPrice * 1000));
}

private boolean isAddOrder(String[] words) {
Copy link
Contributor

Choose a reason for hiding this comment

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

메서드명을 보면 바로 어떤 기능인지 추론할 수 있도록 적어주신 점 좋읍니다

return (words.length == 4 && (words[2].equals("랑") || words[2].equals("이랑")))
|| words.length == 2;
}

private void addOrder(String[] words) {
int total = words[1].length();
orders.add(words[1]);
if (words.length == 4) {
total += words[3].length();
orders.add(words[3]);
}
System.out.printf("네 %d원이요\n", (total * 1000));
Copy link
Contributor

Choose a reason for hiding this comment

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

출력부 분리 해다오..
addOrder라는 메서드는 이름만 봤을 땐 주문 추가만 할줄 알았는데, print까지 하고있었군여!

}

private boolean isRemoveOrder(String[] words) {
return words.length == 3 && words[2].equals("빼");
}

private void removeOrder(String[] words) {
if (orders.contains(words[1])) {
orders.removeIf(word -> word.equals(words[1]));
System.out.println("네");
} else {
System.out.printf("<%s>안 시키셨어요\n", words[1]);
}
}

private boolean isCheckOrder(String[] words) {
return words.length == 3 && words[2].equals("시켰나?");
}

private void checkOrder(String[] words) {
if (orders.contains(words[1])) {
System.out.printf("<%s>시키셨어요\n", words[1]);
} else {
System.out.printf("<%s>안 시키셨어요\n", words[1]);
}
}

private boolean isCountOrder(String[] words) {
return words.length == 3 && words[2].equals("몇 개야?");
}

private void countOrder(String[] words) {
if (orders.contains(words[1])) {
int count = Collections.frequency(orders, words[1]);
System.out.printf("<%s>%d개요\n", words[1], count);
} else {
System.out.printf("<%s>안 시키셨어요\n", words[1]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

words의 길이도 봐야하고, 내부 요소도 하나하나 봐야하고.. 메서드마다 조건이 너무 많은 것 같다는 생각이 든다면,
다음 단계로는 제품과 명령어로 분리하는 방식은 어떨까요?!

Copy link
Author

Choose a reason for hiding this comment

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

빨리 해야한다는 압박 속에서 하다보니 가독성도 안좋고 책임분리도 안된 코드를 제출해 버렸네요.. 이제 보니 그냥 전부 갈아엎어야 할 수준인데 이럴거면 처음부터 시간 들이더라도 꼼꼼히 할걸 그랬습니다!! 리팩토링 꼭 해볼게요!!

}
}
}