Skip to content
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

[오렌지] 치킨집 미션 제출합니다 #4

Open
wants to merge 12 commits into
base: tdd-orange
Choose a base branch
from
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# java-chicken-2019
# java-chicken-2019

## 요구사항

+ [x] 메인 화면에서 주문등록, 결제하기, 프로그램 종료 선택 가능
+ [x] 1,2,3 외의 입력이 들어온 경우 예외처리
+ [x] 주문 등록
+ [x] 테이블 목록 표시
+ [x] 1개 이상 주문이 된 테이블은 ₩ 표시
+ [x] 테이블 선택
+ [x] 1,2,3,5,6,8 외의 입력 들어온 경우 예외처리
+ [x] 메뉴 표시
+ [x] 메뉴 선택
+ [x] 1,2,3,4,5,6,21,22 외 입력 들어온 경우 예외처리
+ [x] 메뉴 수량 입력
+ [x] 0 이하 숫자, 문자 입력시 예외처리
+ [x] 한 메뉴의 이미 주문한 수량 + 입력한 수량이 100 이상이면 예외처리
+ [x] 결제하기
+ [x] 테이블 목록 표시 - 주문 등록과 같음
+ [x] 테이블 선택 - 주문 등록과 같음
+ [x] 주문 내역 표시 - 메뉴, 수량, 금액
+ [x] 신용카드와 현금 선택
+ [x] 1, 2 외 입력시 예외처리
+ [x] 결제 금액 표시
+ [x] 치킨종류의 주문수량이 10마리마다 10000원 할인
+ [x] 현금결제(2)시 5% 할인 - 치킨할인 후 중복할인 적용
+ [x] 프로그램 종료
+ [x] 프로그램 종료 전까지는 계속 유지
+ [x] 주문, 계산 실패 시 이유를 보여주고 다시 가능하도록 구현
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ repositories {
dependencies {
testCompile('org.junit.jupiter:junit-jupiter:5.5.2')
testCompile('org.assertj:assertj-core:3.14.0')
}

test {
useJUnitPlatform()
}
24 changes: 8 additions & 16 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import domain.Menu;
import domain.MenuRepository;
import domain.Table;
import domain.TableRepository;
import view.InputView;
import view.OutputView;

import java.util.List;
import controller.Controller;

public class Application {
// TODO 구현 진행
public static void main(String[] args) {
final List<Table> tables = TableRepository.tables();
OutputView.printTables(tables);

final int tableNumber = InputView.inputTableNumber();

final List<Menu> menus = MenuRepository.menus();
OutputView.printMenus(menus);
public static void main(String[] args) throws IllegalAccessException {
final Controller controller = new Controller();
while (true) {
if (!controller.run()) {
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
return;
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}
}
109 changes: 109 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package controller;

import domain.*;
import view.InputView;
import view.OutputView;

import java.util.List;

public class Controller {

private final List<Table> tables = TableRepository.tables();
private final List<Menu> menus = MenuRepository.menus();

public boolean run() throws IllegalAccessException {
try {
MainType mainType = selectMain();
return runPos(mainType);
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
return true;
}
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

private Boolean runPos(MainType mainType) throws IllegalAccessException {
if (MainType.ORDER.equals(mainType)) {
Table table = selectTable();
order(table);
return true;
}
if (MainType.PAYMENT.equals(mainType)) {
Table table = selectTable();
pay(table);
return true;
}
if (MainType.EXIT.equals(mainType)) {
OutputView.printExit();
return false;
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalAccessException("잘못된 포스기 접근입니다.");
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}

private MainType selectMain() {
OutputView.printMain();
return MainType.of(InputView.inputMain());
}

private Table selectTable() {
while (true) {
try {
OutputView.printTables(tables);
return TableRepository.findTableByNumber(InputView.inputTableNumber());
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
}
Comment on lines +45 to +50

Choose a reason for hiding this comment

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

메서드를 분리하면 depth를 줄일 수 있지 않을까요?

Copy link
Author

Choose a reason for hiding this comment

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

run 예외처리에 대한 답변과 비슷한 맥락이라 메소드를 분리해도 크게 달라질 것 같진 않네요 ㅠㅠ

}
}

private void order(Table table) {
while (true) {
try {
Menu menu = selectMenu();
Count count = inputCount();
Order order = new Order(menu, count);
table.order(order);
return;
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
}
}
}

private Count inputCount() {
while (true) {
try {
return new Count(InputView.inputMenuCount());
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
}
Comment on lines +70 to +74

Choose a reason for hiding this comment

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

어차피 run에서 예외처리를 하는데, 여기서 해야 할 이유가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

run에서는 메인기능 선택에 대한 예외처리를 받으려고 한 거고,
이 예외처리는 Count 입력의 실패만을 위한 처리라... 사용자 입장에서 중간에 실패했는데 처음부터 다시 하라하면 짜증나니까 이렇게 구현을 해 보았습니다 . . .ㅜㅜ

}
}

private Menu selectMenu() {
while (true) {
try {
OutputView.printMenus(menus);
return MenuRepository.findMenuByNumber(InputView.inputMenuNumber());
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
}
}
}

private void pay(Table table) {
OutputView.printOrderedMenus(table);
PaymentType paymentType = selectPayment(table);
OutputView.printTotalPrice(paymentType.calculate(table.getOrders()));
table.resetOrder();
}

private PaymentType selectPayment(Table table) {
while (true) {
try {
return PaymentType.of(InputView.inputPayment(table));
} catch (IllegalArgumentException e) {
OutputView.printError(e.getMessage());
}
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/domain/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package domain;

public class Count {
private static final int MIN_COUNT = 1;
private static final int MAX_COUNT = 99;
private int count;
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public Count(final int count) {
validate(count);
this.count = count;
}

private void validate(final int count) {
if (count < MIN_COUNT) {
throw new IllegalArgumentException("0이하로 주문할 수 없습니다.");
}
if (count > MAX_COUNT) {
throw new IllegalArgumentException("한 메뉴의 최대 주문량은 99입니다.");
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}

public Count addCount(Count count) {
final int addResult = this.count + count.getCount();
return new Count(addResult);
}
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

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

Count를 싱글인스턴스로 사용하는건 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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

불변을 보장하고 싶어서 새 Count객체를 반환하도록 구현했습니다.
앨런은 어떻게 생각하시나요? 정식 리뷰어가 아니어서.. ㅎㅎ 귀찮으시면 대답은 안하셔도 됩니다 ㅋㅋㅋ


public int getCount() {
return count;
}
}
23 changes: 23 additions & 0 deletions src/main/java/domain/MainType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package domain;

import java.util.Arrays;

public enum MainType {
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

ORDER(1),
PAYMENT(2),
EXIT(3);

private final int number;

MainType(int number) {
this.number = number;
}

public static MainType of(int number) {
return Arrays.stream(MainType.values())
.filter(mainType -> mainType.number == number)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 메인메뉴입니다."));
}
}
16 changes: 16 additions & 0 deletions src/main/java/domain/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ public Menu(final int number, final String name, final Category category, final
public String toString() {
return category + " " + number + " - " + name + " : " + price + "원";
}

public boolean isMenuNumber(int number) {
return this.number == number;
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public String getName() {
return name;
}

public int getPrice() {
return price;
}

public boolean isChicken() {
return Category.CHICKEN.equals(category);
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions src/main/java/domain/MenuRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ public class MenuRepository {
public static List<Menu> menus() {
return Collections.unmodifiableList(menus);
}

public static Menu findMenuByNumber(int number) {
return menus().stream()
.filter(menu -> menu.isMenuNumber(number))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 메뉴 번호가 존재하지 않습니다."));
}
}
49 changes: 49 additions & 0 deletions src/main/java/domain/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package domain;

import java.util.Objects;

public class Order {

private final Menu menu;
private Count count;

public Order(Menu menu, Count count) {
validate(menu, count);
this.menu = menu;
this.count = count;
}

private void validate(Menu menu, Count count) {
Objects.requireNonNull(menu, "null 불가");
Objects.requireNonNull(count, "null 불가");
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public boolean isMenuSame(Order order) {
return order.getMenu().equals(this.menu);
}

public void orderMore(Order order) {
this.count = count.addCount(order.getCount());
}

public boolean isChicken() {
return menu.isChicken();
}

public Menu getMenu() {
return menu;
}

private Count getCount() {
return count;
}

public int getCountNumber() {
return count.getCount();
}

public int getTotalPrice() {
return menu.getPrice() * count.getCount();
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}
56 changes: 56 additions & 0 deletions src/main/java/domain/Orders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Orders {
public static final int CHICKEN_SET_UNIT = 10;
private final List<Order> orders = new ArrayList<>();
Comment on lines +7 to +9
Copy link
Member

Choose a reason for hiding this comment

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

Orders의 자료구조를 List를 사용하고 있습니다.
여러개의 주문이 있을때, 각 주문한 메뉴들을 찾아가는 경우가 많을걸로 예상되는데요. (추가 주문)
다른 자료구조를 사용해보는건 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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

어차피 중복 허용도 아니고 순서가 중요한 것도 아니니까 성능상 List보단 Set이 나을 것 같네요.
아마 앨런의 의도는 Map이겠죠? 처음 구현할 땐 Map이랑 고민을 했었어요!
검색에 대한 성능은 메뉴를 기준으로 탐색하는 Map이 더 좋긴 하겠지만,
map(Map<Menu, Count>)으로 관리를 하게 되면 order 클래스에서 하는 일을 orders 에서 해야 하기도 하고
주문한 종류마다 따로 관리를 하고 싶어서 메뉴과 수량을 갖고 있는 Order객체를 따로 만들었습니다.
그래서 결과적으로 Map을 사용하지 않게 되었습니다. Set을 사용하게 되면 성능상으로도 비슷하지 않을까요...?

YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public Orders() {
}
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public void orderMenu(Order order) {
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
if (isNotOrderedMenu(order)) {
orders.add(order);
return;
}
orders.stream()
.filter(menu -> menu.isMenuSame(order))

Choose a reason for hiding this comment

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

order를 넘기지 않고 order에 있는 menu만 넘기는게 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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

어떻게 하든 getMenu()를 호출하는건 똑같아서 order를 넘기나, menu를 넘기나 똑같은데...
우선 저는 같은 Order끼리 비교하고 싶어서 order를 넘겨준 것인데
자연스러운건 menu를 넘기는 게 자연스러워 보이네요.
흠... 뭐가 더 좋은건지는 잘 모르겠네요 ㅜㅜ

.findFirst()
.orElseThrow(() -> new IllegalArgumentException("잘못된 주문입니다."))
.orderMore(order);
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isNotOrderedMenu(Order order) {
return orders.stream()
.noneMatch(menu -> menu.isMenuSame(order));
}

public void resetOrder() {
orders.clear();
}

public int calculateChickenSet() {
int chickenCount = orders.stream()
.filter(Order::isChicken)
.mapToInt(Order::getCountNumber)
.sum();
return chickenCount / CHICKEN_SET_UNIT;
}

public boolean isOrderEmpty() {
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
return orders.isEmpty();
}

//Todo 이름마음에안듬
YerinCho marked this conversation as resolved.
Show resolved Hide resolved
YerinCho marked this conversation as resolved.
Show resolved Hide resolved

public int getOrderedMenuSize() {
return orders.size();
}

public List<Order> getOrders() {
return Collections.unmodifiableList(orders);
}
}
Loading