diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/docs/README.md" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/docs/README.md" new file mode 100644 index 0000000..162602a --- /dev/null +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/docs/README.md" @@ -0,0 +1,19 @@ +# 구현 목록 + +## 입력부 +- [ ] 파싱 + - [x] `<상품>커맨드`를 파싱하는 기능 + - [x] <> 내부의 상품을 Product로 생성하는 기능 + - [x] <>를 제외한 것들을 커맨드로 생성하는 기능 +- 기능 + - [x] 계산 : 길이 * UNIT(1000) + - 담기 + - [x] 커맨드 없이 <상품> + - [x] <상품>랑 + - [x] <상품>이랑<상품> + - [x] 빼기 : 해당 상품을 제외하는 기능 + - [x] 찾기 : 해당 상품을 찾는 기능 + - [x] 개수 세기 : 상품의 개수를 세는 기능 + - [x] 주문 완료 + - [x] <상품> 없이 커맨드만 존재 + - [x] 총 계산값을 반환하는 기능 diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/BotImpl.java" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/BotImpl.java" index 029ec1b..91b9a34 100644 --- "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/BotImpl.java" +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/BotImpl.java" @@ -1,14 +1,98 @@ package com.example.bot; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class BotImpl implements JiwonBehavior { + Output output = new Output(); + private static final int PRICE_UNIT = 1000; + private List products = new ArrayList<>(); @Override - public void takeOrder(String order) { + public void takeOrder(String orderInput) { + List productNames = generateProductNames(orderInput); + List orders = generateOrders(productNames); + Product product = !orders.isEmpty() ? orders.get(0) : null; + + String commandInput = orderInput.replaceAll("<.*?>", "").trim(); + Command command = Command.from(commandInput); + + if (command.getType() == OrderType.CALCULATE) { + addToCart(productNames); + int price = calculateProducts(productNames); + output.printCalculateResult(price); + } + if (command.getType() == OrderType.FIND) { + boolean result = isContain(product); + output.printFindResult(product, result); + } + if (command.getType() == OrderType.REMOVE) { + boolean result = removeProducts(product); + output.printRemoveResult(product, result); + } + if (command.getType() == OrderType.COUNT) { + int count = getProductCount(product); + output.printCountResult(product, count); + } + if (command.getType() == OrderType.RESULT) { + result(); + } + } + + private void addToCart(List productNames) { + productNames.forEach(name -> products.add(Product.from(name))); + } + + private int calculateProducts(List productNames) { + return productNames.stream() + .mapToInt(name -> name.length() * PRICE_UNIT) + .sum(); + } + + private boolean removeProducts(Product product) { + if (!isContain(product)) { + return false; + } + return products.removeIf(p -> p.equals(product)); + } + + private List generateOrders(List productNames) { + return productNames.stream() + .map(Product::from) + .toList(); + } + + private static List generateProductNames(String orderInput) { + List productNames = new ArrayList<>(); // 제품 이름 목록 + + Matcher matcher = Pattern.compile("<(.*?)>") + .matcher(orderInput); + while (matcher.find()) { + productNames.add(matcher.group(1)); + } + return productNames; + } + + + private boolean isContain(Product product) { + return products.contains(product); + } + private int getProductCount(Product product) { + return (int) products.stream() + .filter(element -> element.equals(product)) + .count(); } @Override public void result() { + int totalAmount = products.stream() + .mapToInt(Product::getPrice) + .sum(); + System.out.println("총 " + totalAmount + "원 입니다"); + products.clear(); } } diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Command.java" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Command.java" new file mode 100644 index 0000000..08f4bce --- /dev/null +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Command.java" @@ -0,0 +1,36 @@ +package com.example.bot; + +import java.util.Arrays; + +public enum Command { + NONE("", OrderType.CALCULATE), + DELIMITER_TYPE1("랑", OrderType.CALCULATE), + DELIMITER_TYPE2("이랑", OrderType.CALCULATE), + REMOVE_ORDER("빼", OrderType.REMOVE), + FIND_PRODUCT("시켰나?", OrderType.FIND), + COUNT_PRODUCT("몇 개야?", OrderType.COUNT), + RESULT("얼마야?", OrderType.RESULT); + + private final String command; + private final OrderType type; + + Command(String command, OrderType type) { + this.command = command; + this.type = type; + } + + public static Command from(String cmd) { + return Arrays.stream(values()) + .filter(command -> command.getCommand().equals(cmd)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("해당 커맨드는 학습하지 않았습니다. 데스크에 문의하시던지..")); + } + + public String getCommand() { + return command; + } + + public OrderType getType() { + return type; + } +} diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/OrderType.java" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/OrderType.java" new file mode 100644 index 0000000..dd6ee25 --- /dev/null +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/OrderType.java" @@ -0,0 +1,9 @@ +package com.example.bot; + +public enum OrderType { + CALCULATE, + REMOVE, + FIND, + COUNT, + RESULT; +} diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Output.java" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Output.java" new file mode 100644 index 0000000..577a5d1 --- /dev/null +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Output.java" @@ -0,0 +1,45 @@ +package com.example.bot; + +import java.text.NumberFormat; +import java.util.Locale; + +public class Output { + private static final String NON_ORDERED = "안 시키셨어요"; + + public void printRemoveResult(Product product, boolean result) { + if (!result) { + System.out.println(productFormatter(product.getName()) + NON_ORDERED); + return; + } + System.out.println("네"); + } + + public void printCountResult(Product product, int count) { + if (count == 0) { + System.out.println(productFormatter(product.getName()) + NON_ORDERED); + return; + } + System.out.println(productFormatter(product.getName()) + count + "개요"); + } + + public void printCalculateResult(int price) { + NumberFormat formatter = getKoreanFormat(); + System.out.println("네 " + price + "원이요"); + } + + public void printFindResult(Product product, boolean result) { + if (result) { + System.out.println(productFormatter(product.getName()) + "시키셨어요"); + return; + } + System.out.println(productFormatter(product.getName()) + NON_ORDERED); + } + + private NumberFormat getKoreanFormat() { + return NumberFormat.getInstance(Locale.KOREA); + } + + private String productFormatter(String name) { + return "<" + name + ">"; + } +} diff --git "a/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Product.java" "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Product.java" new file mode 100644 index 0000000..22a91ce --- /dev/null +++ "b/v1/backend/JAVA_LV_1_\354\225\214\353\260\224\354\235\230_\353\213\254\354\235\270/src/main/java/com/example/bot/Product.java" @@ -0,0 +1,50 @@ +package com.example.bot; + +import java.util.Objects; + +public class Product { + private static final int UNIT = 1000; + private final String name; + private final int price; + + public Product(String name, int price) { + this.name = name; + this.price = price; + } + + public static Product from(String name) { + int price = name.length() * UNIT; + if (price == 0) { + throw new IllegalArgumentException("장난하나.."); + } + return new Product(name, price); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + /** + * name을 기준으로 equal을 결정합니다. + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Product product = (Product) o; + return Objects.equals(getName(), product.getName()); + } + + @Override + public int hashCode() { + return Objects.hash(getName()); + } +}