1 00:00:00,000 --> 00:00:04,836 [MUSIC] 2 00:00:04,836 --> 00:00:08,783 >> So when we delivered our minimum viable project, or MVP, for the karaoke project, 3 00:00:08,783 --> 00:00:11,507 there was another story that didn't quite make the cut and 4 00:00:11,507 --> 00:00:13,010 it got left in the backlog. 5 00:00:13,010 --> 00:00:13,660 It was this. 6 00:00:15,270 --> 00:00:18,680 As a KJ I should know which singer requested the song so 7 00:00:18,680 --> 00:00:20,060 that I can call them up to the stage. 8 00:00:21,340 --> 00:00:26,190 Well as it turns out, after some usage most KJs are dying for that feature. 9 00:00:26,190 --> 00:00:27,170 What happens is this. 10 00:00:27,170 --> 00:00:30,920 They keep calling the song and no one comes up because they either forgot or 11 00:00:30,920 --> 00:00:32,420 they are not paying attention. 12 00:00:32,420 --> 00:00:34,930 So let's see if we can add this feature request to our app real quick. 13 00:00:35,940 --> 00:00:39,250 >> A song request is definitely a model right? 14 00:00:39,250 --> 00:00:40,520 So let's navigate over there. 15 00:00:42,550 --> 00:00:49,260 And, in the model package we'll right click, and say New, Java Class. 16 00:00:49,260 --> 00:00:51,350 And let's call it SongRequest. 17 00:00:51,350 --> 00:00:51,850 Cool. 18 00:00:54,640 --> 00:00:56,250 This is some default setting. 19 00:00:56,250 --> 00:00:57,170 I'm gonna get rid of it. 20 00:00:58,770 --> 00:01:00,480 Okay. So, let's see. 21 00:01:00,480 --> 00:01:01,580 We know we want a couple of things. 22 00:01:01,580 --> 00:01:03,490 Right? We wanna have a private 23 00:01:04,800 --> 00:01:08,460 String lets call it mSingerName. 24 00:01:08,460 --> 00:01:14,751 We want a singer and let's go ahead and pop the song in there so private 25 00:01:14,751 --> 00:01:20,220 song and mSong. 26 00:01:20,220 --> 00:01:24,290 Now remember, it was in the same package as a song so we didn't need to import it. 27 00:01:25,630 --> 00:01:29,140 So I want to explore something now called code generation. 28 00:01:29,140 --> 00:01:31,050 So we wanna have a constructor, right? 29 00:01:31,050 --> 00:01:34,545 The good new is is that it can create one for us. 30 00:01:34,545 --> 00:01:37,380 Because remember, what we want it to do is we want it to pass in the singer name and 31 00:01:37,380 --> 00:01:38,740 the song when you get created. 32 00:01:38,740 --> 00:01:40,780 Let's go ahead and we'll go up to Code. 33 00:01:40,780 --> 00:01:41,850 Then we'll choose generate. 34 00:01:42,850 --> 00:01:44,110 Yeah, we're gonna generate a constructor. 35 00:01:45,310 --> 00:01:46,680 We want it to do both of those, right? 36 00:01:46,680 --> 00:01:48,388 We want it to do both those things. 37 00:01:48,388 --> 00:01:49,213 Okay. 38 00:01:49,213 --> 00:01:51,673 Whoops. 39 00:01:51,673 --> 00:01:52,900 Okay, so almost. 40 00:01:52,900 --> 00:01:56,090 The problem is that the parameters here are following our naming structure, 41 00:01:56,090 --> 00:01:58,808 where we put the member variables in at the front, right. 42 00:01:59,950 --> 00:02:03,030 Remember, that's just a coding style that we're doing and 43 00:02:03,030 --> 00:02:06,480 each project that you work on might adhere to different styles. 44 00:02:06,480 --> 00:02:09,010 Well the cool thing is that your editor knows that, 45 00:02:09,010 --> 00:02:10,470 we just need to tell it our style. 46 00:02:10,470 --> 00:02:13,820 So I am going to go ahead and I am going to Cmd+Z this, get rid of that and 47 00:02:13,820 --> 00:02:21,120 let's go under File, Other Settings, Default Settings. 48 00:02:21,120 --> 00:02:28,200 And if we go under Editor, Code Style, Java and Code Generation. 49 00:02:28,200 --> 00:02:29,640 Here's the naming stuff. 50 00:02:29,640 --> 00:02:33,330 Whenever we have a field, we're gonna prefix it with an M. 51 00:02:33,330 --> 00:02:36,550 Okay, that's really all you have to do. 52 00:02:36,550 --> 00:02:38,000 Say Apply. 53 00:02:38,000 --> 00:02:39,890 Let's go take a look and see if that worked. 54 00:02:41,910 --> 00:02:44,630 So now if we come over here, the generating code thing, 55 00:02:44,630 --> 00:02:46,380 I don't know if you saw it, it was Cmd+N. 56 00:02:46,380 --> 00:02:51,310 So I'm gonna generate a constructor and we're gonna use both of those. 57 00:02:53,530 --> 00:02:54,110 Blam, there it is. 58 00:02:54,110 --> 00:02:56,420 So, setting that up. 59 00:02:56,420 --> 00:02:57,470 Song got moved down there. 60 00:02:57,470 --> 00:02:59,030 Let's move him back up to where he was. 61 00:02:59,030 --> 00:03:00,270 There we go. 62 00:03:02,470 --> 00:03:03,340 Okay. 63 00:03:03,340 --> 00:03:07,410 So now the boring part of adding getters and setters. 64 00:03:07,410 --> 00:03:08,320 Did you see that in the list? 65 00:03:08,320 --> 00:03:08,940 We can do that too. 66 00:03:08,940 --> 00:03:10,590 We don't need to do that anymore. 67 00:03:10,590 --> 00:03:15,190 Let's choose getter and setter and let's make them for both. 68 00:03:15,190 --> 00:03:17,530 You'll note here that there's templates. 69 00:03:17,530 --> 00:03:18,070 We're gonna leave them. 70 00:03:18,070 --> 00:03:18,570 That's fine. 71 00:03:19,580 --> 00:03:20,870 Look at that. 72 00:03:20,870 --> 00:03:22,750 We didn't have to write all that code. 73 00:03:22,750 --> 00:03:25,060 What else is in that generate thing? 74 00:03:25,060 --> 00:03:25,560 Let's see. 75 00:03:26,880 --> 00:03:28,330 To string, that's something handy. 76 00:03:29,840 --> 00:03:33,260 So we will generate to string for both of those, yeah? 77 00:03:33,260 --> 00:03:36,170 We're going to both of those. 78 00:03:36,170 --> 00:03:38,810 Okay well that's one way to do it. 79 00:03:38,810 --> 00:03:42,120 Well look, it remembered to do the override for us, which we learned. 80 00:03:42,120 --> 00:03:43,000 The override annotation. 81 00:03:43,000 --> 00:03:47,240 And if we wanted to change this to make our own template, we could, right? 82 00:03:47,240 --> 00:03:52,940 So let's go ahead and I'm gonna undo that, and we will again, GenerateToString. 83 00:03:52,940 --> 00:03:58,510 Up here there's these templates and we had talked about that a little bit. 84 00:03:58,510 --> 00:04:00,510 So there's different options that you could do. 85 00:04:00,510 --> 00:04:02,710 You can also make a new one, if you wanted to. 86 00:04:02,710 --> 00:04:05,370 If you want to come here you can add a new template. 87 00:04:05,370 --> 00:04:08,980 Okay, and if you really feel like nerding out that's something that you can do. 88 00:04:08,980 --> 00:04:10,390 To make it read however you want to, 89 00:04:10,390 --> 00:04:14,660 to be the default it will automatically generate the same looking to string. 90 00:04:14,660 --> 00:04:18,000 The cool thing is when you save this it will be saved into the project file so 91 00:04:18,000 --> 00:04:19,790 everybody else can use the same to string format. 92 00:04:22,670 --> 00:04:25,340 I think we're fine with the final string concatenation. 93 00:04:25,340 --> 00:04:25,840 Or lets. 94 00:04:27,440 --> 00:04:28,730 Yeah, I think that's fine. 95 00:04:30,290 --> 00:04:31,050 Okay. 96 00:04:31,050 --> 00:04:31,550 Great. 97 00:04:32,620 --> 00:04:37,250 Okay, so one thing that we didn't talk about before is equals. 98 00:04:37,250 --> 00:04:39,850 And you can actually override the equals method and 99 00:04:39,850 --> 00:04:43,820 this pretty much should be done on every class that we create that we plan to use, 100 00:04:43,820 --> 00:04:46,280 especially ones that we use in the collections. 101 00:04:46,280 --> 00:04:49,510 Now one reason that I didn't show you how to do this in the last course 102 00:04:49,510 --> 00:04:52,680 is because it's much easier to have the IDE do it for you. 103 00:04:52,680 --> 00:04:54,410 Ready let's do it and then we'll chat about it. 104 00:04:54,410 --> 00:04:55,330 Okay? So, I'm gonna do, 105 00:04:55,330 --> 00:04:59,100 again, Cmd+N and I'm gonna generate the equals and hashCode. 106 00:05:01,020 --> 00:05:03,680 And let's just use the IntelliJ Default. 107 00:05:05,290 --> 00:05:06,960 And there's a couple of options here. 108 00:05:06,960 --> 00:05:08,090 I don't think we should use getters. 109 00:05:08,090 --> 00:05:12,120 We should use the private methods and we will go over this in the future. 110 00:05:13,710 --> 00:05:19,700 Yeah, so we want both of these and we want both of these in the hash code. 111 00:05:19,700 --> 00:05:22,253 And the non null fields. 112 00:05:24,586 --> 00:05:26,720 Let's not create one of these without nulls. 113 00:05:26,720 --> 00:05:29,586 So, those are non nulls. 114 00:05:29,586 --> 00:05:34,348 Okay, so remember that using double equals on objects just means 115 00:05:34,348 --> 00:05:37,650 that they point to the same object in memory. 116 00:05:38,650 --> 00:05:43,740 So what we really want to write here is we want to determine what equality means. 117 00:05:43,740 --> 00:05:46,190 In this case, if the same person asks for 118 00:05:46,190 --> 00:05:49,230 the same song, I mean that's pretty much equal, right? 119 00:05:49,230 --> 00:05:51,310 It's sameish. 120 00:05:51,310 --> 00:05:55,740 So things like hash maps and hash sets use a thing called the hash code. 121 00:05:55,740 --> 00:05:57,510 We talked about this a little bit. 122 00:05:57,510 --> 00:06:00,340 And under the covers, it stores things sensibly. 123 00:06:00,340 --> 00:06:04,560 So it returns a uniquieish integer, that follows the same logic that equals. 124 00:06:04,560 --> 00:06:08,440 So therefore, if you overwrite equals or hash code, you should override them both. 125 00:06:08,440 --> 00:06:10,300 So lets both of those on the page. 126 00:06:10,300 --> 00:06:12,890 So basically, this generated code just checks to see 127 00:06:12,890 --> 00:06:14,650 if things are equal in memory. 128 00:06:14,650 --> 00:06:18,270 Right, if they are the exact same object, than of course they're equal. 129 00:06:18,270 --> 00:06:22,870 And then otherwise it just goes ahead and does the casting that we had done before, 130 00:06:22,870 --> 00:06:24,230 and then it upcalls equals. 131 00:06:24,230 --> 00:06:24,990 And oh, look here. 132 00:06:24,990 --> 00:06:28,150 It's upcalling equals on song. 133 00:06:28,150 --> 00:06:30,670 So, you know what we should probably do, 134 00:06:30,670 --> 00:06:33,670 we should probably go write the equals for song. 135 00:06:33,670 --> 00:06:34,910 So let's go over there to song. 136 00:06:34,910 --> 00:06:36,950 So I wanna show you another option. 137 00:06:36,950 --> 00:06:39,850 So we could do Cmd+O. 138 00:06:39,850 --> 00:06:43,100 And this is basically doing searchable, but this is only for classes. 139 00:06:43,100 --> 00:06:46,100 So if you have a whole bunch of classes, this is kind of handy. 140 00:06:46,100 --> 00:06:48,030 Cmd+O is right in your module as well. 141 00:06:49,740 --> 00:06:53,190 So in the song, let's just go ahead and let's generate equals and hashCode. 142 00:06:53,190 --> 00:06:56,730 And we'll do the same thing, intelliJ default. 143 00:06:56,730 --> 00:06:58,320 All those fields are valid right? 144 00:06:58,320 --> 00:06:59,000 It's gonna be equal. 145 00:07:00,520 --> 00:07:05,390 Yeah, all of those are true in the hash code and anybody could create one of these 146 00:07:05,390 --> 00:07:08,770 songs and it's possible that they could put a null value in there, right? 147 00:07:08,770 --> 00:07:13,310 The absence of that, so I am going to go ahead and leave those as null. 148 00:07:15,480 --> 00:07:18,850 Okay, so it's generated those for us as well. 149 00:07:18,850 --> 00:07:21,870 So the hashCode always looks a little bit strange. 150 00:07:21,870 --> 00:07:23,490 One thing that might look a little bit weird to you and 151 00:07:23,490 --> 00:07:26,700 is a little bit new is this thing called a ternary operator. 152 00:07:26,700 --> 00:07:32,409 So the way that this works is if this is true then this happens. 153 00:07:33,840 --> 00:07:36,480 It returns whatever the result is, if this is true. 154 00:07:36,480 --> 00:07:39,560 Otherwise, it returns whatever this is. 155 00:07:39,560 --> 00:07:41,140 Okay, so, pretty cute, right? 156 00:07:41,140 --> 00:07:43,790 I mean, sometimes it's the most clear way to express things, 157 00:07:43,790 --> 00:07:46,490 but often, it proves less readable. 158 00:07:46,490 --> 00:07:49,660 You're bound to run into them, so I'm glad that you got to see them, but 159 00:07:49,660 --> 00:07:53,230 I just wanted to let you know about ternary operators. 160 00:07:53,230 --> 00:07:55,170 I'll put a link in the teacher's notes. 161 00:07:55,170 --> 00:07:55,730 Cool. 162 00:07:55,730 --> 00:07:58,180 So now we have our generated code over here. 163 00:07:59,290 --> 00:08:00,120 I think we're ready to use it. 164 00:08:01,160 --> 00:08:03,990 >> I know, I know I owe you like five more dunk tank balls for 165 00:08:03,990 --> 00:08:06,080 making you type all that stuff before. 166 00:08:06,080 --> 00:08:07,540 But how much do you appreciate it now? 167 00:08:07,540 --> 00:08:08,990 Right? 168 00:08:08,990 --> 00:08:11,880 There's a couple more code generation tricks that we'll encounter in the next 169 00:08:11,880 --> 00:08:13,080 couple of videos. 170 00:08:13,080 --> 00:08:17,570 Remember, everything is customizable, so you can make generated code adhere to 171 00:08:17,570 --> 00:08:19,810 whatever your team determines to be the best practice. 172 00:08:20,900 --> 00:08:22,540 So, let's get this feature implemented. 173 00:08:22,540 --> 00:08:24,900 We're gonna need to do a little refactoring to get there, but 174 00:08:24,900 --> 00:08:26,180 the ID is gonna help us.