Initial commit
This commit is contained in:
15
lab_4/Furniture.kt
Normal file
15
lab_4/Furniture.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package lab_4
|
||||
|
||||
class Furniture(val name: String, val material: String, val price: Int, private val length: Int, private val width: Int, private val height: Int) {
|
||||
fun print(nameWidth: Int, materialWidth: Int, priceWidth: Int){
|
||||
val printString = StringBuilder()
|
||||
printString
|
||||
// .append("Furniture stats: {")
|
||||
.append("Name: ${this.name.padEnd(nameWidth)} ")
|
||||
.append("Material: ${this.material.padEnd(materialWidth)} ")
|
||||
.append("Price: ${(this.price.toString() + " cu").padEnd(priceWidth + 3)} ")
|
||||
.append("Size: ${this.length}×${this.width}×${this.height}")
|
||||
|
||||
println(printString)
|
||||
}
|
||||
}
|
||||
24
lab_4/main.kt
Normal file
24
lab_4/main.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package lab_4
|
||||
|
||||
fun main() {
|
||||
val furnitureArray = listOf(
|
||||
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.maxBy {it.name.length}.name.length
|
||||
val maxMaterialWidth = furnitureArray.maxBy { it.material.length }.material.length
|
||||
val maxPriceWidth = furnitureArray.maxBy { it.price }.price.toString().length
|
||||
|
||||
println("\nUnsorted array:")
|
||||
furnitureArray.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
||||
|
||||
println("\nSorted alphabetically by name:")
|
||||
furnitureArray.sortedBy { it.name }.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
||||
|
||||
println("\nSorted alphabetically by material:")
|
||||
furnitureArray.sortedBy { it.material }.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
||||
}
|
||||
Reference in New Issue
Block a user