找回密码
 注册
搜索
查看: 105|回复: 0

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

[复制链接]
发表于 2024-7-11 10:38:41 | 显示全部楼层 |阅读模式
​作者:一只野生的八哥


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


附带一份赚钱攻略(说实话,打工赚不了钱,但可以有点点钱):

技术大厂,部门捞人,前/后端测试←感兴趣
要求学历:全日制统招本科(非学院派即可)~
--加班偶尔较多,但周末加班两倍工资。
--15-35K,工资在一线城市属于一般,但二线城市很可以。



贷款利率

·公积金的贷款利率

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



·商业险贷款利率

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



代码实现

以下代码,实现了:我贷款买房需要多给银行还多少钱。

  1. public class LoanAmountCalculation {

  2.    //首套住房5年以内公积金贷款利率
  3.    private static final double FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS = 2.6;
  4.    //首套住房5年以上公积金款利率
  5.    private static final double FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS = 3.1;
  6.    //二房5年以内公积金贷款利率
  7.    private static final double NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS = 3.025;
  8.    //二房5年以上公积金款利率
  9.    private static final double NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS = 3.575;
  10.    //5年以内商业贷款利率
  11.    private static final double COMMERCIAL_LOAN_RATE_WITHIN_FIVE_YEARS = 3.45;
  12.    //5年以上商业贷款利率
  13.    private static final double COMMERCIAL_LOAN_RATE_MORE_FIVE_YEARS = 3.95;

  14.    public static void main(String[] args) {
  15.        Scanner scanner = new Scanner(System.in);

  16.        double houseAmount = getInputValue(scanner, "请输入预计买房金额(单位:W):", "请输出正确的买房金额(>0)!");
  17.        double principal = getInputValue(scanner, "请输入您的本金(单位:W):", "请输出正确的买房金额(>0)!");
  18.        if (principal >= houseAmount) {
  19.            System.out.println("全款买房,崇拜大佬!");
  20.            return;
  21.        }

  22.        double accumulationFundLoanAmount = getInputValue(scanner, "请输入公积金贷款金额(单位:W):", "请输出正确的公积金贷款金额(>0)!");

  23.        double commercialLoanAmount = houseAmount - principal - accumulationFundLoanAmount;
  24.        if(commercialLoanAmount <= 0){
  25.            System.out.println("您的本金+公积金贷款已经够买房啦,恭喜大佬!");
  26.            return;
  27.        }else{
  28.            System.out.println("您的本金+公积金贷款还不够买房哦,需要商业贷款金额为(单位:W):" + commercialLoanAmount + "\n");
  29.        }

  30.        int accumulationFundLoanYears = getInputIntValue(scanner, "请输入公积金贷款年份(单位:年):");
  31.        int commercialLoanAmountYears = getInputIntValue(scanner, "请输入商业贷款年份(单位:年):");

  32.        int isFirstHouse = getInputIntValue(scanner, "请输入是否首房(0:否,1:是):");

  33.        LoanAmount loanAmount = calculateLoanAmount(
  34.                accumulationFundLoanAmount, accumulationFundLoanYears,
  35.                commercialLoanAmount, commercialLoanAmountYears, isFirstHouse);
  36.        System.out.println("详细贷款信息如下:" + "\n" + loanAmount);
  37.    }

  38.    /**
  39.     * 获取double类型的输入
  40.     * @param scanner:Java输入类
  41.     * @param prompt:提示信息
  42.     * @param errorMessage:输入错误的提示信息
  43.     * @return 一个double类型的输入
  44.     */
  45.    private static double getInputValue(Scanner scanner, String prompt, String errorMessage) {
  46.        double value;
  47.        while (true) {
  48.            System.out.println(prompt);
  49.            if (scanner.hasNextDouble()) {
  50.                value = scanner.nextDouble();
  51.                if (value > 0) {
  52.                    break;
  53.                } else {
  54.                    System.out.println(errorMessage);
  55.                }
  56.            } else {
  57.                scanner.next();
  58.                System.out.println(errorMessage);
  59.            }
  60.        }
  61.        return value;
  62.    }

  63.    /**
  64.     * 获取int类型的输入
  65.     * @param scanner:Java输入类
  66.     * @param prompt:提示信息
  67.     * @return 一个int类型的输入
  68.     */
  69.    private static int getInputIntValue(Scanner scanner, String prompt) {
  70.        int value;
  71.        while (true) {
  72.            System.out.println(prompt);
  73.            if (scanner.hasNextInt()) {
  74.                value = scanner.nextInt();
  75.                if (value > 0) {
  76.                    break;
  77.                } else {
  78.                    System.out.println("请输入正确的年份(>0)!");
  79.                }
  80.            } else {
  81.                scanner.next();
  82.                System.out.println("请输入正确的年份(>0)!");
  83.            }
  84.        }
  85.        return value;
  86.    }

  87.    /**
  88.     * 功能:贷款金额计算
  89.     * 入参:
  90.     * 1.accumulationFundLoanAmount:公积金贷款金额  2.accumulationFundLoanYears:公积金贷款年份;
  91.     * 3.commercialLoanAmount:商业贷款金额;        4.commercialLoanAmountYears:商业贷款年份
  92.     * 5.isFirstHouse:是否首房
  93.     */
  94.    private static LoanAmount calculateLoanAmount(double accumulationFundLoanAmount, int accumulationFundLoanYears,
  95.                                                           double commercialLoanAmount, int commercialLoanAmountYears, int isFirstHouse){
  96.        LoanAmount loanAmount = new LoanAmount();
  97.        //公积金贷款还款金额
  98.        double accumulationFundRepaymentAmount;
  99.        if(isFirstHouse == 1){
  100.            accumulationFundRepaymentAmount = accumulationFundLoanYears <= 5 ?
  101.                    accumulationFundLoanAmount * Math.pow((100 + FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, accumulationFundLoanYears)
  102.                    : accumulationFundLoanAmount * Math.pow((100 + FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS) / 100, accumulationFundLoanYears);
  103.        }else{
  104.            accumulationFundRepaymentAmount = accumulationFundLoanYears <= 5 ?
  105.                    accumulationFundLoanAmount * Math.pow((100 + NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, accumulationFundLoanYears)
  106.                    : accumulationFundLoanAmount * Math.pow((100 + NOT_FIRST_HOUSE_ACCUMULATION_FUND_LOAN_RATE_MORE_FIVE_YEARS) / 100, accumulationFundLoanYears);
  107.        }
  108.        loanAmount.setAccumulationFundRepaymentAmount(String.format("%.2f", accumulationFundRepaymentAmount));

  109.        //公积金贷款每年还款金额
  110.        loanAmount.setAccumulationFundAnnualRepaymentAmount(String.format("%.2f", accumulationFundRepaymentAmount / accumulationFundLoanYears));

  111.        //商业贷款还款金额
  112.        double commercialRepaymentAmount = commercialLoanAmountYears <= 5 ?
  113.                commercialLoanAmount * Math.pow((100 + COMMERCIAL_LOAN_RATE_WITHIN_FIVE_YEARS) / 100, commercialLoanAmountYears)
  114.                : commercialLoanAmount * Math.pow((100 + COMMERCIAL_LOAN_RATE_MORE_FIVE_YEARS) / 100, commercialLoanAmountYears);
  115.        loanAmount.setCommercialRepaymentAmount(String.format("%.2f", commercialRepaymentAmount));

  116.        //商业贷款每年还款金额
  117.        loanAmount.setCommercialAnnualRepaymentAmount(String.format("%.2f", commercialRepaymentAmount / commercialLoanAmountYears));

  118.        //公积金贷款超出金额
  119.        loanAmount.setAccumulationFundLoanExceedAmount(String.format("%.2f", accumulationFundRepaymentAmount - accumulationFundLoanAmount));

  120.        //商业贷款超出金额
  121.        loanAmount.setCommercialLoanExceedAmount(String.format("%.2f", commercialRepaymentAmount - commercialLoanAmount));

  122.        loanAmount.setTotalExceedLoanAmount(String.format("%.2f", accumulationFundRepaymentAmount - accumulationFundLoanAmount + commercialRepaymentAmount - commercialLoanAmount));
  123.        return loanAmount;
  124.    }
  125.    @Data
  126.    static class LoanAmount{
  127.        /**
  128.         * 公积金贷款还款金额
  129.         */
  130.        private String accumulationFundRepaymentAmount;
  131.        /**
  132.         * 公积金贷款每年还款金额
  133.         */
  134.        private String accumulationFundAnnualRepaymentAmount;
  135.        /**
  136.         * 商业贷款还款金额
  137.         */
  138.        private String commercialRepaymentAmount;
  139.        /**
  140.         * 商业贷款每年还款金额
  141.         */
  142.        private String commercialAnnualRepaymentAmount;
  143.        /**
  144.         * 公积金贷款超出金额 = 公积金贷款还款金额 - 公积金贷款金额
  145.         */
  146.        private String accumulationFundLoanExceedAmount;
  147.        /**
  148.         * 商业贷款超出金额 = 商业贷款还款金额 - 商业贷款金额
  149.         */
  150.        private String commercialLoanExceedAmount;

  151.        /**
  152.         * 总共贷款超出金额
  153.         */
  154.        private String totalExceedLoanAmount;

  155.        @Override
  156.        public String toString() {
  157.            return "1.公积金贷款还款金额=" + accumulationFundRepaymentAmount + "万元\n" +
  158.                    "2.商业贷款还款金额=" + commercialRepaymentAmount + "万元\n" +
  159.                    "3.公积金贷款每年还款金额=" + accumulationFundAnnualRepaymentAmount + "万元\n" +
  160.                    "4.商业贷款每年还款金额=" + commercialAnnualRepaymentAmount + "万元\n" +
  161.                    "5.公积金贷款超出金额=" + accumulationFundLoanExceedAmount + "万元\n" +
  162.                    "6.商业贷款超出金额=" + commercialLoanExceedAmount + "万元\n" +
  163.                    "7.总共贷款超出金额=" + totalExceedLoanAmount + "万元\n";
  164.        }
  165.    }
  166. }
复制代码


代码输入,输出示例



由上图可知,我要贷款买一套 400w 的房子,本金只有 120w,使用组合贷:公积金贷款 120w(10年),商业贷款 160w(20年)。

最终我需要多还银行 230.07w,相当于买两辆迈巴赫的钱了,巨亏!

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-9-8 09:00 , Processed in 0.049564 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表