fix: delete old cart classes causing redeclaration and type mismatch
This commit is contained in:
1 parent
7334f326a3
commit
823080245d
3 files changed
+1
-50
No files matched your search
@@ -1,17 +0,0 @@
|
||||
package com.example.minishop.cart
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
data class CartItem(
|
||||
val product: Product,
|
||||
val quantity: Int
|
||||
)
|
||||
|
||||
data class CartState(
|
||||
val items: List<CartItem> = emptyList()
|
||||
) {
|
||||
val totalCount: Int get() = items.sumOf { it.quantity }
|
||||
val totalPrice: Double get() = items.sumOf { it.product.price * it.quantity }
|
||||
|
||||
fun quantityOf(productId: String): Int = items.firstOrNull { it.product.id == productId }?.quantity ?: 0
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.example.minishop.cart
|
||||
|
||||
import com.example.minishop.model.Product
|
||||
|
||||
class CartStore {
|
||||
private val mutable = mutableMapOf<String, CartItem>()
|
||||
|
||||
fun state(): CartState = CartState(mutable.values.sortedBy { it.product.id })
|
||||
|
||||
fun add(product: Product) {
|
||||
val exist = mutable[product.id]
|
||||
mutable[product.id] = if (exist == null) CartItem(product, 1) else exist.copy(quantity = exist.quantity + 1)
|
||||
}
|
||||
|
||||
fun increase(productId: String) {
|
||||
val item = mutable[productId] ?: return
|
||||
mutable[productId] = item.copy(quantity = item.quantity + 1)
|
||||
}
|
||||
|
||||
fun decrease(productId: String) {
|
||||
val item = mutable[productId] ?: return
|
||||
if (item.quantity <= 1) mutable.remove(productId) else mutable[productId] = item.copy(quantity = item.quantity - 1)
|
||||
}
|
||||
|
||||
fun remove(productId: String) {
|
||||
mutable.remove(productId)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
mutable.clear()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user