Advertisement
anify

Main Activity - Lemonade Project

Feb 27th, 2023 (edited)
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 7.63 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2021 The Android Open Source Project.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.example.lemonade
  17.  
  18. import androidx.appcompat.app.AppCompatActivity
  19. import android.os.Bundle
  20. import android.widget.ImageView
  21. import android.widget.TextView
  22. import com.google.android.material.snackbar.Snackbar
  23.  
  24. class MainActivity : AppCompatActivity() {
  25.  
  26.     /**
  27.      * DO NOT ALTER ANY VARIABLE OR VALUE NAMES OR THEIR INITIAL VALUES.
  28.      *
  29.      * Anything labeled var instead of val is expected to be changed in the functions but DO NOT
  30.      * alter their initial values declared here, this could cause the app to not function properly.
  31.      */
  32.     private val LEMONADE_STATE = "LEMONADE_STATE"
  33.     private val LEMON_SIZE = "LEMON_SIZE"
  34.     private val SQUEEZE_COUNT = "SQUEEZE_COUNT"
  35.     // SELECT represents the "pick lemon" state
  36.     private val SELECT = "select"
  37.     // SQUEEZE represents the "squeeze lemon" state
  38.     private val SQUEEZE = "squeeze"
  39.     // DRINK represents the "drink lemonade" state
  40.     private val DRINK = "drink"
  41.     // RESTART represents the state where the lemonade has been drunk and the glass is empty
  42.     private val RESTART = "restart"
  43.     // Default the state to select
  44.     private var lemonadeState = "select"
  45.     // Default lemonSize to -1
  46.     private var lemonSize = -1
  47.     // Default the squeezeCount to -1
  48.     private var squeezeCount = -1
  49.  
  50.     private var lemonTree = LemonTree()
  51.     private var lemonImage: ImageView? = null
  52.  
  53.  
  54.     override fun onCreate(savedInstanceState: Bundle?) {
  55.         super.onCreate(savedInstanceState)
  56.         setContentView(R.layout.activity_main)
  57.  
  58.         // === DO NOT ALTER THE CODE IN THE FOLLOWING IF STATEMENT ===
  59.         if (savedInstanceState != null) {
  60.             lemonadeState = savedInstanceState.getString(LEMONADE_STATE, "select")
  61.             lemonSize = savedInstanceState.getInt(LEMON_SIZE, -1)
  62.             squeezeCount = savedInstanceState.getInt(SQUEEZE_COUNT, -1)
  63.         }
  64.         // === END IF STATEMENT ===
  65.  
  66.         lemonImage = findViewById(R.id.image_lemon_state)
  67.         setViewElements()
  68.         lemonImage!!.setOnClickListener {
  69.             // TODO: call the method that handles the state when the image is clicked
  70.             clickLemonImage()
  71.         }
  72.         lemonImage!!.setOnLongClickListener {
  73.             // TODO: replace 'false' with a call to the function that shows the squeeze count
  74.             showSnackbar()
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * === DO NOT ALTER THIS METHOD ===
  80.      *
  81.      * This method saves the state of the app if it is put in the background.
  82.      */
  83.     override fun onSaveInstanceState(outState: Bundle) {
  84.         outState.putString(LEMONADE_STATE, lemonadeState)
  85.         outState.putInt(LEMON_SIZE, lemonSize)
  86.         outState.putInt(SQUEEZE_COUNT, squeezeCount)
  87.         super.onSaveInstanceState(outState)
  88.     }
  89.  
  90.     /**
  91.      * Clicking will elicit a different response depending on the state.
  92.      * This method determines the state and proceeds with the correct action.
  93.      */
  94.     private fun clickLemonImage() {
  95.         // TODO: use a conditional statement like 'if' or 'when' to track the lemonadeState
  96.         //  when the image is clicked we may need to change state to the next step in the
  97.         //  lemonade making progression (or at least make some changes to the current state in the
  98.         //  case of squeezing the lemon). That should be done in this conditional statement
  99.         when(lemonadeState){
  100.             SELECT -> {
  101.                 lemonadeState = SQUEEZE
  102.                 lemonSize = lemonTree.pick()
  103.                 squeezeCount = 0
  104.             }
  105.             SQUEEZE -> {
  106.                 squeezeCount++
  107.                 lemonSize--
  108.                 if (lemonSize == 0){
  109.                     lemonadeState = DRINK
  110.                     lemonSize = -1
  111.                 }
  112.             }
  113.             DRINK -> {
  114.                 lemonadeState = RESTART
  115.             }
  116.             RESTART -> lemonadeState = SELECT
  117.         }
  118.  
  119.  
  120.             // TODO: When the image is clicked in the SELECT state, the state should become SQUEEZE
  121.             //  - The lemonSize variable needs to be set using the 'pick()' method in the LemonTree class
  122.             //  - The squeezeCount should be 0 since we haven't squeezed any lemons just yet.
  123.  
  124.             // TODO: When the image is clicked in the SQUEEZE state the squeezeCount needs to be
  125.             //  INCREASED by 1 and lemonSize needs to be DECREASED by 1.
  126.             //  - If the lemonSize has reached 0, it has been juiced and the state should become DRINK
  127.             //  - Additionally, lemonSize is no longer relevant and should be set to -1
  128.  
  129.             // TODO: When the image is clicked in the DRINK state the state should become RESTART
  130.  
  131.             // TODO: When the image is clicked in the RESTART state the state should become SELECT
  132.  
  133.             // TODO: lastly, before the function terminates we need to set the view elements so that the
  134.             //  UI can reflect the correct state
  135.     }
  136.  
  137.     /**
  138.      * Set up the view elements according to the state.
  139.      */
  140.     private fun setViewElements() {
  141.         val textAction: TextView = findViewById(R.id.text_action)
  142.         // TODO: set up a conditional that tracks the lemonadeState
  143.  
  144.         // TODO: for each state, the textAction TextView should be set to the corresponding string from
  145.         //  the string resources file. The strings are named to match the state
  146.  
  147.         // TODO: Additionally, for each state, the lemonImage should be set to the corresponding
  148.         //  drawable from the drawable resources. The drawables have the same names as the strings
  149.         //  but remember that they are drawables, not strings.
  150.  
  151.         val stateImage = when(lemonadeState) {
  152.             SELECT -> R.drawable.lemon_tree
  153.             SQUEEZE -> R.drawable.lemon_squeeze
  154.             DRINK -> R.drawable.lemon_drink
  155.             RESTART -> R.drawable.lemon_restart
  156.             else -> R.drawable.lemon_restart
  157.         }
  158.  
  159.         val stateText = when(lemonadeState) {
  160.             SELECT -> R.string.lemon_select
  161.             SQUEEZE -> R.string.lemon_squeeze
  162.             DRINK -> R.string.lemon_drink
  163.             else -> R.string.lemon_empty_glass
  164.         }
  165.  
  166.         textAction.setText(stateText)
  167.         lemonImage?.setImageResource(stateImage)
  168.     }
  169.  
  170.     /**
  171.      * === DO NOT ALTER THIS METHOD ===
  172.      *
  173.      * Long clicking the lemon image will show how many times the lemon has been squeezed.
  174.      */
  175.     private fun showSnackbar(): Boolean {
  176.         if (lemonadeState != SQUEEZE) {
  177.             return false
  178.         }
  179.         val squeezeText = getString(R.string.squeeze_count, squeezeCount)
  180.         Snackbar.make(
  181.             findViewById(R.id.constraint_Layout),
  182.             squeezeText,
  183.             Snackbar.LENGTH_SHORT
  184.         ).show()
  185.         return true
  186.     }
  187. }
  188.  
  189. /**
  190.  * A Lemon tree class with a method to "pick" a lemon. The "size" of the lemon is randomized
  191.  * and determines how many times a lemon needs to be squeezed before you get lemonade.
  192.  */
  193. class LemonTree {
  194.     fun pick(): Int {
  195.         return (2..4).random()
  196.     }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement