public class CashContext {
private CashSuper cs;
/**
* 使用构造方法来进行选择具体的收费策略
*
* @param cashSuper
*/
public CashContext(CashSuper cashSuper) {
this.cs = cashSuper;
}
public Double getResult(Double money){
return cs.acceptCash(money);
}
}
public abstract class CashSuper {
/**
* 接收现金支付的方法
* @param money
* @return
*/
public abstract Double acceptCash(Double money);
}
public class CashNormal extends CashSuper {
/**
* 正常收费,即是不参与任何打折活动,故此直接返回当前收费现金
*
* @param money
* @return
*/
@Override
public Double acceptCash(Double money) {
return money;
}
}
public class CashRebate extends CashSuper {
private Double moneyRebate =1D;
/**
* 使用构造器来进行初始化打折率,构造方法的好处就是当初始化这个类的时候就会初始化好当前这个类的字段
*
* @param moneyRebate
*/
public CashRebate(String moneyRebate) {
this.moneyRebate = Double.parseDouble(moneyRebate);
}
@Override
public Double acceptCash(Double money) {
return money *moneyRebate;
}
}
public class CashReturn extends CashSuper {
private Double moneyCondition =0.0D;//返利的条件
private Double moneyReturn =0.0D;//满足多少条件之后返利多少
/**
* 使用构造方法来初始化返利的条件和达到返利条件之后的返利钱数
*
* @param moneyCondition
* @param moneyReturn
*/
public CashReturn(String moneyCondition, String moneyReturn) {
this.moneyCondition = Double.parseDouble(moneyCondition);
this.moneyReturn = Double.parseDouble(moneyReturn);
}
/**
* 判断如果当前的钱数已经大于需要返利的钱数,那就使用Math.floor方法格式化小数点后的小数,是一个取整数的方法
*
* @param money
* @return
*/
@Override
public Double acceptCash(Double money) {
Double result = money;
if (money >=moneyCondition) {
result = money - Math.floor(money /moneyCondition) *moneyReturn;
}
return result;
}
}
/**
* 使用策略模式来执行操作,是用工厂设计模式的缺陷就是算法会跟客户端的交互耦合在一起了,使用策略模式的好处就是相当于在工厂模式的外围再增加了一层保护套
*
* @param model
* @param price
* @param number
* @param type
* @return
*/
@RequestMapping("strategy")
public String strategy(Model model, Double price, Double number, String type) {
Double currentprice = price * number;
CashContext cc =null ;
switch (type){
case "正常收费" :
cc =new CashContext(new CashNormal());
break;
case "满300返100" :
cc =new CashContext(new CashReturn("300","100"));
break;
case "打8折" :
cc =new CashContext(new CashRebate("0.8"));
break;
}
Double totalPrice =0D;
totalPrice = cc.getResult(currentprice);
model.addAttribute("result", totalPrice);
model.addAttribute("detail","单价:" + price +"\n" +"数量:" + number +"\n" +"应付:"
+ currentprice +"\n" +"实付" + totalPrice);
return "login";
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商场收银台</title>
<body>
<form action="/calculate" method="post" >
单价:<input type="text" size="30" name="price"><br>
数量:<input type="text" size="30" name="number"><br>
<select name="type">
<option value="正常收费">正常收费</option>
<option value="打8折">打8折</option>
<option value="满300返100">满300返100</option>
</select><br>
<input type="submit" value="comfirm"><br>
<textarea rows="5" cols="20" >
${(detail)!""}
</textarea><br>
总计:<input type="text" value="${(result)!""}" readonly><br>
</form>
<script type="text/javascript">
$(function () {
$("#reset").click(){
$("input").val("");
}
});
</script>
</body>
</html>