package strategy;
/**
*
* @author Andy
*
*/
public class Apple implements Discountable {
//重量
private double weight;
//单价 实际开发中 设计金钱等精确计算都是BigDecimal;
private double price;
//按购买量打折
// private Discountor d = new AppleWeightDiscountor();
//按购买总价打折
private Discountor d = new ApplePriceDiscountor();
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Apple (double weight,double price ){
super();
this.weight=weight;
this.price=price;
}
@Override
public void discountSell() {
d.discount(this);
}
}
package strategy;
/**
*
* @author Andy
*
*/
public class Banana implements Discountable {
//重量
private double weight;
////单价 实际开发中 涉及金钱等精确计算都是用BigDecimal
private double price;
public Banana(double weight, double price) {
super();
this.weight = weight;
this.price = price;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public void discountSell() {
//打折算法
if(weight < 5) {
System.out.println("Banana未打折价钱: " + weight * price);
}else if(weight >= 5 && weight < 10) {
System.out.println("Banana打八八折价钱: " + weight * price * 0.88 );
}else if(weight >= 10) {
System.out.println("Banana打五折价钱: " + weight * price * 0.5 );
}
}
}
package strategy;
/**
*
* @author Andy
*
*/
public class Market {
/**
* 对可打折的一类事物进行打折
* @param apple
*/
public static void discountSell(Discountable d) {
d.discountSell();
}
}
package strategy;
/**
*
* @author Andy
*
*/
public interface Discountable {
public void discountSell();
}
package strategy;
/**
*
* @author Andy
*
*/
public class Test {
/**
*
* @param args
*/
public static void main(String[] args) {
// 只能对苹果打折 还不能对通用的一类事物打折 而且都是要卖什么就写什么打折算法
// 其实每类事物打折算法又是不一致的
Discountable d = new Apple(10.3, 3.6);
Discountable d1= new Banana(5.4,1.1);
Market.discountSell(d);
Market.discountSell(d1);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有