理财,预期收益

项目中,标的付款方式有,到期本息 、先息后本、等额本息、等额本金四种。

这就意味着客户端在计算预期收益时要根据这一状态执行不同的算法。

通过分析,
到期本息 、先息后本为第一种算法,
等额本息为第二种算法,
等额本金为第三种算法。

之后,在对这块代码进行了三次重构
重构1:将三种算法写到工具类里,对应三种静态方法。
重构2:将算法提取为接口,提供三种策略类。

重构3:通过简单工厂返回具体算法类。

预期收益接口

public interface Interest {

    /**
     * 预期收益
     *
     * @param invest   出借金额
     * @param yearRate 年化率
     * @param month    出借期限
     * @return
     */
    double getInterestCount(double invest, double yearRate, int month);
}

预期收益工厂

public interface InterestFactory {

    /**
     * 根据还款方式 创建预期收益算法类
     *
     * @param payMethod 还款方式
     * @return
     */
    Interest getInterest(int payMethod);
}
第一种算法(到期本息 、先息后本)
public class MaturityInterest implements Interest{

    @Override
    public double getInterestCount(double invest, double yearRate, int month) {
        BigDecimal bigDecimal = new BigDecimal(invest);
        BigDecimal bigDecimal12 = new BigDecimal(12);
        BigDecimal bigDecimalcount = new BigDecimal(month);

        BigDecimal multiply1 = bigDecimal.multiply(new BigDecimal(yearRate));//总收入
        BigDecimal divide = multiply1.divide(bigDecimal12, 4, BigDecimal.ROUND_HALF_EVEN); //单月收入保留4位
        BigDecimal bigDecimal1 = divide.setScale(3, BigDecimal.ROUND_HALF_EVEN);//单月收入保留3位
        BigDecimal multiply = bigDecimal1.multiply(bigDecimalcount); //预期收入
        return multiply.doubleValue();
    }
}
第二种算法(等额本息)
public class PrincipalInterest implements Interest {

    @Override
    public double getInterestCount(double invest, double yearRate, int month) {
        double monthRate = yearRate / 12;   //月利率
        double monthInterest = 0;
        double capital = invest;
        double tmpCapital = 0;
        double totalInterest = 0;
        for (int i = 1; i < month + 1; i++) {
            capital = capital - tmpCapital;
            monthInterest = capital * monthRate;
            tmpCapital = (invest * monthRate * (Math.pow((1 + monthRate), i - 1))) / (Math.pow(1 + monthRate, month) - 1);
            System.out.println("第" + i + "月利息: " + monthInterest);
            totalInterest = totalInterest + monthInterest;
        }
        return totalInterest;
    }
}

第三种算法(等额本金)

public class CapitalInterest implements Interest {

    @Override
    public double getInterestCount(double invest, double yearRate, int month) {
        double monthRate = yearRate / 12;
        // 每月本息金额  = (贷款本金÷还款月数) + (贷款本金-已归还本金累计额)×月利率
        // 每月本金 = 贷款本金÷还款月数
        // 每月利息 = (贷款本金-已归还本金累计额)×月利率
        double monthCapital = 0;
        double tmpCapital = 0;
        double monthInterest = 0;

        double countInterest = 0;
        for (int i = 1; i < month + 1; i++) {
            monthCapital = (invest / month) + (invest - tmpCapital) * monthRate;
            monthInterest = (invest - tmpCapital) * monthRate;
            tmpCapital = tmpCapital + (invest / month);

            countInterest += monthInterest;
            System.out.println("第" + i + "月本息: " + monthCapital + ",本金:" + (invest / month) + ",利息:" + monthInterest);
        }
        return countInterest;
    }
}

具体工厂

public class BidInterestFactory implements InterestFactory {
    @Override
    public Interest getInterest(int payMethod) {
        if (payMethod == 1 || payMethod == 2) { //到期本息  先息后本
            return new MaturityInterest();
        }
        if (payMethod == 3) { //等额本息
            return new PrincipalInterest();
        }
        if (payMethod == 4) { //等额本金
            return new CapitalInterest();
        }
        return new NullInterset();
    }

    /**
     * 空算法
     */
    public class NullInterset implements Interest {

        @Override
        public double getInterestCount(double invest, double yearRate, int month) {
            return 0;
        }
    }
}
使用
  if (interest == null) {
     interest = new BidInterestFactory().getInterest(bidInfo.getPayMethod());
  }
  double interestCount = interest.getInterestCount(Double.valueOf(editable.toString()), rate / 100, bidInfo.getPeroidCount());
  moneyTipTextView.setText("预期收入" + MoneyUtil.formatDouble(interestCount) + "元");

重构就是对软件内部结构的一种调整,目的是不改变软件可观察行为的前提下,提高其可理解性,降低其可修改成本。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值