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

## 입력부
- [ ] 파싱
- [x] `<상품>커맨드`를 파싱하는 기능
- [x] <> 내부의 상품을 Product로 생성하는 기능
- [x] <>를 제외한 것들을 커맨드로 생성하는 기능
- 기능
- [x] 계산 : 길이 * UNIT(1000)
- 담기
- [x] 커맨드 없이 <상품>
- [x] <상품>랑
- [x] <상품>이랑<상품>
- [x] 빼기 : 해당 상품을 제외하는 기능
- [x] 찾기 : 해당 상품을 찾는 기능
- [x] 개수 세기 : 상품의 개수를 세는 기능
- [x] 주문 완료
- [x] <상품> 없이 커맨드만 존재
- [x] 총 계산값을 반환하는 기능
Original file line number Diff line number Diff line change
@@ -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<Product> products = new ArrayList<>();

@Override
public void takeOrder(String order) {
public void takeOrder(String orderInput) {
List<String> productNames = generateProductNames(orderInput);
List<Product> 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<String> productNames) {
productNames.forEach(name -> products.add(Product.from(name)));
}

private int calculateProducts(List<String> 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<Product> generateOrders(List<String> productNames) {
return productNames.stream()
.map(Product::from)
.toList();
}

private static List<String> generateProductNames(String orderInput) {
List<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.bot;

public enum OrderType {
CALCULATE,
REMOVE,
FIND,
COUNT,
RESULT;
}
Original file line number Diff line number Diff line change
@@ -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 + ">";
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}