52 lines
1.1 KiB
Dart
52 lines
1.1 KiB
Dart
import '/models/frequency.dart';
|
|
|
|
class Expense {
|
|
final int? id;
|
|
final String name;
|
|
final double cost;
|
|
final Frequency frequency;
|
|
final String description;
|
|
|
|
const Expense({
|
|
this.id,
|
|
required this.name,
|
|
required this.cost,
|
|
required this.frequency,
|
|
required this.description,
|
|
});
|
|
|
|
@override
|
|
String toString() {
|
|
//return "$name, $cost, ${frequency.title}, $description";
|
|
return toMap().toString();
|
|
}
|
|
|
|
double calcComparableCost() {
|
|
return cost * frequency.timesPerYear;
|
|
}
|
|
|
|
double calcComparableCostDaily() {
|
|
return cost / frequency.numDays;
|
|
}
|
|
|
|
factory Expense.fromMap(Map<String, dynamic> json) => Expense(
|
|
id: json['id'],
|
|
name: json['name'],
|
|
cost: json['cost'],
|
|
frequency: Frequency.values
|
|
.where((expense) => expense.title == json['frequency'])
|
|
.first,
|
|
description: json['description'],
|
|
);
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'name': name,
|
|
'cost': cost,
|
|
'frequency': frequency.title,
|
|
'description': description,
|
|
};
|
|
}
|
|
}
|