复利计算器

最近想给爸妈购买养老储蓄型保险,为了对比是否划算,简单的写了一个复利计算算法,留作记录。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "复利计算器"
        btnCalculateCompound.setOnClickListener {
            if (TextUtils.isEmpty(etCapital.text) || TextUtils.isEmpty(etInterestRate.text) || TextUtils.isEmpty(etYears.text)) {
                return@setOnClickListener
            }

            tvMoney.text = calculateCompoundInterest(etYears.text.toString().toInt()
                    , etInterestRate.text.toString().toDouble()
                    , etCapital.text.toString().toDouble()).toString()
        }

        btnCalculateYearsCompound.setOnClickListener {
            if (TextUtils.isEmpty(etCapital.text) || TextUtils.isEmpty(etInterestRate.text) || TextUtils.isEmpty(etYears.text)) {
                return@setOnClickListener
            }

            tvMoney.text = calculateYearsCompoundInterest(etYears.text.toString().toInt()
                    , etInterestRate.text.toString().toDouble()
                    , etCapital.text.toString().toDouble(), etContinueYears.text.toString().toInt()
                    ?: 0).toString()
        }
    }

    /**
     * 单纯的算复利
     */
    fun calculateCompoundInterest(years: Int, interestRate: Double, capital: Double): Double {
        var currentYear = 0
        var currentMoney = capital
        while (currentYear < years) {
            currentMoney = calculateInterestRates(interestRate, currentMoney)
            currentYear++
        }
        return currentMoney

    }

    /**
     * 计算一年的利息
     */
    fun calculateInterestRates(interestRate: Double, capital: Double): Double {
        return capital * (1 + interestRate)
    }

    /**
     * 计算连续多年存同样的钱,一段时间之后的复利计算
     */
    fun calculateYearsCompoundInterest(years: Int, interestRate: Double, capital: Double, continueYears: Int): Double {
        var currentYear = 0
        var currentMoney = capital
        if (years <= continueYears) {
            while (currentYear < years) {
                currentYear++
                if (years == 1) {
                    currentMoney = calculateInterestRates(interestRate, currentMoney)
                } else {
                    currentMoney = calculateInterestRates(interestRate, currentMoney) + calculateInterestRates(interestRate, capital)
                }
            }
        } else {
            while (currentYear < continueYears) {
                currentYear++
                if (years == 1) {
                    currentMoney = calculateInterestRates(interestRate, currentMoney)
                } else {
                    currentMoney = calculateInterestRates(interestRate, currentMoney) + calculateInterestRates(interestRate, capital)
                }
            }

            var currentYearAftercontinueYears = 0
            while (currentYearAftercontinueYears < years - continueYears) {
                currentYearAftercontinueYears++
                currentMoney = calculateInterestRates(interestRate, currentMoney)
            }
        }

        return currentMoney
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <EditText
            android:id="@+id/etCapital"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:hint="本金"/>

    <EditText
            android:id="@+id/etInterestRate"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:hint="年利率"/>

    <EditText
            android:id="@+id/etYears"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:hint="年期"/>

    <EditText
            android:id="@+id/etContinueYears"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:hint="连续不间断多少年,每年固定存款"/>

    <Button
            android:id="@+id/btnCalculateCompound"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:text="计算复利"/>

    <Button
            android:id="@+id/btnCalculateYearsCompound"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:text="计算每年固定存固定钱数,多年后所得利息"/>

    <TextView
            android:id="@+id/tvMoney"
            android:layout_width="match_parent"
            android:layout_height="44dp"
    />

</LinearLayout>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值