Lab 4, Kotlin.
This commit is contained in:
parent
5f36bd41ae
commit
4d538d2d04
|
@ -1,4 +1,4 @@
|
|||
package OOP.Java.lab_1;
|
||||
package OOP.Java.lab_1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue