0:02 I'm in the middle of creating a movie
0:04 collection system. This movie collection
0:06 system will store up to a 100 movies
0:09 where each movie has a title, ID, and a
0:10 length. We're going to want to be able
0:13 to add, find, update, and print movies
0:16 in our system. So far, we have the
0:18 ability to create individual movies. So,
0:21 each individual movie has a title, an
0:23 ID, and a length. We have been able to
0:27 create a collection. So my collection
0:30 has the movies in it and has the size.
0:32 So it has a 100 movies, but it keeps
0:34 track of how many I actually have in the
0:36 system cuz I might not have all of them
0:39 in the system at any one time. We have
0:42 functions and procedures here to read in
0:44 movies and to update and print those
0:46 movies. And we've got ones to work with
0:49 our collection. I can add a movie into
0:51 my collection and I can print movies out
0:53 from my collection. Now, what I'm
0:55 wanting to do is to be able to find and
0:58 update movies. So, in main here, what
1:00 I've got is I've got my collection, and
1:03 we're starting with the size zero. And
1:05 then what I'm doing is adding two movies
1:07 into my collection. And at the moment,
1:09 printing those movies out. What I want
1:11 to do now is be able to find and update
1:14 a movie within my collection. And I've
1:15 got a bit of a start on that. So, I have
1:18 here a procedure where it's going to
1:21 perform the find and update for me. So,
1:23 it's going to need to find a movie and
1:24 then it's going to update it. In order
1:26 to do that, it needs to know where the
1:27 movies are. So, I have to give it the
1:30 collection of movies to work with. But
1:33 let's start with find. So, find movie is
1:35 going to be used by find and update. And
1:36 this could be useful in lots of other
1:38 cases. If I want to delete movies or if
1:40 I want to do anything else with them,
1:42 then I need to be able to find them. So,
1:45 to find a movie, if you ask me again, to
1:46 find a movie, what do I need? I need to
1:48 know where to look. And in this case, I
1:50 want to tell the user what what they're
1:51 looking for. And so I've written out
1:54 that prompt. So this might be uh you
1:55 know, update a movie. It might be which
1:57 movie do you want to delete. And then
1:59 I'm going to ask the user to enter the
2:01 ID of the movie. So at the moment, we've
2:03 got our collection here. I want to be
2:05 able to know which movie you want to
2:08 delete by ID. So if you say one, then
2:11 I'm update, sorry. If you say one, I'm
2:12 going to update this movie. If you say
2:14 two, we're going to update this movie.
2:16 The find's job is to tell us what the
2:19 index is of that movie. So if I enter
2:22 27, it should tell me that the index of
2:25 that movie is 1. If I update, if I ask
2:28 for ID 1, it should tell me the index
2:31 for that is zero. And if I enter
2:33 anything else, it should tell me that
2:35 it's not one of the values in the array.
2:36 Now, how could we do that? Well, I could
2:40 return anything other than zero or one.
2:42 Now if I return a larger number then I
2:44 might think that that's actually the
2:46 movie to access even though it might be
2:48 larger than the number of elements
2:51 currently stored. So what's always a
2:52 good pattern to use in these cases is
2:55 probably to go with minus one. So if I
3:00 don't find something inside my uh find
3:01 then I know what I can do is return that
3:04 minus one. So minus one is a is a good
3:06 idea and I can use that to indicate that
3:09 it's not found. I did not find it. if I
3:12 return minus one. All right. So, movie
3:14 collection, how do I find it? I need to
3:16 look through all of the
3:18 uh elements or all of the movies in my
3:21 collection to see which one has the ID
3:22 that matches the ID that the user
3:24 entered. Now, once again, if we're doing
3:26 trying to do something, I'm trying to do
3:27 something for all of the elements, I
3:29 know that I should think of that as for
3:33 each element. And I can use my trusty
3:35 for loop to do that. So I can say that
3:37 what I know is I know that I want to
3:40 start from index zero and I want to loop
3:43 while this is less than the size of the
3:46 collection and I want to increment I at
3:48 the end. So I don't really care how big
3:50 the collection is. This will loop over
3:52 all of the elements for me. And now I've
3:54 got to think what do I need to do for
3:57 any one element. So the very first time
4:00 I will be zero as we saw before when we
4:03 hand executed this. So if I if we've got
4:06 I here and I'm executing this uh I will
4:09 be zero. I'll then be looking at this
4:11 element. So how can I check if the if
4:13 this element is the one that I want?
4:15 Well, if this element if it tells me
4:17 what I need here. If this element is the
4:20 one, how do I know? Well, in the
4:24 collection I need to go to the movies
4:28 at that index. So now I have this movie
4:31 and I want to check if that ID is the
4:36 same as the ID that the user is after.
4:39 Okay. So if this ID is right, so if I'm
4:42 after ID 1, then I need to return zero.
4:44 Now where is zero? Well, zero is the
4:46 value I've got up here in my variable I.
4:50 So I has the value zero. So I can return
4:55 not zero. I can return I. So I is the
4:58 value that would return zero. If we go
5:02 through and if it wasn't one then I want
5:03 to go to the next element and check that
5:06 one. So one thing I know that people
5:10 often get wrong here is to do this. Uh
5:12 now let's think about what's going to
5:14 happen if I do this. So if I do this,
5:15 let's check it out. So let's say I do
5:19 want ID of 27. So I've got ID as a
5:21 variable as well. Let's say the user has
5:25 entered 27 into the ID. That's ID there.
5:27 There we go. Hopefully you can see that
5:31 ID 27. Then we loop through. We get I is
5:34 zero. I is less than two. So that's
5:38 fine. So I want to access the movie at
5:41 at index I. I is zero. Movie at I that's
5:43 this one here. Does the ID of that? So
5:46 what's the ID of that? The ID is one. Is
5:50 1 equal to 27? The answer is no. It's
5:52 not. Therefore, I return minus one.
5:54 Well, hang on a minute. Hang hang on a
5:55 minute. I don't want to return minus one
5:58 now, do I? If I return minus one now, I
5:59 didn't even bother checking any of the
6:01 rest of the collection. I don't I don't
6:04 want to do that. So, this else is a
6:06 antiattern. You don't want to do that.
6:08 If you're doing that, think through
6:10 think through it. Think through how it's
6:12 working. What I want to do is keep
6:14 going. So, now what would happen is we
6:16 would get to the end of the loop. I
6:18 would add one to I. So I would go to
6:22 one. One is still less than two. And I
6:25 can access my collection. So collection,
6:26 where's my eraser? There we go. Collection
6:28 Collection
6:32 movies index I. I is one. So this movie
6:34 here, does the ID of that movie, what's
6:39 the ID? 27. Is 27 equal to 27? The
6:42 answer is yes, it is. So that will then
6:45 return 27 the value in variable I which
6:48 is uh sorry that will then return one
6:52 which is the index of the movie which
6:57 has the ID 27. So value of I is one. So
7:00 we return one telling the caller that
7:02 this is the movie that you're looking
7:03 for. This is the movie here. The one
7:06 with index one is the movie that has the
7:09 ID 27. All right. If now we could do the
7:13 same thing again if we do that with say
7:16 the user enters let me redo that that ID
7:20 fear variable. So if let's say I've got
7:22 ID here we go so you can see it a bit
7:25 better. So if we put the ID of one then
7:30 what I do I is initialized to zero here.
7:33 Is I less than size? Is 0 less than two?
7:37 Yes it is. Then we can check this is the
7:40 movie at index I the movie at index I I
7:44 is zero at the moment does this movie's
7:46 ID is one does one equal one? Yes it
7:49 does. Okay I return index zero. So it
7:51 stops executing there. So we can see it
7:54 works for either of these movies. Now if
7:59 the the user entered say 75 as the ID
8:02 then index zero that movie doesn't have
8:05 ID 75. So we go to one. That movie
8:09 doesn't have the ID of 75. So we go to
8:13 two. Two is not less than size. So this
8:14 remember we check that every single time
8:18 we go through the loop. And so that two
8:19 is not less than two. So therefore it
8:22 would end. And that's when we return
8:24 minus one. So I'm returning minus one
8:27 because for each if I find one that
8:29 matches each one I check does it match
8:33 and if it matches I return that index.
8:35 If it doesn't match I I don't I
8:37 therefore go to the next element. So
8:39 performing that for each element. The
8:43 only way I can get to here is if by
8:45 doing that for each element none of the
8:48 elements matched and therefore we got to
8:51 the end and now I return minus one.
8:54 Okay. So this will now find me the ID of
8:56 an element. We can go down here. Let's
9:01 give that a try. Um so I can do oops
9:04 right line and let's just print it out.
9:07 So I'm going to do find movie. So which
9:10 movie do I want to find? So let's have a
9:12 look. I need to pass in the collection.
9:16 So I'll pass in my collection and I'll
9:18 pass in the you know test find. That'll
9:21 be my that'll be my prompt.
9:23 just to see how that works. So save
9:28 that. If I come in, compile
9:33 and run unused variable index. Ah yes.
9:35 Okay. So this is just a warning for the
9:36 moment because I haven't finished find
9:38 and update. We'll come back and fix that
9:41 warning just in a second. And now let's
9:47 run that. So movie one ET
9:53 All right. So which ID do I want to
9:56 find? So if I do 27, when we execute
9:58 that, we see we get the movie ID 1,
10:00 which is what we expected. That is the
10:04 movie at index one. All right. So let's
10:06 finish off this very quickly because the
10:08 find and update is now going to be super
10:10 easy because we've done all the hard
10:14 work. Now I know that find movie returns
10:16 minus one if it can't find it. So we can
10:20 say here it's if index
10:23 is less than zero and we could be extra
10:27 safe if we really wanted to or index is
10:31 larger than or equal to uh the
10:38 So if it's less than zero or larger than
10:42 the size then I can return from this
10:44 because we we did not find it. So in
10:46 which case let's write out a message.
10:50 Write line no movie
10:52 oops to update.
10:55 There we go. And we can return.
10:58 Otherwise, if it succeeded, then I can
11:02 just call our handy update movie
11:04 procedure that we wrote earlier and I
11:06 can pass in the movie. Which movie?
11:08 Well, I know all of the movies are in
11:11 the collection in a field called movies.
11:13 Now, which one do I want? Well, I want
11:17 the one at the index that we found. And
11:22 so, this will let me find find a movie.
11:23 And now I can update that movie by
11:25 accessing it. So if I want to update
11:26 this movie, I'm now going to pass a
11:28 reference to that movie to the update
11:31 movie procedure and it's going to change
11:34 the values in memory at this location.
11:36 So it's really very nice and convenient
11:40 for me. All right, so down here now we
11:43 can remove that test. I can put in this
11:47 find and then printing it out. Okay,
11:49 we're getting very close. Now let's run
11:52 compile this and run it. So if we put in
12:01 and we could put in different values if
12:03 we wanted but this will do. So if I want
12:06 to edit the movie at within ID27 that
12:08 should be the it movie. So we can see
12:12 update it. So I could change the the ID
12:16 of that and let's make it 28 instead.
12:17 And then maybe I can change the length
12:21 to 2.1 and then I'll exit.
12:24 And now we can see that that movie was
12:26 updated. So by writing these small
12:29 procedures and functions, you can very
12:31 quickly start to build functionality
12:33 once you've got the basic things in
12:34 place. All right, I want you to have a
12:36 go at that now. So see if you can
12:39 implement your own find for whatever it
12:42 is you're storing and implement your own
12:44 find and update for that thing. And then
12:47 you should be able to have this working,