Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/src/androidTest/java/com/example/lemonade/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ import androidx.test.espresso.ViewInteraction
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.espresso.matcher.ViewMatchers.*
import com.example.lemonade.DrawableMatcher.withDrawable
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.junit.Rule

/**
* The lemonade app is effectively a state machine.
Expand All @@ -52,9 +48,10 @@ open class BaseTest {
*/
fun testState(textActionResource: Int, drawableResource: Int) {
onView(withId(R.id.text_action))
.check(matches(ViewMatchers.withText(textActionResource)))
.check(matches(withText(textActionResource)))
onView(withId(R.id.image_lemon_state)).check(
matches(withDrawable(drawableResource)))
matches(withDrawable(drawableResource))
)
}

/**
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/lemonade/LemonTree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.lemonade



/**
* 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()
}
}
68 changes: 50 additions & 18 deletions app/src/main/java/com/example/lemonade/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ class MainActivity : AppCompatActivity() {
lemonImage = findViewById(R.id.image_lemon_state)
setViewElements()
lemonImage!!.setOnClickListener {
// TODO: call the method that handles the state when the image is clicked
clickLemonImage()
}
lemonImage!!.setOnLongClickListener {
// TODO: replace 'false' with a call to the function that shows the squeeze count
false
showSnackbar()
}



}

/**
Expand All @@ -91,6 +93,32 @@ class MainActivity : AppCompatActivity() {
* This method determines the state and proceeds with the correct action.
*/
private fun clickLemonImage() {

when(lemonadeState){
SELECT -> {
lemonadeState = SQUEEZE
lemonSize = lemonTree.pick()
squeezeCount = 0
setViewElements()
}
SQUEEZE -> {
squeezeCount++
lemonSize--
if (lemonSize == 0) {
lemonadeState = DRINK
setViewElements()
}
}
DRINK -> {
lemonadeState = RESTART
setViewElements()
}
RESTART -> {
lemonadeState = SELECT
setViewElements()
}

}
// TODO: use a conditional statement like 'if' or 'when' to track the lemonadeState
// when the image is clicked we may need to change state to the next step in the
// lemonade making progression (or at least make some changes to the current state in the
Expand Down Expand Up @@ -118,14 +146,26 @@ class MainActivity : AppCompatActivity() {
*/
private fun setViewElements() {
val textAction: TextView = findViewById(R.id.text_action)
// TODO: set up a conditional that tracks the lemonadeState

// TODO: for each state, the textAction TextView should be set to the corresponding string from
// the string resources file. The strings are named to match the state

// TODO: Additionally, for each state, the lemonImage should be set to the corresponding
// drawable from the drawable resources. The drawables have the same names as the strings
// but remember that they are drawables, not strings.
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)
}
}
}

/**
Expand All @@ -147,12 +187,4 @@ class MainActivity : AppCompatActivity() {
}
}

/**
* 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()
}
}

13 changes: 13 additions & 0 deletions app/src/main/java/com/example/lemonade/data/Dwelling.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.lemonade.data

abstract class Dwelling(private var residents: Int) {

abstract val buildingMaterial: String
abstract val capacity: Int

fun hasRoom(): Boolean {
return capacity > residents
}

abstract fun floorArea(): Double
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/lemonade/data/RoundHut.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.lemonade.data

import java.lang.Math.PI

open class RoundHut(residents:Int, val radius:Double) : Dwelling(residents) {
override val buildingMaterial: String
get() = "Straw"
override val capacity: Int
get() = 4

override fun floorArea(): Double {
return PI * radius * radius
}

fun calculateMaxCarpetLength():Double{
return kotlin.math.sqrt(2.0) * radius
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/lemonade/data/RoundTower.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.lemonade.data

class RoundTower(
private val residents: Int,
radius: Double,
val floors: Int = 2) : RoundHut(residents, radius) {



override val buildingMaterial: String
get() = "Stone"

override val capacity: Int
get() = 4 * floors

override fun floorArea(): Double {
return super.floorArea() * floors
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/lemonade/data/SquareCabin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.lemonade.data

class SquareCabin(residents:Int,val length: Double) : Dwelling(residents) {
override val buildingMaterial: String
get() = "Wood"
override val capacity: Int
get() = 6


override fun floorArea(): Double {
return length * length
}
}
15 changes: 7 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2021 The Android Open Source Project.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,8 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraint_Layout"
Expand All @@ -28,19 +26,20 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lemon_select"
android:textSize="18sp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="parent"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@id/image_lemon_state"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/image_lemon_state"/>
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/image_lemon_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lemon_tree"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
52 changes: 52 additions & 0 deletions app/src/test/java/com/example/lemonade/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.lemonade

import com.example.lemonade.data.RoundHut
import com.example.lemonade.data.RoundTower
import com.example.lemonade.data.SquareCabin
import org.junit.Test


/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {

@Test
fun generates_number() {

val squareCabin = SquareCabin(6,50.0)

//println("\nSquare Cabin\n============")
//println("Capacity: ${squareCabin.capacity}")
//println("Material: ${squareCabin.buildingMaterial}")
//println("Has room? ${squareCabin.hasRoom()}")

with(squareCabin){
println("\nSquare Cabin\n============")
println("Capacity: $capacity")
println("Material: $buildingMaterial")
println("Has room? ${hasRoom()}")
println("floorArea? ${floorArea()}")
}
val roundHut = RoundHut(3,10.0)
with(roundHut) {
println("\nRound Hut\n=========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Has room? ${hasRoom()}")
println("carpet size ${calculateMaxCarpetLength()}")
}

val roundTower = RoundTower(4,15.5)
with(roundTower) {
println("\nRound Tower\n==========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("calculateMaxCarpetLength ${calculateMaxCarpetLength()}")
}
}


}