如果我贷款买一套 400W 的房子,我要给银行多送几辆迈巴赫?
2023 年至今,上海房价一跌再跌。俺已经蠢蠢欲动了,磨刀霍霍向"买房"。但是奈何手里钞票不够,只能向天再借 500 年打工赚钱。但是作为倔强的互联网打工人,想知道自己会被银行割多少韭菜。于是就写了个程序,用于计算我贷款买房需要多给银行还多少钱。这样我就能知道银行割我的韭菜,能省下几辆迈巴赫的钱了。
贷款利率
- 公积金的贷款利率。
- 首房:贷款时间 <=5 年,利率为 2.6%;贷款时间 >= 5 年,利率为 3.1%。
- 非首房:贷款时间 <=5 年,利率为 3.025%;贷款时间 >= 5 年,利率为 3.575%。
-
商业险贷款利率
- 贷款时间 <=5 年,利率为 3.45%;贷款时间 >= 5 年,利率为 3.95%。
代码实现
- 以下代码,实现了:我贷款买房需要多给银行还多少钱。
public class LoanAmountCalculation {
//首套住房5年以内公积金贷款利率
private static final double FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS = 2.6;
//首套住房5年以上公积金款利率
private static final double FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS = 3.1;
//二房5年以内公积金贷款利率
private static final double NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS = 3.025;
//二房5年以上公积金款利率
private static final double NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS = 3.575;
//5年以内商业贷款利率
private static final double COMMERCIAL_LOAN_RATE_WITHIN_FIVE_YEARS = 3.45;
//5年以上商业贷款利率
private static final double COMMERCIAL_LOAN_RATE_MORE_FIVE_YEARS = 3.95;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double houseAmount = getInputValue(scanner, "请输入预计买房金额(单位:W):", "请输出正确的买房金额(>0)!");
double principal = getInputValue(scanner, "请输入您的本金(单位:W):", "请输出正确的买房金额(>0)!");
if (principal >= houseAmount) {
System.out.println("全款买房,崇拜大佬!");
return;
}
double accumulationFundLoanAmount = getInputValue(scanner, "请输入公积金贷款金额(单位:W):", "请输出正确的公积金贷款金额(>0)!");
double commercialLoanAmount = houseAmount - principal - accumulationFundLoanAmount;
if(commercialLoanAmount <= 0){
System.out.println("您的本金+公积金贷款已经够买房啦,恭喜大佬!");
return;
}else{
System.out.println("您的本金+公积金贷款还不够买房哦,需要商业贷款金额为(单位:W):" + commercialLoanAmount + "\n");
}
int accumulationFundLoanYears = getInputIntValue(scanner, "请输入公积金贷款年份(单位:年):");
int commercialLoanAmountYears = getInputIntValue(scanner, "请输入商业贷款年份(单位:年):");
int isFirstHouse = getInputIntValue(scanner, "请输入是否首房(0:否,1:是):");
LoanAmount loanAmount = calculateLoanAmount(
accumulationFundLoanAmount, accumulationFundLoanYears,
commercialLoanAmount, commercialLoanAmountYears, isFirstHouse);
System.out.println("详细贷款信息如下:" + "\n" + loanAmount);
}
/**
* 获取double类型的输入
* @param scanner:Java输入类
* @param prompt:提示信息
* @param errorMessage:输入错误的提示信息
* @return 一个double类型的输入
*/
private static double getInputValue(Scanner scanner, String prompt, String errorMessage) {
double value;
while (true) {
System.out.println(prompt);
if (scanner.hasNextDouble()) {
value = scanner.nextDouble();
if (value > 0) {
break;
} else {
System.out.println(errorMessage);
}
} else {
scanner.next();
System.out.println(errorMessage);
}
}
return value;
}
/**
* 获取int类型的输入
* @param scanner:Java输入类
* @param prompt:提示信息
* @return 一个int类型的输入
*/
private static int getInputIntValue(Scanner scanner, String prompt) {
int value;
while (true) {
System.out.println(prompt);
if (scanner.hasNextInt()) {
value = scanner.nextInt();
if (value > 0) {
break;
} else {
System.out.println("请输入正确的年份(>0)!");
}
} else {
scanner.next();
System.out.println("请输入正确的年份(>0)!");
}
}
return value;
}
/**
* 功能:贷款金额计算
* 入参:
* 1.accumulationFundLoanAmount:公积金贷款金额 2.accumulationFundLoanYears:公积金贷款年份;
* 3.commercialLoanAmount:商业贷款金额; 4.commercialLoanAmountYears:商业贷款年份
* 5.isFirstHouse:是否首房
*/
private static LoanAmount calculateLoanAmount(double accumulationFundLoanAmount, int accumulationFundLoanYears,
double commercialLoanAmount, int commercialLoanAmountYears, int isFirstHouse){
LoanAmount loanAmount = new LoanAmount();
//公积金贷款还款金额
double accumulationFundRepaymentAmount;
if(isFirstHouse == 1){
accumulationFundRepaymentAmount = accumulationFundLoanYears <= 5 ?
accumulationFundLoanAmount * Math.pow((100 + FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, accumulationFundLoanYears)
: accumulationFundLoanAmount * Math.pow((100 + FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS) / 100, accumulationFundLoanYears);
}else{
accumulationFundRepaymentAmount = accumulationFundLoanYears <= 5 ?
accumulationFundLoanAmount * Math.pow((100 + NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, accumulationFundLoanYears)
: accumulationFundLoanAmount * Math.pow((100 + NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS) / 100, accumulationFundLoanYears);
}
loanAmount.setAccumulationFundRepaymentAmount(String.format("%.2f", accumulationFundRepaymentAmount));
//公积金贷款每年还款金额
loanAmount.setAccumulationFundAnnualRepaymentAmount(String.format("%.2f", accumulationFundRepaymentAmount / accumulationFundLoanYears));
//商业贷款还款金额
double commercialRepaymentAmount = commercialLoanAmountYears <= 5 ?
commercialLoanAmount * Math.pow((100 + COMMERCIAL_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, commercialLoanAmountYears)
: commercialLoanAmount * Math.pow((100 + COMMERCIAL_LOAN_RATE_MORE_FIVE_YEARS) / 100, commercialLoanAmountYears);
loanAmount.setCommercialRepaymentAmount(String.format("%.2f", commercialRepaymentAmount));
//商业贷款每年还款金额
loanAmount.setCommercialAnnualRepaymentAmount(String.format("%.2f", commercialRepaymentAmount / commercialLoanAmountYears));
//公积金贷款超出金额
loanAmount.setAccumulationFundLoanExceedAmount(String.format("%.2f", accumulationFundRepaymentAmount - accumulationFundLoanAmount));
//商业贷款超出金额
loanAmount.setCommercialLoanExceedAmount(String.format("%.2f", commercialRepaymentAmount - commercialLoanAmount));
loanAmount.setTotalExceedLoanAmount(String.format("%.2f", accumulationFundRepaymentAmount - accumulationFundLoanAmount + commercialRepaymentAmount - commercialLoanAmount));
return loanAmount;
}
@Data
static class LoanAmount{
/**
* 公积金贷款还款金额
*/
private String accumulationFundRepaymentAmount;
/**
* 公积金贷款每年还款金额
*/
private String accumulationFundAnnualRepaymentAmount;
/**
* 商业贷款还款金额
*/
private String commercialRepaymentAmount;
/**
* 商业贷款每年还款金额
*/
private String commercialAnnualRepaymentAmount;
/**
* 公积金贷款超出金额 = 公积金贷款还款金额 - 公积金贷款金额
*/
private String accumulationFundLoanExceedAmount;
/**
* 商业贷款超出金额 = 商业贷款还款金额 - 商业贷款金额
*/
private String commercialLoanExceedAmount;
/**
* 总共贷款超出金额
*/
private String totalExceedLoanAmount;
@Override
public String toString() {
return "1.公积金贷款还款金额=" + accumulationFundRepaymentAmount + "万元\n" +
"2.商业贷款还款金额=" + commercialRepaymentAmount + "万元\n" +
"3.公积金贷款每年还款金额=" + accumulationFundAnnualRepaymentAmount + "万元\n" +
"4.商业贷款每年还款金额=" + commercialAnnualRepaymentAmount + "万元\n" +
"5.公积金贷款超出金额=" + accumulationFundLoanExceedAmount + "万元\n" +
"6.商业贷款超出金额=" + commercialLoanExceedAmount + "万元\n" +
"7.总共贷款超出金额=" + totalExceedLoanAmount + "万元\n";
}
}
}
- 代码输入,输出示例:
由上图可知,我要贷款买一套 400w 的房子,本金只有 120w,使用组合贷:公积金贷款 120w(10年),商业贷款 160w(20年)。最终我需要多还银行 230.07w,相当于买两辆迈巴赫的钱了,巨亏!
以上就是全部内容了,如果涉及到真实场景,还是需要根据具体的情况计算的!
标签:scanner,double,买房,private,公积金,static,攻略,贷款 From: https://www.cnblogs.com/zp123yyds/p/18075794