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