Beginner question - any idea why the value from lemonSize in SELECT is not carrying over to SQUEEZE when switching lemonadeState? Any help is greatly appreciated :)
The app/code is part of intro to Kotlin course by Google which is for creating a lemonade app that rotates between screens https://developer.android.com/codelabs/basic-android-kotlin-training-project-lemonade?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-four%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-project-lemonade#0
Here's where i think the problem is (I attached logs below that):
// Default lemonSize to -1
private var lemonSize = -1
// Default the squeezeCount to -1
private var squeezeCount = -1
private var lemonTree = LemonTree()
when (lemonadeState) {
SELECT -> {
Log.d(TAG, "lemonadeState at beginning of SELECT *should be SELECT* lemonadeState = $lemonadeState")
// Set the lemonSize (the number of squeezes needed) by calling the pick() method
var lemonSize = LemonTree()//UPDATE
Log.d(TAG, "randomly generated lemonSize using LemonTree -> lemonSize = $lemonSize")
// Setting the squeezeCount (the number of times the user has squeezed the lemon) to 0.
squeezeCount = 0
Log.d(TAG, " setting squeezeCount to 0 -> squeezeCount = $squeezeCount")
// Transition to the SQUEEZE state
lemonadeState = SQUEEZE
Log.d(TAG, "lemonadeState at bottom of SELECT *should be SQUEEZE* lemonadeState = $lemonadeState")
}
// TODO: When the image is clicked in the SQUEEZE state the squeezeCount needs to be
// INCREASED by 1 and lemonSize needs to be DECREASED by 1.
// - If the lemonSize has reached 0, it has been juiced and the state should become DRINK
// - Additionally, lemonSize is no longer relevant and should be set to -1
SQUEEZE -> {
Log.d(TAG, "at top of SQUEEZE *lemonSize should be 2-4* lemonSize = $lemonSize")
Log.d(TAG, "at top of SQUEEZE *lemonadeState should be SQUEEZE* lemonadeState = $lemonadeState")
//squeezeCount needs to be INCREASED by 1
squeezeCount += 1
//lemonSize needs to be DECREASED by 1.
Log.d(TAG, "decreasing lemonSize by 1")
Log.d(TAG, "lemonSize before decreasing by 1 lemonSize = $lemonSize")
lemonSize = lemonSize - 1
Log.d(TAG, "lemonSize after decreasing by 1 lemonSize = $lemonSize")
//lemonSize = 0//REMOVE once done with testing
//Log.d(TAG, "lemonSizeHere = $lemonSize ** REMOVE")
//If the lemonSize has reached 0, it has been juiced and the state should become DRINK
if (lemonSize == 0) {
lemonadeState = DRINK
//lemonSize = -1
Log.d(TAG, "lemonadeState when lemonSize == 0 lemonadeState = $lemonadeState")
}
}
/**
* A Lemon tree class with a method to "pick" a lemon. The "size" of the lemon is randomized
* and determines how many times a lemon needs to be squeezed before you get lemonade.
*/
class LemonTree {
fun pick(): Int {
return (2..4).random()
}
}
Logs:
2022-06-17 16:20:46.837 8549-8578/com.example.lemonade W/OpenGLRenderer: Failed to initialize 101010-2 format, error = EGL_SUCCESS
2022-06-17 16:20:49.788 8549-8549/com.example.lemonade D/MainActivity: lemonadeState at beginning of SELECT *should be SELECT* lemonadeState = select
2022-06-17 16:20:49.789 8549-8549/com.example.lemonade D/MainActivity: randomly generated lemonSize using LemonTree -> lemonSize = com.example.lemonade.LemonTree@5c31497
2022-06-17 16:20:49.790 8549-8549/com.example.lemonade D/MainActivity: setting squeezeCount to 0 -> squeezeCount = 0
2022-06-17 16:20:49.793 8549-8549/com.example.lemonade D/MainActivity: lemonadeState at bottom of SELECT *should be SQUEEZE* lemonadeState = squeeze
2022-06-17 16:21:04.177 8549-8549/com.example.lemonade D/MainActivity: at top of SQUEEZE *lemonSize should be 2-4* lemonSize = -1
2022-06-17 16:21:04.179 8549-8549/com.example.lemonade D/MainActivity: at top of SQUEEZE *lemonadeState should be SQUEEZE* lemonadeState = squeeze
2022-06-17 16:21:04.184 8549-8549/com.example.lemonade D/MainActivity: lemonSize before decreasing by 1 lemonSize = -1
2022-06-17 16:21:04.186 8549-8549/com.example.lemonade D/MainActivity: lemonSize after decreasing by 1 lemonSize = -2