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
26 changes: 26 additions & 0 deletions v1/backend/JAVA_LV_1_알바의_달인/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 기능 구현 목록

## BotImpl

- 주문의 종류를 enum 클래스로 명시해둔다.

- ### 1. 프로퍼티

- 현재 주문 목록을 가지고 있는 map.

- ### 2. 메서드

1) takeOrder
- switch문의 조건식에 Parser 객체의 static caseCheck 메서드를 이용해 요구사항의 종류를 구분한다.
- 각 case마다 요구사항에 적절한 메서드를 불러온다. -> parser의 static split 메서드를 활용해서 매개변수로 넘겨준다.

2) result
- 모든 주문 목록의 가격을 합산해서 출력한다.

## Parser

- ### 메서드
1) caseCheck
- 현재 주문의 종류를 구분하여 enum 타입으로 반환하는 메서드
2) split
- <>를 구분자로 안의 내용만 분리해서 string[]으로 반환하는 메서드
Original file line number Diff line number Diff line change
@@ -1,14 +1,125 @@
package com.example.bot;

import java.util.HashMap;
import java.util.Iterator;

enum ReqType {
ADD_ONE, ADD_TWO, DELETE, FIND, COUNT, END
}

Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

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

enum을 사용하셨네요! 자바의 enum은 c/c++과 다르게 클래스라 더 다양한 방법으로 사용할 수 있답니다
각 타입에 맞는 출력문도 같이 저장해보면 좋을 것 같아요

public class BotImpl implements JiwonBehavior {

private final HashMap<String, Integer> menu;

BotImpl() {
menu = new HashMap<String, Integer>();
}

@Override
public void takeOrder(String order) {
switch (Parser.caseCheck(order)) {
case ADD_ONE:
this.addOne(Parser.split(order));
break;
case ADD_TWO:
this.addTwo(Parser.split(order));
break;
case DELETE:
this.delete(Parser.split(order));
break;
case FIND:
this.find(Parser.split(order));
break;
case COUNT:
this.count(Parser.split(order));
break;
case END:
this.result();
break;
}
}

private void addOne(String[] order) {
if (menu.containsKey(order[0]))
menu.put(order[0], menu.get(order[0]) + 1);
else
menu.put(order[0], 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

추가적으로, map의 getOrDefault 기능이나, merge도 존재합니다!!

System.out.printf("네 %d원이요\n", order[0].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.

addOne이라는 메서드명에서 더하기만 할줄 알았는데 출력까지 하고잇따니~ 다음에는 분리해봅시다
추가적으로, 1000이라는 값이 반복해서 출현하고 있는데 상수로 선언해서 공통적으로 사용하면 어떨까여

}

private void addTwo(String[] order) {
for (int i = 0; i < order.length; i++) {
if (menu.containsKey(order[i]))
menu.put(order[i], menu.get(order[i]) + 1);
else
menu.put(order[i], 1);
}
System.out.printf("네 %d원이요\n", order[0].length() * 1000 + order[1].length() * 1000);
}

private void delete(String[] order) {
if (menu.containsKey(order[0])) {
menu.remove(order[0]);
System.out.println("네");
} else
System.out.println("<" + order[0] + ">" + "안 시키셨어요");
}

private void find(String[] order) {
if (menu.containsKey(order[0])) {
System.out.println("<" + order[0] + ">시키셨어요");
} else
System.out.println("<" + order[0] + ">안 시키셨어요");
}

private void count(String[] order) {
if (menu.containsKey(order[0]))
System.out.println("<" + order[0] + ">" + menu.get(order[0]) + "개요");
else
System.out.println("<" + order[0] + ">안 시키셨어요");
}


@Override
public void result() {
int ret = 0;
Iterator<String> it = menu.keySet().iterator();

while (it.hasNext()) {
String key = it.next(); // next를 호출하면 현재의 key를 불러올면서 이터레이터는 다음 값을 가리키게 된다.

ret += key.length() * menu.get(key);
}
System.out.printf("총 %d원 입니다\n", ret * 1000);
}
}

class Parser {
Copy link
Contributor

Choose a reason for hiding this comment

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

파서를 다른 클래스로 분리하시면 더 좋을 것 같습니다!!


public static String[] split(String order) {
Copy link
Contributor

Choose a reason for hiding this comment

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

공통적으로 사용되는 케이스가 아직 아니라면, 어떤걸 분리하는건지 메서드를 읽고 바로 이해할 수 있도록 명확히 작성해주시면 더 좋을 것 같습니다!

String[] ret;
ret = order.split(">");
if (ret.length > 1 && ret[1].indexOf('<') != -1)
ret[1] = ret[1].substring(ret[1].indexOf('<') + 1);
ret[0] = ret[0].substring(1);
return ret;
}

public static ReqType caseCheck(String order) {
if (order.charAt(order.length() - 1) == '>') {
if (order.split(">").length == 1)
return (ReqType.ADD_ONE);
else
return (ReqType.ADD_TWO);
} else if (order.charAt(order.length() - 1) == '?') {
if (order.charAt(order.length() - 2) == '나')
return (ReqType.FIND);
if (order.charAt(order.length() - 3) == '마')
return (ReqType.END);
else
return (ReqType.COUNT);
} else
return (ReqType.DELETE);
Comment on lines +109 to +122
Copy link

Choose a reason for hiding this comment

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

또한 문자열의 특정 위치의 문자 하나를 비교하면 코드를 읽고 해당 로직이 어떤 요구사항을 가지고 있는지 알기 어렵습니다.
"몇 개야?", "시켯나?" 등의 문자열로 비교한다면 더 가독성이 좋을 것 같습니다!

}
}