import java.util.*;
enum TYPE { NORMAL,CASH_DISCOUNT,CASH_RETURN};
interface Cashsuper {
public double acceptCash(double money);
}
class CashNormal implements CashSuper{// 正常收费子类
public double accptCash(double money){
return money;
}
}
class CashDiscount implements Cashsuper {
private double moneyDiscount; //折扣率
public CashDiscount(double moneyDiscount){
this moneyDiscount = moneyDiscount;
}
public double acceptCash(double money){
return money* moneyDiscount;
}
}
class CashReturn implements Cashsuper { //满额返利
private double moneyCondition;
private double moneyReturn;
public CashReturn(double moneyCondition, double moneyReturn){
this.moneycondition =moneyCondition;//满额数额
this.moneyReturn =moneyReturn;//返利数额
}
public double acceptCash(double money){
double result = money;
if (money >= moneyCondition )
result=money-Math.floor(money/moneyCondition )* moneyReturn;
return result;
}
}
class CashContext{
private Cashsuper cs;
private TYPE t;
public CashContext(TYPE t){
switch(t){
case NORMAL;// 正常收费
cs= new CashNormal();
break;
case CASH_RETURN;//满300返100
cs = new CashReturn(300,100);
break;
case CASH_DISCOUNT;//打8折
cs =new CashDiscount(0.8);
break;
}
}
public double GetResult(double money){
return cs.acceptCash(money);
}
//此处略去main()函数:选择哪一种策略,输入金额
}
策略模式解析
程序吟游2024-05-31 16:31
相关推荐
Chrikk16 分钟前
Go-性能调优实战案例幼儿园老大*19 分钟前
Go的环境搭建以及GoLand安装教程canyuemanyue19 分钟前
go语言连续监控事件并回调处理杜杜的man21 分钟前
【go从零单排】go语言中的指针测开小菜鸟23 分钟前
使用python向钉钉群聊发送消息P.H. Infinity1 小时前
【RabbitMQ】04-发送者可靠性生命几十年3万天1 小时前
java的threadlocal为何内存泄漏caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction萧鼎2 小时前
Python并发编程库:Asyncio的异步编程实战学地理的小胖砸2 小时前
【一些关于Python的信息和帮助】