From ea9723268388efae421dd5da3c3beb8fcf75505e Mon Sep 17 00:00:00 2001 From: GraceeFlower <1491644103@qq.com> Date: Mon, 17 Feb 2020 19:27:37 +0800 Subject: [PATCH] [WM] refactor: add the interface for the discount strategies --- .../com/thoughtworks/DiscountStrategy.java | 8 ++++ .../com/thoughtworks/FullOffStrategy.java | 15 ++++++ .../com/thoughtworks/HalfOffStrategy.java | 29 +++++++++++ .../java/com/thoughtworks/OrderRender.java | 12 ----- .../java/com/thoughtworks/Restaurant.java | 48 ++++--------------- 5 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 src/main/java/com/thoughtworks/DiscountStrategy.java create mode 100644 src/main/java/com/thoughtworks/FullOffStrategy.java create mode 100644 src/main/java/com/thoughtworks/HalfOffStrategy.java diff --git a/src/main/java/com/thoughtworks/DiscountStrategy.java b/src/main/java/com/thoughtworks/DiscountStrategy.java new file mode 100644 index 0000000..116cb0a --- /dev/null +++ b/src/main/java/com/thoughtworks/DiscountStrategy.java @@ -0,0 +1,8 @@ +package com.thoughtworks; + +import java.util.ArrayList; + +public interface DiscountStrategy { + + String getDiscountInfo(ArrayList menu); +} diff --git a/src/main/java/com/thoughtworks/FullOffStrategy.java b/src/main/java/com/thoughtworks/FullOffStrategy.java new file mode 100644 index 0000000..4080d18 --- /dev/null +++ b/src/main/java/com/thoughtworks/FullOffStrategy.java @@ -0,0 +1,15 @@ +package com.thoughtworks; + +import java.util.ArrayList; + +public class FullOffStrategy implements DiscountStrategy { + + public int getDiscountMoney() { + return 6; + } + + @Override + public String getDiscountInfo(ArrayList menu) { + return "满30减6元,省6元\n"; + } +} diff --git a/src/main/java/com/thoughtworks/HalfOffStrategy.java b/src/main/java/com/thoughtworks/HalfOffStrategy.java new file mode 100644 index 0000000..337de85 --- /dev/null +++ b/src/main/java/com/thoughtworks/HalfOffStrategy.java @@ -0,0 +1,29 @@ +package com.thoughtworks; + +import java.util.ArrayList; + +public class HalfOffStrategy implements DiscountStrategy { + + public int getDiscountMoney(ArrayList menu) { + int reducePrice = 0; + for (Dish dish : menu) { + if (DataProvider.getHalfDishIds().contains(dish.getId())) { + reducePrice += (int) dish.getPrice() / 2; + } + } + return reducePrice; + } + + @Override + public String getDiscountInfo(ArrayList menu) { + StringBuilder strategy = new StringBuilder("指定菜品半价("); + for (Dish dish : menu) { + if (DataProvider.getHalfDishIds().contains(dish.getId())) { + strategy.append(dish.getName()).append(","); + } + } + strategy.deleteCharAt(strategy.length() - 1).append("),省") + .append(this.getDiscountMoney(menu)).append("元\n"); + return strategy.toString(); + } +} diff --git a/src/main/java/com/thoughtworks/OrderRender.java b/src/main/java/com/thoughtworks/OrderRender.java index c55119b..f325ce1 100644 --- a/src/main/java/com/thoughtworks/OrderRender.java +++ b/src/main/java/com/thoughtworks/OrderRender.java @@ -13,18 +13,6 @@ public OrderRender(ArrayList menu, int[] subtotal, String strategy) { this.strategy = strategy; } -// public ArrayList getMenu() { -// return menu; -// } -// -// public int[] getSubtotal() { -// return subtotal; -// } -// -// public String getStrategy() { -// return strategy; -// } - public String renderReceipt() { StringBuilder receipt = new StringBuilder("============= 订餐明细 =============\n"); for (int i = 0; i < menu.size(); i++) { diff --git a/src/main/java/com/thoughtworks/Restaurant.java b/src/main/java/com/thoughtworks/Restaurant.java index 44f550b..17f04d6 100644 --- a/src/main/java/com/thoughtworks/Restaurant.java +++ b/src/main/java/com/thoughtworks/Restaurant.java @@ -20,26 +20,20 @@ public int[] getSubtotal(ArrayList menu) { } public String getStrategy(ArrayList menu, int[] subtotal) { - ArrayList halfOffInfo = calculateHalfOff(menu); int total = calculateTotal(subtotal); - int discount = calculateFullOff(total); StringBuilder strategy = new StringBuilder(); - if (discount > 0) { + FullOffStrategy fullOff = new FullOffStrategy(); + HalfOffStrategy halfOff = new HalfOffStrategy(); + if (30 <= total) { strategy.append("-----------------------------------\n").append("使用优惠:\n"); - if (discount < halfOffInfo.get(halfOffInfo.size() - 1)) { - total -= halfOffInfo.get(halfOffInfo.size() - 1); - strategy.append("指定菜品半价("); - int infoLen = halfOffInfo.size(); - for (int i = 0; i < infoLen - 1; i++) { - strategy.append(menu.get(halfOffInfo.get(i)).getName()); - if (i != infoLen - 2) { - strategy.append(","); - } - } - strategy.append("),省").append(halfOffInfo.get(infoLen - 1)).append("元\n"); + int fullReduce = fullOff.getDiscountMoney(); + int halfReduce = halfOff.getDiscountMoney(menu); + if (fullReduce < halfReduce) { + total -= halfReduce; + strategy.append(halfOff.getDiscountInfo(menu)); } else { - total -= discount; - strategy.append("满30减6元,省6元\n"); + total -= fullReduce; + strategy.append(fullOff.getDiscountInfo(menu)); } } strategy.append("-----------------------------------\n总计:").append(total).append("元\n"); @@ -53,26 +47,4 @@ public int calculateTotal(int[] subtotal) { } return total; } - - public ArrayList calculateHalfOff(ArrayList menu) { - ArrayList halfOffInfo = new ArrayList<>(); - int reducePrice = 0; - for (int i = 0; i < menu.size(); i++) { - Dish dish = menu.get(i); - if(DataProvider.getHalfDishIds().contains(dish.getId())) { - reducePrice += (int)dish.getPrice() / 2; - halfOffInfo.add(i); - } - } - halfOffInfo.add(reducePrice); - return halfOffInfo; - } - - public int calculateFullOff(int total) { - int reducePrice = 0; - if (30 <= total) { - reducePrice = 6; - } - return reducePrice; - } }