# Where to put my var's and val's

Newbie here …

As an excercise I am trying to do the following:
Calculate the fuel required for an aeroplane which has 3 tanks (Tank No 1, Center Tank, Tank No 2). The fuel is delivered (by the fuel truck) in `lt` (liters) but must be converted to `kg` (by multiplying the fuel delivered in `lt` by the `S.G` (specific gravity)) in order for the pilot to calculate the take off weight and center of gravity of the aeroplane.

Now there are a few variables to work with:
1 - Tank No 1 (max 6000 liters)
2 - Tank Center (max 10000 liters)
3 - Tank No 2 (max 6000 liters)
4 - Fuel already on-board the aeroplane before fueling (F.O.B) which is the fuel added together in the tanks in `kg`
5 - Fuel required (uplift) in `kg`
6 - Fuel delivered by the fuel truck in `lt`
7 - S.G (specific gravity) which is between 0.775 grams/Liter and 0.840 grams/Liter (this will vary depending on the temperature at the time of fueling)
8 - The fuel delivered by the truck (which is now in the tanks) in `kg` AFTER it has been converted from `lt` to `kg` by DIVIDING the fuel required in `kg` by the S.G

When all calculations are done I must get a conversion factor of between 99.5% and 101.5% by using the following formula:

`((fuel required (kg) - fuel on board the aeroplane (kg)) * 100) / fuel delivered (lt) * S.G`

Example:
Before fueling the aeroplane there is 5100 kg in Tank No 1, 0 kg in the center tank, and 5100 kg in Tank No 2. The pilot requires 18000 kg for his next flight. The refuel operator reports the S.G to be 0.798.

The engineer then subtracts the fuel on board the aeroplane `(kg)` from the required fuel `(kg)` and DIVIDES that with the S.G to get the total liters the fuel truck needs to pump into the aeroplane:
`(18000 - 10200) / 0.798 = 9774 liters` the fuel truck has to deliver.

After fueling the aeroplane I calculate my final percentage value as follows:

`((fuel required for flight (18000kg) - fuel on board the aeroplane (10200kg)) * 100) / (fuel delivered in liters (9774) * S.G (0.798)`

The percentage I get is `100%` which is in limits.

You will see that I have a seekbar who’s min value is `0` and max value is `1` This is to mimic the possible S.G range I can have (I know I said it is between 0.775 and 0.840). This seekbar is just to make it easier to quickly see if I move it to min or max what the effect it will have on the final percentage value. It is not necessary but a `nice to have`.

The code works more or less, however I want to tidy up the code a bit (preferably by NOT using classes). And yes, I know the interface sucks and the code sucks even more, but I am learning still.

Now, here is my problem
If I put the `var`'s and `val`'s inside the `buttonCalculateFormula.setOnClickListener` the app works, however when I put them outside of said listener (to have them available anywhere) the app crashes. Also, I am failing to properly round the floats to 3 digits after the decimal point. And yes, I am aware that `(findViewById(R.id.var1) as EditText).text` is the same as `findViewById<EditText>(R.id.var1).text`

Here is activity_main.xml:

``````<EditText
android:id="@+id/editTextTank1"
android:text="@string/editTextTank1Hint_0"
android:digits="0123456789."
android:layout_width="90dp"
android:layout_height="38dp"
android:ems="10"
android:inputType="numberDecimal|number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:importantForAutofill="no" />

<EditText
android:id="@+id/editTextTankCenter"
android:text="@string/editTextTankCenterHint_0"
android:digits="0123456789."
android:layout_width="90dp"
android:layout_height="38dp"
android:ems="10"
android:inputType="numberDecimal|number"
app:layout_constraintStart_toEndOf="@+id/editTextTank1"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextTank2"
android:text="@string/editTextTank2Hint_0"
android:digits="0123456789."
android:label="Tank 2"
android:layout_width="90dp"
android:layout_height="38dp"
android:ems="10"
android:inputType="numberDecimal|number"
app:layout_constraintStart_toEndOf="@+id/editTextTankCenter"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textViewFOB"
android:layout_width="90dp"
android:layout_height="38dp"
android:hint="@string/textViewFOBHint"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/buttonCalculate"
android:text="@string/buttonCalculate"
android:layout_width="60dp"
android:layout_height="38dp"
android:hint="@string/buttonCalculateFOBHint"
android:textSize="9dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewFuelUpliftInLt" />

<EditText
android:id="@+id/editTextFuelRequiredInKg"
android:text="@string/editTextFuelRequiredInKgHint_0"
android:digits="0123456789."
android:inputType="numberDecimal|number"
android:layout_width="90dp"
android:layout_height="38dp"
android:ems="10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTank1" />

<TextView
android:id="@+id/textViewFuelUpliftInKg"
android:hint="@string/textViewFuelUpliftInKgHint"
android:layout_width="90dp"
android:layout_height="38dp"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewFOB" />

<EditText
android:id="@+id/editTextSpecificGravity"
android:text="@string/editTextSpecificGravityHint_0"
android:digits="0123456789."
android:inputType="numberDecimal|number"
android:layout_width="90dp"
android:layout_height="38dp"
android:ems="10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextFuelRequiredInKg" />

<TextView
android:id="@+id/textViewFuelUpliftInLt"
android:hint="@string/textViewFuelUpliftInLtHint"
android:layout_width="90dp"
android:layout_height="38dp"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewFuelUpliftInKg" />

<TextView
android:id="@+id/textViewPercentage"
android:hint="@string/textViewPercentage"
android:layout_width="160dp"
android:layout_height="38dp"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonCalculate" />

<SeekBar
android:id="@+id/seekBarSpecificGravity"
android:layout_width="200dp"
android:layout_height="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextSpecificGravity" />
``````

`strings.xml`:

``````<resources>
<string name="app_name">fuel_calculations</string>
<string name="editTextTank1Hint_0">0</string>
<string name="editTextTankCenterHint_0">0</string>
<string name="editTextTank2Hint_0">0</string>
<string name="textViewFOBHint">F.O.B</string>
<string name="buttonCalculate">Cal</string>
<string name="buttonCalculateFOBHint">F.O.B</string>
<string name="editTextFuelRequiredInKgHint_0">0</string>
<string name="textViewFuelUpliftInKgHint">Uplift(KG)</string>
<string name="editTextSpecificGravityHint_0">0</string>
<string name="textViewFuelUpliftInLtHint">Uplift(LT)</string>
<string name="textViewPercentage">Percentage</string>
``````

`MainActivity.kt`:

``````package com.example.fuel_calculations

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Button
import android.widget.EditText
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.fuelcalculation3.R
import kotlin.math.roundToLong

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

val buttonCalculateFormula = findViewById<Button>(R.id.buttonCalculate);
val updateTextViewFOB = findViewById<TextView>(R.id.textViewFOB);
val updateTextViewFuelRequiredInKg = findViewById<TextView>(R.id.textViewFuelUpliftInKg);
val updateTextViewFuelUpliftInLt = findViewById<TextView>(R.id.textViewFuelUpliftInLt);
val updateEditTextSpecificGravity = findViewById<EditText>(R.id.editTextSpecificGravity);
val updateTextViewPercentage = findViewById<TextView>(R.id.textViewPercentage);
val getSeekBarSpecificGravity = findViewById<SeekBar>(R.id.seekBarSpecificGravity)
var intEditTextTank1 = (findViewById(R.id.editTextTank1) as EditText).text.toString().toInt()
var intEditTextTankCenter = (findViewById(R.id.editTextTankCenter) as EditText).text.toString().toInt()
var intEditTextTank2 = (findViewById(R.id.editTextTank2) as EditText).text.toString().toInt()
var fuelOnBoard = intEditTextTank1 + intEditTextTankCenter + intEditTextTank2
var intEditTextFuelRequiredInKg = (findViewById(R.id.editTextFuelRequiredInKg) as EditText).text.toString().toInt()
var intEditTextSpecificGravity = (findViewById(R.id.editTextSpecificGravity) as EditText).text.toString().toDouble()
var upliftRequiredInKg = intEditTextFuelRequiredInKg - fuelOnBoard
var upliftRequiredInLt = (upliftRequiredInKg / intEditTextSpecificGravity).roundToLong()
var calculatedPercentage = ((intEditTextFuelRequiredInKg - fuelOnBoard ) * 100 ) / ( upliftRequiredInLt * intEditTextSpecificGravity ).toFloat()

getSeekBarSpecificGravity.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean){
val updatedSeekBarValue = (i * 0.01)
//To change the value in and EditText we use "setText"
updateEditTextSpecificGravity.setText("\$updatedSeekBarValue")
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {
//                Toast.makeText(applicationContext, "stop tracking", Toast.LENGTH_SHORT).show()
}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
//                Toast.makeText(applicationContext, "start tracking", Toast.LENGTH_SHORT).show()
}
})

override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
updateTextViewPercentage.setText("\$calculatedPercentage")
}
})

buttonCalculateFormula.setOnClickListener {
updateTextViewFOB.text = fuelOnBoard.toString()+" Kg(FOB)"
updateTextViewFuelRequiredInKg.text = upliftRequiredInKg.toString()+" Kg(Req)"
updateTextViewFuelUpliftInLt.text = upliftRequiredInLt.toString()+" Lt (required)"

updateTextViewPercentage.text = calculatedPercentage.toString()+"%"
}
}
``````

}`