From 96c89170a0d3add7cc00364c50655452f8ea68d2 Mon Sep 17 00:00:00 2001 From: Frank Denzer Date: Sun, 25 Jun 2023 11:04:24 +0200 Subject: [PATCH] add logic example --- Frontend-user/lib/basket.dart | 2 +- Frontend-user/lib/sample_data.dart | 35 ++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Frontend-user/lib/basket.dart b/Frontend-user/lib/basket.dart index 8acedb1..3e1e76e 100644 --- a/Frontend-user/lib/basket.dart +++ b/Frontend-user/lib/basket.dart @@ -20,7 +20,7 @@ class ShowBasket extends StatelessWidget { return Card( child: ListTile( leading: const Icon(Icons.abc), - title: Text(SampleData().basket.purchases[index].product.name), + title: Text(SampleData().basket.purchases.keys.elementAt(index).name), //trailing: Text(), ), ); diff --git a/Frontend-user/lib/sample_data.dart b/Frontend-user/lib/sample_data.dart index 50787f6..a97d152 100644 --- a/Frontend-user/lib/sample_data.dart +++ b/Frontend-user/lib/sample_data.dart @@ -40,9 +40,22 @@ class Purchase { } class Basket { - List purchases; - int price; + Map purchases; + double price; + Basket(this.purchases, this.price); + + void addItem(Product product, int quantity) { + if (purchases.containsKey(product)) { + purchases.update(product, (existingQuantity) => existingQuantity + quantity); + } else { + purchases[product] = quantity; + } + } + + void removeItem(Product product) { + purchases.remove(product); + } } //sample data @@ -90,13 +103,13 @@ class SampleData { Product('Schokolade', Unit.menge, 0.23, 7, Category.suessesUndKnabbereien) ]; - Basket basket = Basket([ - Purchase(products[0], 20), - Purchase(products[1], 2200), - Purchase(products[2], 2), - Purchase(products[3], 1), - Purchase(products[4], 1), - Purchase(products[5], 2), - Purchase(products[6], 202) - ], 304); + Basket basket = Basket({ + products[0]: 20, + products[1]: 2200, + products[2]: 2, + products[3]: 1, + products[4]: 1, + products[5]: 2, + products[6]: 202 + }, 304); }