简介

迷你版的交易系统,最最最最简单的。。

扣款子模块

卡上有两个账户,分别是自有账户,和固定账户,简化为自由余额,固定余额,两种余额方式。
扣款有两种方式

扣款方式一

两个账户同时扣款一半。
固定余额 = 固定余额 - 交易金额/2
自由余额 = 自由金额 - 交易金额/2
即,各自扣除一半

扣款方式二

全部从自由金额上扣除。

简单复习

策略模式

扣款可以使用策略模式进行扣款。
策略模式是把原先耦合的if拆分成为类,把条件通过注入的方式,达到条件模式。

状态模式,责任链模式

交易扣款的模式是固定的,要根据交易编号来定,交易编号和扣款策略如何对应,使用责任链和状态模式。

UML

代码如下

package ming.Deduction;


import ming.entity.*;

// 扣款
public class DeductionContext {
    // 扣款
    private IDeduction deduction = null;
    // 传递
    public DeductionContext(IDeduction _deduction){
        this.deduction = _deduction;
    }
    // 执行扣款
    public boolean exec(Card card, Trade trade){
        return this.deduction.exec(card, trade);
    }
}

package ming.Deduction;


import ming.entity.*;

// 对扣款模块进行封装
public class DeductionFacade {
    // 扣款信息公布
    public static Card deduct(Card card, Trade trade){
        // 获得消费策略,进行那种消费,实现那种扣款方式
        StrategyMan reg = getDeductionType(trade);
        // 初始化对象
        IDeduction deduction = StrategyFactory.getDeduction(reg);
        // 获取扣款上下文
        DeductionContext context = new DeductionContext(deduction);
        // 进行扣款
        context.exec(card, trade);
        // 返回数据
        return card;
    }

    // 获得对应的商户的消费,即从配置文件中获取那个进行消费
    // 根据交易码进行选择那种扣款方式,交易码是18位的
    private static StrategyMan getDeductionType(Trade trade) {
        String trandeNo = new String(trade.getTradeNo());
        // 模块操作
        if(trandeNo.contains("010")){
            return StrategyMan.FreeDeduction;
        }else{
            return StrategyMan.SteadyDeduction;
        }
    }
}

package ming.Deduction;
import ming.entity.*;

// 自由扣款
public class FreeDeduction implements IDeduction {
    // 自由扣款
    @Override
    public synchronized boolean exec(Card card, Trade trade) {
        int freeDeduction = card.getFreeMoney() - trade.getAmout();
        if(freeDeduction < 0){
            return false;
        }
        card.setFreeMoney(freeDeduction);
        return true;
    }
}

package ming.Deduction;


import ming.entity.*;

// 扣款策略接口
public interface IDeduction {
    // 扣款
    public boolean exec(Card card, Trade trade);
}

package ming.Deduction;

import ming.entity.*;

// 固定扣款方式
public class SteadyDeduction implements IDeduction{
    // 固定扣款
    @Override
    public synchronized boolean  exec(Card card, Trade trade) {
        int halfMoney = (int)Math.rint(trade.getAmout() / 2.0);
        int freeMoney = card.getFreeMoney() - halfMoney;
        // 余额不足
        if(freeMoney < 0){
            return false;
        }
        int stateMoney = card.getSteadyMoney() - halfMoney;
        // 额度不足
        if(stateMoney < 0){
            return false;
        }
        // 扣款
        card.setSteadyMoney(stateMoney);
        card.setFreeMoney(freeMoney);
        return true;
    }
}

package ming.Deduction;

// 策略工厂
public class StrategyFactory {
    public static IDeduction getDeduction(StrategyMan strategyMan){
        IDeduction deduction = null;
        try{
            deduction =  (IDeduction) Class.forName(strategyMan.getValue()).newInstance();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return deduction;
    }
}

package ming.Deduction;

// 配置类
public enum StrategyMan {
    SteadyDeduction("ming.Deduction.SteadyDeduction"),
    FreeDeduction("ming.Deduction.FreeDeduction");
    String value = "";
    private StrategyMan(String _value){
        this.value = _value;
    }
    public String getValue(){
        return this.value;
    }
}

package ming.entity;
// IC卡类
public class Card {
    // IC卡号码
    private String cardNo = "";
    // 卡内金额
    private int steadyMoney = 0;
    // 自由交易金额
    private int freeMoney = 0;
    // get/set
    public String getCardNo(){
        return this.cardNo;
    }
    public void setCardNo(String cardNo){
        this.cardNo = cardNo;
    }
    public void setSteadyMoney(int steadyMoney){
        this.steadyMoney = steadyMoney;
    }
    public int getSteadyMoney(){
        return this.steadyMoney;
    }
    public int getFreeMoney(){
        return freeMoney;
    }
    public void setFreeMoney(int freeMoney){
        this.freeMoney = freeMoney;
    }
}

package ming.entity;
// 交易类
public class Trade {
    // 编号
    private byte[] tradeNo = new byte[18];
    // 金额
    private int amout = 0;
    // get set
    public byte[] getTradeNo(){
        return this.tradeNo;
    }
    public void setTradeNo(byte[] tradeNo){
        this.tradeNo = tradeNo;
    }
    public int getAmout(){
        return this.amout;
    }
    public void setAmout(int amout){
        this.amout = amout;
    }
}