1
00:00:01,440 --> 00:00:06,060
All right, so we're done the hard part, we're done designing all of our classes, we wrote all of

2
00:00:06,060 --> 00:00:06,900
our unit tests.

3
00:00:06,930 --> 00:00:09,720
Now we just glue everything together inside men.

4
00:00:10,440 --> 00:00:13,560
So the first task was to create a new bank object.

5
00:00:13,590 --> 00:00:14,790
We're done that already.

6
00:00:15,090 --> 00:00:19,530
And the second task was to define a method that creates an account object and returns it.

7
00:00:19,890 --> 00:00:24,990
So what I'm going to do is define a public static method that returns an account.

8
00:00:26,450 --> 00:00:28,220
Called Create Object.

9
00:00:29,550 --> 00:00:34,470
And this method expects an array of string values, and from these values, it's going to create an

10
00:00:34,470 --> 00:00:35,010
object.

11
00:00:36,320 --> 00:00:39,620
Now you can expect the era of string values to have four elements.

12
00:00:39,830 --> 00:00:46,850
For example, let me just copy and paste one of those as a comment into here so you can expect an array

13
00:00:46,850 --> 00:00:51,920
with four elements where the first element tells us what kind of object we want to create, whether

14
00:00:51,920 --> 00:00:53,390
it's checking savings or loan.

15
00:00:53,720 --> 00:00:58,340
The second, third and fourth elements are actually used to create the object.

16
00:00:58,730 --> 00:01:02,330
So what we can actually do here is approach this one or two ways.

17
00:01:02,840 --> 00:01:08,600
We could create a switch statement that compares the first element against three possible cases.

18
00:01:09,200 --> 00:01:10,610
Case checking.

19
00:01:12,640 --> 00:01:15,970
And here we would create a checking object.

20
00:01:17,780 --> 00:01:21,380
Based on these values, we could do case savings.

21
00:01:26,030 --> 00:01:27,740
And create a Settings object.

22
00:01:29,040 --> 00:01:30,570
Or we could even do case lone.

23
00:01:31,880 --> 00:01:33,680
And then create a lone object.

24
00:01:35,070 --> 00:01:40,200
There is a reason why I don't like this method, and that's because it promotes copying and pasting,

25
00:01:40,800 --> 00:01:45,630
because if you think about it, let's just say the case is checking, then here you would just return

26
00:01:46,050 --> 00:01:47,310
new checking.

27
00:01:50,100 --> 00:01:52,650
Value add index one.

28
00:01:55,500 --> 00:01:56,610
Index two and three.

29
00:01:58,080 --> 00:02:03,720
There you would say, double that price double from the string value because our constructor expect

30
00:02:03,720 --> 00:02:04,230
to double.

31
00:02:06,520 --> 00:02:12,430
Now, so far, it's fine, but the issue is that savings and loan, they have the exact same constructor

32
00:02:12,430 --> 00:02:13,090
as checking.

33
00:02:13,390 --> 00:02:16,150
So you could literally just copy and paste this.

34
00:02:19,200 --> 00:02:22,200
And then just change the class name.

35
00:02:23,890 --> 00:02:29,860
But you know, me, I'm not a big fan of copying and pasting in this case, I could permit it because

36
00:02:29,860 --> 00:02:33,760
it's not always guaranteed that different classes are going to have the same constructor.

37
00:02:34,090 --> 00:02:35,340
But in this case they do.

38
00:02:35,350 --> 00:02:38,320
And you know me, I'm not a big fan of copying and pasting.

39
00:02:38,740 --> 00:02:42,610
So what I'm going to do is actually show you a dynamic way of creating objects.

40
00:02:44,650 --> 00:02:47,740
Go back to learn the part, I left you the syntax right here.

41
00:02:48,480 --> 00:02:49,270
Well, it over.

42
00:02:51,970 --> 00:02:53,770
So here I'm going to return.

43
00:02:55,060 --> 00:02:55,690
Following.

44
00:02:59,330 --> 00:03:04,670
First, you want to specify the class that you want to create an object from the class name includes

45
00:03:04,670 --> 00:03:08,180
the directories that you have to drill into until you get to it.

46
00:03:08,450 --> 00:03:10,940
Let's just say I want to reference the checking class.

47
00:03:11,180 --> 00:03:15,910
We would need to drill into source dot main notice here.

48
00:03:15,920 --> 00:03:17,380
It's not slight.

49
00:03:17,390 --> 00:03:19,280
That's just how you need to pass it into four name.

50
00:03:19,520 --> 00:03:23,720
So I would need to go to source Typekit main Typekit model.

51
00:03:24,770 --> 00:03:26,000
Typekit account.

52
00:03:26,540 --> 00:03:28,850
And here I would use Typekit checking.

53
00:03:29,510 --> 00:03:32,780
So to the string, I'm going to append dot checking.

54
00:03:35,850 --> 00:03:37,360
But obviously, we don't want to do that.

55
00:03:37,380 --> 00:03:41,250
We want to put values at index zero.

56
00:03:43,090 --> 00:03:46,480
So here we're specifying the class that we want to create an object from.

