Как создать кредитный калькулятор — приложение для Android

Посмотрев урок, вы узнаете, как написать кредитный калькулятор для android, который можно использовать для расчета процентов и вычисления платежа по кредиту.

https://youtu.be/qjJi-xWnPok

Исходный код:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="1"
    android:stretchColumns="1">

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/loan_amount_prompt" />

        <EditText
            android:id="@+id/loan_amount"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal">

            <requestFocus></requestFocus>
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/interest_rate_prompt" />

        <EditText
            android:id="@+id/interest_rate"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/loan_period_prompt" />

        <EditText
            android:id="@+id/loan_period"
            android:layout_height="wrap_content"
            android:inputType="number" />
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:onClick="showLoanPayments"
            android:text="@string/loan_button_text" />
    </TableRow>

    <TableRow android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/monthly_payment_prompt" />

        <TextView
            android:id="@+id/monthly_payment_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textColor="#ff0000" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/total_payments_prompt" />

        <TextView
            android:id="@+id/total_payments_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textColor="#ff0000" />
    </TableRow>
</TableLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class LoanCalculatorActivity extends AppCompatActivity {

    private EditText mLoanAmount, mInterestRate, mLoanPeriod;
    private TextView mMontlyPaymentResult, mTotalPaymentsResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_calculator);

        mLoanAmount = (EditText) findViewById(R.id.loan_amount);
        mInterestRate = (EditText) findViewById(R.id.interest_rate);
        mLoanPeriod = (EditText) findViewById(R.id.loan_period);
        mMontlyPaymentResult = (TextView) findViewById(R.id.monthly_payment_result);
        mTotalPaymentsResult = (TextView) findViewById(R.id.total_payments_result);

    }

    public void showLoanPayments(View clickedButton) {

        double loanAmount = Integer.parseInt(mLoanAmount.getText().toString());
        double interestRate = (Integer.parseInt(mInterestRate.getText().toString()));
        double loanPeriod = Integer.parseInt(mLoanPeriod.getText().toString());
        double r = interestRate / 1200;
        double r1 = Math.pow(r + 1, loanPeriod);

        double monthlyPayment = (double) ((r + (r / (r1 - 1))) * loanAmount);
        double totalPayment = monthlyPayment * loanPeriod;

        mMontlyPaymentResult.setText(new DecimalFormat("##.##").format(monthlyPayment));
        mTotalPaymentsResult.setText(new DecimalFormat("##.##").format(totalPayment));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_loan_calculator, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Источник