Skip to content

Lec20 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/lannstark/lec18/Fruit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lannstark.lec18;

data class Fruit(
val id: Long,
val name: String,
val factoryPrice: Long,
val currentPrice: Long?,
) {
fun nullOrValue(): Long? {
return currentPrice
}

val isSamePrice: Boolean
get() = factoryPrice == currentPrice
}
48 changes: 48 additions & 0 deletions src/main/java/com/lannstark/lec18/Lec18Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.lannstark.lec18;

import java.util.*

fun main() {

val fruits = Arrays.asList(
Fruit(1, "사과", 1000, 1400),
Fruit(2, "사과", 1200, 1400),
Fruit(3, "사과", 1200, null),
Fruit(4, "사과", 1500, 1600),
Fruit(5, "바나나", 3000, 3400),
Fruit(6, "바나나", 3200, 3500),
Fruit(7, "바나나", 2500, 2800),
Fruit(8, "수박", 10000, 12000)
)

val fruitsInList: List<List<Fruit>> = listOf(
listOf(
Fruit(1L, "사과", 1_000, 1_500),
Fruit(2L, "사과", 1_200, 1_500),
Fruit(3L, "사과", 1_200, 1_500),
Fruit(4L, "사과", 1_500, 1_500),
),
listOf(
Fruit(5L, "바나나", 3_000, 3_200),
Fruit(6L, "바나나", 3_200, 3_200),
Fruit(7L, "바나나", 2_500, 3_200),
),
listOf(
Fruit(8L, "수박", 10_000, 10_000),
),
)

}

private fun filterFruits(fruits: List<Fruit>, filter: (Fruit) -> Boolean): List<Fruit> {
val results = mutableListOf<Fruit>()
for (fruit in fruits) {
if (filter(fruit)) {
results.add(fruit)
}
}
return results
}

val List<Fruit>.samePriceFilter: List<Fruit>
get() = this.filter(Fruit::isSamePrice)
15 changes: 15 additions & 0 deletions src/main/java/com/lannstark/lec19/Fruit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lannstark.lec19

data class Fruit(
val id: Long,
val name: String,
val factoryPrice: Long,
val currentPrice: Long?,
) {
fun nullOrValue(): Long? {
return currentPrice
}

val isSamePrice: Boolean
get() = factoryPrice == currentPrice
}
24 changes: 24 additions & 0 deletions src/main/java/com/lannstark/lec19/Lec19Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.lannstark.lec19

fun main() {
val numbers = listOf(1, 2, 3)
}

fun filterFruits(fruits: List<Fruit>, filter: (Fruit) -> Boolean) {

}

data class Person(
val name: String,
val age: Int
)

const val number = 0

fun getNumberOrNull(): Int? {
return if (number <= 0) {
null
} else {
number
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/lannstark/lec19/a/Print.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lannstark.lec19.a

fun printHelloWorld() {
println("Hello World A")
}
5 changes: 5 additions & 0 deletions src/main/java/com/lannstark/lec19/b/Print.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lannstark.lec19.b

fun printHelloWorld() {
println("Hello World B")
}
32 changes: 32 additions & 0 deletions src/main/java/com/lannstark/lec20/Lec20Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.lannstark.lec20

fun main() {
val person = Person("홍길동", 100)

val value1 = person.let {
it.age
}

val value2 = person.run {
this.age
}

val value3 = person.also {
it.age
}

val value4 = person.apply {
this.age
}

mutableListOf("one", "two", "three")
.also { println("The list elements before adding new one: ${it}") }
.add("four")
}

fun printPerson(person: Person?) {
if (person != null) {
println(person.name)
println(person.age)
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/lannstark/lec20/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.lannstark.lec20

class Person constructor (
var name: String,
var age: Int,
) {

}