-
Notifications
You must be signed in to change notification settings - Fork 0
Be/main/jongmlee java lv1 #18
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
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 |
|---|---|---|
| @@ -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<>(); | ||
|
|
||
| @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) { | ||
|
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. 메서드명을 보면 바로 어떤 기능인지 추론할 수 있도록 적어주신 점 좋읍니다 |
||
| 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)); | ||
|
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. 출력부 분리 해다오.. |
||
| } | ||
|
|
||
| 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]); | ||
| } | ||
|
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. words의 길이도 봐야하고, 내부 요소도 하나하나 봐야하고.. 메서드마다 조건이 너무 많은 것 같다는 생각이 든다면,
Author
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.
상품을 뺄 때 해당하는 상품을 모두 빼야 하니까 상품과 개수를 한 번에 관리할 수 있는 Map같은 자료구조를 사용하는 것도 좋을 것 같아요