Computer scienceProgramming languagesKotlinAdvanced featuresDSL

Require, check, and assert

Application

Report a typo

We have a class that handles a shopping cart and a function to add an Item object, which has a price and quantity, fun addItem(item: Item).

We want to ensure that this function is only performed if the quantity of objects is greater than zero. Otherwise, we should throw an appropriate exception with the message: "Item quantity must be greater than zero". What piece of code would you use at the beginning of the function?

Enter a short text

Create a free account to access the full topic