Android实战(二)——房贷计算器

更新时间:2024-11-06 16:42:42 阅读: 评论:0


2023年5月22日发(作者:hamburg)

Android实战(⼆)——房贷计算器

知识点:

下拉框的学习。

Spinner

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnChe

ckedChangeListener {

......

initYearSpinner();

initRatioSpinner();

}

//

初始化贷款年限

private void initYearSpinner() {

ArrayAdapter<String> yearAdapter = new ArrayAdapter<>(this, R.layout.item_select, yearDescArray);

yearAdapter.setDropDownViewResource(R.layout.item_dropdown);

Spinner sp_year = findViewById(R.id.sp_year);

sp_year.setPrompt("请选择贷款年限");

sp_year.setAdapter(yearAdapter);

sp_year.setSelection(0);

sp_year.setOnItemSelectedListener(new YearSelectedListener());

}

private String[] yearDescArray = {"5", "10", "15", "20", "30"};

private int[] yearArray = {5, 10, 15, 20, 30};

//

初始化基准利率下拉框

private void initRatioSpinner(){

ArrayAdapter<String> ratioAdapter = new ArrayAdapter<>(this, R.layout.item_select, ratioDescArray);

ratioAdapter.setDropDownViewResource(R.layout.item_dropdown);

Spinner sp_radio = findViewById(R.id.sp_ratio);

sp_radio.setPrompt("请选择基准利率");

sp_radio.setAdapter(ratioAdapter);

sp_radio.setSelection(0);

sp_radio.setOnItemSelectedListener(new RatioSelectedListener());

}

private String[] ratioDescArray = {

"20151024 五年期商贷利率 4.90% 公积⾦利率 3.25%",

"20150826 五年期商贷利率 5.15% 公积⾦利率 3.25%",

"20150628 五年期商贷利率 5.40% 公积⾦利率 3.50%",

"20150511 五年期商贷利率 5.65% 公积⾦利率 3.75%",

"20150301 五年期商贷利率 5.90% 公积⾦利率 4.00%",

"20141122 五年期商贷利率 6.15% 公积⾦利率 4.25%",

"20120706 五年期商贷利率 6.55% 公积⾦利率 4.50%",

};

private double[] businessArray = {4.90, 5.15, 5.40, 5.65, 5.90, 6.15, 6.55};

private double[] accumulationArray = {3.25, 3.25, 3.50, 3.75, 4.00, 4.25, 4.50};

@Override

public void onClick(View v) {

if(v.getId() == R.id.btn_loan){

if (TextUtils.isEmpty(et_price.getText().toString())){

Toast.makeText(this, "购房总价不能为空", Toast.LEGTH_SHORT).show();

return;

}

if(TextUtils.isEmpty(et_loan.getText().toString())){

Toast.makeText(this, "按揭部分不能为空", Toast.LEGTH_SHORT).show();

return;

}

showLoan(); // i

显⽰计算好的贷款总额

}else if(v.getId() == R.id.btn_calculate){

if(hasBusiness && TextUtils.isEmpty(et_business.getText().toString())){

Toast.makeText(this, "商业贷款总额不能为空", Toast.LEGTH_SHORT).show();

return;

}

if(hasAccumulation && TextUtils.isEmpty(et_accumulation.getText().toString())){

Toast.makeText(this, "公积⾦贷款总额不能为空", Toast.LEGTH_SHORT).show();

return;

}

if(!hasBusiness && !hasAccumulation){

Toast.makeText(this, "请选择商业贷款或公积⾦贷款", Toast.LEGTH_SHORT).show();

return;

}

showRepayment(); //

显⽰计算好的还款明细

}

}

//

根据购房总价和按揭⽐例,计算贷款总额

private void showLoan(){

double total = Double.parseDouble(et_price.getText().toString());

double rate = Double.parseDouble(et_loan.getText().toString()) / 100;

String desc = String.format("您的贷款总额为%s万元", formatDecimal(total * rate, 2));

tv_loan.setText(desc);

}

//

根据贷款的相关条件,计算还款总额、利息总额,以及⽉供

private void showRepayment() {

Repayment businessResult = new Repayment();

Repayment accumulationResult = new Repayment();

if (hasBusiness) { //

申请了商业贷款

double businessLoad = Double.parseDouble(et_business.getText().toString()) * 10000;

double businessTime = mYear * 12;

double businessRate = mBusinessRatio / 100;

//

计算商业贷款部分的还款明细

businessResult = calMortgage(businessLoad, businessTime, businessRate, isInterest);

}

if (hasAccumulation) { //

申请了公积⾦贷款

double accumulationLoad = Double.parseDouble(et_accumulation.getText().toString()) * 10000;

double accumulationTime = mYear * 12;

double accumulationRate = mAccumulationRatio / 100;

//

计算公积⾦贷款部分的还款明细

accumulationResult = calMortgage(accumulationLoad, accumulationTime, accumulationRate, isInterest);

}

String desc = String.format("您的贷款总额为%s万元", formatDecimal(

(businessResult.mTotal + accumulationResult.mTotal) / 10000, 2));

desc = String.format("%sn 还款总额为%s万元", desc, formatDecimal(

(businessResult.mTotal + businessResult.mTotalInterest +

accumulationResult.mTotal + accumulationResult.mTotalInterest) / 10000, 2));

desc = String.format("%sn其中利息总额为%s万元", desc, formatDecimal(

(businessResult.mTotalInterest + accumulationResult.mTotalInterest) / 10000, 2));

desc = String.format("%sn 还款总时间为%d", desc, mYear * 12);

if (isInterest) { //

如果是等额本息⽅式

desc = String.format("%sn每⽉还款⾦额为%s", desc, formatDecimal(

businessResult.mMonthRepayment + accumulationResult.mMonthRepayment, 2));

} else { //

如果是等额本⾦⽅式

desc = String.format("%sn⾸⽉还款⾦额为%s元,其后每⽉递减%s", desc, formatDecimal(

businessResult.mMonthRepayment + accumulationResult.mMonthRepayment, 2),

formatDecimal(businessResult.mMonthMinus + accumulationResult.mMonthMinus, 2));

}

tv_payment.setText(desc);

}

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if (checkedId == R.id.rb_interest) { //

选择了等额本息⽅式

isInterest = true;

} else if (checkedId == R.id.rb_principal) { //

选择了等额本⾦⽅式

isInterest = false;

}

}

}

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (buttonView.getId() == R.id.ck_business) { //

勾选了商业贷款

hasBusiness = isChecked;

} else if (buttonView.getId() == R.id.ck_accumulation) { //

勾选了公积⾦贷款

hasAccumulation = isChecked;

}

}

//

精确到⼩数点后第⼏位

private String formatDecimal(double value, int digit) {

BigDecimal bd = new BigDecimal(value);

bd = bd.setScale(digit, RoundingMode.HALF_UP);

return bd.toString();

}

//

根据贷款⾦额、还款年限、基准利率,计算还款信息

private Repayment calMortgage(double ze, double nx, double rate, boolean bInterest) {

double zem = (ze * rate / 12 * Math.pow((1 + rate / 12), nx))

/ (Math.pow((1 + rate / 12), nx) - 1);

double amount = zem * nx;

double rateAmount = amount - ze;

double benjinm = ze / nx;

double lixim = ze * (rate / 12);

double diff = benjinm * (rate / 12);

double huankuanm = benjinm + lixim;

double zuihoukuan = diff + benjinm;

double av = (huankuanm + zuihoukuan) / 2;

double zong = av * nx;

double zongli = zong - ze;

Repayment result = new Repayment();

result.mTotal = ze;

if (bInterest) {

result.mMonthRepayment = zem;

result.mTotalInterest = rateAmount;

} else {

result.mMonthRepayment = huankuanm;

result.mMonthMinus = diff;

result.mTotalInterest = zongli;

}

return result;

}

private class YearSelectedListener implements AdapterView.OnItemSelectedListener {

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

mYear = yearArray[position];

}

@Override

public void onothingSelected(AdapterView<?> parent) {}

}

private class RatioSelectedListener implements AdapterView.OnItemSelectedListener {

@Override

public void onothingSelected(AdapterView<?> parent) {}

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

mBusinessRatio = businessArray[position];

mAccumulationRatio = accumulationArray[position];

mAccumulationRatio = accumulationArray[position];

}

}

}

运⾏效果:


本文发布于:2023-05-22 19:51:34,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/falv/fa/82/96065.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 站长QQ:55-9-10-26