# How to Build SIP Calculator Android App using MVP Architecture?

• Last Updated : 18 Oct, 2021

SIP, the Systematic Investment Plan. One of the methods to invest in Mutual Funds is weekly, monthly quarterly, or half-yearly. The SIP calculator is the tool to get an idea for investors about their investment returns. However, the returns of mutual funds vary on various factors. For example, the SIP calculator does not calculate the Absolute returns because some mutual funds have exit load and expense ratio charged. So, it only calculates estimated returns and provides the result of maturity value. This article its been discussed, implementing the SIP calculator in Android with robust MVP architecture. Have a look at the following to get an idea of the end product.

### How Does SIP return Calculator Helps?

SIPs provide very lucrative returns when compared to lumpsum investments in mutual funds. The SIP calculator helps investors to make investment plans for the long term as it provides the following:

1. It determines the total amount invested over time.
2. Tells about estimated returns over the expected rate of return.
3. Also calculates the maturity value, which is estimated returns + total invested amount.

Note: Estimated returns are simply the maturity value – total invested amount.

The formula to calculate the SIP returns is simple.

m = p * ( { ( 1 + i ) ^ n – 1 } / i ) * ( i + 1 )

m -> Maturity Value (Estimated Return + Total Amount Invested).

p -> principal amount (to be invested monthly).

i -> period interest rate.

n -> number of payments made.

### Benefits one get from this calculator

• Based on the estimated return, one can plan investments for the long term in mutual funds.
• It provides accurate estimations.
• As per your financial needs, it helps to shape your portfolio by providing accuracy referring to your researched mutual fund past performance.

### Steps to implement the SIP Calculator in Android

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

The main layout of the application contains 3 edit texts and associated sliders with them so that users can slide and make changes in the edit text instead of typing. One Calculate button which upon pressing it calculates according to the formula mentioned. One is a pie chart and this pie chart got two slices, one is for the invested amount over the period and another slice is estimated returns the investment generated over the expected rate of return and selected tenure. To implement the same invoke the following code inside the activity_main.xml file.

## XML



However following is optional, the application is branded with the Roboto type system suggested by Google Material Design. Include the following code inside the themes.xml file.

## XML



Step 3: Working with the Architecture

As the architecture components required for this application are small yet easy to maintain and document and scale the application. So the following image represents that one should create architecture components classes and associated interface as the interface here is called a contract, in the MVP architecture. Create a package for each component of the architecture.

Step 4: Working with all the interfaces(contracts) of the architecture

SipCalculatorModelInterface:

## Kotlin

 interface SipCalculatorModelInterface {    fun getTotalInvestedAmount(): Long    fun getEstimatedReturns(): Long    fun getTotalValue(): Long}

SipCalculatorPresenterInterface:

## Kotlin

 interface SipCalculatorPresenterInterface {    fun forCalculation(        monthlyInvestmentAmount: String,        expectedReturnRate: String,        investmentTimePeriod: String    )}

SipCalculatorViewInterface:

## Kotlin

 interface SipCalculatorViewInterface {    fun onCalculationResult(        totalInvestedAmount: String,        estimatedReturns: String,        totalValue: String    )}

Step 5: Working with SipCalculatorModel.kt class

This class implements the SipCalculatorModelInterface. and this contains the main logic that is calculating all the results. Comments are added for better understanding.

## Kotlin

 class SipCalculatorModel(    monthlyInvestmentAmount: String,    expectedReturnRate: String,    investmentTimePeriod: String    ) : SipCalculatorModelInterface {      val TAG = SipCalculatorModel::class.java.simpleName      // convert all the inputs to integer.    private var monthlyInvestmentAmountInt: Int = monthlyInvestmentAmount.toInt()    private var expectedReturnRateInt: Int = expectedReturnRate.toInt()    private var investmentTimePeriodInt: Int = investmentTimePeriod.toInt() * 12      // total investment is considered here is according to monthly investment plans    override fun getTotalInvestedAmount(): Long {        return (monthlyInvestmentAmountInt * investmentTimePeriodInt).toLong()    }      // estimated returns = maturity value - total investment amount    override fun getEstimatedReturns(): Long {        return getTotalValue() - getTotalInvestedAmount()    }      // calculate the maturity value according to the formula    override fun getTotalValue(): Long {        val periodicInterest: Float = ((expectedReturnRateInt.toFloat() / 12) / 100)          return (monthlyInvestmentAmountInt * (((Math.pow(            (1 + periodicInterest).toDouble(),            investmentTimePeriodInt.toDouble()        )                - 1) / periodicInterest) * (1 + periodicInterest)))            .toLong()    }}

Step 6: Working with SipCalculatorPresenter.kt class

This class implements the SipCalculatorPresenterInterface, this simply acts between the SipCalculatorModel and SipCalculatorView. Invoke the following code inside the SipCalculatorPresenter.kt class. Comments are added for better understanding.

## Kotlin

 import com.adityamshidlyali.gfgsipcalculator.Model.SipCalculatorModelimport com.adityamshidlyali.gfgsipcalculator.View.SipCalculatorViewInterface  class SipCalculatorPresenter(    private val sipCalculatorViewInterface: SipCalculatorViewInterface    ) : SipCalculatorPresenterInterface {      override fun forCalculation(        monthlyInvestmentAmount: String,        expectedReturnRate: String,        investmentTimePeriod: String    ) {          // create instance of the sip model and calculate all the results.        val sipModel = SipCalculatorModel(            monthlyInvestmentAmount,            expectedReturnRate,            investmentTimePeriod        )          // pass the data to view by accepting the context of the view class        sipCalculatorViewInterface.onCalculationResult(            sipModel.getTotalInvestedAmount().toString(),            sipModel.getEstimatedReturns().toString(),            sipModel.getTotalValue().toString()        )    }}

Step 7: Working with SipCalculatorView.kt class

This class implements the SipCalculatorViewInterface and the context of this class is provided to the Presenter class to get the calculated result. And it creates instances of each UI element and after getting calculated results from the presenter it updates UI elements. To implement the same invoke the following code inside the SipCalculatorView.kt class. Comments are added for better understanding.