
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.
Instructions:
- Define the 
Productclass with primary constructor propertiesname,price, and parameterquantity. - Implement a property 
quantitywith a custom setter. - Implement the 
calculateTotalValuemethod to calculate and return the total value. - Implement the 
restockmethod to increase the quantity. - Test your implementation by creating instances of the 
Productclass and using its methods. 
Example usage:
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.
Hints:
- Use the 
fieldkeyword in the custom setter to modify the backing field. - Calculate the total value by multiplying 
priceandquantity. 
Once you are done with the exercise, you can check your solution here.
Playground
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(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) } }