Lab 4, Kotlin.

This commit is contained in:
Rhinemann 2023-05-13 14:31:07 +03:00
parent 5f36bd41ae
commit 4d538d2d04
4 changed files with 39 additions and 2 deletions

View File

@ -1,4 +1,4 @@
package OOP.Java.lab_1; package OOP.Java.lab_1;
import java.util.Scanner; import java.util.Scanner;

11
Java/lab_4/Furniture.kt Normal file
View File

@ -0,0 +1,11 @@
package OOP.Java.lab_4
class Furniture(val name: String, val material: String, val price: Int, val length: Int, val width: Int, val height: Int) {
fun print(nameWidth: Int, materialWidth: Int, priceWidth: Int){
print("Furniture stats: {")
print("Name: ${this.name.padEnd(nameWidth)} ")
print("Material: ${this.material.padEnd(materialWidth)} ")
print("Price: ${(this.price.toString() + " cu").padEnd(priceWidth + 3)} ")
print("Size: ${this.length}×${this.width}×${this.height}};\n")
}
}

26
Java/lab_4/lab_3.kt Normal file
View File

@ -0,0 +1,26 @@
package OOP.Java.lab_4
fun main() {
val furnitureArray = arrayOf(
Furniture("Chair", "Wood", 10, 5, 5, 10),
Furniture("Counter-top", "Marble", 1_000, 10, 5, 1),
Furniture("Dinner table", "Glass", 500, 15, 10, 1),
Furniture("Office table", "Wood", 200, 10, 7, 1),
Furniture("Refrigerator", "Stainless steel", 20_000, 8, 4, 10)
)
val maxNameWidth = furnitureArray.maxWith(Comparator.comparingInt { it.name.length }).name.length
val maxMaterialWidth = furnitureArray.maxWith(Comparator.comparingInt { it.material.length }).material.length
val maxPriceWidth = furnitureArray.maxWith(Comparator.comparingInt { it.price }).price.toString().length
println("\nUnsorted array:")
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
println("\nSorted alphabetically by name:")
furnitureArray.sortBy { it.name }
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
println("\nSorted alphabetically by material:")
furnitureArray.sortBy { it.material }
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
}