-
Notifications
You must be signed in to change notification settings - Fork 0
Be/main/yeoshin java lv1 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ae4da95
9bdaa53
d89d896
d14636d
a2e5f6e
9939186
013dfcf
d38e6fa
569632d
d00e685
ff8971e
346eb7f
24505f5
32c19b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
|
|
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추가적으로, map의 getOrDefault 기능이나, merge도 존재합니다!! |
||
| System.out.printf("네 %d원이요\n", order[0].length() * 1000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addOne이라는 메서드명에서 더하기만 할줄 알았는데 출력까지 하고잇따니~ 다음에는 분리해봅시다 |
||
| } | ||
|
|
||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파서를 다른 클래스로 분리하시면 더 좋을 것 같습니다!! |
||
|
|
||
| public static String[] split(String order) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 또한 문자열의 특정 위치의 문자 하나를 비교하면 코드를 읽고 해당 로직이 어떤 요구사항을 가지고 있는지 알기 어렵습니다. |
||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum을 사용하셨네요! 자바의 enum은 c/c++과 다르게 클래스라 더 다양한 방법으로 사용할 수 있답니다
각 타입에 맞는 출력문도 같이 저장해보면 좋을 것 같아요