Created
February 16, 2020 19:00
-
-
Save Abion47/f4c2080c7c6257016bfba84cc87fd9f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction { | |
final int amount; | |
final DateTime date; | |
final String title; | |
final String accountType; | |
final String notes; | |
final String id; | |
final String repeat; | |
Transaction({ | |
this.amount, | |
this.date, | |
this.title, | |
this.accountType, | |
this.notes, | |
this.id, | |
this.repeat, | |
}); | |
} | |
Map<String, Transaction> _userTransactions; | |
int getDailyBalance(DateTime targetDate) { | |
// (Start with a list of all the values in the map) | |
return _userTransactions.values | |
// ...identify all Transaction that have a certain value for the argument date | |
.where((t) => t.date == targetDate) | |
// Then I need access to the values from the argument amount | |
.map((t) => t.amount) | |
// ...add up all values for the daily balance | |
.reduce((a1, a2) => a1 + a2) | |
// (Default to zero if no matching values are found) | |
?? 0; | |
} | |
void main() { | |
final now = DateTime.now(); | |
_userTransactions = { | |
'one': Transaction(amount: -45, date: now, title: 'socks', accountType: 'timr', notes: 'joa' , id: 'kololdcd', repeat: 'always'), | |
'two': Transaction(amount: -60, date: now, title: 'present', accountType: 'timr', notes: 'brother' , id: 'kolofkfrcd', repeat: 'never'), | |
} ; | |
print(getDailyBalance(now)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment