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

[앨런] 치킨집 미션 제출합니다. #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# java-chicken-2019
# 시나리오
0. 기능을 입력받는다.
1. 주문등록
- 테이블을 보여주고 입력받는다.
- 등록된 테이블을 골라야한다.
- 메뉴를 보여주고 메뉴 번호를 입력받는다.
- 등록된 메뉴 번호여야 한다.
- 메뉴의 수량을 입력받는다.
- 기존 수량 + 입력한 수량 값이 99를 넘지 않는다.
2. 결제하기
- 테이블을 보여주고 테이블을 입력받는다.
- 주문이 있는 테이블을 골라야한다.
- 주문내역을 보여주고, 결제 방식을 입력받는다.
- 치킨은 10마리당 만원씩 할인된다.
- 추가로 현금결제시 5% 할인된다.
- 최종 결제금액을보여준다.
3. 게임 종료
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ repositories {
mavenCentral()
}

test {
useJUnitPlatform()
}

dependencies {
testCompile('org.junit.jupiter:junit-jupiter:5.5.2')
testCompile('org.assertj:assertj-core:3.14.0')
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import domain.Menu;
import domain.MenuRepository;
import domain.Table;
import domain.TableRepository;
import java.util.Scanner;

import controller.ChickenController;
import view.InputView;
import view.OutputView;

import java.util.List;

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) {
new ChickenController(new InputView(new Scanner(System.in)), new OutputView()).run();
}
}
59 changes: 59 additions & 0 deletions src/main/java/controller/ChickenController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package controller;

import domain.PosMenu;
import domain.payment.PaymentType;
import domain.table.Table;
import domain.table.TableRepository;
import domain.table.Tables;
import domain.table.order.OrderCount;
import domain.table.order.menu.Menu;
import domain.table.order.menu.MenuRepository;
import domain.table.order.menu.Menus;
import view.InputView;
import view.OutputView;

public class ChickenController {
private final InputView inputView;
private final OutputView outputView;

public ChickenController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
outputView.printMain();

Menus menus = new Menus(MenuRepository.menus());
Tables tables = new Tables(TableRepository.tables());

while (true) {

Choose a reason for hiding this comment

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

하핫....말 안해도...아시죠??😂😂
depth, indent...화이팅 하십쇼!!!

Copy link

Choose a reason for hiding this comment

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

😉

int function = inputView.inputSelectFunction();
PosMenu posMenu = PosMenu.of(function);
if (PosMenu.ORDER == posMenu) {
outputView.printTables(tables);
Table table = tables.findTable(inputView.inputTableNumber());

outputView.printMenus(menus);
Menu menu = menus.findMenu(inputView.inputSelectMenu());

OrderCount orderCount = OrderCount.of(inputView.inputMenuAmount());

table.addOrder(menu, orderCount);
} else if (PosMenu.PAYMENT == posMenu) {
outputView.printTables(tables);
Table table = tables.findOrderedTable(inputView.inputTableNumber());

outputView.printOrderHistory(table);
PaymentType paymentType = PaymentType.of(inputView.inputSelectPayment(table));

long price = paymentType.calculatePrice(table.getOrder());
outputView.printPrice(price);

table.clear();
} else if (PosMenu.EXIT == posMenu) {
System.exit(0);
}

Choose a reason for hiding this comment

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

함수(또는메서드)의 길이가 15라인을 넘어가지 않도록 구현한다.

  • 함수(또는메서드)가 한가지 일만 잘 하도록 구현한다.
  • else예약어를 쓰지 않는다.
    +힌트: if조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다.
  • else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다.

}
}
}
17 changes: 0 additions & 17 deletions src/main/java/domain/Category.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/domain/Menu.java

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/java/domain/MenuRepository.java

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/domain/PosMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package domain;

import java.util.Arrays;

public enum PosMenu {
ORDER(1),
PAYMENT(2),
EXIT(3);

private final int function;

PosMenu(int function) {
this.function = function;
}

public static PosMenu of(int functionNumber) {
return Arrays.stream(values())
.filter(posMenu -> posMenu.function == functionNumber)
.findAny()

Choose a reason for hiding this comment

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

findFirst()가 더 좋지 않을까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

1개의 요소를 찾을때라서 순서가 상관없는데요.
findany()가 성능이 안좋은건가요?

.orElseThrow(() -> new IllegalArgumentException("찾을 수 없는 메뉴입니다. functionNumber=" + functionNumber));
}
}
14 changes: 0 additions & 14 deletions src/main/java/domain/Table.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/domain/TableRepository.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/domain/payment/CacheDiscountStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package domain.payment;

import domain.table.order.Order;

public class CacheDiscountStrategy implements DiscountStrategy {
private static final double DISCOUNT_RATE = 0.95;

@Override
public long calculate(long money, Order order) {
return (long)((double)money * DISCOUNT_RATE);
}
}
14 changes: 14 additions & 0 deletions src/main/java/domain/payment/ChickenDiscountStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain.payment;

import domain.table.order.Order;

public class ChickenDiscountStrategy implements DiscountStrategy {
private static final int DISCOUNT_PRICE = 10000;

@Override
public long calculate(long money, Order order) {
int count = order.calculateChickenCount();
int discount = (count / 10) * DISCOUNT_PRICE;
return discount;
}
}
7 changes: 7 additions & 0 deletions src/main/java/domain/payment/DiscountStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package domain.payment;

import domain.table.order.Order;

public interface DiscountStrategy {
long calculate(long money, Order order);
}
60 changes: 60 additions & 0 deletions src/main/java/domain/payment/PaymentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package domain.payment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import domain.table.order.Order;

public enum PaymentType {
CARD(1, Arrays.asList(new ChickenDiscountStrategy())),
CACHE(2, Arrays.asList(new ChickenDiscountStrategy()), Arrays.asList(new CacheDiscountStrategy()));

private final int type;
private final List<DiscountStrategy> totalDiscountStrategies;
Copy link

Choose a reason for hiding this comment

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

제 생각에는 PaymentType에서 totalDiscountStrategies를 가지는 것보다 afterDiscountStrategies만 가지고 있는 편이 좋을 것 같다는 생각이 드는데 이 의견에 대해서는 어떻게 생각하시나요? PaymentType에 따라서 totalDiscountStrategies가 바뀌는게 아닐 것 같아서요.

private final List<DiscountStrategy> afterDiscountStrategies;

PaymentType(int type, List<DiscountStrategy> totalDiscountStrategies) {
this.type = type;
this.totalDiscountStrategies = totalDiscountStrategies;
this.afterDiscountStrategies = new ArrayList<>();
}

PaymentType(int type, List<DiscountStrategy> totalDiscountStrategies,
List<DiscountStrategy> afterDiscountStrategies) {
this.type = type;
this.totalDiscountStrategies = totalDiscountStrategies;
this.afterDiscountStrategies = afterDiscountStrategies;
}

public static PaymentType of(int type) {
return Arrays.stream(values())
.filter(paymentType -> paymentType.type == type)
.findAny()
.orElseThrow(() -> new IllegalArgumentException("찾을 수 없는 결제방식입니다. type =" + type));
}

public long calculatePrice(Order order) {
long money = order.calculateTotalMoney();
money = preDiscount(order, money);
return afterDiscount(order, money);
}

private long afterDiscount(Order order, long money) {
for (DiscountStrategy discountStrategy : afterDiscountStrategies) {
money = discountStrategy.calculate(money, order);
}
return money;
}
Comment on lines +37 to +48
Copy link

Choose a reason for hiding this comment

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

현재 DiscountStrategy를 사용하고 있는 방향성이 두가지로(할인액 자체를 구하는 경우, 할인 후 총가격을 구하는 경우) 나뉜 것 같습니다. 사용자에게 혼란을 줄 수 있을 것 같은데 어떻게 생각하시나요?


private long preDiscount(Order order, long money) {
long weight = 0;

for (DiscountStrategy discountStrategy : totalDiscountStrategies) {
weight += discountStrategy.calculate(money, order);
}

money -= weight;
return money;
}
}
40 changes: 40 additions & 0 deletions src/main/java/domain/table/Table.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package domain.table;

import domain.table.order.Order;
import domain.table.order.OrderCount;
import domain.table.order.menu.Menu;

public class Table {
private final int number;
private final Order order;

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

public void addOrder(Menu menu, OrderCount orderCount) {
order.add(menu, orderCount);
}

@Override
public String toString() {
return Integer.toString(number);
}

public int getNumber() {
return number;
}

public Order getOrder() {
return order;
}

public void clear() {
order.clear();
}

public boolean isEmpty() {
return order.isEmpty();
}
}
Loading