Skip to content

Introduction to traits #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 26, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.scala.traits

trait Composition extends Orchestration with Mixing {
var composer: String

def compose(): String

var studio: String

def getStudio(): String = s"Composed at studio $studio"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.scala.traits

trait Mixing {
var mixer: String
val qualityRatio: Double = 3.14

def algorithm: MixingAlgorithm = HighInstrumentalQuality
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.scala.traits

sealed trait MixingAlgorithm

case object LowInstrumentalQuality extends MixingAlgorithm {
override def toString(): String = "Low instrumental quality"
}

case object HighInstrumentalQuality extends MixingAlgorithm {
override def toString(): String = "High instrumental quality"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.scala.traits

trait Orchestration {
var orchestra: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung.scala.traits

class RecordLabel
25 changes: 25 additions & 0 deletions core-scala/src/main/scala/com/baeldung/scala/traits/Score.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.scala.traits

class Score(var composer: String,
var engineer: String,
var orchestra: String,
var mixer: String,
override val qualityRatio: Double,
var studio: String)
extends RecordLabel with Composition with SoundProduction {

override def compose(): String =
s"""The score is composed by $composer,
|Orchestration by $orchestra,
|Mixed by $mixer""".stripMargin

override def produce(): String = s"The score is produced by $engineer"

override def algorithm(): MixingAlgorithm = {
if (qualityRatio < 3) LowInstrumentalQuality
else super.algorithm
}

override def getStudio(): String =
super[Composition].getStudio() + ", " + super[SoundProduction].getStudio()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.scala.traits

trait SoundProduction {
this: RecordLabel =>
var engineer: String

def produce(): String

var studio: String

def getStudio(): String = s"Produced at studio $studio"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.scala.traits

trait Vocals {
val sing: String = "Vocals mixin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.baeldung.scala.traits

import org.junit.Assert.assertEquals
import org.junit.Test

class ScoreUnitTest {

@Test
def givenScore_whenComposeCalled_thenCompositionIsReturned() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)

assertEquals(score.compose(),
s"""The score is composed by $composer,
|Orchestration by $orchestra,
|Mixed by $mixer""".stripMargin)
}

@Test
def givenScore_whenProduceCalled_thenSoundProductionIsReturned() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 3, studio)

assertEquals(score.produce(), s"The score is produced by $engineer")
}

@Test
def givenScore_whenLowQualityRatioSet_thenCorrectAlgorithmIsReturned() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 1, studio)

assertEquals(score.algorithm().toString, "Low instrumental quality")
}

@Test
def givenScore_whenHighQualityRatioSet_thenCorrectAlgorithmIsReturned() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)

assertEquals(score.algorithm().toString, "High instrumental quality")
}

@Test
def givenScore_whenVocalsMixinAttached_thenSingCanBeCalled() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 10, studio) with Vocals

assertEquals(score.sing, "Vocals mixin")
}

@Test
def givenScore_whenGetStudioCalled_thenStudiosAreReturned() = {

val composer = "Hans Zimmer"
val engineer = "Matt Dunkley"
val orchestra = "Berlin Philharmonic"
val mixer = "Dave Stewart"
val studio = "Abbey Studios"
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)

assertEquals(
score.getStudio(),
s"Composed at studio $studio, Produced at studio $studio"
)
}
}