WEBVTT 1 00:00:01.939 --> 00:00:06.450 so lets just run this program again just to make sure that it is showing the four 2 00:00:06.450 --> 00:00:11.639 field variables that we have defined and you can see down their we got the fourth fields putting 3 00:00:11.639 --> 00:00:16.260 up the artist field the album field the year field and the song field so that's 4 00:00:16.260 --> 00:00:21.690 working fine so the first approach that I'll take to putting 5 00:00:21.690 --> 00:00:26.070 the data into the classes follows naturally from the data in other words 6 00:00:26.070 --> 00:00:31.420 as each row is read we are gonna create a song object and then add that to the album and when 7 00:00:31.420 --> 00:00:35.489 a new album is found in the data the current album is gonna be stored in the 8 00:00:35.489 --> 00:00:40.659 artist's album list and a new album object created with that current rows 9 00:00:40.659 --> 00:00:45.829 details and likewise the same things can happen when a record for a new artist is 10 00:00:45.829 --> 00:00:51.899 read the current artist is gonna be saved in the artists list and new artist objects 11 00:00:51.899 --> 00:00:57.359 is gonna be then created with the data in the current row and we are gonna put all the code to populate 12 00:00:57.359 --> 00:01:01.829 the objects from the data file in a function and that function will return a list 13 00:01:01.829 --> 00:01:08.260 of artists objects when we are finishing processing the file so lets start by expanding the 14 00:01:08.260 --> 00:01:12.590 existing code that we got in our load data so we got this code we wrote 15 00:01:12.590 --> 00:01:20.610 in the previous video I'm gonna change this a bit now actually what will do is change it 16 00:01:20.610 --> 00:01:25.650 a little bit lets just expanded and put some formatting in their so its easier to read so 17 00:01:25.650 --> 00:01:28.650 I'm going to add some formatting code..... 18 00:01:35.240 --> 00:02:22.299 .... 19 00:02:22.299 --> 00:02:33.290 ....and what that means 20 00:02:33.290 --> 00:02:39.150 at this point is that we just read details for a new artist so we've just read 21 00:02:39.150 --> 00:02:47.940 details from new artist so what we want to do their is to store the current album in 22 00:02:47.940 --> 00:02:52.769 the current artist collection and create a new artist object so will just put our 23 00:02:52.769 --> 00:02:55.769 documentation..... 24 00:02:57.250 --> 00:03:00.579 .... 25 00:03:01.720 --> 00:03:29.420 ..... 26 00:03:29.420 --> 00:03:35.840 .... 27 00:03:36.480 --> 00:03:41.920 .... 28 00:03:41.920 --> 00:03:52.280 .....and then moving down here so we are going to do 29 00:03:52.280 --> 00:04:02.000 a test here....remembering the previous if was on line 30 00:04:02.000 --> 00:04:07.030 94 we are testing for artists we are not doing the test for album so.... 31 00:04:07.030 --> 00:04:14.450 .... 32 00:04:14.450 --> 00:04:30.550 ..... 33 00:04:30.550 --> 00:04:41.520 .... 34 00:04:41.520 --> 00:04:46.050 ....just like before when we are reading details about a new artist in this case we've just read a 35 00:04:46.050 --> 00:04:51.440 new album for the current artists so.... 36 00:04:51.440 --> 00:04:54.550 .... 37 00:04:54.550 --> 00:05:03.710 ....so we want to store the current album in 38 00:05:03.710 --> 00:05:12.600 the artist collection and then create a new album object so.... 39 00:05:12.600 --> 00:05:57.780 .... 40 00:05:57.780 --> 00:06:03.130 ... 41 00:06:03.130 --> 00:06:12.550 .... 42 00:06:16.100 --> 00:06:36.600 .... 43 00:06:36.600 --> 00:06:46.380 ....ok so that is 44 00:06:46.380 --> 00:06:50.640 going through the entire code going through the entire text file albums.txt 45 00:06:50.640 --> 00:06:56.850 which we started doing it or started the with open on line 87 so 46 00:06:56.850 --> 00:07:01.970 down here then what we want to do after we finished reading the last line of 47 00:07:01.970 --> 00:07:05.700 that albums.txt text file then we're gonna have an artist and an album that 48 00:07:05.700 --> 00:07:14.160 haven't been stored so we need to do that now so... 49 00:07:14.160 --> 00:07:39.860 ..... 50 00:07:41.610 --> 00:07:50.080 ... 51 00:07:50.080 --> 00:08:09.180 .... 52 00:08:10.440 --> 00:08:19.590 .... 53 00:08:19.590 --> 00:08:38.620 ..... 54 00:08:38.620 --> 00:08:45.170 ....so we want the 4 to execute to go 55 00:08:45.170 --> 00:08:50.180 through the entire file then this code here is going to start executing after that 56 00:08:50.180 --> 00:08:54.250 4 is complete so its process the file and we are just processing the last line there 57 00:08:54.250 --> 00:08:59.310 that's fixed that up and then returning as I mentioned the artist list that we've 58 00:08:59.310 --> 00:09:04.720 created from this code from reading in that file one thing to be aware of 59 00:09:04.720 --> 00:09:09.380 with this approach is that the objects aren't stored in their collections until a new 60 00:09:09.380 --> 00:09:13.310 record is read from the file now for example the current album is only added 61 00:09:13.310 --> 00:09:18.800 to the artists album list when a record containing a different album is read that's 62 00:09:18.800 --> 00:09:22.900 just the way that we set this up to process so ultimately what that means is the 63 00:09:22.900 --> 00:09:27.610 last set of data won't be stored and that's why I've added this code from line 118 64 00:09:27.610 --> 00:09:33.010 down to process that lasted bit of code to check if theirs a valid artist object on that 65 00:09:33.010 --> 00:09:37.450 last line adds that to the global list of artist and also adds the current album 66 00:09:37.450 --> 00:09:43.660 the last one read into the artists list of albums so let's make a change so we just load data 67 00:09:43.660 --> 00:09:51.870 here so lets test to see whether this works and we are going to do it differently we gonna put.... 68 00:09:51.870 --> 00:10:01.580 ...so we are gonna save the result the artist list that has been returned and print out how many their is so... 69 00:10:01.580 --> 00:10:04.580 .... 70 00:10:06.019 --> 00:10:16.170 ....so lets running this and see what happens we can see it says 71 00:10:16.170 --> 00:10:20.749 there's twenty eight artist and that seems to be working ok but how can we have be 72 00:10:20.749 --> 00:10:25.220 sure that it is well theirs a number of ways we could validate the data contained 73 00:10:25.220 --> 00:10:29.459 in the artist the album the song objects but this sort of checking is probably 74 00:10:29.459 --> 00:10:33.850 best left to the computer because the ability to compare two or more files has 75 00:10:33.850 --> 00:10:37.470 been provided with most operating systems for years but rather than using 76 00:10:37.470 --> 00:10:42.679 command-line tools we can do we can now used the compare files feature 77 00:10:42.679 --> 00:10:46.819 of IntelliJ to do this comparison for us much better to get the computer to be 78 00:10:46.819 --> 00:10:50.829 doing this sort of comparison checking rather than using you 79 00:10:51.329 --> 00:10:55.339 know ourselves to do that so to achieve this all we really have to do is use our objects to 80 00:10:55.339 --> 00:11:00.429 write a new file in the same format as the original and getting IntelliJ to check 81 00:11:00.429 --> 00:11:02.279 for differences 82 00:11:02.279 --> 00:11:06.989 so lets go and write a function to write the check file and then we are gonna call it in the main program 83 00:11:06.989 --> 00:11:17.160 after we got this print out here so close that down and write that method will start down here... 84 00:11:17.160 --> 00:11:25.629 ....so we define a file called... 85 00:11:25.629 --> 00:12:02.619 .... 86 00:12:03.520 --> 00:12:28.040 .... 87 00:12:28.040 --> 00:12:37.640 ..... 88 00:12:37.640 --> 00:13:23.710 .... 89 00:13:23.710 --> 00:13:29.580 ....and that really should be all we need to do to write that out and that should be in the same format as 90 00:13:29.580 --> 00:13:34.520 the original albums.txt files because we've printed it out and with 91 00:13:34.520 --> 00:13:40.700 the relevant information separated by tabs of course as the original file had so to 92 00:13:40.700 --> 00:13:48.020 test this we can then add a call after the print here.... 93 00:13:48.020 --> 00:14:00.150 .....so let's just run that you can see there 94 00:14:00.150 --> 00:14:08.400 I made a mistake in 1.name and I got a bracket there instead of a right brace.... 95 00:14:09.200 --> 00:14:17.880 and run it again....ok we've now got a check file txt that's been created so that's good we have just a quick 96 00:14:17.880 --> 00:14:21.760 look to see that it looks roughly the same theirs the original albums.txt 97 00:14:21.760 --> 00:14:28.250 check file does seem to look pretty good but lets end the video here and in the 98 00:14:28.250 --> 00:14:35.190 next video will start going through and we'll use IntelliJ compare files option to confirm 99 00:14:35.190 --> 00:14:37.840 that these files are identical so let's do that in the next video