And that's that, moving on to android
This commit is contained in:
15
src/Book.kt
15
src/Book.kt
@@ -1,15 +0,0 @@
|
||||
open class Book(title: String, author: String) {
|
||||
private var currentPage: Int = 1
|
||||
|
||||
open fun readPage() {
|
||||
currentPage.inc()
|
||||
}
|
||||
}
|
||||
|
||||
class eBook(title: String, author: String, var format: String = "text"): Book(title, author) {
|
||||
private var wordsRead = 0
|
||||
|
||||
override fun readPage() {
|
||||
wordsRead += 250
|
||||
}
|
||||
}
|
||||
21
src/ams/Aquarium.kt
Normal file
21
src/ams/Aquarium.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package ams
|
||||
|
||||
data class Fish(var name: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
fishExamples()
|
||||
}
|
||||
|
||||
fun fishExamples() {
|
||||
val fish = Fish("splashy")
|
||||
|
||||
myWith(fish.name) {
|
||||
capitalize()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fun myWith(name: String, block: String.() -> Unit) {
|
||||
name.block()
|
||||
}
|
||||
40
src/aquarium/generics/Aquarium.kt
Normal file
40
src/aquarium/generics/Aquarium.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package aquarium.generics
|
||||
|
||||
open class WaterSupply(var needsProcessed: Boolean)
|
||||
|
||||
class TapWater : WaterSupply(needsProcessed = true) {
|
||||
fun addChemicalCleaners() {
|
||||
needsProcessed = false
|
||||
}
|
||||
}
|
||||
|
||||
class FishStoreWater: WaterSupply(needsProcessed = false)
|
||||
|
||||
class LakeWater: WaterSupply(needsProcessed = true) {
|
||||
fun filter() {
|
||||
needsProcessed = false
|
||||
}
|
||||
}
|
||||
|
||||
interface Cleaner<in T: WaterSupply> {
|
||||
fun clean(waterSupply: T)
|
||||
}
|
||||
|
||||
class TapWaterCleaner: Cleaner<TapWater> {
|
||||
override fun clean(waterSupply: TapWater) {
|
||||
waterSupply.addChemicalCleaners()
|
||||
}
|
||||
}
|
||||
|
||||
class Aquarium<T: WaterSupply>(val waterSupply: T) {
|
||||
fun addWater(cleaner: Cleaner<T>) {
|
||||
if(waterSupply.needsProcessed) {
|
||||
cleaner.clean(waterSupply)
|
||||
}
|
||||
println("adding water from $waterSupply")
|
||||
}
|
||||
|
||||
inline fun <reified R: WaterSupply> hasWaterSupplyOfType() = waterSupply is R
|
||||
}
|
||||
|
||||
|
||||
20
src/aquarium/generics/main.kt
Normal file
20
src/aquarium/generics/main.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package aquarium.generics
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
genericExample()
|
||||
}
|
||||
|
||||
fun genericExample() {
|
||||
val cleaner = TapWaterCleaner()
|
||||
|
||||
val aquarium = Aquarium(TapWater())
|
||||
isWaterClean(aquarium)
|
||||
aquarium.addWater(cleaner)
|
||||
}
|
||||
|
||||
inline fun <reified R: WaterSupply>Aquarium<*>.hasWaterSupplyOfType() = waterSupply is R
|
||||
inline fun <reified T: WaterSupply>WaterSupply.isOfType() = this is T
|
||||
|
||||
fun <T: WaterSupply>isWaterClean(aquarium: Aquarium<T>) {
|
||||
println("aquarium water is clean: ${aquarium.waterSupply.needsProcessed}")
|
||||
}
|
||||
32
src/book/Book.kt
Normal file
32
src/book/Book.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package book
|
||||
|
||||
const val CHECKOUT_MAX = 3;
|
||||
|
||||
class Book(
|
||||
var title: String,
|
||||
var author: String,
|
||||
var year: Int,
|
||||
var pages: Int
|
||||
) {
|
||||
private var currentPage: Int = 1
|
||||
|
||||
fun getTitleAndAuthor(): Pair<String, String> {
|
||||
return title to author
|
||||
}
|
||||
|
||||
fun getTitleAuthorYear(): Triple<String, String, Int> {
|
||||
return Triple(title, author, year)
|
||||
}
|
||||
|
||||
fun readPage() {
|
||||
if (currentPage < pages) {
|
||||
currentPage.inc()
|
||||
} else {
|
||||
println("Already at end of book")
|
||||
}
|
||||
}
|
||||
|
||||
fun canBorrow(booksOut: Int): Boolean {
|
||||
return booksOut < CHECKOUT_MAX
|
||||
}
|
||||
}
|
||||
31
src/book/main.kt
Normal file
31
src/book/main.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package book
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val b1 = Book("20,000 Leagues Under the Sea", "Jules Verne", 1870, 426)
|
||||
puppyFunTime()
|
||||
}
|
||||
|
||||
|
||||
fun puppyFunTime() {
|
||||
val puppy = Puppy()
|
||||
// val book = Book("Oliver Twist", "Charles Dickens", 1837, 540)
|
||||
val book = Book("20,000 Leagues Under the Sea", "Jules Verne", 1870, 426)
|
||||
|
||||
while (book.pages > 0) {
|
||||
puppy.playWithBook(book)
|
||||
println("${book.pages} left in ${book.title}")
|
||||
}
|
||||
println("Sad puppy, no more pages in ${book.title}. ")
|
||||
}
|
||||
|
||||
|
||||
fun Book.weight(): Double = (pages * 1.5)
|
||||
fun Book.tornPages(torn: Int) = if (pages >= torn) pages -= torn else pages = 0
|
||||
|
||||
class Puppy() {
|
||||
fun playWithBook(book: Book) {
|
||||
book.tornPages(Random().nextInt(12))
|
||||
}
|
||||
}
|
||||
39
src/buildings/Buildings.kt
Normal file
39
src/buildings/Buildings.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
package buildings
|
||||
|
||||
open class BaseBuildingMaterial() {
|
||||
open val numberNeeded = 1
|
||||
}
|
||||
|
||||
class Wood(): BaseBuildingMaterial() {
|
||||
override val numberNeeded = 4
|
||||
}
|
||||
class Brick(): BaseBuildingMaterial() {
|
||||
override val numberNeeded = 8
|
||||
}
|
||||
|
||||
class Building<out T: BaseBuildingMaterial>(val buildingMaterial: T) {
|
||||
private val baseMaterialsNeeded = 100
|
||||
val actualMaterialsNeeded = baseMaterialsNeeded * buildingMaterial.numberNeeded
|
||||
|
||||
fun build() {
|
||||
println("$actualMaterialsNeeded ${buildingMaterial::class.simpleName} required")
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val b = Building(Wood())
|
||||
b.isSmallBuilding()
|
||||
b.build()
|
||||
val b2 = Building(Brick())
|
||||
b2.isSmallBuilding()
|
||||
b2.build()
|
||||
|
||||
}
|
||||
|
||||
fun Building<*>.isSmallBuilding() {
|
||||
if (this.actualMaterialsNeeded < 500) {
|
||||
println("yes (small building)")
|
||||
} else {
|
||||
println("no (large building)")
|
||||
}
|
||||
}
|
||||
44
src/directions.kt
Normal file
44
src/directions.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
enum class Direction {
|
||||
NORTH,
|
||||
SOUTH,
|
||||
EAST,
|
||||
WEST,
|
||||
START,
|
||||
END
|
||||
}
|
||||
|
||||
class Game {
|
||||
var running = true;
|
||||
var path = mutableListOf(Direction.START)
|
||||
var north = {path.add(Direction.NORTH)}
|
||||
var south = {path.add(Direction.SOUTH)}
|
||||
var east = {path.add(Direction.EAST)}
|
||||
var west = {path.add(Direction.WEST)}
|
||||
var end = {
|
||||
running = false
|
||||
path.add(Direction.END)
|
||||
}
|
||||
|
||||
private fun move(where: () -> Boolean) {
|
||||
where.invoke()
|
||||
}
|
||||
|
||||
fun makeMove(direction: String?) {
|
||||
when(direction) {
|
||||
"n" -> move(north)
|
||||
"s" -> move(south)
|
||||
"e" -> move(east)
|
||||
"w" -> move(west)
|
||||
else -> move(end)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val game = Game()
|
||||
while(game.running) {
|
||||
print("Enter a direction: n/s/e/w:")
|
||||
game.makeMove(readLine())
|
||||
println("You moved ${game.path.last().name}")
|
||||
}
|
||||
}
|
||||
9
src/divisibleBy.kt
Normal file
9
src/divisibleBy.kt
Normal file
@@ -0,0 +1,9 @@
|
||||
fun main(args: Array<String>) {
|
||||
val numbers = listOf<Int>(1,2,3,4,5,6,7,8,9,0)
|
||||
val result = numbers.divisibleBy { it.rem(3) }
|
||||
print(result)
|
||||
}
|
||||
|
||||
fun List<Int>.divisibleBy(block: (Int) -> Int): List<Int> {
|
||||
return filter{ block(it) === 0 }
|
||||
}
|
||||
Reference in New Issue
Block a user