Skip to content

Instantly share code, notes, and snippets.

@alexaleluia12
Created October 18, 2023 20:42
Show Gist options
  • Save alexaleluia12/f2d31c92af560328d9afb650348ae827 to your computer and use it in GitHub Desktop.
Save alexaleluia12/f2d31c92af560328d9afb650348ae827 to your computer and use it in GitHub Desktop.
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() {
val msg = "Device name: $name, category: $category, type $deviceType"
println(msg)
}
}
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)
override fun turnOn() {
super.turnOn()
println(
"$name is turned on. Speaker volume set to $speakerVolume and channel number is " +
"set to $channelNumber."
)
}
override fun turnOff() {
super.turnOff()
println("$name turned off")
}
fun increaseSpeakerVolume() {
speakerVolume++
println("Speaker volume increased to $speakerVolume.")
}
fun decreaseSpeakerVolume() {
speakerVolume--
println("Speaker volume decreased to $speakerVolume.")
}
fun nextChannel() {
channelNumber++
println("Channel number increased to $channelNumber.")
}
fun previousChannel() {
channelNumber--
println("Channel number decrease to $channelNumber.")
}
}
class SmartLightDevice(deviceName: String, deviceCategory: String) :
SmartDevice(name = deviceName, category = deviceCategory) {
override val deviceType = "Smart Light"
private var brightnessLevel by RangeRegulator(initialValue = 2, minValue = 0, maxValue = 100)
override fun turnOn() {
super.turnOn()
brightnessLevel = 2
println("$name is turned on. The brightness level is $brightnessLevel.")
}
override fun turnOff() {
super.turnOff()
brightnessLevel = 0
println("$name turned off")
}
fun increaseBrightness() {
brightnessLevel++
println("Brightness increased to $brightnessLevel.")
}
fun decreaseBrighteness() {
brightnessLevel--
println("Brightness decreased to $brightnessLevel.")
}
}
class SmartHome(val smartTvDevice: SmartTvDevice, val smartLightDevice: SmartLightDevice) {
var deviceTurnOnCount = 0
private set
fun turnOnTv() {
deviceTurnOnCount++
smartTvDevice.turnOn()
}
fun turnOffTv() {
deviceTurnOnCount--
smartTvDevice.turnOff()
}
fun increaseTvVolume() {
if (smartTvDevice.deviceStatus != "on") return;
smartTvDevice.increaseSpeakerVolume()
}
fun decreaseTvVolume() {
if (smartTvDevice.deviceStatus != "on") return;
smartTvDevice.decreaseSpeakerVolume()
}
fun changeTvChannelToNext() {
if (smartTvDevice.deviceStatus != "on") return;
smartTvDevice.nextChannel()
}
fun changeTvChannelToPrev() {
if (smartTvDevice.deviceStatus != "on") return;
smartTvDevice.previousChannel()
}
fun turnOnLight() {
deviceTurnOnCount++
smartLightDevice.turnOn()
}
fun turnOffLight() {
deviceTurnOnCount--
smartLightDevice.turnOff()
}
fun increaseLightBrightness() {
if (smartLightDevice.deviceStatus != "on") return;
smartLightDevice.increaseBrightness()
}
fun decreaseLightBrightness() {
if (smartLightDevice.deviceStatus != "on") return;
smartLightDevice.decreaseBrighteness()
}
fun turnOffAllDevices() {
turnOffTv()
turnOffLight()
}
fun showAllDevicesInfo() {
smartTvDevice.printDeviceInfo()
smartLightDevice.printDeviceInfo()
}
}
class RangeRegulator(
initialValue: Int,
private val minValue: Int,
private val maxValue: Int
) : ReadWriteProperty<Any?, Int> {
private 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 smartHome = SmartHome(
SmartTvDevice(deviceName = "Android TV", deviceCategory = "Entertainment"),
SmartLightDevice(deviceName = "Google light", deviceCategory = "Utility")
)
smartHome.turnOnTv()
smartHome.turnOnLight()
println("Total number of devices currently turned on: ${smartHome.deviceTurnOnCount}")
println()
smartHome.increaseTvVolume()
smartHome.changeTvChannelToNext()
smartHome.increaseLightBrightness()
println()
smartHome.decreaseLightBrightness()
smartHome.changeTvChannelToPrev()
smartHome.turnOffAllDevices()
smartHome.decreaseLightBrightness()
smartHome.decreaseTvVolume()
smartHome.showAllDevicesInfo()
println("Total number of devices currently turned on: ${smartHome.deviceTurnOnCount}.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment