策略模式结构

策略模式结构

策略A:直接打九折
策略B:满200之后全部打八五折
策略C:满200之后的价格打七折处理

结构

定义异常

考虑到输入金额会出错,自定义一个异常

public class AmountOutOfArangeException extends Exception{
    public AmountOutOfArangeException(String message){
        super(message);
    }
}

定义一个折扣接口

public interface Discount {
     void discount(double amount) throws AmountOutOfArangeException;
}

定义折扣策略

  1. 折扣A
public class DiscountA implements Discount{
    @Override
    public void discount(double amount) throws AmountOutOfArangeException{
        if(amount<=0)
            throw new AmountOutOfArangeException("至少买点吧");
        else{
            double money;
            money = amount*0.9;
            System.out.println("使用A策略实际花费"+money+"元");
        }
    }
}
  1. 折扣B
public class DiscountB implements Discount{
    @Override
    public void discount(double amount) throws AmountOutOfArangeException{
        if(amount<=0){
            throw new AmountOutOfArangeException("至少买点吧། – _ – །");
        }
        else if(amount<=200) {
            throw new AmountOutOfArangeException("不满足策略B实际情况!(ノ`⊿´)ノ");
        }
        else{
            double money;
            money = amount*0.85;
            System.out.println("使用B策略实际花费"+money+"元");
        }
    }
}
  1. 折扣C
public class DiscountC implements Discount {
    public void discount(double amount) throws AmountOutOfArangeException{
        if(amount<=0){
            throw new AmountOutOfArangeException("至少买点吧། – _ – །");
        }
        else if(amount<=200) {
            throw new AmountOutOfArangeException("不如用策略A(°ー°〃)");
        }
        else{
            double money;
            money = 200+(amount-200)*0.7;
            System.out.println("使用C策略实际花费"+money+"元");
        }
    }
}

折扣实现服务

public class DiscountService {

    public void discountService(Discount discount, double amount) {
        try {
            discount.discount(amount);
        } catch (AmountOutOfArangeException e) {
            System.out.println("金额无效"+e.getMessage());
        }catch (Exception e){
            System.out.println("未知错误"+e.getMessage());
        }
    }
}

实践!

public class DiscountTest {
    public static void main(String[] args) {
        int amount;
        String strategy;
        Scanner scanner = new Scanner(System.in);
        DiscountService discountpay = new DiscountService();
        for(int i = 0;i < 5;i++){
            System.out.println("请输入你的金额与你选择的折扣策略");
            amount = scanner.next();
            strategy = scanner.nextLine();
            switch (strategy){
                case("A")->{
                    discountpay.discountService(new DiscountA(),amount);
                }
                case("B")->{
                    discountpay.discountService(new DiscountB(),amount);
                }
                case("C")->{
                    discountpay.discountService(new DiscountC(),amount);
                }
            }
        }
    }
}

结果

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值