如果我贷款买一套 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,相当于买两辆迈巴赫的钱了,巨亏!

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

相关推荐
琳少_11 小时前
java程序设计笔记
java·开发语言·数据库·笔记
小荷才露尖尖角,早有蜻蜓立上头12 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix25013 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
CoderYanger14 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠15 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
吃我一窝jio16 小时前
Java多线程核心知识点梳理
java·开发语言·笔记
这个昵称不容易18 小时前
在线考试系统设计与实现:14 张表 158 个接口,组卷、答题、判分一条线走通
java·springboot·毕设
码云社区19 小时前
Spring Boot上门按摩服务系统源码:上门到店服务目录与门店协同方案
java·spring boot·后端·上门服务·上门按摩·上门小程序
30955666619 小时前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite
ikgy19 小时前
JAVA EE:5.锁机制、CAS 与 JUC 体系全解析
java·java-ee