1 00:00:05,200 --> 00:00:09,500 Hello, everyone. Welcome back to the challenge solution for section 13. 2 00:00:10,490 --> 00:00:14,190 If you look through the project that you really found what we had to implement was 3 00:00:14,190 --> 00:00:18,490 basically three methods in the movies.cpp file. 4 00:00:18,990 --> 00:00:23,490 And if you look at your movies h file, you can basically see what's going on here. 5 00:00:23,790 --> 00:00:27,790 We have an add movie method that expects the movie name, movie rating and 6 00:00:27,790 --> 00:00:29,090 the number of times watched. 7 00:00:29,090 --> 00:00:33,990 And I want to add that movie to this vector right here, this collection of movies. 8 00:00:34,290 --> 00:00:37,090 But I have two strings and an integer, I don't have a movie object. 9 00:00:37,090 --> 00:00:39,290 So obviously, we need to create a movie object. 10 00:00:39,590 --> 00:00:42,390 But we only create that movie object and add it 11 00:00:42,390 --> 00:00:46,290 if the movie isn't in there already. Okay. And we're deciding that by the name. 12 00:00:47,090 --> 00:00:48,490 So that's one thing we've got to do. 13 00:00:49,590 --> 00:00:52,490 The other thing we need to do is implement this increment watched. 14 00:00:52,790 --> 00:00:56,290 And it's very similar the user is passing in the name of the movie that they want 15 00:00:56,290 --> 00:00:58,490 the watch number incremented by one. 16 00:00:58,490 --> 00:01:02,490 So what do we do. We search through that vector again right here. 17 00:01:02,490 --> 00:01:06,090 And if we find that movie by name, 18 00:01:06,090 --> 00:01:10,590 then we increment its count. Otherwise, we return false. And we do that here as well. 19 00:01:10,590 --> 00:01:14,090 So if we add the movie, if that was successful, we're returning true. 20 00:01:14,090 --> 00:01:18,970 Otherwise, we're returning false. Same here. If we increment the watch account, we're returning true. 21 00:01:18,970 --> 00:01:22,570 Otherwise, we're returning false. We couldn't do it because the movie wasn't there. 22 00:01:22,570 --> 00:01:25,870 And then the last method, we'll implement is this display method, 23 00:01:25,870 --> 00:01:29,860 which just basically loops through that vector and prints out all the movie objects. 24 00:01:29,860 --> 00:01:33,850 Okay. So why don't we get started let's head over to the movie cpp file, 25 00:01:33,850 --> 00:01:38,150 that's where we're going to implement these methods. Now you'll notice we have a default constructor, 26 00:01:38,150 --> 00:01:42,150 we have a destructor. We don't have a copy constructor. We don't really 27 00:01:42,150 --> 00:01:45,950 have anything going on like that. Again, if you look at your movies file, 28 00:01:47,110 --> 00:01:49,310 we don't have any raw pointers here. 29 00:01:49,560 --> 00:01:54,460 So we're fine with the default behavior of the copy constructor. So we don't have to implement it. 30 00:01:54,710 --> 00:01:57,710 Okay. So again let's go back to movie cpp. 31 00:01:57,710 --> 00:02:01,710 We don't really need to implement a destructor since we're not -- there's nothing to destroy. 32 00:02:01,710 --> 00:02:05,110 Unless you want to put output statements in here so that you can see them happening, 33 00:02:05,110 --> 00:02:06,990 then you would certainly implement them that way. 34 00:02:06,990 --> 00:02:09,650 So let's start with this add movie. 35 00:02:09,650 --> 00:02:11,250 Right now it's just returning false 36 00:02:11,750 --> 00:02:15,410 and increment watched also is returning faults. And this will actually run right now. 37 00:02:15,410 --> 00:02:18,510 If you run this project, that's kind of what you get 38 00:02:18,510 --> 00:02:22,110 pretty meaningless but it all works it all compiles. 39 00:02:22,110 --> 00:02:25,410 So that's the next step is trying to get the the functionality right. 40 00:02:25,410 --> 00:02:29,210 So let's start with add movie. So we're right here. I'm going to get rid of that 41 00:02:29,210 --> 00:02:30,410 return statement. 42 00:02:31,070 --> 00:02:34,270 And let's start coding this method right here. 43 00:02:34,270 --> 00:02:37,970 So what do we want to do? Well, we want to loop over that collection movies. 44 00:02:38,630 --> 00:02:42,990 And for every object in there, remember, every object will be a movie object. 45 00:02:42,990 --> 00:02:46,650 We want to get the name for that movie object and compare it to this name right here. 46 00:02:47,310 --> 00:02:51,410 Okay. And if we find the name in there, that means that, 47 00:02:51,410 --> 00:02:54,960 that movie's already in there, we don't want to add it again, so we'll return false. 48 00:02:54,960 --> 00:02:56,730 Otherwise we'll add the movie. 49 00:02:56,730 --> 00:02:59,730 Okay. So let's take this one step at a time. Let's just use 50 00:02:59,730 --> 00:03:04,090 a range-based for loop to do this, and we'll start here. We'll say for and 51 00:03:04,590 --> 00:03:07,490 we'll modify this a little bit as we go, movie 52 00:03:08,370 --> 00:03:12,270 that's each item in the collection, and we're going 53 00:03:12,270 --> 00:03:13,870 to loop over movies. 54 00:03:14,370 --> 00:03:16,370 So there's my range-based for loop. 55 00:03:16,970 --> 00:03:19,970 Now we can check for each movie 56 00:03:20,870 --> 00:03:23,470 that we're going through. We can get the name for the movie. 57 00:03:26,470 --> 00:03:30,820 And if the name of the movie that we're iterating through at each step is equal to 58 00:03:30,820 --> 00:03:33,820 the name that's being passed in, that's that guy right here, 59 00:03:34,920 --> 00:03:36,420 then what happened. 60 00:03:36,420 --> 00:03:40,320 Well, then we found the movie that's in there. There's no sense in adding it again. 61 00:03:40,320 --> 00:03:44,220 We just want to return false. And we're out of here right now. 62 00:03:44,220 --> 00:03:48,420 We could certainly have some false logic and so forth, but why bother. At this point, 63 00:03:48,720 --> 00:03:50,020 remember, if it's the first one, 64 00:03:50,020 --> 00:03:51,920 we're out if it's the last one we're out. 65 00:03:51,920 --> 00:03:55,920 But in any case, if we find a movie that has the same name as the one,we're trying to add, 66 00:03:55,920 --> 00:03:57,420 we're going to return false. 67 00:03:57,670 --> 00:04:02,170 At this point, I've looped through the entire collection, I have not found the movie. 68 00:04:02,170 --> 00:04:04,670 So now what I want to do is add the movie. 69 00:04:04,670 --> 00:04:08,030 Well, in order to add the movie I need to create a movie object to add. 70 00:04:08,030 --> 00:04:09,330 So we'll create movie, 71 00:04:09,880 --> 00:04:12,180 we can just call it temp or anything we'd like 72 00:04:12,180 --> 00:04:15,540 and we'll construct it given the name, the rating 73 00:04:17,529 --> 00:04:18,779 and the watched. 74 00:04:22,140 --> 00:04:24,740 Now we've got an object. We can simply 75 00:04:25,140 --> 00:04:28,940 add it to the movies vector. We can push back, 76 00:04:29,300 --> 00:04:30,800 and we can push back temp. 77 00:04:31,790 --> 00:04:36,450 And at this point, we want to return true right because we were able to add that movie. 78 00:04:36,450 --> 00:04:37,440 That's it. 79 00:04:37,440 --> 00:04:40,440 Now there's a couple little things we can do here to make this a little better. 80 00:04:40,440 --> 00:04:43,940 First of all this, loop right here, that's looping through 81 00:04:43,940 --> 00:04:46,440 that collection or iterating through that collection, 82 00:04:47,430 --> 00:04:51,930 it really -- it's not going to modify the collection.And I want to be sure that it doesn't modify the collection. 83 00:04:51,930 --> 00:04:54,590 This will do that right because these are all returned by copy, 84 00:04:54,950 --> 00:04:58,550 but it's not very efficient. So what I'd rather do is change this 85 00:04:58,550 --> 00:05:01,150 to -- so this is a reference right here. 86 00:05:01,510 --> 00:05:06,370 But if I just leave it as a reference, then this could potentially change it. So I really want to make this a const. 87 00:05:06,870 --> 00:05:10,230 So I want this to be a reference to a const movie at each iteration. 88 00:05:10,230 --> 00:05:14,230 So if I try to change it by accident or by mistake or even intentionally, 89 00:05:14,230 --> 00:05:18,230 if I tried to change that movie in here, I'm going to get a compiler error. 90 00:05:19,030 --> 00:05:21,130 Okay. So that's basically done. 91 00:05:21,130 --> 00:05:24,790 Now let's take a look at the increment watched. Again, I'll get rid of 92 00:05:24,790 --> 00:05:27,390 the return value since we'll do it from scratch. 93 00:05:28,190 --> 00:05:31,590 And what we'll do here is again we want to loop through. 94 00:05:31,590 --> 00:05:35,490 So we're going -- this is a little different though because I want to loop through 95 00:05:35,490 --> 00:05:38,790 until I find it, right. And then when i find it, I want to update it. 96 00:05:39,190 --> 00:05:43,340 So it's a little different logic than we just had before but it's the same idea. 97 00:05:43,340 --> 00:05:47,700 So let's say for movie and we could use auto here if we wanted. 98 00:05:48,560 --> 00:05:51,360 And I want a reference to each one of those movie 99 00:05:51,360 --> 00:05:54,960 objects because if I find the one that matches, I need to change it, right. 100 00:05:54,960 --> 00:05:57,320 So it can't be a const, it needs to be a reference. 101 00:05:57,870 --> 00:06:01,870 So I'm -- again, I'm looping through and let me just -- 102 00:06:01,870 --> 00:06:03,870 I forgot that bit movies right here. 103 00:06:05,870 --> 00:06:08,230 There we go so. Now we've got a range-based for loop. 104 00:06:08,230 --> 00:06:10,790 We're looping through movies. And at each iteration, 105 00:06:10,790 --> 00:06:13,290 we have a movie and it's a reference so we can change it. 106 00:06:13,290 --> 00:06:17,990 Okay. So what are we doing here. Well, we check to see if the name of the movie 107 00:06:17,990 --> 00:06:21,980 is the name that I want to change. So here I'm simply going to say if 108 00:06:22,580 --> 00:06:24,580 that movie's name 109 00:06:28,180 --> 00:06:31,380 if that movie object's name is equal to 110 00:06:31,980 --> 00:06:34,080 the name that was just passed into me, 111 00:06:34,630 --> 00:06:37,630 then at this point, I've got the one that I want to update right. 112 00:06:38,130 --> 00:06:41,330 So I can simply call movie, which is the object 113 00:06:41,690 --> 00:06:44,690 and increment watched. 114 00:06:45,050 --> 00:06:47,350 That's a method in the movie class. 115 00:06:49,010 --> 00:06:52,010 And I want to return true because it was successful and I'm out. 116 00:06:52,670 --> 00:06:54,670 Now at this point, down here, 117 00:06:54,670 --> 00:06:58,030 what happened was I looped through the entire vector 118 00:06:58,030 --> 00:07:01,030 and I did not find any movie with that name. 119 00:07:01,690 --> 00:07:06,190 So at this point, right here, I simply want to return false, right because I'm done. 120 00:07:06,190 --> 00:07:09,190 I wasn't able to increment anything because it wasn't there. 121 00:07:09,990 --> 00:07:11,590 So that should take care of that. 122 00:07:11,590 --> 00:07:14,890 And then the last method that we're going to implement is this display method. 123 00:07:15,250 --> 00:07:19,710 So what do we need to do here? Well, let's first check to see if there are any movies at all, 124 00:07:19,710 --> 00:07:24,170 right. And notice this is a const method so that's real important, right. We can't modify 125 00:07:24,170 --> 00:07:25,870 that movie's object in here. 126 00:07:26,270 --> 00:07:29,470 So I'm going to say if my movies 127 00:07:31,070 --> 00:07:35,270 .size that's the vector size, how many items are in there. 128 00:07:35,670 --> 00:07:36,970 If that is equal to 0, 129 00:07:37,630 --> 00:07:40,330 then I really have nothing to display. 130 00:07:40,330 --> 00:07:43,530 So I'll just say something like -- let's output, 131 00:07:43,530 --> 00:07:47,190 something like let's say sorry, I think that's what I had before, 132 00:07:47,190 --> 00:07:48,990 sorry, no movies to display. 133 00:07:51,790 --> 00:07:55,150 I'm going to put a new line in there, and we'll put an endline in there as well. 134 00:07:56,950 --> 00:08:00,850 Okay. So we've just taken care of the case where there are no movies. 135 00:08:00,850 --> 00:08:03,550 Now the other case is there are movies. 136 00:08:03,910 --> 00:08:07,570 So in this case, what I need to do is basically loop through that 137 00:08:07,570 --> 00:08:11,230 movie's vector again. And every time I see a movie object, 138 00:08:11,230 --> 00:08:14,230 tell it to display itself. It knows how to display itself. 139 00:08:14,230 --> 00:08:17,220 So let me just put in one of these 140 00:08:18,320 --> 00:08:21,620 lines just so I don't have to keep typing so much. 141 00:08:21,620 --> 00:08:25,420 So we've got a header line here and we'll put another one here, 142 00:08:26,080 --> 00:08:29,280 and we'll put everything else in between that we've got a nice little separation. 143 00:08:29,280 --> 00:08:31,780 So what do I want to do in here? 144 00:08:31,780 --> 00:08:35,780 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, 145 00:08:36,280 --> 00:08:38,150 for auto movie, 146 00:08:39,350 --> 00:08:42,650 that's my variable name in my movies collection. 147 00:08:43,900 --> 00:08:48,260 So every time I see one of these guys, what do I want to do? Well, I want to tell the movie 148 00:08:48,760 --> 00:08:50,260 to display itself. 149 00:08:52,860 --> 00:08:57,160 It's really that simple. So we're done with this. We've got our logic set up. 150 00:08:57,520 --> 00:09:00,120 Now let's compile and see if we've got any compiler errors. 151 00:09:02,320 --> 00:09:06,920 We've got a compiler error right here because I forgot my standard namespace. 152 00:09:07,720 --> 00:09:10,320 And let's try that one more time. That's it. 153 00:09:10,320 --> 00:09:12,920 We're done everything's working as expected. 154 00:09:13,880 --> 00:09:18,480 Sorry, no movies to display. We add those three items. We display the three movies. 155 00:09:18,480 --> 00:09:21,980 Cinderella already exists. We add ice age. Now we display the four. 156 00:09:21,980 --> 00:09:23,980 We don't display Cinderella twice. 157 00:09:23,980 --> 00:09:28,280 And we increment big an ice age, so we've got 3 and 13. It was 158 00:09:28,640 --> 00:09:30,640 2 and 12. 159 00:09:31,040 --> 00:09:34,640 And then finally, we can't increment that xxx 160 00:09:35,000 --> 00:09:36,800 movie because there is no such thing. 161 00:09:37,680 --> 00:09:39,780 Okay. So that really is about it. 162 00:09:40,440 --> 00:09:43,800 You could add an interface to this. You can add some more attributes 163 00:09:43,800 --> 00:09:47,800 to this to extend it and practice some more. What I would suggest you do is 164 00:09:47,800 --> 00:09:51,400 go to your movies.h file right here 165 00:09:52,170 --> 00:09:55,670 and make this one change, make that movies 166 00:09:56,030 --> 00:09:58,230 not a movie object but a pointer. 167 00:09:58,780 --> 00:10:02,080 So now this movies contains a pointer to a vector. 168 00:10:02,380 --> 00:10:05,380 And now you've really got to think about what you're doing because now we need copy 169 00:10:05,380 --> 00:10:08,780 constructors that are doing deep copies, and we need move constructors, 170 00:10:08,780 --> 00:10:11,440 and we need to be a little bit more careful and more efficient. 171 00:10:11,440 --> 00:10:15,590 So I'll leave this as an exercise to you, but I encourage you to do this. 172 00:10:16,190 --> 00:10:18,690 All right. I hope you had fun doing this project. 173 00:10:20,050 --> 00:10:21,350 Hey, everybody. I'm back. 174 00:10:21,350 --> 00:10:24,710 I just realized that when I wrote this line of code here on line 76, 175 00:10:24,710 --> 00:10:29,010 I was trying to show you that you could use auto here, but I was a little quick on the gun. 176 00:10:29,010 --> 00:10:33,470 We have to be a little bit more careful than just this because this is just going to create an object by copy, 177 00:10:33,470 --> 00:10:34,770 and it's going to be inefficient. 178 00:10:34,770 --> 00:10:38,770 So what we really want to do is we want to replace it with const. We can still use auto, 179 00:10:38,770 --> 00:10:42,370 but we want to make sure it's a reference to a constant object. 180 00:10:42,370 --> 00:10:45,370 And in this case, the auto makes it a movie, right. So we've got 181 00:10:45,370 --> 00:10:47,370 this is equivalent to typing this. 182 00:10:48,250 --> 00:10:51,500 And if we do this with auto, I'll save, 183 00:10:51,500 --> 00:10:53,000 and we'll build and run, 184 00:10:53,790 --> 00:10:56,790 and we've got the same output as before but a little bit more efficient.