Skip to content

Commit c77f798

Browse files
kotlin-samples-pusher-botdkrasnoff
authored andcommitted
test(samples): add new samples
1 parent 737a045 commit c77f798

8 files changed

+192
-7
lines changed
Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
1+
class Address {
2+
var name: String = "Holmes, Sherlock"
3+
var street: String = "Baker"
4+
var city: String = "London"
5+
}
6+
7+
interface ContactInfo {
8+
val email: String
9+
}
10+
11+
object Company {
12+
var name: String = "Detective Inc."
13+
val country: String = "UK"
14+
}
15+
16+
class PersonContact : ContactInfo {
17+
override val email: String = "sherlock@example.com"
18+
}
19+
120
//sampleStart
2-
class Rectangle(val width: Int, val height: Int) {
3-
val area: Int // property type is optional since it can be inferred from the getter's return type
4-
get() = this.width * this.height
21+
fun copyAddress(address: Address): Address {
22+
val result = Address()
23+
// Accesses properties in the result instance
24+
result.name = address.name
25+
result.street = address.street
26+
result.city = address.city
27+
return result
528
}
6-
//sampleEnd
29+
730
fun main() {
8-
val rectangle = Rectangle(3, 4)
9-
println("Width=${rectangle.width}, height=${rectangle.height}, area=${rectangle.area}")
10-
}
31+
val sherlockAddress = Address()
32+
val copy = copyAddress(sherlockAddress)
33+
// Accesses properties in the copy instance
34+
println("Copied address: ${copy.name}, ${copy.street}, ${copy.city}")
35+
// Copied address: Holmes, Sherlock, Baker, London
36+
37+
// Accesses properties in the Company object
38+
println("Company: ${Company.name} in ${Company.country}")
39+
// Company: Detective Inc. in UK
40+
41+
val contact = PersonContact()
42+
// Access properties in the contact instance
43+
println("Email: ${contact.email}")
44+
// Email: sherlock@email.com
45+
}
46+
//sampleEnd
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//sampleStart
2+
class Rectangle(val width: Int, val height: Int) {
3+
val area: Int
4+
get() = this.width * this.height
5+
}
6+
//sampleEnd
7+
fun main() {
8+
val rectangle = Rectangle(3, 4)
9+
println("Width=${rectangle.width}, height=${rectangle.height}, area=${rectangle.area}")
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Point(var x: Int, var y: Int) {
2+
var coordinates: String
3+
get() = "$x,$y"
4+
set(value) {
5+
val parts = value.split(",")
6+
x = parts[0].toInt()
7+
y = parts[1].toInt()
8+
}
9+
}
10+
11+
fun main() {
12+
val location = Point(1, 2)
13+
println(location.coordinates)
14+
// 1,2
15+
16+
location.coordinates = "10,20"
17+
println("${location.x}, ${location.y}")
18+
// 10, 20
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class BankAccount(initialBalance: Int) {
2+
var balance: Int = initialBalance
3+
// Only the class can modify the balance
4+
private set
5+
6+
fun deposit(amount: Int) {
7+
if (amount > 0) balance += amount
8+
}
9+
10+
fun withdraw(amount: Int) {
11+
if (amount > 0 && amount <= balance) balance -= amount
12+
}
13+
}
14+
15+
fun main() {
16+
val account = BankAccount(100)
17+
println("Initial balance: ${account.balance}")
18+
// 100
19+
20+
account.deposit(50)
21+
println("After deposit: ${account.balance}")
22+
// 150
23+
24+
account.withdraw(70)
25+
println("After withdrawal: ${account.balance}")
26+
// 80
27+
28+
// account.balance = 1000
29+
// Error: cannot assign because setter is private
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Scoreboard {
2+
var score: Int = 0
3+
set(value) {
4+
field = value
5+
// Adds logging when updating the value
6+
println("Score updated to $field")
7+
}
8+
}
9+
10+
fun main() {
11+
val board = Scoreboard()
12+
board.score = 10
13+
// Score updated to 10
14+
board.score = 20
15+
// Score updated to 20
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class ShoppingCart {
2+
// Backing property
3+
private val _items = mutableListOf<String>()
4+
5+
// Public read-only view
6+
val items: List<String>
7+
get() = _items
8+
9+
fun addItem(item: String) {
10+
_items.add(item)
11+
}
12+
13+
fun removeItem(item: String) {
14+
_items.remove(item)
15+
}
16+
}
17+
18+
fun main() {
19+
val cart = ShoppingCart()
20+
cart.addItem("Apple")
21+
cart.addItem("Banana")
22+
23+
println(cart.items)
24+
// [Apple, Banana]
25+
26+
cart.removeItem("Apple")
27+
println(cart.items)
28+
// [Banana]
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Temperature {
2+
// Backing property storing temperature in Celsius
3+
private var _celsius: Double = 0.0
4+
5+
var celsius: Double
6+
get() = _celsius
7+
set(value) { _celsius = value }
8+
9+
var fahrenheit: Double
10+
get() = _celsius * 9 / 5 + 32
11+
set(value) { _celsius = (value - 32) * 5 / 9 }
12+
}
13+
14+
fun main() {
15+
val temp = Temperature()
16+
temp.celsius = 25.0
17+
println("${temp.celsius}°C = ${temp.fahrenheit}°F")
18+
// 25.0°C = 77.0°F
19+
20+
temp.fahrenheit = 212.0
21+
println("${temp.celsius}°C = ${temp.fahrenheit}°F")
22+
// 100.0°C = 212.0°F
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class WeatherStation {
2+
lateinit var latestReading: String
3+
4+
fun printReading() {
5+
// Checks whether the property is initialized
6+
if (this::latestReading.isInitialized) {
7+
println("Latest reading: $latestReading")
8+
} else {
9+
println("No reading available")
10+
}
11+
}
12+
}
13+
14+
fun main() {
15+
val station = WeatherStation()
16+
17+
station.printReading()
18+
// No reading available
19+
station.latestReading = "22°C, sunny"
20+
station.printReading()
21+
// Latest reading: 22°C, sunny
22+
}

0 commit comments

Comments
 (0)