如果我贷款买一套 400W 的房子,我要给银行多送几辆迈巴赫?

买房攻略

2023 年至今,上海房价一跌再跌。俺已经蠢蠢欲动了,磨刀霍霍向"买房"。但是奈何手里钞票不够,只能向天再借 500 年打工赚钱。但是作为倔强的互联网打工人,想知道自己会被银行割多少韭菜。于是就写了个程序,用于计算我贷款买房需要多给银行还多少钱 。这样我就能知道银行割我的韭菜,能省下几辆迈巴赫的钱了。

贷款利率

  • 公积金的贷款利率

    • 首房:贷款时间 <=5 年,利率为 2.6% ;贷款时间 >= 5 年,利率为 3.1%
    • 非首房:贷款时间 <=5 年,利率为 3.025% ;贷款时间 >= 5 年,利率为 3.575%
  • 商业险贷款利率

    • 贷款时间 <=5 年,利率为 3.45% ;贷款时间 >= 5 年,利率为 3.95%

代码实现

  • 以下代码,实现了:我贷款买房需要多给银行还多少钱
java 复制代码
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,相当于买两辆迈巴赫的钱了,巨亏!

以上就是全部内容了,如果涉及到真实场景,还是需要根据具体的情况计算的!

相关推荐
hong_zc1 小时前
算法【Java】—— 二叉树的深搜
java·算法
进击的女IT2 小时前
SpringBoot上传图片实现本地存储以及实现直接上传阿里云OSS
java·spring boot·后端
Miqiuha2 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
一 乐3 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
数云界3 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
阑梦清川3 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
快乐就好ya5 小时前
Java多线程
java·开发语言
IT学长编程5 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
CS_GaoMing5 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
艾伦~耶格尔6 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构