-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgof_builder.kt
41 lines (34 loc) · 1018 Bytes
/
gof_builder.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
class Gun(val bullets:Int, val color:String, val features:List<String>){
class Builder{
private var bullets:Int = 0
private var color:String = "#212121"
private var features = listOf<String>()
fun setBullets(bullets:Int):Builder{
this.bullets = bullets
return this
}
fun setColor(color:String):Builder{
this.color = color
return this
}
fun addFeature(feature:String):Builder{
this.features += feature
return this
}
fun build():Gun{
require(this.bullets<=0){"Gun needs at least one bullet"}
return Gun(bullets,color,features)
}
}
}
fun main(){
val gun = Gun.Builder()
.setBullets(0)
.setColor("Green")
.addFeature("Heavy")
.addFeature("Portable")
.build()
println(gun.bullets)
println(gun.color)
println(gun.features)
}