Alright, time to correct the exercise. So, well, let's 1
start. Uh so on the left here I have my console running with 2
nodemon in quiet mode. So I'm going to remove that and so 3
we're going to start. So we have to create a bank account 4
with basic operations, add money and withdraw money first. 5
So all right? I'm going to create a class that I'm going 6
to call bank account and I will have a private balance this way 7
it's protected and noone can access the balance outside of 8
the bank account class so this is going to be a number so I'm 9
going to create a constructor that will be able to receive 10
the balance and I'm just going to initialize it with this 11
value so it means that outside for example I could create an 12
account one and now do a new bank account and I could send 13
my balance so for example 14, 000 now I want to be able to 14
add money and withdraw money so I'm going to create two 15
functions I'm going to create deposits and I'm going to 16
create withdraw so deposit is it going to return something no 17
technically no and withdraw nope it's not supposed to 18
return anything it just suppose to do an operation so deposit 19
well you have to tell how much and we're going to receive an 20
amount that will be a number and we're just going to update 21
the balance and add the amount and technically we're good with 22
the add money now with the withdraw it's almost the same 23
but we have to make sure that we're not going to go under the 24
minimum balance that is allowed so by default we're going to 25
suppose that you cannot go under zero right so first we 26
have to receive an amount again here to withdraw. 27
And we're just going to first check if the balance minus the 28
amount is still above zero so if it's under zero then we're 29
going to throw an error so throw new error and we're going 30
to write insufficient funds otherwise we can update the 31
balance and subtract the amount and technically we're good with 32
the withdrawing so now let's do this one get the current 33
balance so for now the balance is private and that's perfect 34
we're just going to create a getter for the balance so it 35
will be get balance and we're going to return this dot 36
balance and technically we're good with this one so we're 37
going to test this three first so I'm just going to do account 38
one. for example deposit 10 000 I'm going to log if I have 39
50000 so account one. get balance I'm just going to write 40
balance before and see Okay. Balance 3 thousand. So it seems 41
to be working. Now I'm going to try to withdraw some money. So 42
account one dot withdraw and I'm going to remove 50000 so 43
this should work. Oh I have to log it after. 44
So balance after withdraw yes zero okay we're good now we're 45
going to try to withdraw one more dollar and this should 46
trigger an error okay so if I save yes okay seems like I have 47
a huge error so we have to to catch this error so we're going 48
to wrap this using try 49
Then we're going to catch the error. 50
So the error here is of type any So you think that this is 51
of type error probably and that would make sense. Um but as you 52
can see you cannot force the type for an error in a catch 53
statement because there is nothing that can really tell 54
you that it's only this error that is going to be thrown 55
okay? Maybe you will have a maximum stack exceed error for 56
example if you made a bad loop in your code or something. So 57
type script assume that it cannot really know what is 58
going to be the error. So you have to set the type unknown. 59
You don't know what it's going to be. And then we're going to 60
print error.message to access only the message. But you'll 61
see that we will have an error. So as you can see message does 62
not exist on the type unknown, okay? There is no such thing as 63
message in an unknown type. So you will have to tell 64
explicitly that you know what's the type of error now. And 65
you're going to use something that called the casting. So 66
you're going to say that error is actually of type error. 67
You're going to the type of error like this. This way you 68
can call message on it because message exists on the type 69
error. Right? So this is called the casting and sometime useful 70
when at first you don't know the type but you as a developer 71
know for sure what it's going to be. Okay? And now yes I have 72
my error displayed. So I cannot perform my operation. Okay 73
cool. So it seems that add money, withdraw money and get 74
current balance are working. So cool, so in the next video 75
we're going to continue. See you in the next one.