1
1
Fork 0
AtomicKotlinCourse/Power Tools/Late Initialization/Exercicse 3/task.md

31 lines
929 B
Markdown
Raw Normal View History

2020-09-28 18:08:41 +03:00
## Late Initialization (#3)
2020-09-28 18:08:41 +03:00
The starter code provides:
2020-09-28 18:08:41 +03:00
```kotlin
data class Generator(val id: Int)
2020-09-28 18:08:41 +03:00
class Turbine(val id: Int) {
private lateinit var _generator: Generator
val generator: Generator
...
```
2020-09-28 18:08:41 +03:00
Add a `get()` for `generator` that checks to see if `_generator` has been
initialized, and if not initializes it before returning `_generator`. Add a
`toString()` that starts with "Generator $id running: " and then indicates
if the generator is running.
2020-09-28 18:08:41 +03:00
The starter code provides:
2020-09-28 18:08:41 +03:00
```kotlin
class PowerPlant(nTurbines: Int = 4) {
private val turbines: List<Turbine> =
...
```
Finish the initialization for `turbines`, then add
`fun generator(number: Int): Generator` which checks to make sure `number`
is in range, then returns the desired generator. Finally, add a `status()`
member function that uses `forEach` to show the status of each generator in the
`PowerPlant`. The code in `main()` tests your solution.