实现50万的存款目标,俗称 “50计划”。
private static int targetAmount = 500000;
达成这个目标,我们将在第xxxx年xx月实现?⌛
答案:文末给出了结果。
纳入考虑的可变因素,目前列举了7项:投资年化率、工资、收房租、年终奖、生活费、付房租、贷款月供。
不纳入考虑的因素:生病、意外事故、工资不固定的情况,此类不太适用,误差较大。
private static double yearRate = 0.10;
//income
private static int wages = 9200;
private static int rentCollect = 0;
private static int yearEndAward = 20000;
//payout
private static int livingCost = 800;
private static int rent = 800;
private static int monthlyPayment = 0;
这是一个算法,通过修改7项可变因素,将会得出具有参考价值的数据。
计算的核心就是利用复利效应,充分发挥资金价值。
源代码如下:
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static BigDecimal MONTHS_A_YEAR = new BigDecimal(12);
public static BigDecimal MONTHS_TWO_YEAR = new BigDecimal(24);
private static int targetAmount = 500000;
private static double yearRate = 0.10;
//income
private static int wages = 9200;
private static int rentCollect = 0;
private static int yearEndAward = 20000;
//payout
private static int livingCost = 800;
private static int rent = 800;
private static int monthlyPayment = 0;
private static List<Map<BigDecimal, BigDecimal>> yearAmountMap = new ArrayList<>();
private static List<Map<BigDecimal, BigDecimal>> monthAmountMap = new ArrayList<>();
public static void main(String[] args) {
BigDecimal yearRemainIncome = getYearRemainIncome();
gotoAchieveGoals(yearRemainIncome);
}
private static BigDecimal getYearRemainIncome() {
int remainWages = wages - livingCost - rent - monthlyPayment;
BigDecimal remainWagesBd = new BigDecimal(remainWages);
BigDecimal yearEndAwardBd = new BigDecimal(yearEndAward);
BigDecimal rentCollectBd = new BigDecimal(rentCollect);
BigDecimal yearWageBd = remainWagesBd.multiply(MONTHS_A_YEAR);
BigDecimal yearRentBd = rentCollectBd.multiply(MONTHS_A_YEAR);
BigDecimal yearWageAndRent = yearWageBd.add(yearRentBd);
BigDecimal yearWageAndRentAndYearEndAward = yearWageAndRent.add(yearEndAwardBd);
return yearWageAndRentAndYearEndAward;
}
private static void gotoAchieveGoals(BigDecimal yearRemainIncome) {
yearAmountMap.clear();
BigDecimal targetAmount = new BigDecimal(Test.targetAmount);
BigDecimal monthRemainIncome = yearRemainIncome.divide(MONTHS_A_YEAR, 2, BigDecimal.ROUND_HALF_UP);
BigDecimal achieveGoalsTotalMonths = targetAmount.divide(monthRemainIncome, 2, BigDecimal.ROUND_HALF_UP);
BigDecimal achieveGoalsTotalYears = achieveGoalsTotalMonths.divide(MONTHS_A_YEAR, 2, RoundingMode.HALF_UP);
BigDecimal investmentAndPrincipalTotalAmount = getInvestmentAndPrincipalTotalAmount(yearRemainIncome, achieveGoalsTotalMonths);
BigDecimal totalInvestmentIncomeAmount = investmentAndPrincipalTotalAmount.subtract(targetAmount);
double achieveGoalsWhereYear = getAchieveGoalsWhereYear();
System.out.println("--------------Total Result--------------");
System.out.println(" A year fixed income : " + yearRemainIncome);
System.out.println(" Total investment income : " + totalInvestmentIncomeAmount);
System.out.println(" Total principal income : " + investmentAndPrincipalTotalAmount.subtract(totalInvestmentIncomeAmount));
System.out.println(" Total investment and principal income : " + investmentAndPrincipalTotalAmount);
System.out.println(" Total year achieving target by fixed income : " + achieveGoalsTotalYears);
System.out.println(" Total year achieving target by fixed and investment income : " + achieveGoalsWhereYear);
}
private static BigDecimal getInvestmentAndPrincipalTotalAmount(BigDecimal principal, BigDecimal achieveGoalsTotalMonths) {
BigDecimal yearRateBd = new BigDecimal(yearRate);
yearRateBd = yearRateBd.setScale(2, RoundingMode.HALF_UP);
BigDecimal monthRate = yearRateBd.divide(MONTHS_TWO_YEAR, 6, BigDecimal.ROUND_HALF_UP);
monthRate = monthRate.setScale(4, RoundingMode.HALF_UP);
double yearAccumulateAmount = getYearAccumulateAmount(principal, achieveGoalsTotalMonths, yearRateBd);
double yearRemainMonthAccumulateAmount = getYearRemainMonthAccumulateAmount(principal, new BigDecimal(yearAccumulateAmount), achieveGoalsTotalMonths, monthRate);
BigDecimal accumulateAmountBd = new BigDecimal(yearAccumulateAmount + yearRemainMonthAccumulateAmount);
accumulateAmountBd = accumulateAmountBd.setScale(2, RoundingMode.HALF_UP);
return accumulateAmountBd;
}
private static double getYearAccumulateAmount(BigDecimal principal, BigDecimal totalMonths, BigDecimal yearRate) {
System.out.println("--------------year_rate(" + yearRate + ")--------------");
int year = totalMonths.setScale(0, RoundingMode.HALF_UP).intValue() / 12;
BigDecimal yearAccumulateAmount = new BigDecimal(0);
BigDecimal investmentAndPrincipalAmount;
for (int i = 1; i <= year; i++) {
if (i == 1) {
investmentAndPrincipalAmount = principal;
} else {
investmentAndPrincipalAmount = yearAccumulateAmount;
}
BigDecimal yearInvestmentIncome = investmentAndPrincipalAmount.multiply(yearRate).setScale(2, RoundingMode.HALF_UP);
yearAccumulateAmount = yearAccumulateAmount.add(principal.add(yearInvestmentIncome));
System.out.println("The " + i + " year, yearAccumulateAmount - yearInvestmentIncome = " + yearAccumulateAmount + " - " + yearInvestmentIncome + " = " + (yearAccumulateAmount.subtract(yearInvestmentIncome).doubleValue()));
HashMap<BigDecimal, BigDecimal> map = new HashMap<>();
map.put(BigDecimal.valueOf(i), yearAccumulateAmount);
yearAmountMap.add(map);
}
return yearAccumulateAmount.doubleValue();
}
private static double getYearRemainMonthAccumulateAmount(BigDecimal principal, BigDecimal originYearAccumulateAmount, BigDecimal achieveGoalsTotalMonths, BigDecimal monthRate) {
System.out.println("--------------remain_month_rate(" + monthRate + ")--------------");
BigDecimal monthFixedIncome = principal.divide(MONTHS_A_YEAR, 2, RoundingMode.HALF_UP);
int remainMonths = achieveGoalsTotalMonths.intValue() % 12;
BigDecimal yearAccumulateAmountBd = originYearAccumulateAmount;
yearAccumulateAmountBd = yearAccumulateAmountBd.setScale(2, RoundingMode.HALF_UP);
BigDecimal targetYearAccumulateAmount = yearAccumulateAmountBd;
BigDecimal yearRemainMonthAccumulateAmount;
for (int i = 1; i <= remainMonths; i++) {
yearRemainMonthAccumulateAmount = targetYearAccumulateAmount;
yearRemainMonthAccumulateAmount = yearRemainMonthAccumulateAmount.setScale(2, RoundingMode.HALF_UP);
BigDecimal monthInvestmentIncome = yearRemainMonthAccumulateAmount.multiply(monthRate).setScale(2, RoundingMode.HALF_UP);
BigDecimal fixedInvestmentIncome = (monthFixedIncome.add(monthInvestmentIncome));
targetYearAccumulateAmount = targetYearAccumulateAmount.add(fixedInvestmentIncome);
System.out.println("The remain month" + i + " ," + " targetYearAccumulateAmount - monthInvestmentIncome = " + " " + targetYearAccumulateAmount + " - " + monthInvestmentIncome + " = " + (targetYearAccumulateAmount.subtract(monthInvestmentIncome).doubleValue()));
HashMap<BigDecimal, BigDecimal> map = new HashMap<>();
map.put(BigDecimal.valueOf(i), targetYearAccumulateAmount);
monthAmountMap.add(map);
}
return targetYearAccumulateAmount.subtract(yearAccumulateAmountBd).doubleValue();
}
public static double getAchieveGoalsWhereYear() {
BigDecimal targetAmountBd = new BigDecimal(targetAmount);
targetAmountBd = targetAmountBd.setScale(2, RoundingMode.HALF_UP);
double totalYear = yearAmountMap.size();
for (Map<BigDecimal, BigDecimal> map : yearAmountMap) {
for (Map.Entry<BigDecimal, BigDecimal> entry : map.entrySet()) {
if (entry.getValue().compareTo(targetAmountBd) >= 0) {
return entry.getKey().doubleValue();
}
}
}
//year amount is not enough,so find the month
for (Map<BigDecimal, BigDecimal> map : monthAmountMap) {
for (Map.Entry<BigDecimal, BigDecimal> entry : map.entrySet()) {
if (entry.getValue().compareTo(targetAmountBd) >= 0) {
return totalYear + (entry.getKey().divide(MONTHS_A_YEAR, 2, RoundingMode.HALF_UP)).doubleValue();
}
}
}
double month = BigDecimal.valueOf(monthAmountMap.size()).divide(MONTHS_A_YEAR, 2, RoundingMode.HALF_UP).doubleValue();
return totalYear + month;
}
}
输出:
--------------year_rate(0.10)--------------
The 1 year, totalIncome - investmentIncomeBd = 122320.00 - 11120.00 = 111200.0
The 2 year, totalIncome - investmentIncomeBd = 245752.00 - 12232.00 = 233520.0
The 3 year, totalIncome - investmentIncomeBd = 381527.20 - 24575.20 = 356952.0
The 4 year, totalIncome - investmentIncomeBd = 530879.92 - 38152.72 = 492727.2
--------------remain_month_rate(0.0042)--------------
The remain month1 , totalIncome - investmentIncomeBd = 542376.29 - 2229.70 = 540146.59
The remain month2 , totalIncome - investmentIncomeBd = 553920.94 - 2277.98 = 551642.96
The remain month3 , totalIncome - investmentIncomeBd = 565514.08 - 2326.47 = 563187.61
The remain month4 , totalIncome - investmentIncomeBd = 577155.91 - 2375.16 = 574780.75
The remain month5 , totalIncome - investmentIncomeBd = 588846.63 - 2424.05 = 586422.58
--------------Total Result--------------
A year fixed income : 111200
Total investment income : 88846.63
Total principal income : 500000.00
Total investment and principal income : 588846.63
Total year achieving target by fixed income : 4.50
Total year achieving target by fixed and investment income : 4.0
111200:一年能攒下的金额;
88846.63:投资收入总金额;
4.50:只拿固定工资不投资需要4年半才能达成目标;
4.0:通过固定工资和投资理财则只需要4年即可达成目标;
要不要去看看自己的情况,那就去编译源码,修改7项可变因素吧。
原创不易,求个关注。
微信公众号:一粒尘埃的漫旅
里面有很多想对大家说的话,就像和朋友聊聊天。
写代码,做设计,聊生活,聊工作,聊职场。
我见到的世界是什么样子的?
搜索关注我吧。