买房攻略

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

借款利率

  • 公积金的借款利率

    • 首房:借款时刻 <=5 年,利率为 2.6% ;借款时刻 >= 5 年,利率为 3.1%
    • 非首房:借款时刻 <=5 年,利率为 3.025% ;借款时刻 >= 5 年,利率为 3.575%

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

  • 商业险借款利率

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

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

代码完成

  • 以下代码,完成了:我借款买房需求多给银行还多少钱
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 的房子,我要给银行多送几辆迈巴赫?

由上图可知,我要借款买一套 400w 的房子,本金只有 120w,使用组合贷:公积金借款 120w(10年),商业借款 160w(20年)。终究我需求多还银行 230.07w,相当于买两辆迈巴赫的钱了,巨亏!

以上就是全部内容了,假如涉及到真实场景,仍是需求依据详细的情况核算的!