Okay so now we're going to work on the favorite accounts 1
feature so technically it seems that it's just a regular array 2
of accounts okay we can access the list of the favorite 3
accounts we can add an account into the list and we can remove 4
an account to the list by it's ID that's the only one where we're 5
going to need a little bit more logic but otherwise it's just a 6
regular accounts array right so let's go into our project and 7
let's create now a favorite accounts 8
and it's going to be a bank account array, so we should have a 9
function to be able to add something into it so I'm going 10
to add account to favorites and we're going to receive a bank 11
account. 12
And so simply so this is not going to return anything it's 13
just going to perform an operation and here we're just 14
going to push this account into the list so this.favorite 15
accounts.push account and we're good we should have 16
something to return the favorite accounts so I'm going 17
to create a get favorites accounts so this is going to 18
return an array of bank account so bank account array like this 19
and we don't expect any parameters because we're going 20
to use only what we have in our class and we're just going to 21
return this.favorite accounts so we can try to see 22
if we can add an account and get the favorite accounts but 23
we're going to have a problem because we have never 24
initialized the bank accounts here, okay? Uh we have to set a 25
default value so we should put anempty array here. Otherwise, you 26
won't be able to push inside and we'll have an error. So 27
this way it's going to be initialized. Now we could just 28
test it. So technically I can just do an account one dot 29
Add account favorites I'm just going to create a new account 30
so account two 31
I'm going to Create another one. 32
Like this and I'm going to add this account to its favorites. 33
And technically I should be able to see this account if I 34
do a get favorite accounts and take the first one for example 35
and look at the balance it should be 100000 so let's see 36
Okay, it's working fine. So we're good with accessing the 37
favorite accounts, we're good with adding an account, but now 38
we have to remove an account from the list by its ID, 39
meaning that an account should have an ID, which is not the 40
case yet. So we are going to create an ID that is going to 41
be a number. And we're going to receive this ID okay so we will 42
assume that it's not generated but sent through the 43
constructor so I'm going to add an ID that is going to be a 44
number maybe I can put that on first parameter like this okay 45
we're going to receive this ID from the constructor good so 46
now technically here this could be the ID1 and this could be 47
the ID2 and I need to create a function to remove an account 48
from the favorite list by its ID so we're going to create a 49
remove account by ID remove favorite account by ID and 50
we're going to receive an ID and this is not going to return 51
anything it's just going to perform an operation so in the 52
end what we're going to do is we're going to look at the 53
favorite accounts and just remove one element so we're 54
going to use the function splice and and with splice 55
basically you can say that you want to remove exactly one 56
element and here you're going to say starting from where from 57
which index so we need the index to remove this one 58
element so we're going to use the find by index so we're 59
going to take the favorite accounts and find by index and 60
here you can send a function so here we're going to run a 61
function for every element so this is an account so bank 62
account and here we are going to return the index of the 63
element that match this very predicate here that we're going 64
to write so something that returns true or false and we're 65
going to say that if this account 66
in my list is equal equal to the ID that I have sent here 67
then I want to return this element and it's going to 68
return me the index so I will have here the index to Remove. 69
And then I can just send this here but be careful if the 70
element is not found this is going to return minus one so we 71
have to check that if this is equal to minus one that's then 72
we're going to throw an error throw new error account not 73
found in favorites and we should be good so now we could 74
just try to test it quickly So we have the ID2 into our list. 75
So I'm going to remove that and I'm just going to do an account 76
one dot remove favorite account by ID I'm going to remove the 77
ID2 and then print account one.get favorite accounts.length 78
technically should be zero 79
Yep, it is, okay, fine. And now if I try to remove again this, 80
this should trigger an error. Yep, okay, it's crashing, 81
perfect. Okay, so we're good with the specification. Now in 82
the next video we're just going to work on the story. So let's 83
do that in the next one.