CalculatorUtil.java
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.lhcredit.common.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
/**
* 计算器工具类
*/
public class CalculatorUtil {
/**
* 理财产品收益计算器
*
* @param productType 理财产品类型(封闭式:0,开放式:1)
* @param investmentAmount 投资金额
* @param annualizedRate 预期年化收益率
* @param startTime 开始日期
* @param endTime 结束日期
* @return
*/
public static Map<String, Object> calculator(String productType, String investmentAmount, String annualizedRate, String startTime, String endTime) {
Map<String, Object> map = new HashMap<>();
BigDecimal incomeAmount = new BigDecimal(0.00);
BigDecimal totalAmount = new BigDecimal(0.00);
try {
if (checkParams(productType, investmentAmount, annualizedRate, startTime, endTime)) {
if ("0".equals(productType) || "1".equals(productType)) {
incomeAmount = calc(investmentAmount, annualizedRate, startTime, endTime);
totalAmount = totalCalc(new BigDecimal(investmentAmount), incomeAmount);
map.put("incomeAmount", incomeAmount.setScale(2, RoundingMode.HALF_UP).toString());
map.put("totalAmount", totalAmount.setScale(2, RoundingMode.HALF_UP).toString());
} else {
System.out.println("请检测您输入的理财产品类型是否正确!");
}
} else {
System.out.println("参数可能不全");
}
} catch (Throwable e) {
LogUtils.logError("理财产品收益计算异常", e);
}
return map;
}
/**
* 输入参数校验
*
* @param productType
* @param investmentAmount
* @param annualizedRate
* @param startTime
* @param endTime
* @return
*/
private static boolean checkParams(String productType, String investmentAmount, String annualizedRate, String startTime, String endTime) {
boolean flag = false;
try {
if (productType != null && productType.length() > 0 && investmentAmount != null &&
investmentAmount.length() > 0 && annualizedRate != null && annualizedRate.length() > 0 &&
startTime != null && startTime.length() > 8 && endTime != null && endTime.length() > 8 &&
DateUtils.getDayPoor(DateUtils.parseDate(endTime), DateUtils.parseDate(startTime)) > 0) {
flag = true;
}
} catch (Throwable e) {
LogUtils.logError("理财产品收益计算_参数判断异常", e);
}
return flag;
}
/**
* 收益计算
*
* @param investmentAmount
* @param annualizedRate
* @param startTime
* @param endTime
* @return
*/
private static BigDecimal calc(String investmentAmount, String annualizedRate, String startTime, String endTime) {
return new BigDecimal(investmentAmount).multiply(new BigDecimal(annualizedRate)).
multiply(new BigDecimal(DateUtils.getDayPoor(DateUtils.parseDate(endTime),
DateUtils.parseDate(startTime)))).divide(new BigDecimal(36500),2, RoundingMode.HALF_UP);
}
/**
* 本息和
*
* @param investmentAmount
* @param incomeAmount
* @return
*/
private static BigDecimal totalCalc(BigDecimal investmentAmount, BigDecimal incomeAmount) {
return incomeAmount.add(investmentAmount);
}
}