Android实现房贷计算器功能

本文实例为大家分享了Android实现房贷计算器的具体代码,供大家参考,具体内容如下

package com.atomic.moretool;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MortgageCal extends AppCompatActivity {
    private EditText allLoan,yearInterestRate,loanYear;
    private Button calLoan;
    private ListView ShowDebx,ShowDebj;
    private TextView debxTotalInterest;
    private TextView debjTotalInterest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mortgagecal);
        findCompent();
        calLoan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDebx();
                showDebj();
            }
        });
    }
    private void showDebx(){
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,cal_debx(),R.layout.show_debx,
                new String[]{"debxmonth","debxmonthloan","debxmonthprincipal","debxmonthinterest"},
                new int[]{R.id.debx_month,R.id.listview_debx_month_loan,R.id.listview_debx_month_principal,R.id.listview_debx_month_interest});
        ShowDebx.setAdapter(simpleAdapter);
    }
    private void showDebj(){
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,cal_debj(),R.layout.show_debj,
                new String[]{"debjmonth","debjmonthloan","debjmonthprincipal","debjmonthinterest","debjmonthdecrease"},
                new int[]{R.id.debj_month,R.id.listview_debj_month_loan,R.id.listview_debj_month_principal,R.id.listview_debj_month_interest,R.id.listview_debj_month_decrease});
        ShowDebj.setAdapter(simpleAdapter);
    }
    private void findCompent() {
        allLoan=findViewById(R.id.all_loan);
        yearInterestRate=findViewById(R.id.year_interest_rate);
        loanYear=findViewById(R.id.loan_year);
        allLoan.setSelectAllOnFocus(true);
        yearInterestRate.setSelectAllOnFocus(true);
        loanYear.setSelectAllOnFocus(true);
        calLoan=findViewById(R.id.cal_loan);
        ShowDebx=findViewById(R.id.show_debx);
        ShowDebj=findViewById(R.id.show_debj);
        debxTotalInterest=findViewById(R.id.debx_total_interest);
        debjTotalInterest=findViewById(R.id.debj_total_interest);
    }
    private List> cal_debx(){
        /*  
        每月还款总额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]
        每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
        每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
        总利息=还款月数×每月还款总额-贷款本金
         */

        String AllLoan=allLoan.getText().toString().trim();//贷款多少
        String YearInterestRate=yearInterestRate.getText().toString().trim();//年利率
        String LoanYear=loanYear.getText().toString().trim();//贷款年数
        if (!AllLoan.equals("") && !YearInterestRate.equals("") && !LoanYear.equals("")){
            double allloan=Double.parseDouble(AllLoan);//贷款多少
            double yearinterestrate=Double.parseDouble(YearInterestRate);//年利率
            double monthinterestrate=yearinterestrate/12;//月利率
            double loanyear=Double.parseDouble(LoanYear);//贷款年数
            double loanmonth=loanyear*12;//还款月数
            //......需要设置还款月序号
            //......需要已归还本金累计额
            //......需要剩余本金
            List> debx_list=new ArrayList<>();
            for (int i=1;i<=(int)loanmonth;i++){
                Map map=new HashMap<>();
                // 
                //每月还款总额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]
                double DebxMonthLoan=new BigDecimal(allloan*monthinterestrate*Math.pow((1+monthinterestrate),loanmonth)/(Math.pow((1+monthinterestrate),loanmonth)-1)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                //每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
                double DebxMonthPrincipal=new BigDecimal(allloan*monthinterestrate*Math.pow((1+monthinterestrate),(i-1))/(Math.pow((1+monthinterestrate),loanmonth)-1)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                //每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
                double DebxMonthInterest=new BigDecimal(allloan*monthinterestrate*((Math.pow((1+monthinterestrate),loanmonth))-Math.pow((1+monthinterestrate),(i-1)))/(Math.pow((1+monthinterestrate),loanmonth)-1)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                map.put("debxmonth",String.valueOf(i)+"月");
                map.put("debxmonthloan",String.valueOf(DebxMonthLoan));
                map.put("debxmonthprincipal",String.valueOf(DebxMonthPrincipal));
                map.put("debxmonthinterest",String.valueOf(DebxMonthInterest));
                debx_list.add(map);
            }
            //每月还款总额
            double DebxMonthLoan=new BigDecimal(allloan*monthinterestrate*Math.pow((1+monthinterestrate),loanmonth)/(Math.pow((1+monthinterestrate),loanmonth)-1)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            //总利息=还款月数×每月还款总额-贷款本金
            double DebxInterest=new BigDecimal(loanmonth*DebxMonthLoan-allloan).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            debxTotalInterest.setText(String.valueOf(DebxInterest));
            return debx_list;
        }else{
            Toast.makeText(this, "先输入与选择内容", Toast.LENGTH_SHORT).show();
        }
        return null;
    }
    private List> cal_debj() {
        /* 
        每月还款总额=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
        每月应还本金=贷款本金÷还款月数
        每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率。
        每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
        总利息=还款月数×(总贷款额×月利率-月利率×(总贷款额÷还款月数)*(还款月数-1)÷2+总贷款额÷还款月数)
        */
        String AllLoan = allLoan.getText().toString().trim();//贷款多少
        String YearInterestRate = yearInterestRate.getText().toString().trim();//年利率
        String LoanYear = loanYear.getText().toString().trim();//贷款年数
        if (!AllLoan.equals("") && !YearInterestRate.equals("") && !LoanYear.equals("")) {
            double allloan = Double.parseDouble(AllLoan);//贷款多少
            double yearinterestrate = Double.parseDouble(YearInterestRate);//年利率
            double monthinterestrate = yearinterestrate / 12;//月利率
            double loanyear = Double.parseDouble(LoanYear);//贷款年数
            double loanmonth = loanyear * 12;//还款月数
            //......需要已归还本金累计额
            //......需要剩余本金
            List> debj_list = new ArrayList<>();
            for (int i = 1; i <= (int) loanmonth; i++) {
                Map map = new HashMap<>();
                // 
                //每月应还本金=贷款本金÷还款月数
                double DebjMonthPrincipal = new BigDecimal(allloan / loanmonth).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                //每月还款总额=(贷款本金÷还款月数)+(贷款本金-累计已还款本金)×月利率
                double DebjMonthLoan = new BigDecimal((allloan / loanmonth) + (allloan - DebjMonthPrincipal*i) * monthinterestrate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                //每月应还利息=剩余本金×月利率=(贷款本金-累计已还款本金)×月利率。
                double DebjMonthInterest = new BigDecimal((allloan-DebjMonthPrincipal*i) * monthinterestrate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                //每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
                double DebjMonthDecrease = new BigDecimal(DebjMonthPrincipal * monthinterestrate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                map.put("debjmonth",String.valueOf(i)+"月");
                map.put("debjmonthloan",String.valueOf(DebjMonthLoan));
                map.put("debjmonthprincipal",String.valueOf(DebjMonthPrincipal));
                map.put("debjmonthinterest",String.valueOf(DebjMonthInterest));
                map.put("debjmonthdecrease",String.valueOf(DebjMonthDecrease));
                debj_list.add(map);
            }
            //总利息=还款月数×(总贷款额×月利率-月利率×(总贷款额÷还款月数)*(还款月数-1)÷2+总贷款额÷还款月数)
            double DebjInterest = new BigDecimal(((allloan/loanmonth+allloan*monthinterestrate)+allloan/loanmonth*(1+monthinterestrate))/2*loanmonth-allloan).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            debjTotalInterest.setText(String.valueOf(DebjInterest));
            return debj_list;
        } else {
            Toast.makeText(this, "先输入与选择内容", Toast.LENGTH_SHORT).show();
        }
        return null;
    }
}

xml:


    
        
        

        
        
    
    
        
        
        

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现房贷计算器功能)