class Product(
val name: String,
val price: Double,
initialQuantity: Int
) {
var quantity: Int = initialQuantity
set(value) {
field = if (value >= 0) value else 0
}
fun calculateTotalValue(): Double {
return price * quantity
}
fun restock(additionalQuantity: Int) {
if (additionalQuantity > 0) {
quantity += additionalQuantity
}
}
}
Example solution in playground
import org.junit.Test
import kotlin.test.assertEquals
class Product(
val name: String,
val price: Double,
initialQuantity: Int
) {
var quantity: Int = initialQuantity
set(value) {
field = if (value >= 0) value else 0
}
fun calculateTotalValue(): Double {
return price * quantity
}
fun restock(additionalQuantity: Int) {
if (additionalQuantity > 0) {
quantity += additionalQuantity
}
}
}
fun main() {
val laptop = Product("Laptop", 999.99, 5)
println(laptop.name) // Laptop
println(laptop.quantity) // 5
println(laptop.calculateTotalValue()) // 4999.95
laptop.restock(3)
println(laptop.quantity) // 8
println(laptop.calculateTotalValue()) // 7999.92
laptop.quantity = -2
println(laptop.quantity) // 0
println(laptop.calculateTotalValue()) // 0.0
laptop.quantity = 10
println(laptop.quantity) // 10
println(laptop.calculateTotalValue()) // 9999.9
}
class ProductTest {
@Test
fun `should keep name, price and quantity`() {
val product = Product("Apple", 1.0, 10)
assertEquals("Apple", product.name)
assertEquals(1.0, product.price)
assertEquals(10, product.quantity)
}
@Test
fun `should calculate total value`() {
assertEquals(10.0, Product("Apple", 1.0, 10).calculateTotalValue())
assertEquals(0.0, Product("Apple", 1.0, 0).calculateTotalValue())
assertEquals(0.0, Product("Apple", 0.0, 10).calculateTotalValue())
assertEquals(4.8, Product("Apple", 1.2, 4).calculateTotalValue())
}
@Test
fun `should restock`() {
val product = Product("Apple", 1.0, 10)
product.restock(5)
assertEquals(15, product.quantity)
product.restock(-5)
assertEquals(15, product.quantity)
product.restock(0)
assertEquals(15, product.quantity)
}
@Test
fun `should not allow negative quantity`() {
val product = Product("Apple", 1.0, 10)
product.quantity = -5
assertEquals(0, product.quantity)
product.quantity = 8
assertEquals(8, product.quantity)
product.quantity = 0
assertEquals(0, product.quantity)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.