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 ={
"2015年10⽉24⽇五年期商贷利率 4.90% 公积⾦利率 3.25%",
"2015年08⽉26⽇五年期商贷利率 5.15% 公积⾦利率 3.25%",
"2015年06⽉28⽇五年期商贷利率 5.40% 公积⾦利率 3.50%",
"2015年05⽉11⽇五年期商贷利率 5.65% 公积⾦利率 3.75%",
"2015年03⽉01⽇五年期商贷利率 5.90% 公积⾦利率 4.00%",
"2014年11⽉22⽇五年期商贷利率 6.15% 公积⾦利率 4.25%",
"2012年07⽉06⽇五年期商贷利率 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){
Id()== R.id.btn_loan){
if(TextUtils.isEmpty(Text().toString())){
Toast.makeText(this,"购房总价不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(Text().toString())){
Toast.makeText(this,"按揭部分不能为空", Toast.LENGTH_SHORT).show();
return;
}
showLoan();// 显⽰i计算好的贷款总额
}else Id()== R.id.btn_calculate){
if(hasBusiness && TextUtils.isEmpty(Text().toString())){
Toast.makeText(this,"商业贷款总额不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(hasAccumulation && TextUtils.isEmpty(Text().toString())){适合乡镇创业项目
Toast.makeText(this,"公积⾦贷款总额不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(!hasBusiness &&!hasAccumulation){
Toast.makeText(this,"请选择商业贷款或公积⾦贷款", Toast.LENGTH_SHORT).show();
return;数学分析教材
}
qq说说怎么批量删除showRepayment();// 显⽰计算好的还款明细
}
}
// 根据购房总价和按揭⽐例,计算贷款总额
private void showLoan(){
double total = Double.parseDouble(Text().toString());
double rate = Double.parseDouble(Text().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(Text().toString())*10000;
double businessTime = mYear *12;
double businessRate = mBusinessRatio /100;
// 计算商业贷款部分的还款明细
businessResult =calMortgage(businessLoad, businessTime, businessRate, isInterest);
}
if(hasAccumulation){// 申请了公积⾦贷款
double accumulationLoad = Double.parseDouble(Text().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("%s\n 还款总额为%s万元", desc,formatDecimal(
(businessResult.mTotal + businessResult.mTotalInterest +
accumulationResult.mTotal + accumulationResult.mTotalInterest)/10000,2));
全国一本大学
desc = String.format("%s\n其中利息总额为%s万元", desc,formatDecimal(
(businessResult.mTotalInterest + accumulationResult.mTotalInterest)/10000,2));
desc = String.format("%s\n 还款总时间为%d⽉", desc, mYear *12);
if(isInterest){// 如果是等额本息⽅式
desc = String.format("%s\n每⽉还款⾦额为%s元", desc,formatDecimal(
businessResult.mMonthRepayment + accumulationResult.mMonthRepayment,2));
}else{// 如果是等额本⾦⽅式
desc = String.format("%s\n⾸⽉还款⾦额为%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){ Id()== R.id.ck_business){// 勾选了商业贷款
hasBusiness = isChecked;
}else Id()== 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);
String();
}
// 根据贷款⾦额、还款年限、基准利率,计算还款信息
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 onNothingSelected(AdapterView<?> parent){}
}
private class RatioSelectedListener implements AdapterView.OnItemSelectedListener { @Override
public void onNothingSelected(AdapterView<?> parent){}
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position,long id){ mBusinessRatio = businessArray[position];
mAccumulationRatio = accumulationArray[position];
mAccumulationRatio = accumulationArray[position];
}
}
}
运⾏效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论