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

[치킨집] 히히 미션 제출합니다! #78

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# java-chicken-2019
# java-chicken-2019

## 기능목록
Pos 기 기능 출력하기
원하는 기능 선택하기
테이블 목록 출력하기 (주어짐)
테이블 선택하기 (주어짐)
메뉴 출력하기 (주어짐)
등록할 메뉴 선택하기
메뉴의 수량 입력하기
주문내역 출력하기
카드결제 할지 현금결제 할지 선택하기
최종 결제할 금액 출력하기
주문 정보 등록하기
결제 금액 계산하기
할인율 계산하기
테이블 별로 주문 내역 관리하기
테이블 당 주문 내역 관리하기
결제 완료된 테이블 결제 완료 처리하기

## 객체 설계
- Menu
- 아는 것 : 메뉴 자신의 번호, 이름, 종류, 가격
- PaymentMethod
- 아는 것 : 지불 방식 스스로에 대해 대응되는 번호(code)
- PosFunction
- 아는 것 : pos 기계의 기능 스스로에 대해 대응되는 번호(code), 자신의 한국어 이름
- Table
- 아는 것 : 주문내역,
- 하는 것 : 자신에 대한 주문내역 등록, 결제하기
19 changes: 3 additions & 16 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
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) {
Controller.run();
}
}
108 changes: 108 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package controller;

import domain.Menu;
import domain.MenuRepository;
import domain.PaymentMethod;
import domain.PosFunction;
import domain.Table;
import domain.TableRepository;
import java.util.List;
import java.util.stream.Collectors;
import view.IllegalUserInputException;
import view.InputView;
import view.OutputView;

public class Controller {

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

public static void run() {
try {
runWithoutExceptionCatch();
} catch (Exception e) {
OutputView.printExceptionMessage(e.getMessage());
}
}

private static void runWithoutExceptionCatch() {
PosFunction posFunction;

do {
OutputView.printPosFunctions(PosFunction.getPosFunctionsByCodeKoreanMap());
posFunction = PosFunction.of(InputView.inputPosFunction());

runOnce(posFunction);
} while (posFunction != PosFunction.EXIT);
}

private static void runOnce(PosFunction posFunction) {
if (posFunction == PosFunction.ORDER_REGISTRATION) {
registerOrder();
}
if (posFunction == PosFunction.PAYMENT) {
pay();
}
}

private static void registerOrder() {
OutputView.printTables(getTableNumbers(), getOrderedTableNumbers());
final Table table = readTable();

OutputView.printMenus(menus);
final Menu menuToRegister = readMenu();

table.registerMenu(menuToRegister, InputView.inputMenuCount());
}

private static List<Integer> getTableNumbers() {
return tables.stream()
.mapToInt(Table::getNumber)
.boxed()
.collect(Collectors.toList());
}

private static List<Integer> getOrderedTableNumbers() {
return tables.stream()
.filter(Table::hasOrdered)
.mapToInt(Table::getNumber)
.boxed()
.collect(Collectors.toList());
}

private static Table readTable() {
int tableNumber = InputView.inputTableNumber();

return tables.stream()
.filter(table -> table.isNumber(tableNumber))
.findFirst()
.orElseThrow(() -> new IllegalUserInputException("존재하지 않는 테이블입니다."));
}

private static Menu readMenu() {
int menuNumber = InputView.inputMenuNumber();

return menus.stream()
.filter(targetMenu -> targetMenu.isNumber(menuNumber))
.findFirst()
.orElseThrow(() -> new IllegalUserInputException("존재하지 않는 메뉴입니다."));
}

private static void pay() {
OutputView.printTables(getTableNumbers(), getOrderedTableNumbers());
final Table table = readTable();
table.validatePayable();

OutputView.printOrderHistory(table.getOrderHistory());

final PaymentMethod paymentMethod = readPaymentMethodOf(table);

OutputView.printFinalPaymentAmount(table.getPaymentAmount());
table.pay(paymentMethod);
}

private static PaymentMethod readPaymentMethodOf(Table table) {
OutputView.printMakingPaymentMessage(table.getNumber());
return PaymentMethod.of(InputView.inputPaymentMethod());
}
}
12 changes: 12 additions & 0 deletions src/main/java/domain/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public Menu(final int number, final String name, final Category category, final
this.price = price;
}

public boolean isNumber(final int number) {
return this.number == number;
}

public String getName() {
return this.name;
}

public int getPrice() {
return this.price;
}

@Override
public String toString() {
return category + " " + number + " - " + name + " : " + price + "원";
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/domain/PaymentMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package domain;

import java.util.Arrays;

public enum PaymentMethod {
CREDIT_CARD(1),
CASH(2);

private int code;

PaymentMethod(int code) {
this.code = code;
}

public static PaymentMethod of(int code) {
return Arrays.stream(values())
.filter(paymentMethod -> paymentMethod.code == code)
.findFirst()
.orElseThrow(() ->
new IllegalArgumentException("존재하지 않는 결제방식 번호입니다."));
}
}
46 changes: 46 additions & 0 deletions src/main/java/domain/PosFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package domain;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public enum PosFunction {
ORDER_REGISTRATION(1, "주문등록"),
PAYMENT(2, "결제하기"),
EXIT(3, "프로그램 종료");

private int code;
private String korean;

PosFunction(int code, String korean) {
this.code = code;
this.korean = korean;
}

public static PosFunction of(int code) {
return Arrays.stream(PosFunction.values())
.filter(posFunction -> posFunction.code == code)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 Pos 기능번호입니다."));
}

public int getCode() {
return this.code;
}

public static Map<Integer, String> getPosFunctionsByCodeKoreanMap() {
Map<Integer, String> posFunctions = new HashMap<>();

Arrays.stream(PosFunction.values())
.forEach(posFunction ->
posFunctions.put(posFunction.code, posFunction.korean));

return Collections.unmodifiableMap(posFunctions);
}

@Override
public String toString() {
return this.korean;
}
}
47 changes: 47 additions & 0 deletions src/main/java/domain/Table.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
package domain;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Table {

private final int number;
private final Map<Menu, Integer> orderHistory = new HashMap<>();

public Table(final int number) {
this.number = number;
}

public void registerMenu(Menu menu, int menuCount) {
if (!orderHistory.containsKey(menu)) {
orderHistory.put(menu, 0);
}
orderHistory.put(menu, orderHistory.get(menu) + menuCount);
}

public boolean hasOrdered() {
return !orderHistory.isEmpty();
}

public Map<Menu, Integer> getOrderHistory() {
return Collections.unmodifiableMap(orderHistory);
}

public int getPaymentAmount() {
validatePayable();
return orderHistory.keySet().stream()
.mapToInt(Menu::getPrice)
.sum();
}

public void pay(PaymentMethod paymentMethod) {
validatePayable();
this.orderHistory.clear();
}

public void validatePayable() {
if (!hasOrdered()) {
throw new IllegalStateException("결제할 주문내역이 없습니다.");
}
}

public boolean isNumber(int number) {
return this.number == number;
}

public int getNumber() {
return this.number;
}

@Override
public String toString() {
return Integer.toString(number);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/view/IllegalUserInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package view;

public class IllegalUserInputException extends RuntimeException {

public IllegalUserInputException() {
}

public IllegalUserInputException(String message) {
super(message);
}
}
33 changes: 31 additions & 2 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@
import java.util.Scanner;

public class InputView {

private static final Scanner scanner = new Scanner(System.in);

public static int inputPosFunction() {
System.out.println("## 원하는 기능을 선택하세요.");
return readIntegerByOneLine();
}

public static int inputTableNumber() {
System.out.println("## 주문할 테이블을 선택하세요.");
return scanner.nextInt();
System.out.println("## 테이블을 선택하세요.");
return readIntegerByOneLine();
}

public static int inputMenuNumber() {
System.out.println("## 등록할 메뉴를 선택하세요.");
return readIntegerByOneLine();
}

public static int inputMenuCount() {
System.out.println("## 메뉴의 수량을 선택하세요.");
return readIntegerByOneLine();
}

public static int inputPaymentMethod() {
System.out.println("## 신용카드는 1번, 현금은 2번");
return readIntegerByOneLine();
}

private static int readIntegerByOneLine() {
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
throw new IllegalUserInputException("정수만 입력 가능합니다.");
}
}
}
Loading