-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUse classes and objects in Kotlin
256 lines (203 loc) · 6.44 KB
/
Use classes and objects in Kotlin
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
open class SmartDevice(val name: String, val category: String) {
var deviceStatus = "online"
protected set
open val deviceType = "unknown"
open fun turnOn() {
deviceStatus = "on"
}
open fun turnOff() {
deviceStatus = "off"
}
fun printDeviceInfo() {
println("Device name: $name, category: $category, type: $deviceType")
}
}
class SmartTvDevice(deviceName: String, deviceCategory: String) :
SmartDevice(name = deviceName, category = deviceCategory) {
override val deviceType = "Smart TV"
private var speakerVolume by RangeRegulator(initialValue = 2, minValue = 0, maxValue = 100)
private var channelNumber by RangeRegulator(initialValue = 1, minValue = 0, maxValue = 200)
fun increaseSpeakerVolume() {
speakerVolume++
println("Speaker volume increased to $speakerVolume.")
}
fun decreaseVolume() {
speakerVolume--
println("Speaker volume decreased to $speakerVolume.")
}
fun nextChannel() {
channelNumber++
println("Channel number increased to $channelNumber.")
}
fun previousChannel() {
channelNumber--
println("channel number decreased to $channelNumber")
}
override fun turnOn() {
super.turnOn()
println(
"$name is turned on. Speaker volume is set to $speakerVolume and channel number is " +
"set to $channelNumber."
)
}
override fun turnOff() {
super.turnOff()
println("$name turned off")
}
}
class SmartLightDevice(deviceName: String, deviceCategory: String) :
SmartDevice(name = deviceName, category = deviceCategory) {
override val deviceType = "Smart Light"
private var brightnessLevel by RangeRegulator(initialValue = 0, minValue = 0, maxValue = 100)
fun increaseBrightness() {
brightnessLevel++
println("Brightness increased to $brightnessLevel.")
}
fun decreaseBrightness() {
brightnessLevel--
println("Brightness decreased to $brightnessLevel.")
}
override fun turnOn() {
super.turnOn()
brightnessLevel = 2
println("$name turned on. The brightness level is $brightnessLevel.")
}
override fun turnOff() {
super.turnOff()
brightnessLevel = 0
println("Smart Light turned off")
}
}
class SmartHome(
val smartTvDevice: SmartTvDevice,
val smartLightDevice: SmartLightDevice
) {
var deviceTurnOnCount = 0
private set
fun turnOnTv() {
if (smartTvDevice.deviceStatus != "on"){
deviceTurnOnCount++
smartTvDevice.turnOn()
} else {
println("Can't turn on the TV. Device is not currently on.")
}
}
fun turnOffTv() {
if (smartTvDevice.deviceStatus == "on"){
deviceTurnOnCount--
smartTvDevice.turnOff()
} else {
println("Can't turn off the TV. Device is not currently on.")
}
}
fun increaseTvVolume() {
if (smartTvDevice.deviceStatus == "on") {
smartTvDevice.increaseSpeakerVolume()
} else {
println("Can't increase the volume. Device is not currently on.")
}
}
fun decreaseTvVolume() {
if (smartTvDevice.deviceStatus == "on") {
smartTvDevice.decreaseVolume()
} else {
println("Can't decrease the volume. Device is not currently on.")
}
}
fun changeTvChannelToNext() {
if (smartTvDevice.deviceStatus == "on") {
smartTvDevice.nextChannel()
} else {
println("Can't change TV channel. Device is not currently on.")
}
}
fun changeTvChannelToPrevious() {
if (smartTvDevice.deviceStatus == "on") {
smartTvDevice.previousChannel()
} else {
println("Can't change TV channel. Device is not currently on.")
}
}
fun turnOnLight() {
if (smartLightDevice.deviceStatus != "on") {
deviceTurnOnCount++
smartLightDevice.turnOn()
} else {
println("Cannot turn on light. Device is not currently on.")
}
}
fun turnOffLight() {
if (smartLightDevice.deviceStatus == "on") {
deviceTurnOnCount--
smartLightDevice.turnOff()
} else {
println("Cannot turn off light. Device is not currently on.")
}
}
fun increaseLightBrightness() {
if (smartLightDevice.deviceStatus == "on") {
smartLightDevice.increaseBrightness()
} else {
println("Cannot increase light brightness. Device is not currently on.")
}
}
fun decreaseLightBrightness() {
if (smartLightDevice.deviceStatus == "on") {
smartLightDevice.decreaseBrightness()
} else {
println("Cannot decrease light brightness. Device is not currently on.")
}
}
fun turnOffAllDevices() {
turnOffTv()
turnOffLight()
}
fun printSmartTvInfo() {
smartTvDevice.printDeviceInfo()
}
fun printSmartLightInfo() {
smartLightDevice.printDeviceInfo()
}
}
class RangeRegulator(
initialValue: Int,
private val minValue: Int,
private val maxValue: Int
) : ReadWriteProperty<Any?, Int> {
var fieldData = initialValue
override fun getValue(thisRef: Any?, property: KProperty<*>): Int {
return fieldData
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
if (value in minValue..maxValue) {
fieldData = value
}
}
}
fun main() {
val smartTv = SmartTvDevice("Android TV", "Entertainment")
val smartLight = SmartLightDevice("Google Light", "Utility")
val smartHome = SmartHome(smartTv, smartLight)
// Turn on TV and Light
smartHome.turnOnTv()
smartHome.turnOnLight()
// Increase TV volume
smartHome.increaseTvVolume()
// Decrease TV volume
smartHome.decreaseTvVolume()
// Next channel
smartHome.changeTvChannelToNext()
// Previous channel
smartHome.changeTvChannelToPrevious()
// Increase brightness
smartHome.increaseLightBrightness()
// Decrease brightness
smartHome.decreaseLightBrightness()
// Print TV info
smartHome.printSmartTvInfo()
// Print Light info
smartHome.printSmartLightInfo()
smartHome.turnOffAllDevices()
}