Hello, everyone. Welcome back to the challenge solution for section 13.
If you look through the project that you really found what we had to implement was
basically three methods in the movies.cpp file.
And if you look at your movies h file, you can basically see what's going on here.
We have an add movie method that expects the movie name, movie rating and
the number of times watched.
And I want to add that movie to this vector right here, this collection of movies.
But I have two strings and an integer, I don't have a movie object.
So obviously, we need to create a movie object.
But we only create that movie object and add it
if the movie isn't in there already. Okay. And we're deciding that by the name.
So that's one thing we've got to do.
The other thing we need to do is implement this increment watched.
And it's very similar the user is passing in the name of the movie that they want
the watch number incremented by one.
So what do we do. We search through that vector again right here.
And if we find that movie by name,
then we increment its count. Otherwise, we return false. And we do that here as well.
So if we add the movie, if that was successful, we're returning true.
Otherwise, we're returning false. Same here. If we increment the watch account, we're returning true.
Otherwise, we're returning false. We couldn't do it because the movie wasn't there.
And then the last method, we'll implement is this display method,
which just basically loops through that vector and prints out all the movie objects.
Okay. So why don't we get started let's head over to the movie cpp file,
that's where we're going to implement these methods. Now you'll notice we have a default constructor,
we have a destructor. We don't have a copy constructor. We don't really
have anything going on like that. Again, if you look at your movies file,
we don't have any raw pointers here.
So we're fine with the default behavior of the copy constructor. So we don't have to implement it.
Okay. So again let's go back to movie cpp.
We don't really need to implement a destructor since we're not -- there's nothing to destroy.
Unless you want to put output statements in here so that you can see them happening,
then you would certainly implement them that way.
So let's start with this add movie.
Right now it's just returning false
and increment watched also is returning faults. And this will actually run right now.
If you run this project, that's kind of what you get
pretty meaningless but it all works it all compiles.
So that's the next step is trying to get the the functionality right.
So let's start with add movie. So we're right here. I'm going to get rid of that
return statement.
And let's start coding this method right here.
So what do we want to do? Well, we want to loop over that collection movies.
And for every object in there, remember, every object will be a movie object.
We want to get the name for that movie object and compare it to this name right here.
Okay. And if we find the name in there, that means that,
that movie's already in there, we don't want to add it again, so we'll return false.
Otherwise we'll add the movie.
Okay. So let's take this one step at a time. Let's just use
a range-based for loop to do this, and we'll start here. We'll say for and
we'll modify this a little bit as we go, movie
that's each item in the collection, and we're going
to loop over movies.
So there's my range-based for loop.
Now we can check for each movie
that we're going through. We can get the name for the movie.
And if the name of the movie that we're iterating through at each step is equal to
the name that's being passed in, that's that guy right here,
then what happened.
Well, then we found the movie that's in there. There's no sense in adding it again.
We just want to return false. And we're out of here right now.
We could certainly have some false logic and so forth, but why bother. At this point,
remember, if it's the first one,
we're out if it's the last one we're out.
But in any case, if we find a movie that has the same name as the one,we're trying to add,
we're going to return false.
At this point, I've looped through the entire collection, I have not found the movie.
So now what I want to do is add the movie.
Well, in order to add the movie I need to create a movie object to add.
So we'll create movie,
we can just call it temp or anything we'd like
and we'll construct it given the name, the rating
and the watched.
Now we've got an object. We can simply
add it to the movies vector. We can push back,
and we can push back temp.
And at this point, we want to return true right because we were able to add that movie.
That's it.
Now there's a couple little things we can do here to make this a little better.
First of all this, loop right here, that's looping through
that collection or iterating through that collection,
it really -- it's not going to modify the collection.And I want to be sure that it doesn't modify the collection.
This will do that right because these are all returned by copy,
but it's not very efficient. So what I'd rather do is change this
to -- so this is a reference right here.
But if I just leave it as a reference, then this could potentially change it. So I really want to make this a const.
So I want this to be a reference to a const movie at each iteration.
So if I try to change it by accident or by mistake or even intentionally,
if I tried to change that movie in here, I'm going to get a compiler error.
Okay. So that's basically done.
Now let's take a look at the increment watched. Again, I'll get rid of
the return value since we'll do it from scratch.
And what we'll do here is again we want to loop through.
So we're going -- this is a little different though because I want to loop through
until I find it, right. And then when i find it, I want to update it.
So it's a little different logic than we just had before but it's the same idea.
So let's say for movie and we could use auto here if we wanted.
And I want a reference to each one of those movie
objects because if I find the one that matches, I need to change it, right.
So it can't be a const, it needs to be a reference.
So I'm -- again, I'm looping through and let me just --
I forgot that bit movies right here.
There we go so. Now we've got a range-based for loop.
We're looping through movies. And at each iteration,
we have a movie and it's a reference so we can change it.
Okay. So what are we doing here. Well, we check to see if the name of the movie
is the name that I want to change. So here I'm simply going to say if
that movie's name
if that movie object's name is equal to
the name that was just passed into me,
then at this point, I've got the one that I want to update right.
So I can simply call movie, which is the object
and increment watched.
That's a method in the movie class.
And I want to return true because it was successful and I'm out.
Now at this point, down here,
what happened was I looped through the entire vector
and I did not find any movie with that name.
So at this point, right here, I simply want to return false, right because I'm done.
I wasn't able to increment anything because it wasn't there.
So that should take care of that.
And then the last method that we're going to implement is this display method.
So what do we need to do here? Well, let's first check to see if there are any movies at all,
right. And notice this is a const method so that's real important, right. We can't modify
that movie's object in here.
So I'm going to say if my movies
.size that's the vector size, how many items are in there.
If that is equal to 0,
then I really have nothing to display.
So I'll just say something like -- let's output,
something like let's say sorry, I think that's what I had before,
sorry, no movies to display.
I'm going to put a new line in there, and we'll put an endline in there as well.
Okay. So we've just taken care of the case where there are no movies.
Now the other case is there are movies.
So in this case, what I need to do is basically loop through that
movie's vector again. And every time I see a movie object,
tell it to display itself. It knows how to display itself.
So let me just put in one of these
lines just so I don't have to keep typing so much.
So we've got a header line here and we'll put another one here,
and we'll put everything else in between that we've got a nice little separation.
So what do I want to do in here?
Well, I want to say for my movie and I could say auto here as well, it's going to figure out what it is,
for auto movie,
that's my variable name in my movies collection.
So every time I see one of these guys, what do I want to do? Well, I want to tell the movie
to display itself.
It's really that simple. So we're done with this. We've got our logic set up.
Now let's compile and see if we've got any compiler errors.
We've got a compiler error right here because I forgot my standard namespace.
And let's try that one more time. That's it.
We're done everything's working as expected.
Sorry, no movies to display. We add those three items. We display the three movies.
Cinderella already exists. We add ice age. Now we display the four.
We don't display Cinderella twice.
And we increment big an ice age, so we've got 3 and 13. It was
2 and 12.
And then finally, we can't increment that xxx
movie because there is no such thing.
Okay. So that really is about it.
You could add an interface to this. You can add some more attributes
to this to extend it and practice some more. What I would suggest you do is
go to your movies.h file right here
and make this one change, make that movies
not a movie object but a pointer.
So now this movies contains a pointer to a vector.
And now you've really got to think about what you're doing because now we need copy
constructors that are doing deep copies, and we need move constructors,
and we need to be a little bit more careful and more efficient.
So I'll leave this as an exercise to you, but I encourage you to do this.
All right. I hope you had fun doing this project.
Hey, everybody. I'm back.
I just realized that when I wrote this line of code here on line 76,
I was trying to show you that you could use auto here, but I was a little quick on the gun.
We have to be a little bit more careful than just this because this is just going to create an object by copy,
and it's going to be inefficient.
So what we really want to do is we want to replace it with const. We can still use auto,
but we want to make sure it's a reference to a constant object.
And in this case, the auto makes it a movie, right. So we've got
this is equivalent to typing this.
And if we do this with auto, I'll save,
and we'll build and run,
and we've got the same output as before but a little bit more efficient.