Write a function to aggregate sales data from a list of transactions, each containing multiple product IDs. Use a ProductsDatabase
class with methods to retrieve product details by ID and to get all products. The function should return the names of the most sold products.
Include an option to limit the number of product names returned (e.g., top 5 most sold products).
- Fill in the code.
- You can copy the code into IntelliJ/Android Studio, but it shouldn’t be necessary.
- Click “run” – the green triangle. The code will compile and several tests will be run.
- In the logs, you will see “try again” if the solution is incorrect, or “success” and a special code.
- Provide this code in the form below along with your email and click “send”.
data class Product(val id: Int, val name: String)
data class Transaction(val productIds: List<Int>, val customerId: Int)
class ProductsDatabase {
private val products = listOf(
Product(1, "Keyboard"),
Product(2, "Mouse"),
Product(3, "Monitor")
)
fun getAllProducts() = products
fun getProductById(id: Int) = products.firstOrNull { it.id == id }
}
ps. Again – there are few ways to achieve that 🙂