/
LiskovBreakingTheLaw.kt
113 lines (84 loc) · 3.15 KB
/
LiskovBreakingTheLaw.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.example.liskov
import java.lang.RuntimeException
object BreakingTheLaw1 {
interface Fruit {
val color: String
val weightInGrams: Int
fun calories(): Float
//and so on
}
data class Apple(override val color: String, override val weightInGrams: Int) : Fruit {
override fun calories(): Float = (weightInGrams.toFloat() / 100) * 52
}
data class Orange(override val color: String, override val weightInGrams: Int) : Fruit {
override fun calories(): Float = (weightInGrams.toFloat() / 100) * 47
}
data class Ackee(override val color: String, override val weightInGrams: Int) : Fruit {
override fun calories(): Float = throw RuntimeException("You can't eat this!")
}
data class Person(
val name: String,
val caloriesConsumed: Float = 0.0f,
val weightOfConsumedFuits: Int = 0
) {
fun eat(fruit: Fruit): Person =
this.copy(
caloriesConsumed = (caloriesConsumed + fruit.calories()),
weightOfConsumedFuits = weightOfConsumedFuits + fruit.weightInGrams
)
}
private fun caloriesAndWeightCalculator(person: Person, fruits: List<Fruit>): Person =
fruits.fold(person) { acc, fruit -> acc.eat(fruit) }
@JvmStatic
fun main(args: Array<String>) {
val consumedFruits = listOf(
Apple("Red", 50),
Apple("Red", 100),
Orange("Orange", 100),
Ackee("Orange", 100)
)
println(caloriesAndWeightCalculator(Person("Niklas"), consumedFruits))
val inventory = mapOf<Fruit, Int>(
)
}
}
object FollowThePrinciple {
interface Fruit {
val color: String
val weightInGrams: Int
//and so on
}
interface EatableFruit : Fruit {
fun calories(): Float
}
data class Apple(override val color: String, override val weightInGrams: Int) : EatableFruit {
override fun calories(): Float = (weightInGrams.toFloat() / 100) * 52
}
data class Orange(override val color: String, override val weightInGrams: Int) : EatableFruit {
override fun calories(): Float = (weightInGrams.toFloat() / 100) * 47
}
data class Ackee(override val color: String, override val weightInGrams: Int) : Fruit
data class Person(
val name: String,
val caloriesConsumed: Float = 0.0f,
val weightOfConsumedFuits: Int = 0
) {
fun eat(fruit: EatableFruit): Person =
this.copy(
caloriesConsumed = (caloriesConsumed + fruit.calories()),
weightOfConsumedFuits = weightOfConsumedFuits + fruit.weightInGrams
)
}
private fun caloriesAndWeightCalculator(person: Person, fruits: List<EatableFruit>): Person =
fruits.fold(person) { acc, fruit -> acc.eat(fruit) }
@JvmStatic
fun main(args: Array<String>) {
val consumedFruits = listOf(
Apple("Red", 50),
Apple("Red", 100),
Orange("Orange", 100)//,
// Ackee("Red", 100)
)
println(caloriesAndWeightCalculator(Person("Niklas"), consumedFruits))
}
}