Skip to content

Commit

Permalink
[WM] refactor: add the interface for the discount strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
GraceeFlower committed Feb 17, 2020
1 parent 3490196 commit ea97232
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/thoughtworks/DiscountStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thoughtworks;

import java.util.ArrayList;

public interface DiscountStrategy {

String getDiscountInfo(ArrayList<Dish> menu);
}
15 changes: 15 additions & 0 deletions src/main/java/com/thoughtworks/FullOffStrategy.java
Original file line number Diff line number Diff line change
@@ -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<Dish> menu) {
return "满30减6元,省6元\n";
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/thoughtworks/HalfOffStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thoughtworks;

import java.util.ArrayList;

public class HalfOffStrategy implements DiscountStrategy {

public int getDiscountMoney(ArrayList<Dish> 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<Dish> 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();
}
}
12 changes: 0 additions & 12 deletions src/main/java/com/thoughtworks/OrderRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ public OrderRender(ArrayList<Dish> menu, int[] subtotal, String strategy) {
this.strategy = strategy;
}

// public ArrayList<Dish> 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++) {
Expand Down
48 changes: 10 additions & 38 deletions src/main/java/com/thoughtworks/Restaurant.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,20 @@ public int[] getSubtotal(ArrayList<Dish> menu) {
}

public String getStrategy(ArrayList<Dish> menu, int[] subtotal) {
ArrayList<Integer> 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");
Expand All @@ -53,26 +47,4 @@ public int calculateTotal(int[] subtotal) {
}
return total;
}

public ArrayList<Integer> calculateHalfOff(ArrayList<Dish> menu) {
ArrayList<Integer> 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;
}
}

0 comments on commit ea97232

Please sign in to comment.