-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WM] refactor: add the interface for the discount strategies
- Loading branch information
1 parent
3490196
commit ea97232
Showing
5 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters