Skip to content

Commit

Permalink
TypesExercises solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukestephenson-zendesk committed Oct 10, 2023
1 parent 51e277c commit 9d3acdd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
35 changes: 27 additions & 8 deletions src/main/scala/introcourse/level02/TypesExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object TypesExercises {
**/
def showPerson1(person: Person): String =
person match {
case Person(name, age) => s"${???} is ${???} years old"
case Person(name, age) => s"$name is ${age} years old"
}

/**
Expand All @@ -58,7 +58,7 @@ object TypesExercises {
* Hint: Navigate the Person class' fields using the "." operator
*/
def showPerson2(person: Person): String =
s"${???} is ${???} years old"
s"${person.name} is ${person.age} years old"

/**
* Write a function that changes the age of a person.
Expand All @@ -72,7 +72,7 @@ object TypesExercises {
*
* Hint: Use the .copy method
*/
def changeAge(newAge: Int, person: Person): Person = ???
def changeAge(newAge: Int, person: Person): Person = person.copy(age = newAge)

/**
* Let's look at another data type.
Expand All @@ -95,7 +95,7 @@ object TypesExercises {
*
* You can solve this like how you solved `showPerson1` or `showPerson2`.
*/
def showWallet(wallet: Wallet): String = ???
def showWallet(wallet: Wallet): String = s"The wallet amount is ${wallet.amount}"

/**
* Here is another example of working with immutable values.
Expand All @@ -106,7 +106,10 @@ object TypesExercises {
*
* Hint: You need to calculate the new amount first.
**/
def purchase(cost: Double, wallet: Wallet): Wallet = ???
def purchase(cost: Double, wallet: Wallet): Wallet = {
val newBalance = wallet.amount - cost
wallet.copy(amount = newBalance)
}

/**
* *********************************************
Expand Down Expand Up @@ -140,7 +143,15 @@ object TypesExercises {
/**
* Implement the following showTrafficLightStr function to pass all your tests!
*/
def showTrafficLightStr(trafficLight: String): String = ???
def showTrafficLightStr(trafficLight: String): String = {
trafficLight match {
case "red" => "The traffic light is red"
case "yellow" => "The traffic light is yellow"
case "green" => "The traffic light is green"
case "flashing" => "The traffic light is flashing"
case _ => "The traffic light is invalid"
}
}


/**
Expand Down Expand Up @@ -180,7 +191,7 @@ object TypesExercises {
* This technique helps you make invalid states/values irrepresentable in your programs
*/
enum TrafficLight {
case Red, Yellow, Green
case Red, Yellow, Green, Flashing
}

// Scala 2.12 equivalent syntax
Expand Down Expand Up @@ -209,7 +220,15 @@ object TypesExercises {
* Hint: Use pattern matching
**/

def showTrafficLight(trafficLight: TrafficLight): String = ???
def showTrafficLight(trafficLight: TrafficLight): String = {
val colour = trafficLight match
case TrafficLight.Red => "red"
case TrafficLight.Yellow => "yellow"
case TrafficLight.Green => "green"
case TrafficLight.Flashing => "flashing"

s"The traffic light is $colour"
}

/**
* *********************************************************
Expand Down
12 changes: 9 additions & 3 deletions src/test/scala/introcourse/level02/TypesExercisesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ class TypesExercisesTest extends AnyFunSpec with TypeCheckedTripleEquals {
}

it("should return a default on other inputs") {
???
val str = showTrafficLightStr("foo")

assert(str === "The traffic light is invalid")
}

it("should show flashing") {
???
val str = showTrafficLightStr("flashing")

assert(str === "The traffic light is flashing")
}

}
Expand All @@ -120,7 +124,9 @@ class TypesExercisesTest extends AnyFunSpec with TypeCheckedTripleEquals {
}

it("should show Flashing") {
???
val str = showTrafficLight(Flashing)

assert(str === "The traffic light is flashing")
}

}
Expand Down

0 comments on commit 9d3acdd

Please sign in to comment.