Day38 of #100DaysOfCode

Kushagra Kesav
3 min readMar 16, 2022

--

#CodeTogether Day 38/100

Hii folks 🙌

Today I developed a “Lemonade app” by myself. It was fun building one. Enjoyed many glasses of Lemonade 😄

Unit 1: Kotlin basics

Source: https://developer.android.com/courses/android-basics-kotlin/course

  • The lemonade app will consist of a single screen. When users first launch the app, they’re greeted with a prompt to pick a lemon by tapping a picture of a lemon tree.
Screen1
  • Tapping the lemon tree presents the user with a lemon that they can tap to “squeeze” for an unspecified number of times (the exact number of required squeezes is randomly generated) before moving to the next screen.
Screen2
  • Once the user has tapped to squeeze the lemon the correct number of times, they will see an image of a glass to “drink” the lemonade.
Screen3
  • After clicking to drink the lemonade, the glass appears empty, and the user can tap the image again to return to the first screen, and select another lemon from the tree.
Screen4
  • The different states of the app (whether the user is selecting a lemon, squeezing the lemon, drinking the lemonade, and, finally, the empty glass) is represented by something called a state machine.

* This method determines the state and proceeds with the correct action.
*/
private fun clickLemonImage() {
when (lemonadeState) {

SELECT -> {
lemonadeState = SQUEEZE
lemonSize = lemonTree.pick()
squeezeCount = 0
}
SQUEEZE -> {
squeezeCount += 1
lemonSize -= 1
lemonadeState = if (lemonSize == 0) {
DRINK
} else SQUEEZE
}
DRINK -> {
lemonadeState = RESTART
lemonSize = -1
}
RESTART -> lemonadeState = SELECT
}
setViewElements()
}
  • Then we set up the view elements according to the state.
/** Set up the view elements according to the state.
*/
private fun setViewElements() {
val textAction: TextView = findViewById(R.id.text_action)
// val lemonImage: ImageView = findViewById(R.id.image_lemon_state)

when (lemonadeState) {
SELECT -> {
textAction.text = getString(R.string.lemon_select)
lemonImage?.setImageResource(R.drawable.lemon_tree)
}
SQUEEZE -> {
textAction.text = getString(R.string.lemon_squeeze)
lemonImage?.setImageResource(R.drawable.lemon_squeeze)
}
DRINK -> {
textAction.text = getString(R.string.lemon_drink)
lemonImage?.setImageResource(R.drawable.lemon_drink)
}
RESTART -> {
textAction.text = getString(R.string.lemon_empty_glass)
lemonImage?.setImageResource(R.drawable.lemon_restart)
}
}
}
  • Now, tested the app and all the 6 test cases have been passed. ✅
TestCase

That is all for Day38 ✅

Thanks for reading, See you tomorrow!

--

--