Calculate the time it takes to achieve your money goal in Kotlin I am writing a very simple test application in Kotlin for Android, where I just want to calculate the time it takes for someone to reach a specific amount of money, taking into count his salary and increase in salary each year.

I am very new to Android Studio, which is why I don't really know about the best practises yet. Currently, I wonder wheather I should put the EditText into global scope, so that I can access them without passing any parameters to all the functions.

This is my class:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById(R.id.resultsButton).setOnClickListener {

            val moneyField: EditText = findViewById(R.id.moneyField) as EditText
            val moneyGoal: Float = getNumberFromTextField(moneyField)

            val salaryField: EditText = findViewById(R.id.salaryField) as EditText
            val salary: Float = getNumberFromTextField(salaryField)

            val increaseField: EditText = findViewById(R.id.increaseField) as EditText
            val increase: Float = getNumberFromTextField(increaseField)

            calculateAndDisplayTimeUntilMoneyGoal(moneyGoal, salary, increase)
        }
    }

    fun getNumberFromTextField(field: EditText): Float {
        return field.text.toString().toFloat()
    }

    fun calculateAndDisplayTimeUntilMoneyGoal(goal: Float, salary: Float, increaseInPercent: Float){
        var moneyEarned: Float = 0f
        var currentSalary = salary
        var months: Int = 0
        var yearsPassed: Int = 0

        while (moneyEarned &lt; goal){
            moneyEarned += currentSalary
            months++

            if (oneYearPassed(months)){
                currentSalary += increaseSalary(currentSalary, increaseInPercent)
                months = 0
                yearsPassed++
            }
        }

        val resultsString = "After $yearsPassed years and $months months you will have earned $moneyEarned!"
        (findViewById(R.id.displayText) as TextView).text = resultsString
    }

    fun oneYearPassed(months: Int): Boolean = (months == 12)

    fun increaseSalary(currentSalary: Float, increaseInPercent: Float): Float {
        return (currentSalary / 100) * increaseInPercent
    }
}


Any suggestions on how I can improve it? (Altough Kotlin is new to me, I have experience in other programming languages)
Hi yes I would store the textEdit references in a field of the class (not actual global scope), if you want to access them more often. 

Running through the view hierarchy to find it on each click is not the most performant thing for more complex layout and so not best practice. These references stay valid until onDestroyed is called, so it's ok to store them.

BUT with Kotlin you would usually do it like this without casts etc. https://kotlinlang.org/docs/tutorials/android-plugin.html

import kotlinx.android.synthetic.main.activity_main.*

class MyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        resultsButton.setOnClickListener {
            // Either try catch NumberFormatException or validating the string with
            // TextWatcher will be required to avoid problems here, but also in the original code.
            calculateAndDisplayTimeUntilMoneyGoal(
                goal: Float.valueOf(moneyField.getText().toString()),
                salary: Float.valueOf(salaryField.getText().toString()), 
                increaseInPercent: Float.valueOf(increaseField.getText().toString()))
        }
    }
};