Skip to content

Commit 67e93c4

Browse files
SCALA-24: traits
1 parent 94bd877 commit 67e93c4

File tree

9 files changed

+171
-0
lines changed

9 files changed

+171
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala.traits
2+
3+
trait Composition extends Orchestration with Mixing {
4+
var composer: String
5+
6+
def compose(): String
7+
8+
var studio: String
9+
def getStudio(): String = s"Composed at studio $studio"
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.scala.traits
2+
3+
trait Mixing {
4+
var mixer: String
5+
val qualityRatio: Double = 3.14
6+
7+
def algorithm: MixingAlgorithm = HighInstrumentalQuality
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.scala.traits
2+
3+
sealed trait MixingAlgorithm
4+
5+
case object LowInstrumentalQuality extends MixingAlgorithm {
6+
override def toString(): String = "Low instrumental quality"
7+
}
8+
9+
case object HighInstrumentalQuality extends MixingAlgorithm {
10+
override def toString(): String = "High instrumental quality"
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.scala.traits
2+
3+
trait Orchestration {
4+
var orchestra: String
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.scala.traits
2+
3+
class RecordLabel
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.scala.traits
2+
3+
class Score(var composer: String,
4+
var engineer: String,
5+
var orchestra: String,
6+
var mixer: String,
7+
override val qualityRatio: Double,
8+
var studio: String)
9+
extends RecordLabel
10+
with Composition
11+
with SoundProduction {
12+
13+
override def compose(): String =
14+
s"""The score is composed by $composer,
15+
|Orchestration by $orchestra,
16+
|Mixed by $mixer""".stripMargin
17+
18+
override def produce(): String = s"The score is produced by $engineer"
19+
20+
override def algorithm(): MixingAlgorithm = {
21+
if (qualityRatio < 3) LowInstrumentalQuality
22+
else super.algorithm
23+
}
24+
25+
override def getStudio(): String =
26+
super[Composition].getStudio() + ", " + super[SoundProduction].getStudio()
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.scala.traits
2+
3+
trait SoundProduction {
4+
this: RecordLabel =>
5+
var engineer: String
6+
7+
def produce(): String
8+
9+
var studio: String
10+
def getStudio(): String = s"Produced at studio $studio"
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.scala.traits
2+
3+
trait Vocals {
4+
val sing: String = "Vocals mixin"
5+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.scala.traits
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class ScoreUnitTest {
7+
8+
@Test
9+
def givenScore_whenComposeCalled_thenCompositionIsReturned() = {
10+
11+
val composer = "Hans Zimmer"
12+
val engineer = "Matt Dunkley"
13+
val orchestra = "Berlin Philharmonic"
14+
val mixer = "Dave Stewart"
15+
val studio = "Abbey Studios"
16+
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)
17+
18+
assertEquals(score.compose(), s"""The score is composed by $composer,
19+
|Orchestration by $orchestra,
20+
|Mixed by $mixer""".stripMargin)
21+
}
22+
23+
@Test
24+
def givenScore_whenProduceCalled_thenSoundProductionIsReturned() = {
25+
26+
val composer = "Hans Zimmer"
27+
val engineer = "Matt Dunkley"
28+
val orchestra = "Berlin Philharmonic"
29+
val mixer = "Dave Stewart"
30+
val studio = "Abbey Studios"
31+
val score = new Score(composer, engineer, orchestra, mixer, 3, studio)
32+
33+
assertEquals(score.produce(), s"The score is produced by $engineer")
34+
}
35+
36+
@Test
37+
def givenScore_whenLowQualityRatioSet_thenCorrectAlgorithmIsReturned() = {
38+
39+
val composer = "Hans Zimmer"
40+
val engineer = "Matt Dunkley"
41+
val orchestra = "Berlin Philharmonic"
42+
val mixer = "Dave Stewart"
43+
val studio = "Abbey Studios"
44+
val score = new Score(composer, engineer, orchestra, mixer, 1, studio)
45+
46+
assertEquals(score.algorithm().toString, "Low instrumental quality")
47+
}
48+
49+
@Test
50+
def givenScore_whenHighQualityRatioSet_thenCorrectAlgorithmIsReturned() = {
51+
52+
val composer = "Hans Zimmer"
53+
val engineer = "Matt Dunkley"
54+
val orchestra = "Berlin Philharmonic"
55+
val mixer = "Dave Stewart"
56+
val studio = "Abbey Studios"
57+
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)
58+
59+
assertEquals(score.algorithm().toString, "High instrumental quality")
60+
}
61+
62+
@Test
63+
def givenScore_whenVocalsMixinAttached_thenSingCanBeCalled() = {
64+
65+
val composer = "Hans Zimmer"
66+
val engineer = "Matt Dunkley"
67+
val orchestra = "Berlin Philharmonic"
68+
val mixer = "Dave Stewart"
69+
val studio = "Abbey Studios"
70+
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)
71+
with Vocals
72+
73+
assertEquals(score.sing, "Vocals mixin")
74+
}
75+
76+
@Test
77+
def givenScore_whenGetStudioCalledSet_thenStudiosAreReturned() = {
78+
79+
val composer = "Hans Zimmer"
80+
val engineer = "Matt Dunkley"
81+
val orchestra = "Berlin Philharmonic"
82+
val mixer = "Dave Stewart"
83+
val studio = "Abbey Studios"
84+
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)
85+
86+
assertEquals(
87+
score.getStudio(),
88+
s"Composed at studio $studio, Produced at studio $studio"
89+
)
90+
}
91+
}

0 commit comments

Comments
 (0)