Hello, everyone. Welcome to the section 13 challenge.
Congratulations on finishing up this section.
This section was long. It had a lot of information.
Some of it pretty easy, some of it not so easy.
So now we're at the challenge. And what we want to do here is we want to
apply some of the things that we've learned.
Obviously, we're not going to do everything that we've learned in one challenge project,
that would make it a very complex project.
But we want to model some objects.
We want to model other objects in terms of those objects we want to be able to call methods
and break up our program into separate files
so that we can manage it easily. So let's talk about the challenge.
For this challenge, we're going to develop the foundation for a program for movie fanatics.
And what we want to do is they want to keep track of the movies they've watched
and how many times they've watched each movie. So the program has to support the following.
We're going to have a class called movie.
Now this models a single movie,
which includes the movie's name, the movie's rating and how many times we've watched the movie.
Of course, we can add fields to this like um
the rating, you're rating for the movie, so like 1 2 3 4 5,
depending on how much you like it.
Also a note maybe a little comment area that you could type information about the movie.
All kinds of information maybe the who the leading star of the movie is and so forth. forth.
You get the idea.
But at a minimum, this is what we're going to do just the movie's name,the movie's rating,
which would be gpg pg pg-13 or r,
and how many times you've watched the movie.
We're also going to model a collection of movies.
And that's what the movies class does.
Now notice this is plural because it's modeling a collection of movie objects.
We're going to model it with a vector.
Now obviously, the movie's class needs to know about the movie
since it is a collection of movie objects.
However, our main driver should only interact with the movies class.
So it's going to create a movie's object and work with that.
Okay. So here's a simple example of what main could do.
So we're going to create a movies object.
Then I'm going to ask that movie's object to add a movie by providing the movie's name,
the rating and the watch count.
I could ask the movie's object to increment the watch count for a specific movie given its name.
And I could also ask the movie's objects to display all the movies that it has.
Now obviously the movies class has a bunch of movies in it.
So in that case, it could just ask each movie,
hey, display yourself, display yourself, display yourself. And that's kind of what we're going to do.
Now additionally,
we need to have a little bit of logic here so that we don't have duplicate movies.
So if we try to add a movie whose name is already in the movies collection,
we should display an error to the user.
And if we try to increment the watched count
for a movie whose name is not in the movies collection, we should also display an error to the user.
I've created a basic shell, and that basic shell is right here
in the section 13 challenge project.
Movie and main have been fully implemented for you with comments,
movies has not.
So the idea here is that you would go into the movies.cpp
in the movies.h, and I'll talk about the structure in a second
and implement those.
Now the shell is there. So the stubs are all there
just, look for the places where i write you implement this.
The other option is don't use any of my stuff. Do it yourself.
Just go based on this description
as well as the main because the mail the main gives you a little bit of insight
into the structure and then implement movie cpp, movies cpp,
the h files all that stuff yourself
depending on on how comfortable you feel with c++ plus right now.
If you're a beginner, just start out with the
shell that I'm giving you. If you feel
more comfortable and you've a little bit more advanced, then just try it on your own.
Okay. So how are we going to structure our project.
Well, we're going to have different files. We want to be able to separate our files.
So this is what it's going to look like.
We're going to have movie.h, that's an include file
that has the specification
for a movie class. And again, remember, a movie
is a model of a single movie.
Then we've got movie.cpp, which is our implementation
just like we've seen before. I've got movies.h,
which is our include file for the movies class specification.
And then movies.cpp is the implementation.
And then finally,I have a main cpp that just has the main driver
that creates the movies object and adds movies to it, increments counts and displays.
Okay. So again this program might sound simple. It's not
it's not trivial, for sure.
It's definitely not trivial if you try to do it 100% on your own.
But it's a fun project to do, and it'll really help you with the coding.
Now what I want to do is I want to show you the main.
And I've included the main down here
in the description, but I'm going to show it to you in the source code,it's the same thing.
And I'm going to show you the main for the challenge solution here.
So I'm going to double click on that main cpp file.
And you can see that there's two helper functions that I've already written,
and they're the function prototypes here.
And the reason I wrote those is because
when I asked the movie's object to add a movie and that movie is already there,
I need to display an error message, right. So I have all that if else logic.
So if the movie's not there at it, if it is,
give me an error message, that kind of thing.
So I wanted to wrap that up in a helper function
so that my main looks really nice like this. Just
add movie rather than have all that if else print out logic
all in the main, which kind of gets really ugly and hard to follow.
But let's walk through this really quickly here.
Here's my main, and I'm going to create a movies object that's the collection.
And then I'm going to ask that movie's object to display its movies.
Obviously, there are no movies in there now. So it should display something like no movies or
you have no movies yet or something, something meaningful.
Then what I want to do is
I want to call add movie. Now add movie is this helper function that I created, right.
Here you can see the comment right here.
So I'm going to call add movie. I'm going to pass in my movies,
right, as a reference.
I'm going to pass in the information for the movie. So in this case, I want to add
big, which is the movie's name pg-13, and I've watched it twice.
At that point, this function here gets called.
And notice, what it's doing.
It's asking the movies object to add the movie.
Okay.So at this point,it's really delegating that behavior to the movie's object.
And again, the only reason this function is here right here
is to help us with that error checking so we don't clutter up all the main code.
That's it. Now we're going to add another movie, star wars. And I'm going to add a third movie, Cinderella.
So if I run this at this point, this is what I've got.
You can see, I'll move this over so we can kind of line them up.
Here's my movies. And I'm going to say display the movies.
And it's going to say, sorry, no movies to display.
Now I'm going to add a movie, add a movie and add a movie.
And this is going to say big added,
star wars added, Cinderella added and that message is coming right from here.
Okay. So now i want to display the movies.
So what I can do is I'll just comment this line out. We'll take it one step at a time
that way you'll really understand what's going on.
So I'm going to ask the my movies objects to display itself.
Well, it's going to loop through its collection and print out all the movie objects that it has.
So in this case, we've added big, star wars and Cinderella.
So that's what I expect to print out. So let's run this.
And here you see again
that we added big, star wars and Cinderella like before.
Now when we call the display method of the my movies class,
this is what I'm going to get:
big, star wars and Cinderella. So it's going to print out those three movies.
Okay. So it's really simple as that. And let's just add a couple of more
here
and display those too. Now this is a little bit different now because I'm adding
my movie, and I'm adding Cinderella.
Well, I've already added Cinderella so I expect this to tell me sorry it already exists.
Then I want to add ice age, which should be okay. And then I want to print those out again.
I should only see Cinderella once, right. If i see Cinderella twice, then there's something wrong with my code.
So let's run this.
And you can see here big added, star wars added, Cinderella added, just like we did before.
We're displaying those three movies.
Now Cinderella already exists, so we didn't add it again.
We did add ice age. And then when we display all four of the movies now that's we expect.
We don't expect to see Cinderella twice.
Okay.
So a couple more things.
We can obviously watch a movie again,
and this doesn't store this information on
on disk on file or anything. We'll get to all that stuff later. But right now,
it's just keeping it all in memory.
So in this case, I'm going to say increment watched
my movie. So what I'm saying here is I want to increment the watch count
for big.
And I want to increment the watch account for ice age.
Now if you remember, the watch count for big is 2
and for ice age was 12.
So now when I display my movies, I would expect to see 3 and 13, right.
So let's try that out.
So big was 2,
ice age was 12.
Now big is 3, ice age is 13,
just like what we expect.
And now the last test case here,
and this is by no means a lot of test cases, but it's enough to get you started.
Now what I want to do is I want to increment the number of times that I've watched
this movie called xxx,
and that could be anything as long as it's not in the collection.
Obviously, it's not in the collection, so I can't implement -- I can't increment it, right. It makes no sense.
So I would expect some sign of some sort of error message there. So let's run this one last time.
And what we get is
that last statement xxx not found so I obviously
there's no movie called xxx. So I could not have incremented the watch out for it.
Okay. So that's a simple run, you get the idea.
Notice there's no user menu where you can say select 1 to add a movie, select 2 to delete a movie.
We could certainly add that later but we need to get this part working first.
So now let's take a look at the movie h
file, which is our header. And there it is right here.
This models a movie with the following attributes. And you can see I've already implemented this.
I've got a name, a rating and a watch, which are strings and integers.
I've got a constructor that expects all three of those attributes.
I've got a copy constructor that -- it's really just empty.
I've got a destructor again that's empty that we're not using any raw pointers,
so we really don't need these.
And then I've implemented the basic getters and setters,
set name, get name and so forth.
Notice that these are const correct. So my get names are all const.
are all const. My get functions, I should say,
as well as my display.
So I don't want my display modifying anything. So I'm going to make that a const method.
That's it.
Now let's look at movie cpp, which is the implementation.
And you can see here I'm implementing the constructor as an initializer list.
Here's my copy constructor, which is simply delegating to the constructor.
I could have simply used the default that I get for free from the compiler,
but I decided to implement it explicitly.
So again, just to get a little bit more practice,
my destructor is here. It doesn't do anything.
And here is the display method. The display method just displays
the name, the rating and the watched for each one of those objects.
That's it.
Now let's take a look at movies.h.
Here's the specification for my movies class.
And again, movies is a collection of movie objects.
And there's my vector.
I have a constructor. I have a destructor.
I have the add movie method, which you have to implement.
And I have the increment watched method which you have to implement as well as a display method.
So if we look at the cpp files, those are all in there, right.
I've got no code in my constructor right now a default constructor.
I've got no code in my destructor.
And there's my add movie.
Right now it's just returning false. You need to implement the logic in this method.
And here's my increment watched, which again returns false.
And then finally, we have the display method, which displays those objects.
Now obviously, all you want to do here is just loop through that collection
and display each movie object.
And remember, each movie knows how to display itself. So just call
that movie object.display method.
Okay. So a lot here. Take your time, try to understand the logic.
If you have any questions, let me know. And good luck with this.
When you're done, take out -- check out the challenge solution.
Obviously, if you're doing this from scratch, my solution is going to be different from yours.
But good luck with it, and most of all have fun.