Exercise: Implementing the Product class
Define the Product
class with properties:
name
(String): The product's name.price
(Double): The product's price.quantity
(Int): The initial quantity of the product.
Implement a custom setter for the quantity
property that ensures that negative values are set to zero.
Add a method named calculateTotalValue
that returns the product's total value.
Implement a method named restock
that increases the product's quantity by a specified positive amount. If a negative value is specified, the method should do nothing.
- Define the
Product
class with primary constructor properties name
, price
, and parameter quantity
. - Implement a property
quantity
with a custom setter. - Implement the
calculateTotalValue
method to calculate and return the total value. - Implement the
restock
method to increase the quantity. - Test your implementation by creating instances of the
Product
class and using its methods.
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
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in essentials/classes/Product.kt. You can find there example usage and unit tests.
- Use the
field
keyword in the custom setter to modify the backing field. - Calculate the total value by multiplying
price
and quantity
.
Once you are done with the exercise, you can check your solution here.
import org.junit.Test
import kotlin.test.assertEquals
// TODO
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(3.6, Product("Apple", 1.2, 3).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.