From 96c89170a0d3add7cc00364c50655452f8ea68d2 Mon Sep 17 00:00:00 2001 From: Frank Denzer Date: Sun, 25 Jun 2023 11:04:24 +0200 Subject: [PATCH 1/2] 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); } -- 2.45.2 From 48e7d76e1a9b61e7c46bbf4076bce3285064253d Mon Sep 17 00:00:00 2001 From: Frank Denzer Date: Sun, 25 Jun 2023 11:11:55 +0200 Subject: [PATCH 2/2] add uuid lib and use it --- Frontend-user/lib/sample_data.dart | 7 +++++-- Frontend-user/pubspec.lock | 24 ++++++++++++++++++++++++ Frontend-user/pubspec.yaml | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Frontend-user/lib/sample_data.dart b/Frontend-user/lib/sample_data.dart index a97d152..78232e9 100644 --- a/Frontend-user/lib/sample_data.dart +++ b/Frontend-user/lib/sample_data.dart @@ -1,3 +1,5 @@ +import 'package:uuid/uuid.dart'; + final now = DateTime.now(); enum TransaktionArt { monatlBeitrag, aufladung, einkauf, korrektur } @@ -27,7 +29,7 @@ class Product { final int id = 0; final String name; final Unit unit; - final double price; //pro Kilogramm oder Stück + final double price; //pro Kilogramm oder Stück, d.h. pro unit final double vat; final Category category; const Product(this.name, this.unit, this.price, this.vat, this.category); @@ -42,8 +44,9 @@ class Purchase { class Basket { Map purchases; double price; + String guid; - Basket(this.purchases, this.price); + Basket(this.purchases, this.price): guid = const Uuid().v4(); void addItem(Product product, int quantity) { if (purchases.containsKey(product)) { diff --git a/Frontend-user/pubspec.lock b/Frontend-user/pubspec.lock index dd54301..d3bb239 100644 --- a/Frontend-user/pubspec.lock +++ b/Frontend-user/pubspec.lock @@ -41,6 +41,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.1" + crypto: + dependency: transitive + description: + name: crypto + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" + source: hosted + version: "3.0.3" cupertino_icons: dependency: "direct main" description: @@ -184,6 +192,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.1" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" + uuid: + dependency: "direct main" + description: + name: uuid + sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313" + url: "https://pub.dev" + source: hosted + version: "3.0.7" vector_math: dependency: transitive description: diff --git a/Frontend-user/pubspec.yaml b/Frontend-user/pubspec.yaml index 23ea054..0c8e232 100644 --- a/Frontend-user/pubspec.yaml +++ b/Frontend-user/pubspec.yaml @@ -28,6 +28,7 @@ environment: # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: + uuid: ^3.0.4 flutter: sdk: flutter -- 2.45.2