Implementing multi-item cart functionality in Android e-commerce app using Kotlin

I’m working on an e-commerce app for Android using Kotlin. Right now I can show products on the home screen and let users add one product to the cart from its detail page. But I’m stuck on how to add multiple products with different quantities to the cart.

Here’s what I want to do:

  1. Let users add various products to the cart
  2. Allow different quantities for each product
  3. Display each product on a separate card in the cart screen

My current setup only works for one product at a time and the quantity is the same for all products when clicked. I’ve got a ProductDetail fragment where users can add items to the cart and a MainViewModel to handle the data.

Has anyone tackled this before? What’s the best way to manage multiple products and quantities in the cart? Any tips on structuring the code or using specific Kotlin features would be super helpful. Thanks!

yo Max_31Surf, i’ve dealt with this before. here’s a quick tip:

use a List in ur ViewModel to store multiple products. each CartItem can have product details and quantity.

then use a RecyclerView with custom adapter to show each item on a card. makes it easy to update quantities n stuff.

for persisting cart data, Room is pretty solid. give it a shot!

For implementing multi-item cart functionality, consider using a List to store CartItem objects. Each CartItem can contain product details and quantity. Here’s a basic structure:

data class CartItem(val id: Int, val name: String, val price: Double, var quantity: Int)

class CartManager {
    private val items = mutableListOf<CartItem>()

    fun addItem(item: CartItem) {
        val existing = items.find { it.id == item.id }
        if (existing != null) {
            existing.quantity += item.quantity
        } else {
            items.add(item)
        }
    }

    fun getItems() = items.toList()
}

Integrate this with your MainViewModel to manage cart data across fragments. For the UI, use a RecyclerView with a custom adapter to display each product on its own card. This approach allows for easy addition, removal, and quantity updates of cart items.

Consider using Room for local storage if you need to persist the cart between app launches. This setup should give you a solid foundation for managing multiple products with different quantities in your cart.

Hey Max_31Surf! I totally get your struggle with the cart functionality. Been there, done that! :sweat_smile:

Have you thought about using a List or Map to store multiple cart items? Something like:

val cartItems = mutableListOf<CartItem>()

Where CartItem could be a data class holding product info and quantity. Then you could add/remove/update items easily:

fun addToCart(product: Product, quantity: Int) {
    val existingItem = cartItems.find { it.productId == product.id }
    if (existingItem != null) {
        existingItem.quantity += quantity
    } else {
        cartItems.add(CartItem(product.id, product.name, product.price, quantity))
    }
}

For the UI, a RecyclerView would work great to show each product on its own card. You could use data binding to make updating the view super easy.

Just curious, have you considered using Room for local storage? It might be helpful if you want to persist the cart between app launches.

How are you handling the product selection on the detail page? Are you using a number picker or something else for quantity? Would love to hear more about your setup!