57
00:03:46,750 --> 00:03:49,330
This could be either checking savings or loan.

58
00:03:49,870 --> 00:03:52,360
And then we want to get the constructor for that class.

59
00:03:52,630 --> 00:03:59,170
Lucky for us, checking savings and loan, all of them share the same constructor string string double.

60
00:03:59,920 --> 00:04:03,820
And this line is where you basically pass parameters into the constructor.

61
00:04:04,270 --> 00:04:05,500
Here I'll pass.

62
00:04:06,540 --> 00:04:07,160
The idea?

63
00:04:12,320 --> 00:04:13,220
The person's name.

64
00:04:14,630 --> 00:04:15,710
And their balance.

65
00:04:19,550 --> 00:04:24,830
We're getting an error because our function expects us to return an account object, but this isn't

66
00:04:24,830 --> 00:04:27,290
sure what type of class you're creating an object from.

67
00:04:27,320 --> 00:04:29,750
So we need to type, cast to account.

68
00:04:34,160 --> 00:04:34,800
All right.

69
00:04:34,820 --> 00:04:37,070
And now we get a lot of exceptions.

70
00:04:37,520 --> 00:04:41,900
Now we're not going to throw all of these exceptions separately, that would be a pain.

71
00:04:42,320 --> 00:04:47,930
Instead, consider that the exception class is the base class for every single exception, whether it's

72
00:04:47,930 --> 00:04:49,100
checked or unchecked.

73
00:04:49,550 --> 00:04:53,660
In other words, we can treat every exception in Java as type exception.

74
00:05:08,440 --> 00:05:13,570
All right, now, the last thing I want to stress is let's assume we got these values into the values

75
00:05:13,570 --> 00:05:14,110
parameter.

76
00:05:14,410 --> 00:05:19,090
This would be no different than writing this return new checking.

77
00:05:22,040 --> 00:05:24,470
Values one, values two and values three.

78
00:05:25,950 --> 00:05:26,400
That's all.

79
00:05:26,880 --> 00:05:31,170
But since we want to avoid repetition, we don't want to say new checking, new savings, new loan.

80
00:05:31,410 --> 00:05:36,690
We can use this syntax to create objects on the fly to dynamically create objects.

81
00:05:39,620 --> 00:05:39,920
All right.

82
00:05:39,950 --> 00:05:44,120
What I want to do is confirm that this works, so I'm going to call this method for men.

83
00:05:45,140 --> 00:05:50,720
We'll say create object an inside or in a pass in an array of string values.

84
00:05:55,510 --> 00:05:57,610
Glenn Close, all of these and double quotes.

85
00:06:11,140 --> 00:06:14,140
We get an error because this throws exception.

86
00:06:14,170 --> 00:06:16,540
So what we're going to do is try to run the code.

87
00:06:20,060 --> 00:06:23,630
And catch the exception if it fails, so we'll catch the exception.

88
00:06:24,810 --> 00:06:25,920
And just print it for now.

89
00:06:27,350 --> 00:06:28,850
E get message.

90
00:06:31,120 --> 00:06:31,570
All right.

91
00:06:32,110 --> 00:06:36,280
And a breakpoint right here and make sure that an object gets created.

92
00:06:40,700 --> 00:06:44,390
We get an illegal argument, exception, argument type mismatch.

93
00:06:47,440 --> 00:06:49,450
Oh, that makes a lot of sense.

94
00:06:50,210 --> 00:06:55,360
Assume are creating an object of the checking class the constructor expect to string string and double.

95
00:06:56,500 --> 00:07:01,450
As specified here, string string double, but we're passing in string, string and string.

96
00:07:01,840 --> 00:07:03,610
Accordingly, it throws an exception.

97
00:07:03,940 --> 00:07:08,200
We need to pass this argument into a double before passing it into the constructor.

98
00:07:12,080 --> 00:07:12,440
OK.

99
00:07:13,190 --> 00:07:15,170
Really useful printing that message, by the way.

100
00:07:18,910 --> 00:07:20,080
Let's hope this works.

101
00:07:21,830 --> 00:07:25,820
And it did, but we don't see anything because we're going to have to store this in a variable, so

102
00:07:25,820 --> 00:07:28,130
what I'll do is I'll just say count.

103
00:07:29,330 --> 00:07:32,830
Account is equal to create object restart the runtime.

104
00:07:35,770 --> 00:07:38,170
And wow, this is flood.

105
00:07:38,320 --> 00:07:40,100
I'll put a random print statement.

106
00:07:40,120 --> 00:07:40,780
Try it again.

107
00:07:44,760 --> 00:07:45,540
There it is.

108
00:07:45,720 --> 00:07:46,470
Perfect.

109
00:07:47,040 --> 00:07:51,720
Now we have a way of dynamically creating checking savings and loan objects.

110
00:07:52,500 --> 00:07:58,140
All right now, task for us to define a method that loads every single account from the accounts text

111
00:07:58,140 --> 00:07:59,340
file into the bank.

112
00:07:59,790 --> 00:08:03,090
So what we're going to do is define a method called load accounts.

113
00:08:03,780 --> 00:08:07,440
We'll say public static void load accounts.

114
00:08:08,720 --> 00:08:12,020
It's going to receive an array list of account objects.

115
00:08:16,200 --> 00:08:21,150
And inside of this method, we're going to run through every single accounts in the accounts are list.

116
00:08:22,320 --> 00:08:27,540
And the reason why things aren't working is because we're going to have to import the aerialist class.

117
00:08:36,860 --> 00:08:43,440
And for every account inside of the accounts list, we're going to add it to the bank bank account accounts.

118
00:08:46,530 --> 00:08:50,340
This is great and all, but where are we going to get the ArrayList of accounts from?

119
00:08:51,030 --> 00:08:55,800
We need a method that returns every single account from the accounts that tax filing.

120
00:08:57,920 --> 00:09:02,930
So inside, man, Java will create another method called return accounts public.

121
00:09:04,020 --> 00:09:07,620
Static, this method returns in a rare list of accounts.

122
00:09:07,950 --> 00:09:11,250
It returns the error list of accounts that we're going to use over here.

123
00:09:16,960 --> 00:09:18,070
Return accounts.

124
00:09:20,810 --> 00:09:24,740
And I already know it's going to throw a file not found exception, so we'll just do it right now.

125
00:09:26,060 --> 00:09:30,470
And now inside this method, we're going to create a new object of the final input stream class fan

126
00:09:30,470 --> 00:09:37,610
input stream if this is equal to new file input stream and this file input stream is going to connect

127
00:09:37,610 --> 00:09:38,870
to the accounts file.

128
00:09:41,330 --> 00:09:47,330
And then we're going to create a new object of the scanner class scanner scan is equal to a new object

129
00:09:47,450 --> 00:09:50,480
scanner that receives input from the file input stream.

130
00:09:54,030 --> 00:09:54,450
OK.

131
00:09:54,480 --> 00:09:55,740
Don't forget to import it.

132
00:09:58,660 --> 00:10:04,730
And then we'll initialize the array, a list of accounts that we're going to populate array list accounts

133
00:10:04,750 --> 00:10:12,310
accounts is equal to a new object of the ArrayList class that can store account objects.

134
00:10:14,550 --> 00:10:20,340
And now we want to scan every single line in the accounts file, so we'll say, well, Scan Dot has

135
00:10:20,340 --> 00:10:26,460
next line while the accounts file has a next line, we're going to add it to the list of accounts accounts

136
00:10:26,460 --> 00:10:27,060
don't add.

137
00:10:29,610 --> 00:10:36,810
And expects an account object to here will create objects and create objects, expects an array of four

138
00:10:36,810 --> 00:10:37,590
values.

139
00:10:38,220 --> 00:10:44,130
And if you look back at the accounts file, every line contains four values separated by a comma.

140
00:10:44,460 --> 00:10:47,400
How do we split the line into four values based on a comma?

141
00:10:49,190 --> 00:10:54,590
We can scan the entire line, scanned our next line and then say, does split.

142
00:10:55,510 --> 00:10:59,920
And we'll use the comma separator to split the line into an array of string values.

143
00:11:03,320 --> 00:11:03,710
OK.

144
00:11:03,770 --> 00:11:08,150
The last thing we got to do is just return the error list return accounts.

145
00:11:19,160 --> 00:11:23,660
And it expects us to handle the exception here, but you know what, I think it might make more sense

146
00:11:23,660 --> 00:11:27,380
to go back to create objects and try to run the following code.

147
00:11:32,340 --> 00:11:37,080
If it happens to fail, our catch method is going to catch the failure catch exception.

148
00:11:38,570 --> 00:11:40,190
Here we're just going to print the message.

149
00:11:41,230 --> 00:11:43,780
E get message and return, no.

150
00:11:46,720 --> 00:11:50,890
We're catching the exception from inside the function, which means the function is not going to throw

151
00:11:50,890 --> 00:11:51,910
an exception anymore.

152
00:11:52,630 --> 00:11:57,790
Whoever is calling it, they can rest assured the exception gets caught in here and we don't need this

153
00:11:57,790 --> 00:11:58,660
anymore as well.

154
00:12:03,160 --> 00:12:05,650
OK, now what I want to do is call this method for men.

155
00:12:07,790 --> 00:12:08,960
We'll try to run the code.

156
00:12:09,380 --> 00:12:16,310
Return accounts will store the result inside of an ArrayList variable that can store account objects.

157
00:12:19,050 --> 00:12:24,600
And if the code happens to fail, we're going to catch the file not found exception and print the message.

158
00:12:26,520 --> 00:12:27,870
E get message.

159
00:12:31,290 --> 00:12:34,350
I'll put a random prince statement before starting the debugger.

160
00:12:35,770 --> 00:12:38,680
And if everything works, we should see 16 elements.

161
00:12:39,040 --> 00:12:40,720
So we'll debug the runtime.

162
00:12:43,170 --> 00:12:43,830
Step over.

163
00:12:45,930 --> 00:12:46,980
And indeed, we do.

164
00:12:47,040 --> 00:12:52,050
This is perfect in the next video, we'll continue the remaining tasks.

