Skip to content

Commit 1761e96

Browse files
authored
Merge pull request #50 from ramkumarvenkat/SCALA-24
Introduction to traits
2 parents f2d4ac3 + a6794c3 commit 1761e96

File tree

9 files changed

+171
-0
lines changed

9 files changed

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