mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02: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/pages/finish_shopping_page.dart';
|
||||||
import 'package:mobile/repos/cart.dart';
|
import 'package:mobile/repos/cart.dart';
|
||||||
import 'package:mobile/repos/product.dart';
|
import 'package:mobile/repos/product.dart';
|
||||||
|
import 'package:mobile/repos/user.dart';
|
||||||
import 'package:mobile/results.dart';
|
import 'package:mobile/results.dart';
|
||||||
import 'package:mobile/widgets/primary_button.dart';
|
import 'package:mobile/widgets/primary_button.dart';
|
||||||
import 'package:mobile/widgets/sized_card.dart';
|
import 'package:mobile/widgets/sized_card.dart';
|
||||||
@ -141,7 +142,8 @@ class CartItemView extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CartPage extends StatelessWidget {
|
class CartPage extends StatelessWidget {
|
||||||
const CartPage({super.key});
|
final User user;
|
||||||
|
const CartPage({super.key, required this.user});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -318,7 +320,7 @@ class CartPage extends StatelessWidget {
|
|||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) =>
|
builder: (context) =>
|
||||||
const FinishShoppingPage()));
|
FinishShoppingPage(user: user)));
|
||||||
},
|
},
|
||||||
child: const Text("Afslut indkøb")),
|
child: const Text("Afslut indkøb")),
|
||||||
),
|
),
|
||||||
|
@ -19,7 +19,7 @@ class Dashboard extends StatelessWidget {
|
|||||||
user: user,
|
user: user,
|
||||||
),
|
),
|
||||||
const AllProductsPage(),
|
const AllProductsPage(),
|
||||||
const CartPage(),
|
CartPage(user: user),
|
||||||
const AllReceiptsPage(),
|
const AllReceiptsPage(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,16 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:mobile/repos/cart.dart';
|
import 'package:mobile/repos/cart.dart';
|
||||||
import 'package:mobile/repos/paying_state.dart';
|
import 'package:mobile/repos/paying_state.dart';
|
||||||
import 'package:mobile/repos/receipt.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/primary_button.dart';
|
||||||
import 'package:mobile/widgets/receipt_item.dart';
|
import 'package:mobile/widgets/receipt_item.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class FinishShoppingPage extends StatelessWidget {
|
class FinishShoppingPage extends StatelessWidget {
|
||||||
const FinishShoppingPage({super.key});
|
final User user;
|
||||||
|
|
||||||
|
const FinishShoppingPage({super.key, required this.user});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -53,8 +57,28 @@ class FinishShoppingPage extends StatelessWidget {
|
|||||||
child: PrimaryButton(
|
child: PrimaryButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
payingStateRepo.next();
|
payingStateRepo.next();
|
||||||
receiptRepo.createReceipt(cart);
|
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
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();
|
payingStateRepo.next();
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
cartRepo.clearCart();
|
cartRepo.clearCart();
|
||||||
|
@ -83,7 +83,7 @@ class ProductRepo extends ChangeNotifier {
|
|||||||
Product(
|
Product(
|
||||||
id: _nextId++,
|
id: _nextId++,
|
||||||
name: "Jägermeister 750 ml",
|
name: "Jägermeister 750 ml",
|
||||||
price: 60,
|
price: 120,
|
||||||
description: ""),
|
description: ""),
|
||||||
Product(
|
Product(
|
||||||
id: _nextId++,
|
id: _nextId++,
|
||||||
|
@ -32,7 +32,8 @@ class UsersRepo extends ChangeNotifier {
|
|||||||
return Err("User with mail $mail already exists");
|
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);
|
users.add(user);
|
||||||
|
|
||||||
return Ok(user);
|
return Ok(user);
|
||||||
@ -51,11 +52,24 @@ class UsersRepo extends ChangeNotifier {
|
|||||||
return Err("User with mail $mail doesn't exist");
|
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() {
|
void addTestUsers() {
|
||||||
users
|
users
|
||||||
..add(User(
|
..add(User(
|
||||||
id: nextId++, mail: "test@test.com", name: "test", password: "test"))
|
id: nextId++,
|
||||||
..add(User(id: nextId++, mail: "", name: "", password: ""));
|
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 mail;
|
||||||
final String name;
|
final String name;
|
||||||
final String password;
|
final String password;
|
||||||
|
int balance;
|
||||||
|
|
||||||
User(
|
User(
|
||||||
{required this.id,
|
{required this.id,
|
||||||
required this.mail,
|
required this.mail,
|
||||||
required this.name,
|
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