mirror of
				https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
				synced 2025-10-31 15:47:00 +01:00 
			
		
		
		
	user balance logic
This commit is contained in:
		
							parent
							
								
									d934195326
								
							
						
					
					
						commit
						432053bcaf
					
				| @ -5,6 +5,7 @@ import 'package:flutter/material.dart'; | ||||
| import 'package:mobile/pages/finish_shopping_page.dart'; | ||||
| import 'package:mobile/repos/cart.dart'; | ||||
| import 'package:mobile/repos/product.dart'; | ||||
| import 'package:mobile/repos/user.dart'; | ||||
| import 'package:mobile/results.dart'; | ||||
| import 'package:mobile/widgets/primary_button.dart'; | ||||
| import 'package:mobile/widgets/sized_card.dart'; | ||||
| @ -141,7 +142,8 @@ class CartItemView extends StatelessWidget { | ||||
| } | ||||
| 
 | ||||
| class CartPage extends StatelessWidget { | ||||
|   const CartPage({super.key}); | ||||
|   final User user; | ||||
|   const CartPage({super.key, required this.user}); | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
| @ -318,7 +320,7 @@ class CartPage extends StatelessWidget { | ||||
|                                   context, | ||||
|                                   MaterialPageRoute( | ||||
|                                       builder: (context) => | ||||
|                                           const FinishShoppingPage())); | ||||
|                                           FinishShoppingPage(user: user))); | ||||
|                             }, | ||||
|                             child: const Text("Afslut indkøb")), | ||||
|                       ), | ||||
|  | ||||
| @ -19,7 +19,7 @@ class Dashboard extends StatelessWidget { | ||||
|         user: user, | ||||
|       ), | ||||
|       const AllProductsPage(), | ||||
|       const CartPage(), | ||||
|       CartPage(user: user), | ||||
|       const AllReceiptsPage(), | ||||
|     ]); | ||||
|   } | ||||
|  | ||||
| @ -2,12 +2,16 @@ import 'package:flutter/material.dart'; | ||||
| import 'package:mobile/repos/cart.dart'; | ||||
| import 'package:mobile/repos/paying_state.dart'; | ||||
| import 'package:mobile/repos/receipt.dart'; | ||||
| import 'package:mobile/repos/user.dart'; | ||||
| import 'package:mobile/results.dart'; | ||||
| import 'package:mobile/widgets/primary_button.dart'; | ||||
| import 'package:mobile/widgets/receipt_item.dart'; | ||||
| import 'package:provider/provider.dart'; | ||||
| 
 | ||||
| class FinishShoppingPage extends StatelessWidget { | ||||
|   const FinishShoppingPage({super.key}); | ||||
|   final User user; | ||||
| 
 | ||||
|   const FinishShoppingPage({super.key, required this.user}); | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
| @ -53,8 +57,28 @@ class FinishShoppingPage extends StatelessWidget { | ||||
|                     child: PrimaryButton( | ||||
|                         onPressed: () async { | ||||
|                           payingStateRepo.next(); | ||||
|                           receiptRepo.createReceipt(cart); | ||||
|                           await Future.delayed(const Duration(seconds: 1)); | ||||
|                           if (user.pay(cartRepo.totalPrice()) is Err) { | ||||
|                             if (context.mounted) { | ||||
|                               showDialog<String>( | ||||
|                                 context: context, | ||||
|                                 builder: (BuildContext context) => AlertDialog( | ||||
|                                   content: const Text( | ||||
|                                       'Du har desværre ikke råd til at købe dette'), | ||||
|                                   actions: <Widget>[ | ||||
|                                     TextButton( | ||||
|                                       onPressed: () => | ||||
|                                           Navigator.pop(context, 'OK'), | ||||
|                                       child: const Text('OK'), | ||||
|                                     ), | ||||
|                                   ], | ||||
|                                 ), | ||||
|                               ); | ||||
|                             } | ||||
|                             payingStateRepo.reset(); | ||||
|                             return; | ||||
|                           } | ||||
|                           receiptRepo.createReceipt(cart); | ||||
|                           payingStateRepo.next(); | ||||
|                           await Future.delayed(const Duration(seconds: 1)); | ||||
|                           cartRepo.clearCart(); | ||||
|  | ||||
| @ -83,7 +83,7 @@ class ProductRepo extends ChangeNotifier { | ||||
|       Product( | ||||
|           id: _nextId++, | ||||
|           name: "Jägermeister 750 ml", | ||||
|           price: 60, | ||||
|           price: 120, | ||||
|           description: ""), | ||||
|       Product( | ||||
|           id: _nextId++, | ||||
|  | ||||
| @ -32,7 +32,8 @@ class UsersRepo extends ChangeNotifier { | ||||
|       return Err("User with mail $mail already exists"); | ||||
|     } | ||||
| 
 | ||||
|     final user = User(id: nextId++, name: name, mail: mail, password: password); | ||||
|     final user = User( | ||||
|         id: nextId++, name: name, mail: mail, password: password, balance: 0); | ||||
|     users.add(user); | ||||
| 
 | ||||
|     return Ok(user); | ||||
| @ -51,11 +52,24 @@ class UsersRepo extends ChangeNotifier { | ||||
|     return Err("User with mail $mail doesn't exist"); | ||||
|   } | ||||
| 
 | ||||
|   Result<int, String> pay(int userId, int amount) { | ||||
|     final user = getUserById(userId); | ||||
|     if (user is Ok) { | ||||
|       return (user as User).pay(amount); | ||||
|     } | ||||
|     return Err("User with id $userId doesn't exist"); | ||||
|   } | ||||
| 
 | ||||
|   void addTestUsers() { | ||||
|     users | ||||
|       ..add(User( | ||||
|           id: nextId++, mail: "test@test.com", name: "test", password: "test")) | ||||
|       ..add(User(id: nextId++, mail: "", name: "", password: "")); | ||||
|           id: nextId++, | ||||
|           mail: "test@test.com", | ||||
|           name: "test", | ||||
|           password: "test", | ||||
|           balance: 1000)) | ||||
|       ..add( | ||||
|           User(id: nextId++, mail: "", name: "", password: "", balance: 10000)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| @ -64,10 +78,20 @@ class User { | ||||
|   final String mail; | ||||
|   final String name; | ||||
|   final String password; | ||||
|   int balance; | ||||
| 
 | ||||
|   User( | ||||
|       {required this.id, | ||||
|       required this.mail, | ||||
|       required this.name, | ||||
|       required this.password}); | ||||
|       required this.password, | ||||
|       required this.balance}); | ||||
| 
 | ||||
|   Result<int, String> pay(int amount) { | ||||
|     if (balance < amount) { | ||||
|       return Err("User can not afford paying amount $amount"); | ||||
|     } | ||||
|     balance -= amount; | ||||
|     return Ok(balance); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user