diff --git a/src/Book.kt b/src/Book.kt deleted file mode 100644 index a1f7ffc..0000000 --- a/src/Book.kt +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/src/ams/Aquarium.kt b/src/ams/Aquarium.kt new file mode 100644 index 0000000..77fc9e6 --- /dev/null +++ b/src/ams/Aquarium.kt @@ -0,0 +1,21 @@ +package ams + +data class Fish(var name: String) + +fun main(args: Array) { + fishExamples() +} + +fun fishExamples() { + val fish = Fish("splashy") + + myWith(fish.name) { + capitalize() + } + + +} + +fun myWith(name: String, block: String.() -> Unit) { + name.block() +} \ No newline at end of file diff --git a/src/aquarium/generics/Aquarium.kt b/src/aquarium/generics/Aquarium.kt new file mode 100644 index 0000000..09067f0 --- /dev/null +++ b/src/aquarium/generics/Aquarium.kt @@ -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 { + fun clean(waterSupply: T) +} + +class TapWaterCleaner: Cleaner { + override fun clean(waterSupply: TapWater) { + waterSupply.addChemicalCleaners() + } +} + +class Aquarium(val waterSupply: T) { + fun addWater(cleaner: Cleaner) { + if(waterSupply.needsProcessed) { + cleaner.clean(waterSupply) + } + println("adding water from $waterSupply") + } + + inline fun hasWaterSupplyOfType() = waterSupply is R +} + + diff --git a/src/aquarium/generics/main.kt b/src/aquarium/generics/main.kt new file mode 100644 index 0000000..0e9f415 --- /dev/null +++ b/src/aquarium/generics/main.kt @@ -0,0 +1,20 @@ +package aquarium.generics + +fun main(args: Array) { + genericExample() +} + +fun genericExample() { + val cleaner = TapWaterCleaner() + + val aquarium = Aquarium(TapWater()) + isWaterClean(aquarium) + aquarium.addWater(cleaner) +} + +inline fun Aquarium<*>.hasWaterSupplyOfType() = waterSupply is R +inline fun WaterSupply.isOfType() = this is T + +fun isWaterClean(aquarium: Aquarium) { + println("aquarium water is clean: ${aquarium.waterSupply.needsProcessed}") +} \ No newline at end of file diff --git a/src/book/Book.kt b/src/book/Book.kt new file mode 100644 index 0000000..c99deb2 --- /dev/null +++ b/src/book/Book.kt @@ -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 { + return title to author + } + + fun getTitleAuthorYear(): Triple { + 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 + } +} diff --git a/src/book/main.kt b/src/book/main.kt new file mode 100644 index 0000000..73f1c06 --- /dev/null +++ b/src/book/main.kt @@ -0,0 +1,31 @@ +package book + +import java.util.* + +fun main(args: Array) { + 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)) + } +} \ No newline at end of file diff --git a/src/buildings/Buildings.kt b/src/buildings/Buildings.kt new file mode 100644 index 0000000..b6b3667 --- /dev/null +++ b/src/buildings/Buildings.kt @@ -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(val buildingMaterial: T) { + private val baseMaterialsNeeded = 100 + val actualMaterialsNeeded = baseMaterialsNeeded * buildingMaterial.numberNeeded + + fun build() { + println("$actualMaterialsNeeded ${buildingMaterial::class.simpleName} required") + } +} + +fun main(args: Array) { + 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)") + } +} \ No newline at end of file diff --git a/src/directions.kt b/src/directions.kt new file mode 100644 index 0000000..af050ab --- /dev/null +++ b/src/directions.kt @@ -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) { + val game = Game() + while(game.running) { + print("Enter a direction: n/s/e/w:") + game.makeMove(readLine()) + println("You moved ${game.path.last().name}") + } +} \ No newline at end of file diff --git a/src/divisibleBy.kt b/src/divisibleBy.kt new file mode 100644 index 0000000..994eb2f --- /dev/null +++ b/src/divisibleBy.kt @@ -0,0 +1,9 @@ +fun main(args: Array) { + val numbers = listOf(1,2,3,4,5,6,7,8,9,0) + val result = numbers.divisibleBy { it.rem(3) } + print(result) +} + +fun List.divisibleBy(block: (Int) -> Int): List { + return filter{ block(it) === 0 } +} \ No newline at end of file