1 00:00:04,679 --> 00:00:11,679 2 00:00:12,808 --> 00:00:16,079 This presentation is delivered by the Stanford Center for Professional 3 00:00:16,079 --> 00:00:23,079 Development. 4 00:00:24,318 --> 00:00:27,568 So I want to spend a little bit more time talking about today, well, 5 00:00:27,568 --> 00:00:30,980 actually a lot of time talking about today, is thinking about data structures, 6 00:00:30,980 --> 00:00:33,929 building large-scale data structures. And we begin to talk about it just a little bit 7 00:00:33,929 --> 00:00:35,649 before the break and it's been a while 8 00:00:35,649 --> 00:00:38,660 so we're going to review it a little bit and kind of build up even more. 9 00:00:38,659 --> 00:00:42,669 But one of the things we talked about, in the past, right, what a lot of our computers do is they 10 00:00:42,670 --> 00:00:46,219 manage data. They manage lots of data. And in fact, I would venture to guess 11 00:00:46,219 --> 00:00:50,329 that there's a whole bunch of applications out there that manage a whole bunch of data 12 00:00:50,329 --> 00:00:51,609 about you, 13 00:00:51,609 --> 00:00:54,839 but you may not have thought about all the data they actually manage. So 14 00:00:54,840 --> 00:00:59,550 some of the things that actually come up, for example, online stores, right. Anyone 15 00:00:59,549 --> 00:01:02,979 actually bought anything online, just wondering. 16 00:01:02,979 --> 00:01:06,049 Yeah. There's a huge amount of data's that involved with that. Not only the 17 00:01:06,049 --> 00:01:08,230 particular transactions you make when you buy something, 18 00:01:08,230 --> 00:01:10,710 but keeping track of accurate transactions, 19 00:01:10,709 --> 00:01:14,029 figuring out things like people who buy product X also, tend to by 20 00:01:14,030 --> 00:01:17,039 produce Y. All of that is data management. And what makes those 21 00:01:17,039 --> 00:01:18,210 companies successful 22 00:01:18,209 --> 00:01:21,799 is they just do a very good job of managing their data. Okay. 23 00:01:21,799 --> 00:01:27,450 There are other things like, I'm almost frightened to ask, but social networks, 24 00:01:27,450 --> 00:01:28,010 like, 25 00:01:28,010 --> 00:01:33,390 Facebook, or MySpace, or ORCHID, or Friendster, or Linkdin, 26 00:01:33,390 --> 00:01:35,618 or you could just keep going on. 27 00:01:35,618 --> 00:01:38,049 Anyone on a social network? 28 00:01:38,049 --> 00:01:41,310 Just wondering. Yeah. That's good because your next assignment is going to be to 29 00:01:41,310 --> 00:01:43,890 implement one so you can see what it's actually like. But that will be coming in a couple of 30 00:01:43,890 --> 00:01:45,290 days. 31 00:01:45,290 --> 00:01:49,150 And they're not that hard, really. But what it is is a data management problem. Right? And it 32 00:01:49,150 --> 00:01:50,609 keeps track of things like 33 00:01:50,609 --> 00:01:54,390 who you are and information in your profile in the social network, and who your friends are, 34 00:01:54,390 --> 00:01:54,820 and 35 00:01:54,819 --> 00:01:56,129 all that happy news. 36 00:01:56,129 --> 00:01:59,689 Or you know, even things like a friend web search, 37 00:01:59,689 --> 00:02:02,909 right? There's a huge amount of data you need to be able to keep track of to be able to web 38 00:02:02,909 --> 00:02:03,840 search, right? So 39 00:02:03,840 --> 00:02:05,189 all these things 40 00:02:05,189 --> 00:02:09,379 are all about managing data well and so part of this class, right, is you've got a 41 00:02:09,379 --> 00:02:13,019 whole bunch experience in terms of building up code, and different kinds of 42 00:02:13,020 --> 00:02:14,620 classes, and 43 00:02:14,620 --> 00:02:17,659 doing nice things with user interfaces, and the whole deal. 44 00:02:17,659 --> 00:02:20,250 And one of the things that we need to spend a little bit more time on is talking 45 00:02:20,250 --> 00:02:22,629 about how do you manage lots of data 46 00:02:22,629 --> 00:02:24,938 and then do something interesting with that. Okay. 47 00:02:24,938 --> 00:02:27,968 So here's some principles to think about, if we think about good software 48 00:02:27,968 --> 00:02:33,649 engineering, some of the principles of thinking about data kind of in the large. Okay. 49 00:02:33,649 --> 00:02:36,558 When you think about keeping track of lots of data, one of the things you 50 00:02:36,558 --> 00:02:37,699 want to think about is 51 00:02:37,699 --> 00:02:41,049 the information you want to keep track of, what are the nouns you want to keep 52 00:02:41,050 --> 00:02:42,550 track. And you're like, I don't want 53 00:02:42,550 --> 00:02:43,760 any nouns, what do you mean by the nouns? 54 00:02:43,759 --> 00:02:46,478 Let's say I was writing an application that was an online store, 55 00:02:46,479 --> 00:02:49,079 to keep track of, oh, let's say, music. 56 00:02:49,079 --> 00:02:52,419 And so one of the things I would want to think about is, where are the nouns that are associated with 57 00:02:52,419 --> 00:02:53,759 music? 58 00:02:53,758 --> 00:02:55,049 You're like, okay, 59 00:02:55,050 --> 00:02:57,439 now you're really getting weird. No, it's pretty simple. Things like a song, 60 00:02:57,439 --> 00:02:58,710 right, is a noun, 61 00:02:58,710 --> 00:03:01,659 that's associated with music, or an album, or an artist, 62 00:03:01,658 --> 00:03:04,438 right. And so what you want to think about is the things that are the nouns 63 00:03:04,438 --> 00:03:06,578 in the domain that you're dealing with 64 00:03:06,579 --> 00:03:10,248 oftentimes end up translating into what your classes are. 65 00:03:10,248 --> 00:03:13,209 So you may end up having a class that keeps track of information about a 66 00:03:13,209 --> 00:03:16,090 particular song or class that keeps track of information about a particular 67 00:03:16,090 --> 00:03:17,989 album. So 68 00:03:17,989 --> 00:03:21,158 the good linguists out there tell me we not only nouns but we 69 00:03:21,158 --> 00:03:22,468 also have verbs, 70 00:03:22,468 --> 00:03:25,998 not unless you happen to be talking to my son, who seems to only have nouns, but 71 00:03:25,998 --> 00:03:27,549 that's a different story. 72 00:03:27,549 --> 00:03:29,629 And he loves jarens by the way. 73 00:03:29,628 --> 00:03:31,468 But like, why are you telling me this? 74 00:03:31,468 --> 00:03:32,620 Just cause it's fun, 75 00:03:32,620 --> 00:03:35,808 because I just spent a whole week dealing with it. 76 00:03:35,808 --> 00:03:39,099 In terms of verbs, these are oftentimes the methods that 77 00:03:39,099 --> 00:03:42,560 are associated with your classes, right. So when you want to do something, some 78 00:03:42,560 --> 00:03:46,759 noun takes some action, which is a verb, which is some class, has some method that 79 00:03:46,759 --> 00:03:48,000 operates on that class. 80 00:03:48,000 --> 00:03:51,210 So at an abstract level that's what you want to think about in terms of high-level 81 00:03:51,210 --> 00:03:52,479 principles of design. 82 00:03:52,479 --> 00:03:55,279 Now, there are some other sort of more concrete things that you might want to think 83 00:03:55,278 --> 00:03:56,209 about, 84 00:03:56,209 --> 00:03:57,658 things that have to do with 85 00:03:57,658 --> 00:04:01,179 what are the characteristics of the data you actually want to store 86 00:04:01,179 --> 00:04:05,229 so one thing that comes up, oftentimes, is thinking of the notion of having a 87 00:04:05,229 --> 00:04:06,879 unique 88 00:04:06,878 --> 00:04:09,038 identifier, 89 00:04:09,038 --> 00:04:14,198 identifier. What do I mean by unique identifier? All of you have unique identifiers, 90 00:04:14,199 --> 00:04:17,098 whether or not you like it or not, as a result of being at Stanford. 91 00:04:17,098 --> 00:04:19,519 Your Stanford University I.D. number 92 00:04:19,519 --> 00:04:22,620 is a unique identifier for you at Stanford. 93 00:04:22,620 --> 00:04:24,929 Every student has an I.D. number. 94 00:04:24,928 --> 00:04:29,508 Okay. So it identifies you and it's unique. No two students share the same I.D. number. 95 00:04:29,509 --> 00:04:33,430 So you get issued this number when you show up here and you have it for life. 96 00:04:33,430 --> 00:04:37,250 When you leave it's still with you. I know, I left, I came back, I have the same 97 00:04:37,250 --> 00:04:38,309 student I.D. number. 98 00:04:38,309 --> 00:04:41,150 It just exists and this uniquely identifies you. 99 00:04:41,149 --> 00:04:44,409 And in different cases you might want to think about what are unique identifiers. 100 00:04:44,410 --> 00:04:47,270 Right. So in some cases, for example, if you had a social network 101 00:04:47,269 --> 00:04:50,758 you might consider the names of people and not the social network to be 102 00:04:50,759 --> 00:04:53,420 identifiers, or say the names of their profiles, for example. 103 00:04:53,420 --> 00:04:56,400 In other cases, you might have something different. If you're managing a store 104 00:04:56,399 --> 00:04:59,888 you might have some I.D. number for books, an ISBN number, or if 105 00:04:59,889 --> 00:05:03,228 you're keeping track of music, you might say that the combination of the songs' 106 00:05:03,228 --> 00:05:06,719 name and the band that plays it is a unique identifier for that song. In 107 00:05:06,720 --> 00:05:09,759 some cases the unique identifier can be a combination of things. But if 108 00:05:09,759 --> 00:05:12,580 you think about your data having a unique identifier 109 00:05:12,579 --> 00:05:16,189 that also gives you some insights about what kind of data structures you might 110 00:05:16,189 --> 00:05:20,168 want to use to keep track of certain things. On other unique identifier that some 111 00:05:20,168 --> 00:05:25,909 of you've already grapple with is saying Name Surfer. Right? If you think about the 112 00:05:25,910 --> 00:05:31,099 data in Name Surfer what's the unique identifier there? Name. Name, 113 00:05:31,098 --> 00:05:33,680 right? Name is a unique identifier and for every name 114 00:05:33,680 --> 00:05:37,180 you have some list of values associated with it, which was the rank of that name 115 00:05:37,180 --> 00:05:41,189 over the last century in terms of how popular it was for names. But 116 00:05:41,189 --> 00:05:44,949 every name, well, I shouldn't say every name has some value associated with it, but every 117 00:05:44,949 --> 00:05:46,550 unique identifier in the system 118 00:05:46,550 --> 00:05:50,000 has some value associated with it and only one set of values. 119 00:05:50,000 --> 00:05:50,959 And so 120 00:05:50,959 --> 00:05:53,888 the important thing to keep track of there is when you actually are doing 121 00:05:53,889 --> 00:05:57,900 your Name Surfer assignment to fact of this thing is a unique identifier 122 00:05:57,899 --> 00:06:00,478 can potentially help you keep track of the data that you're using. And we'll sort of 123 00:06:00,478 --> 00:06:03,769 go into that as we go along in the class. Okay? 124 00:06:03,769 --> 00:06:07,749 So some other principles we can kind of think about in terms of designing data 125 00:06:07,749 --> 00:06:10,430 structure, in terms of actually doing the design, 126 00:06:10,430 --> 00:06:12,959 there's some questions you want to ask yourself. And the questions you want to 127 00:06:12,959 --> 00:06:18,709 ask yourself is, are you keeping track of some collection of objects? Right? So 128 00:06:18,709 --> 00:06:22,758 there comes some collection of objects through data that you want to have. 129 00:06:22,759 --> 00:06:26,800 And if you have a collection of objects, say in an online music store, you might have a 130 00:06:26,800 --> 00:06:29,250 collection of songs that you want to keep track of, 131 00:06:29,250 --> 00:06:32,779 this word should be a tip off too, that perhaps there's an interesting 132 00:06:32,779 --> 00:06:36,549 collection that exist in Java that would be a way of keeping track of that 133 00:06:36,550 --> 00:06:39,348 information. It may not be in Java if you're programming in some other language. 134 00:06:39,348 --> 00:06:42,089 But the fact that Java has something called a collection and the reason why 135 00:06:42,089 --> 00:06:43,829 they gave the name of collections 136 00:06:43,829 --> 00:06:46,428 to a certain group of stuff is because they're used to keep track of a 137 00:06:46,428 --> 00:06:49,708 collection of objects. And the question that you ask yourself then is what 138 00:06:49,709 --> 00:06:51,990 collections do you actually want to use? Okay. 139 00:06:51,990 --> 00:06:56,329 So with that said, what we can do is spend a moment, and it will be a brief moment, 140 00:06:56,329 --> 00:06:58,189 revisiting the collection hierarchy. 141 00:06:58,189 --> 00:07:01,120 Right? You've seen this picture before but I'm just showing it to you again, because the 142 00:07:01,120 --> 00:07:04,509 last time you saw it was like two weeks ago, which is a lifetime in a quarter. Right? I 143 00:07:04,509 --> 00:07:07,400 think it too, it's about a fifth of the quarter. You're like, oh, what was I doing two weeks ago? Was 144 00:07:07,399 --> 00:07:08,418 the break 145 00:07:08,418 --> 00:07:11,769 out, was I learning Printland? No, no it wasn't that long ago. But 146 00:07:11,769 --> 00:07:14,370 what you were learning about, a little bit, was collections. 147 00:07:14,370 --> 00:07:18,278 And o there are some collections, for example, like an ArrayList that 148 00:07:18,278 --> 00:07:21,838 going all the way up the chain of the hierarchy is itself a collection. Or 149 00:07:21,838 --> 00:07:24,370 there are other things, for example, like a HashMap. 150 00:07:24,370 --> 00:07:27,430 And a HashMap, if you said hey, I have some HashMap, 151 00:07:27,430 --> 00:07:31,870 the set of keys in that HashMap ends up actually being a set, 152 00:07:31,870 --> 00:07:34,259 which happens to be a collection. Okay. 153 00:07:34,259 --> 00:07:36,889 And so what you want to think about, do I have different things that I can keep track 154 00:07:36,889 --> 00:07:39,949 of? Like an ArrayList is one way to keep track of things. A HashMap may be 155 00:07:39,949 --> 00:07:43,270 another way of keeping track of things. When is the appropriate 156 00:07:43,269 --> 00:07:45,639 time to use one thing versus another? 157 00:07:45,639 --> 00:07:49,348 And so when you want to think about the appropriate times of one versus the other, 158 00:07:49,348 --> 00:07:50,490 you want to think about 159 00:07:50,490 --> 00:07:53,620 what are the methods that a collection provides to you. And it turns out all 160 00:07:53,620 --> 00:07:54,538 collections 161 00:07:54,538 --> 00:07:57,909 that implement the collection interface, like the ArrayList or the key set of the 162 00:07:57,910 --> 00:07:58,899 HashMap, 163 00:07:58,899 --> 00:08:00,569 have all of these properties. 164 00:08:00,569 --> 00:08:03,019 And some of you have seen them before, but just to review. 165 00:08:03,019 --> 00:08:06,809 You can add a value, right? So this is a parameterized values type. Like you can have an 166 00:08:06,809 --> 00:08:10,588 ArrayList of strings and you can add some value to it, and it adds it to the collection, 167 00:08:10,588 --> 00:08:13,428 and little did you know, or maybe you did know, but 168 00:08:13,428 --> 00:08:16,438 at the time we didn't' really care about it, was it returned a bullion. Most of 169 00:08:16,439 --> 00:08:18,830 the times we just returned the bullion, we didn't care about it. 170 00:08:18,829 --> 00:08:21,680 But actually returned true the collection changed. 171 00:08:21,680 --> 00:08:24,910 So in an ArrayList, it always returned true because when you were adding a value, 172 00:08:24,910 --> 00:08:27,800 it didn't care about duplicates, it would always just add them to the 173 00:08:27,800 --> 00:08:29,278 end and always return true. 174 00:08:29,278 --> 00:08:32,399 Some collections, like sets, actually don't allow you to have duplicates. So if 175 00:08:32,399 --> 00:08:36,408 you try to add something to a set that already has the value you're trying to add, 176 00:08:36,408 --> 00:08:40,389 it will not change the set and return false, because it says, hey, I already have that 177 00:08:40,389 --> 00:08:42,480 value and nothing changed. A 178 00:08:42,480 --> 00:08:45,409 couple other things that you should know about, most of these you've seen. 179 00:08:45,409 --> 00:08:45,938 Remove, 180 00:08:45,938 --> 00:08:49,748 removes the first instance of an element as it appears and returns true if 181 00:08:49,749 --> 00:08:53,389 a match is found or returns false if it didn't find anything to actually remove. 182 00:08:53,389 --> 00:08:56,960 And clear, basically, just sort of nukes the whole collection. It just says get 183 00:08:56,960 --> 00:08:59,269 rid of everything in the collection. I'm done with that 184 00:08:59,269 --> 00:09:02,090 and the collection is dead. Actually, the collection is not dead to you, it still 185 00:09:02,090 --> 00:09:04,530 exists, it's just an empty shell of what it was before. 186 00:09:04,529 --> 00:09:07,139 There are violins playing in the background. 187 00:09:07,139 --> 00:09:10,330 And then size, you can get the size of the collection. You've seen this, you've probably used 188 00:09:10,330 --> 00:09:14,139 a lot of these before in your programs. Contains, that's an important one, right? 189 00:09:14,139 --> 00:09:17,149 You actually want to see if a collection contains some particular 190 00:09:17,149 --> 00:09:18,539 value, 191 00:09:18,539 --> 00:09:21,529 if a collection is empty. And here's one that's sort of interesting that we 192 00:09:21,529 --> 00:09:24,909 talked about a little bit but we didn't actually talk about the fact that 193 00:09:24,909 --> 00:09:28,669 a collection or all collections can give you one these. All collections can 194 00:09:28,669 --> 00:09:30,019 give you an iterator. 195 00:09:30,019 --> 00:09:34,319 So we talked about, for example, having an iterator over the key set of HashMap. 196 00:09:34,320 --> 00:09:37,100 That's one thing we did before. We said we had some HashMap 197 00:09:37,100 --> 00:09:40,778 that lets a map from strings from some other strings. And we want, say, 198 00:09:40,778 --> 00:09:43,200 hey what I want to see is get a set of all the keys 199 00:09:43,200 --> 00:09:45,660 and I want to iterate all over those keys. 200 00:09:45,659 --> 00:09:48,439 That's great! You can do that and that's perfectly fine. 201 00:09:48,440 --> 00:09:52,600 When we used ArrayList we always had like a four loop and said, oh, from zero 202 00:09:52,600 --> 00:09:56,239 up to the size of the ArrayList do something. But if we actually wanted to, 203 00:09:56,239 --> 00:09:59,209 we could have an iterator over the ArrayList and then this would give us the 204 00:09:59,208 --> 00:10:01,149 elements of that ArrayList one at a time. 205 00:10:01,149 --> 00:10:04,490 So because an ArrayList is a collection it can also give us an iterator. And that's just 206 00:10:04,490 --> 00:10:06,330 something to keep in mind is that 207 00:10:06,330 --> 00:10:09,990 there's common patterns that get used in programming. One of the common patterns that 208 00:10:09,990 --> 00:10:14,320 get used is known as an iteration pattern, which again, is an iterator over some 209 00:10:14,320 --> 00:10:14,700 collection 210 00:10:14,700 --> 00:10:18,080 and you just go through and do something like printout the values for every 211 00:10:18,080 --> 00:10:21,090 element of that collection. And if you want to write it in the most general 212 00:10:21,090 --> 00:10:24,889 case, you don't care if that collection happens to be the key set of the HashMap, 213 00:10:24,889 --> 00:10:26,129 or an ArrayList, or whatever, 214 00:10:26,129 --> 00:10:29,439 you just say, hey, you're a collection, give me an iterator and I can go through all 215 00:10:29,440 --> 00:10:32,660 your elements one at a time and, for example, print them out. Okay. 216 00:10:32,659 --> 00:10:36,299 So there's just simple pattern's that we get into. Now, 217 00:10:36,299 --> 00:10:39,699 you're like, okay, Marilyn, that's fine, you told me some design principles over 218 00:10:39,700 --> 00:10:42,050 here, you told me about some collections over here. 219 00:10:42,049 --> 00:10:45,159 Show me something concrete, like put it all together. So let's actually put it all 220 00:10:45,159 --> 00:10:46,620 together. Okay. 221 00:10:46,620 --> 00:10:49,110 And we'll view a little example, 222 00:10:49,110 --> 00:10:52,560 which 223 00:10:52,559 --> 00:10:53,419 224 00:10:53,419 --> 00:10:55,278 is going to an online music store. 225 00:10:55,278 --> 00:10:58,600 And because many names for online music stores are already taken, our music store 226 00:10:58,600 --> 00:11:02,310 is going to be called Flytunes cause 227 00:11:02,309 --> 00:11:05,009 they're tunes that will fly. All right. 228 00:11:05,009 --> 00:11:07,689 Yeah, man, when you're like 229 00:11:07,690 --> 00:11:11,000 in your mid 30's you just can't be that cool. 230 00:11:11,000 --> 00:11:12,809 But trust me, it is. Okay? 231 00:11:12,809 --> 00:11:16,119 So we're going to make a little store that just keeps track of music and albums, 232 00:11:16,120 --> 00:11:20,509 and that music and actually lets us keep track of 233 00:11:20,509 --> 00:11:21,349 information and prices. 234 00:11:21,349 --> 00:11:24,158 And so what we want to think about is what are the things that we actually are going to do 235 00:11:24,158 --> 00:11:26,600 in that store, okay? So 236 00:11:26,600 --> 00:11:30,170 one of the nouns of that store is going to be a song, okay? So 237 00:11:30,169 --> 00:11:33,879 a song is some basic thing that we're going to sell. This is what we want 238 00:11:33,879 --> 00:11:36,350 to be able to do with the song. Now, you could say, 239 00:11:36,350 --> 00:11:39,129 well, what does that mean, do I have some method called sell? 240 00:11:39,129 --> 00:11:42,370 If we're doing inventory management we might not actually have a method 241 00:11:42,370 --> 00:11:44,078 called selling a song, 242 00:11:44,078 --> 00:11:48,309 but we might, for example, want to add for inventory to do things like 243 00:11:48,309 --> 00:11:49,489 add songs. 244 00:11:49,490 --> 00:11:51,240 And similarly, 245 00:11:51,240 --> 00:11:54,450 songs, oftentimes, are put together into albums. 246 00:11:54,450 --> 00:11:58,060 Okay, so we may also want to keep track of albums and do things like add albums 247 00:11:58,059 --> 00:11:59,119 to our inventory. 248 00:11:59,120 --> 00:12:02,690 Now, the interesting thing with an online music store that differentiates it from 249 00:12:02,690 --> 00:12:05,240 say a physical music store, is 250 00:12:05,240 --> 00:12:08,649 you can do interesting things, right? You can actually have songs that are not on 251 00:12:08,649 --> 00:12:09,919 any albums. 252 00:12:09,919 --> 00:12:12,740 And that works, right? It's kind of like thinking of a single, right. When you 253 00:12:12,740 --> 00:12:14,350 go and buy a single somewhere. 254 00:12:14,350 --> 00:12:17,409 In the days of yore, you could actually buy a little record single that had two sides 255 00:12:17,409 --> 00:12:20,778 on it so you got two songs, so it wasn't really a notion of a real single, single. I guess 256 00:12:20,778 --> 00:12:24,730 now, there are like CD singles. But who wants a CD single when it comes down to it? 257 00:12:24,730 --> 00:12:26,499 You can get songs that are on albums. 258 00:12:26,499 --> 00:12:30,499 At the same time, you can have the same song be on multiple albums, right? That 259 00:12:30,499 --> 00:12:33,178 always happens. There's a band, I won't mention their name, but I remember 260 00:12:33,178 --> 00:12:34,088 from the early e80s, 261 00:12:34,089 --> 00:12:37,829 they had two albums. They had their first album and they had their best of albums, 262 00:12:37,828 --> 00:12:40,929 which were half the songs from their first album. 263 00:12:40,929 --> 00:12:44,299 Just anything you can do to milk the consumer. 264 00:12:44,299 --> 00:12:47,289 But basically, what that meant was songs can show up on multiple albums. 265 00:12:47,289 --> 00:12:50,860 Okay, so we want to begin to think about how that might actually affect our 266 00:12:50,860 --> 00:12:51,860 design. 267 00:12:51,860 --> 00:12:55,570 Now, if we think about putting the information together, right, 268 00:12:55,570 --> 00:12:59,129 nouns become our classes. So if we're going to have song as a noun, we're 269 00:12:59,129 --> 00:13:02,470 probably going to have some class song that's going to keep track of all the 270 00:13:02,470 --> 00:13:04,509 information 271 00:13:04,509 --> 00:13:07,060 associated with a song. And so 272 00:13:07,059 --> 00:13:10,000 just for the sake of brevity, I'll tell you what information's going to be 273 00:13:10,000 --> 00:13:12,259 associated with songs that we care about in our store. 274 00:13:12,259 --> 00:13:16,739 There's a notion of the name of the song, the band or artists that perform that 275 00:13:16,739 --> 00:13:17,360 song, 276 00:13:17,360 --> 00:13:20,800 and then a price, because we're going to allow for songs to be sold individually, 277 00:13:20,799 --> 00:13:24,509 so individual songs, as opposed to whole albums, have prices. Okay. 278 00:13:24,509 --> 00:13:27,340 And you can think about these things and think about, oh, what data types do you 279 00:13:27,340 --> 00:13:31,620 want to have for them. Right, so what type data type makes sense for a name, for example, 280 00:13:31,620 --> 00:13:35,529 string type, or if you want to have a band name, this would probably be a 281 00:13:35,529 --> 00:13:39,309 string. Price is always an interesting one. You could sort of say, well, now, and there's 282 00:13:39,309 --> 00:13:42,279 multiple things I could have it be. I could have it be an [inaudible], for example, if I was going 283 00:13:42,279 --> 00:13:44,120 to have it be the number of cents. 284 00:13:44,120 --> 00:13:47,769 In the simplest case, I'm just going to have it be a double. Even 285 00:13:47,769 --> 00:13:50,539 though we know there's no fractional money unless you're a banker, in which case there 286 00:13:50,539 --> 00:13:53,139 is fractional money. But we won't talk about that right now. 287 00:13:53,139 --> 00:13:56,639 It was just like Superman III. Anyone see that movie? 288 00:13:56,639 --> 00:13:57,590 No. 289 00:13:57,590 --> 00:14:01,899 It's not worth watching, trust me. But fractional money does exist outside of 290 00:14:01,899 --> 00:14:04,129 movies, bad movies in Hollywood. 291 00:14:04,129 --> 00:14:07,340 So that's the information we want to keep track of for a song and then we 292 00:14:07,340 --> 00:14:10,550 want to think about what are some of the things that we want to be able to do 293 00:14:10,549 --> 00:14:12,889 in relations to those songs. 294 00:14:12,889 --> 00:14:16,500 The other thing we also want to think about is our friend the unique 295 00:14:16,500 --> 00:14:17,058 identifier. 296 00:14:17,058 --> 00:14:20,799 Is there some unique identifier for a song? 297 00:14:20,799 --> 00:14:23,939 And this is one of those things you really need to think about the 298 00:14:23,940 --> 00:14:26,370 application that you're using, what assumptions you can make. 299 00:14:26,370 --> 00:14:29,899 We might like to say that the name is a unique identifier for a song, but 300 00:14:29,899 --> 00:14:33,360 unfortunately, there are many songs that have the same name. Okay. 301 00:14:33,360 --> 00:14:37,050 But I would venture to guess that the combination of the name and the band 302 00:14:37,049 --> 00:14:39,719 would perhaps be a unique identifier for a song. 303 00:14:39,720 --> 00:14:43,060 The only thing is we don't have one string that we keep track of that keeps 304 00:14:43,059 --> 00:14:46,159 name and band in it. So that's another thing that we need to think about, and we'll 305 00:14:46,159 --> 00:14:47,750 get into code when we get into code, 306 00:14:47,750 --> 00:14:51,029 that we need to think about the design of that particular object. 307 00:14:51,029 --> 00:14:55,269 The other thing we need to think about is what changes in an object during its 308 00:14:55,269 --> 00:14:57,429 lifetime and what doesn't change. 309 00:14:57,429 --> 00:15:02,189 Like, so if I have a song its name and the band that made it for a particular song, 310 00:15:02,190 --> 00:15:05,730 like, some band can go uncover the song they learn, but that's a different song, 311 00:15:05,730 --> 00:15:08,840 the name and the band name don't change for a song. 312 00:15:08,840 --> 00:15:12,899 But hey, it can go on sale and you know, I can jack up the price at 313 00:15:12,899 --> 00:15:14,970 the holidays and all that kind of stuff. 314 00:15:14,970 --> 00:15:17,210 So the price is something that's malleable. 315 00:15:17,210 --> 00:15:20,100 So another thing you think about in terms of the principles of design is, 316 00:15:20,100 --> 00:15:22,950 of the data that I have associated with a particular object, 317 00:15:22,950 --> 00:15:26,870 what's going to remain static when that object's created and what's going to be 318 00:15:26,870 --> 00:15:27,960 potentially changed? 319 00:15:27,960 --> 00:15:30,610 And that's what gives you some insight about 320 00:15:30,610 --> 00:15:34,509 what's some of the data, for example, that you only get from an object, 321 00:15:34,509 --> 00:15:37,278 what's some data that you can potentially set in the object, 322 00:15:37,278 --> 00:15:40,938 and if you think about what potentially uniquely identifies that object, 323 00:15:40,938 --> 00:15:43,259 what data do you actually need at the time 324 00:15:43,259 --> 00:15:45,490 that you construct the object, 325 00:15:45,490 --> 00:15:48,899 right. To say this object is actually some particular unique thing that I care 326 00:15:48,899 --> 00:15:51,370 about. Okay. So 327 00:15:51,370 --> 00:15:54,149 let's turn that into a little bit of code just to make it a little more 328 00:15:54,149 --> 00:15:55,259 concrete. 329 00:15:55,259 --> 00:15:59,338 So we'll get rid of our friend, Power Point, and we fire up our friend, Blitz. 330 00:15:59,339 --> 00:16:02,040 Ah, and look, a song, how convenient. 331 00:16:02,039 --> 00:16:05,500 So here's the information to keep track of a song. It's just a class called 332 00:16:05,500 --> 00:16:06,120 song. 333 00:16:06,120 --> 00:16:10,120 And what we want to do is keep track of song, the song's name, the band name, and the 334 00:16:10,120 --> 00:16:10,840 price. 335 00:16:10,840 --> 00:16:14,080 So when we create the song, one of the things we might do is say, hey, give me all that 336 00:16:14,080 --> 00:16:16,920 information to start with. Because if you're going to put some song in your store and 337 00:16:16,919 --> 00:16:17,860 you're going to sell it, 338 00:16:17,860 --> 00:16:19,318 it better have some 339 00:16:19,318 --> 00:16:22,509 song name and band name that I can use to refer to it by, because that's going to be its 340 00:16:22,509 --> 00:16:23,600 unique identifier 341 00:16:23,600 --> 00:16:25,360 and give me some initial 342 00:16:25,360 --> 00:16:26,339 starting price. 343 00:16:26,339 --> 00:16:29,430 Now, we might necessarily not require an initial starting price, because it's 344 00:16:29,429 --> 00:16:32,409 something that's going to change during the duration of the program, and isn't 345 00:16:32,409 --> 00:16:34,959 in support of our unique identifier. But 346 00:16:34,960 --> 00:16:35,950 in this case, 347 00:16:35,950 --> 00:16:38,230 we're just going to ask for an initial price. 348 00:16:38,230 --> 00:16:41,750 The thing we do care about, in terms of the malleability of what's actually in 349 00:16:41,750 --> 00:16:43,038 this data structure, 350 00:16:43,038 --> 00:16:47,239 is thinking about song name, band name, and price. So song name, we only have a 351 00:16:47,239 --> 00:16:51,038 getter for there. There's no setter. Once the object's created, you can't change the band 352 00:16:51,038 --> 00:16:52,519 name for that song. You can't say, 353 00:16:52,519 --> 00:16:54,310 oh yeah, you know, that was 354 00:16:54,309 --> 00:16:57,179 "In Your Eyes," by Peter Gabriel and now it's going to be like, 355 00:16:57,179 --> 00:16:59,019 "In Your Eyes," by Kanye West. 356 00:16:59,019 --> 00:17:02,289 Like, that's a different song. And I don't know if that's happened. It's probably not 357 00:17:02,289 --> 00:17:04,578 a good idea. 358 00:17:04,578 --> 00:17:08,919 But the song remains the same, if you're a Led Zeppelin fan, right? And the 359 00:17:08,920 --> 00:17:12,120 band name, actually, the band name is also going to remain the same for that 360 00:17:12,119 --> 00:17:13,168 particular song. 361 00:17:13,169 --> 00:17:15,740 But the price has both a getter and a setter. 362 00:17:15,740 --> 00:17:18,370 Right? Because it's something that's malleable. After we create that song, yeah, we 363 00:17:18,369 --> 00:17:20,438 might change its price. And because we know that 364 00:17:20,439 --> 00:17:23,740 we provide both of those things in the definition of the class. 365 00:17:23,740 --> 00:17:26,299 Now, as we talked about, in days of yore, 366 00:17:26,299 --> 00:17:30,079 whenever you create any class it should also have a method called Two String. And Two 367 00:17:30,079 --> 00:17:32,869 String just returns a string representation of the data in that class. 368 00:17:32,869 --> 00:17:34,719 So this just prints out 369 00:17:34,720 --> 00:17:37,720 inside double quotes, which is why we have this backslash, quote, that's a single 370 00:17:37,720 --> 00:17:38,809 double quote character, 371 00:17:38,809 --> 00:17:42,950 the title of the song in double quotes by the band name, and then it says cost, 372 00:17:42,950 --> 00:17:46,039 and it has the price associater with the cost. So it just returns a string to 373 00:17:46,039 --> 00:17:47,899 baseline caps lets the data. 374 00:17:47,900 --> 00:17:48,500 And here's 375 00:17:48,500 --> 00:17:51,769 the private instance variables of that particular class. Right. There's a 376 00:17:51,769 --> 00:17:55,298 title, a band, and a price for the title of the song, the band that made the song, and 377 00:17:55,298 --> 00:17:56,349 the price of the song. 378 00:17:56,349 --> 00:17:58,339 And that's all the information that's in there. 379 00:17:58,339 --> 00:18:01,699 But it captures and encapsulates the notion of having a song 380 00:18:01,700 --> 00:18:05,110 and what parts of the song are static or can't change, and what parts of the song 381 00:18:05,109 --> 00:18:07,509 are mutable or can change. Okay. 382 00:18:07,509 --> 00:18:10,539 So besides songs, we also have this thing called albums. Any question about the 383 00:18:10,539 --> 00:18:11,519 song portion? 384 00:18:11,519 --> 00:18:14,879 If you're sort of feeling good with song, nod your head. 385 00:18:14,880 --> 00:18:18,540 All right, good times. If you're not feeling good song, shake your head. 386 00:18:18,539 --> 00:18:19,659 If you're awake 387 00:18:19,660 --> 00:18:21,120 nod 388 00:18:21,119 --> 00:18:23,789 your head. There's a few that's not nodding, but that's okay. 389 00:18:23,789 --> 00:18:24,879 That's cool, too. 390 00:18:24,880 --> 00:18:27,880 So let's do the class for an album. 391 00:18:27,880 --> 00:18:31,260 So the class for an album 392 00:18:31,259 --> 00:18:34,140 is another thing we care about. And albums become a little more interesting 393 00:18:34,140 --> 00:18:37,520 because an album not only has a name, right, so this is going to be a 394 00:18:37,519 --> 00:18:39,569 name, and yeah, the name will probably be some string. 395 00:18:39,569 --> 00:18:43,619 And there's also a band, potentially, that produces the album. 396 00:18:43,619 --> 00:18:47,629 Now, the interesting thing is the band - you might say, but Marilyn, isn't that redundant? 397 00:18:47,630 --> 00:18:50,730 Like, don't I have some album and it's going to have a bunch of songs on it, and 398 00:18:50,730 --> 00:18:53,450 so I already have names for the 399 00:18:53,450 --> 00:18:55,620 band for those songs? So why do I need the 400 00:18:55,619 --> 00:18:57,789 name of the band for the album? 401 00:18:57,789 --> 00:18:58,960 Anyone know? Want to 402 00:18:58,960 --> 00:19:02,558 venture a guess? 403 00:19:02,558 --> 00:19:06,410 Anyone have an album that's like this, 404 00:19:06,410 --> 00:19:07,890 e80s 405 00:19:07,890 --> 00:19:09,820 compilation is the critical word? Right. 406 00:19:09,819 --> 00:19:11,500 You can have an album 407 00:19:11,500 --> 00:19:15,160 that's band isn't actually a real band name. Its band name could just be something 408 00:19:15,160 --> 00:19:16,548 like compilation. 409 00:19:16,548 --> 00:19:20,308 And it's going to have a bunch of songs on it, each of one which has a distinct 410 00:19:20,308 --> 00:19:21,289 band. Okay. 411 00:19:21,289 --> 00:19:24,769 So that's perfectly fine. There's no reason why an album, especially in the 412 00:19:24,769 --> 00:19:27,389 online world when you can sort of create mixes all the time, 413 00:19:27,390 --> 00:19:29,620 needs to have a 414 00:19:29,619 --> 00:19:31,128 single band. And so 415 00:19:31,128 --> 00:19:34,189 there wouldn't be a need for having bands associated with songs. We still need 416 00:19:34,190 --> 00:19:36,120 to have bands associated with the songs. 417 00:19:36,119 --> 00:19:39,019 And potentially, at a higher level, we might want to be able to say, is this 418 00:19:39,019 --> 00:19:43,450 whole album by one band, or one artist, or is it actually a compilation. Okay? 419 00:19:43,450 --> 00:19:45,140 Now, the interesting part though, 420 00:19:45,140 --> 00:19:49,980 is that an album not only has a band and name, but it has a list of songs. 421 00:19:49,980 --> 00:19:50,960 422 00:19:50,960 --> 00:19:54,630 So how might we keep track of that list of songs? What would be a reasonable data 423 00:19:54,630 --> 00:19:57,070 structure we could use? [Inaudible]. 424 00:19:57,069 --> 00:20:00,559 An Array, our friend an Array. Well, the only problem with an Array is, right, it needs to 425 00:20:00,559 --> 00:20:02,250 have some fixed size. 426 00:20:02,250 --> 00:20:06,859 There's some albums out there that are very short, like "In A Gadda Da Vida," 427 00:20:06,859 --> 00:20:10,619 Iron Butterfly, there's one song that's one side of the album, if you 428 00:20:10,619 --> 00:20:14,389 were back on the LP days, and what a fine album it is. 429 00:20:14,390 --> 00:20:17,960 And there's other albums that are just like, oh, look there's like 300 430 00:20:17,960 --> 00:20:19,490 songs on here. Okay. 431 00:20:19,490 --> 00:20:23,390 So an Array with just a fixed size might potentially waste a lot of space. What's 432 00:20:23,390 --> 00:20:25,990 the more malleable version we could use? [Inaudible]. 433 00:20:25,990 --> 00:20:31,880 Oh, yeah. I love it when it's just all around. All 434 00:20:31,880 --> 00:20:34,110 right. 435 00:20:34,109 --> 00:20:35,599 [Inaudible]. 436 00:20:35,599 --> 00:20:38,500 [Inaudible] one, I think. Like 437 00:20:38,500 --> 00:20:41,369 that 438 00:20:41,369 --> 00:20:47,289 post Thanksgiving. It's like the tryptophan, still like working its way. 439 00:20:47,289 --> 00:20:54,289 Yeah. You know. - albums, to begin 440 00:21:00,349 --> 00:21:05,959 with. How do we actually add some list of songs on it. We need to have a 441 00:21:05,960 --> 00:21:08,250 way to be able to add 442 00:21:08,250 --> 00:21:09,169 songs to this album, 443 00:21:09,169 --> 00:21:12,320 and once we actually add songs to a list of songs on the album, 444 00:21:12,319 --> 00:21:16,339 we need to have some way of being able to list the [inaudible], or perhaps, iterating 445 00:21:16,339 --> 00:21:20,240 over them. The only thing with an ArrayList is 446 00:21:20,240 --> 00:21:24,819 enter implements collection interface so that it actually provides you enter it. Okay. 447 00:21:24,819 --> 00:21:27,829 So let's look at the code for that, just real quickly and then things will become 448 00:21:27,829 --> 00:21:30,089 more interesting, afterwards. Okay. 449 00:21:30,089 --> 00:21:31,099 So here's 450 00:21:31,099 --> 00:21:34,299 an album. Inside an album we have an album name and a band, those are the things that are going 451 00:21:34,299 --> 00:21:37,839 to start off by constructing an album. So we say here's the initial album name 452 00:21:37,839 --> 00:21:39,339 and band, and what I want to do 453 00:21:39,339 --> 00:21:41,899 is build up the contents of that album. 454 00:21:41,900 --> 00:21:45,830 So it lets you get the album name and get the band name but you can't set them. Those 455 00:21:45,829 --> 00:21:47,629 things are fixed. Okay. 456 00:21:47,630 --> 00:21:50,260 The other thing that I'm actually going to assume here, which is something I didn't 457 00:21:50,259 --> 00:21:51,549 assume for songs, 458 00:21:51,549 --> 00:21:56,019 is that the name of the album is a unique identifier for the album. 459 00:21:56,019 --> 00:21:58,750 Because if I can potentially have compilation albums that's 460 00:21:58,750 --> 00:22:02,038 a compilation of multiple bands, so the band name is just something like 461 00:22:02,038 --> 00:22:04,660 compilation or maybe the band name is empty string, 462 00:22:04,660 --> 00:22:07,920 the album name by itself should be a unique identifier. Now, you might say, but 463 00:22:07,920 --> 00:22:10,920 Marilyn, that's not true in the real world. I have multiple albums that have the same 464 00:22:10,920 --> 00:22:12,450 title on them. 465 00:22:12,450 --> 00:22:15,529 We're just going to assume that for the purposes of what we're doing here, and 466 00:22:15,529 --> 00:22:16,869 it'll be okay. 467 00:22:16,869 --> 00:22:20,329 How do we build up the album? We have a notion of adding a song to an album and 468 00:22:20,329 --> 00:22:23,220 getting an iterator over the songs on the album. 469 00:22:23,220 --> 00:22:26,380 And so the way we do that is we're going to have something called songs. Let me 470 00:22:26,380 --> 00:22:31,620 show you songs down here. Songs is just an ArrayList of songs. Okay. 471 00:22:31,619 --> 00:22:32,378 And so 472 00:22:32,378 --> 00:22:36,398 if I want to add a song to the album, I pass it in an actual song object and it 473 00:22:36,398 --> 00:22:40,498 adds it to its ArrayList. And if I want to list out all these songs that are on 474 00:22:40,499 --> 00:22:43,940 the album, I ask for an iterator over all the songs on the album. So what I actually 475 00:22:43,940 --> 00:22:47,519 get is an iterator over song objects. Okay. 476 00:22:47,519 --> 00:22:51,480 Two stings just returns the title and the band, it doesn't actually list out all 477 00:22:51,480 --> 00:22:54,909 the songs. It just says, hey, it's just this name of this title and this band, 478 00:22:54,909 --> 00:22:57,300 and that's all that's in an album. Okay. 479 00:22:57,299 --> 00:23:00,319 Again, we think about what's mutable and what's not mutable. 480 00:23:00,319 --> 00:23:00,889 Now, 481 00:23:00,890 --> 00:23:03,990 to put the whole store together, this is where things get a little more 482 00:23:03,990 --> 00:23:04,729 interesting. 483 00:23:04,729 --> 00:23:07,569 To put the whole store together, you need to think about what's the store going to 484 00:23:07,569 --> 00:23:11,658 do. So let me show you a simple store running and this is the basic text 485 00:23:11,659 --> 00:23:13,810 interface for a store. It's kind of like 486 00:23:13,809 --> 00:23:16,159 online store circuit of 1995. 487 00:23:16,160 --> 00:23:19,830 Okay. So I can list out all the songs, I can list out all the albums, I can add a song, 488 00:23:19,829 --> 00:23:22,019 I can add an album. When the store starts, 489 00:23:22,019 --> 00:23:25,269 I have not songs or albums in the store. I need to add them all. I 490 00:23:25,269 --> 00:23:28,859 can list all the songs on a particular album and I can also update 491 00:23:28,859 --> 00:23:30,548 the price for a song. Okay. 492 00:23:30,548 --> 00:23:34,369 So if I list out all the songs. It says all songs carried by the store and says 493 00:23:34,369 --> 00:23:38,119 nothing, because there's no songs that the store currently has. And list 494 00:23:38,119 --> 00:23:40,969 out all the albums carried by the store and list out nothing here, because 495 00:23:40,970 --> 00:23:45,319 there're no albums. But I can go ahead and do something like add a song. 496 00:23:45,319 --> 00:23:48,710 And let's say the song I want to add is "In Your Eyes," 497 00:23:48,710 --> 00:23:52,160 Peter Gabriel. Any Peter Gabriel fans out there? 498 00:23:52,160 --> 00:23:55,080 No? 499 00:23:55,079 --> 00:23:57,609 A little bit? 500 00:23:57,609 --> 00:23:59,808 Come on. Oh, 501 00:23:59,808 --> 00:24:01,369 man. 502 00:24:01,369 --> 00:24:02,949 I give up. 503 00:24:02,950 --> 00:24:04,390 It's all over. 504 00:24:04,390 --> 00:24:07,509 I just don't believe it. All right. 505 00:24:07,509 --> 00:24:11,529 We'll say the song is, I say, okay, it'll be 99 cents. Go get it. All 506 00:24:11,529 --> 00:24:14,548 right. So we add a song and if we list all the songs, 507 00:24:14,548 --> 00:24:15,859 now we have 508 00:24:15,859 --> 00:24:19,379 here's the string representation of a song, "In Your Eyes," by Peter Gabriel, cost 509 00:24:19,380 --> 00:24:22,130 99 cents. We still have no albums, rights, we just have a particular 510 00:24:22,130 --> 00:24:23,830 song that 511 00:24:23,829 --> 00:24:25,349 we can potentially 512 00:24:25,349 --> 00:24:28,398 sell by itself and we don't have any albums. So we'll come back to this. But 513 00:24:28,398 --> 00:24:31,469 this is the basic idea. We want to be able to list all the songs and albums, 514 00:24:31,470 --> 00:24:35,880 add songs, add albums, and then list the information for a particular album. Okay. 515 00:24:35,880 --> 00:24:39,910 So if we think about that, what we need is a bigger data structure to keep track 516 00:24:39,910 --> 00:24:44,790 of all this information about multiple songs and multiple albums. 517 00:24:44,789 --> 00:24:45,909 Okay. 518 00:24:45,910 --> 00:24:48,370 Now, if we want to manage an inventor, 519 00:24:48,369 --> 00:24:52,279 the two things we have to keep in mind are also what I mentioned before. A song 520 00:24:52,279 --> 00:24:55,309 can exist in our data 521 00:24:55,309 --> 00:24:57,440 that is not on any particular album. 522 00:24:57,440 --> 00:25:01,620 So as a result it's not sufficient to just say what albums are carried by 523 00:25:01,619 --> 00:25:02,529 the store, 524 00:25:02,529 --> 00:25:05,668 because some songs may not be on any album, but we still sell them 525 00:25:05,669 --> 00:25:06,479 individually. 526 00:25:06,479 --> 00:25:08,230 So we need to have some 527 00:25:08,230 --> 00:25:10,750 notion of keeping track of a list of songs. 528 00:25:10,750 --> 00:25:13,429 Now there's different things we could think of for a data structure to keep track 529 00:25:13,429 --> 00:25:16,940 of songs. One thing is an ArrayList, right. That's what we're using in albums to keep 530 00:25:16,940 --> 00:25:18,759 track of a whole list of songs. 531 00:25:18,759 --> 00:25:22,339 Another thing we could consider is a HashMap of songs. And so 532 00:25:22,339 --> 00:25:25,439 if we think about a map versus an ArrayList, what question that you want to 533 00:25:25,440 --> 00:25:29,080 think about gets back to this identifier question, right. 534 00:25:29,079 --> 00:25:30,730 Because if you want to have a map, 535 00:25:30,730 --> 00:25:37,180 say for example, some string to song, and you want this string to uniquely identify a 536 00:25:37,180 --> 00:25:37,650 song, 537 00:25:37,650 --> 00:25:41,210 this string needs to be something that is a unique identifier. 538 00:25:41,210 --> 00:25:44,670 But a song doesn't have one string that's a unique identifier, it's unique 539 00:25:44,670 --> 00:25:48,180 identifier's a combination of a name and a band. And so all kinds of funky things 540 00:25:48,180 --> 00:25:51,110 that are things that people consider. Oh, how can I connect those two strings 541 00:25:51,109 --> 00:25:54,099 together? People actually do that in real applications. 542 00:25:54,099 --> 00:25:55,599 We're not going to do that here. 543 00:25:55,599 --> 00:25:58,639 We're just gonna say, there's too much complexity in dealing with this, we're 544 00:25:58,640 --> 00:26:01,309 going to go for a much simpler approach and just say we're going to have an 545 00:26:01,308 --> 00:26:02,069 ArrayList 546 00:26:02,069 --> 00:26:03,579 of all of our songs 547 00:26:03,579 --> 00:26:10,148 and not worry about the unique identifier issue. So here we have an ArrayList of 548 00:26:10,148 --> 00:26:14,369 type song, and we'll just call this songs, that's all the songs in our database. And 549 00:26:14,369 --> 00:26:19,529 so here we create a new ArrayList 550 00:26:19,529 --> 00:26:22,558 of song and we call it constructor. Okay. 551 00:26:22,558 --> 00:26:25,558 Now, life in the album world's a little bit different. Besides just keeping 552 00:26:25,558 --> 00:26:28,660 track of a list of songs, we also need to keep track of albums. 553 00:26:28,660 --> 00:26:30,900 But in the album world the 554 00:26:30,900 --> 00:26:33,028 name is actually a unique identifier. 555 00:26:33,028 --> 00:26:37,079 And if we want to be able to look up albums quickly, it might make sense to use 556 00:26:37,079 --> 00:26:40,128 a HashMap. So part of doing this whole example is to actually show you both 557 00:26:40,128 --> 00:26:45,538 ArrayList and HashMap in one application. So what we could do is have a HashMap 558 00:26:45,538 --> 00:26:52,190 that maps from stings to albums where the map, this string, is in some sense 559 00:26:52,190 --> 00:26:56,879 the name of the album and this is the actual album object. 560 00:26:56,878 --> 00:27:03,459 And we'll call this albums and we can do all the new, you know, la de da HashMap we 561 00:27:03,460 --> 00:27:06,950 actually created. Okay. So now we have these two big data structures that actually keep 562 00:27:06,950 --> 00:27:08,509 track of stuff for us. 563 00:27:08,509 --> 00:27:12,849 Now, here's where things get a little bit funky. And when things get funky, what you're 564 00:27:12,849 --> 00:27:15,740 going to need when you deal with big data structures, you need a guide. And you'll 565 00:27:15,740 --> 00:27:19,309 see this in just a second because you're going to see some of the 566 00:27:19,309 --> 00:27:23,639 code that we write gets very long when we deal with big data structures. So 567 00:27:23,640 --> 00:27:26,320 I'll be your guide. All right. 568 00:27:26,319 --> 00:27:29,829 So in the days of yore, I almost bought the whole outfit. But it's 569 00:27:29,829 --> 00:27:31,730 a little hot in here, under the lights. 570 00:27:31,730 --> 00:27:34,430 So in order to actually think about 571 00:27:34,430 --> 00:27:37,830 how you get the information and store the information 572 00:27:37,829 --> 00:27:40,339 when you have a large data structure, 573 00:27:40,339 --> 00:27:42,839 paper and pencil is your friend. Right 574 00:27:42,839 --> 00:27:46,669 If you spend all your time just staring at a computer screen it doesn't really allow you to 575 00:27:46,670 --> 00:27:49,130 internalize what is your data structure really look like 576 00:27:49,130 --> 00:27:52,480 and what's going on. So break out some pencil and paper, not right now but when 577 00:27:52,480 --> 00:27:54,130 you're working on data structures, 578 00:27:54,130 --> 00:27:58,030 and draw out, potentially, what things look like. So here's songs and songs, 579 00:27:58,029 --> 00:28:01,420 and songs is an ArrayList. And it's going to have multiple, let's say at this point, 580 00:28:01,420 --> 00:28:02,720 three songs in it. 581 00:28:02,720 --> 00:28:05,960 And over here we have albums 582 00:28:05,960 --> 00:28:09,220 and albums is a HashMap, 583 00:28:09,220 --> 00:28:10,079 albums, 584 00:28:10,079 --> 00:28:14,490 that maps from names of an album to a particular album object. 585 00:28:14,490 --> 00:28:18,210 Now, the important thing to keep in mind in objects, and this is kind of the whole 586 00:28:18,210 --> 00:28:19,850 key to big data structure, 587 00:28:19,849 --> 00:28:24,839 is all objects, when you refer to them in Java, are references to objects. Remember when 588 00:28:24,839 --> 00:28:28,279 we talked about that. When you pass an object to a particular method 589 00:28:28,279 --> 00:28:29,990 590 00:28:29,990 --> 00:28:31,529 in some application, 591 00:28:31,529 --> 00:28:36,589 you're passing a reference to the object. You're passing where that object lives. Okay. 592 00:28:36,589 --> 00:28:39,970 Which means that when you have an ArrayList of songs, which what you really have here 593 00:28:39,970 --> 00:28:43,769 are a bunch of references, which we can think of as pointers 594 00:28:43,769 --> 00:28:48,220 that refer to the actual objects that contain the songs. Okay. 595 00:28:48,220 --> 00:28:50,058 So over here there's a 596 00:28:50,058 --> 00:28:52,069 "In Your Eyes," 597 00:28:52,069 --> 00:28:55,909 by Peter Gabriel and it was 99 cents. And over here we might have 598 00:28:55,910 --> 00:29:00,470 say, "Ramble On," tell me there's some Zeppelin fans out there. 599 00:29:00,470 --> 00:29:05,210 All right, good, good. We will not have to end lecture early. And "Ramble On" is such 600 00:29:05,210 --> 00:29:08,970 a great song, it's like $12.99 by itself, single son. That's 601 00:29:08,970 --> 00:29:11,308 probably why most people don't listen to it. 602 00:29:11,308 --> 00:29:15,740 And over here we have the master, "Stairway to Heaven," 603 00:29:15,740 --> 00:29:19,359 Stairway to H, we'll just abbreviate it. Because it's that good, we'll just have a moment of silence, 604 00:29:19,359 --> 00:29:23,829 also, by Led Zeppelin, and we'll just say that one should be like, 605 00:29:23,829 --> 00:29:28,509 49 cents so everyone can listen to it. It's just kind of like the bonus tune. All right. 606 00:29:28,509 --> 00:29:32,069 And so that's what we have in a list of songs. Now, here's the interesting part, right. If 607 00:29:32,069 --> 00:29:36,089 I'm going to have some albums, so I add some albums. So let's say add some album on here 608 00:29:36,089 --> 00:29:37,250 like 609 00:29:37,250 --> 00:29:39,289 "Soul," by Peter Gabriel, 610 00:29:39,289 --> 00:29:41,139 and Soul actually has 611 00:29:41,140 --> 00:29:43,830 the song, "In Your Eyes" on it. Okay. 612 00:29:43,829 --> 00:29:45,980 Now there's two things that come up 613 00:29:45,980 --> 00:29:48,740 we will need to think about when we actually do this. We need to say, 614 00:29:48,740 --> 00:29:53,150 hey, this has got some ArrayList associate in there, and so I can create a new 615 00:29:53,150 --> 00:29:59,519 object that is a song for "In Your Eyes" and set my ArrayList to be a reference to that 616 00:29:59,519 --> 00:30:00,460 object. 617 00:30:00,460 --> 00:30:04,090 And that's a reasonable thing to do in some cases. The only problem is 618 00:30:04,089 --> 00:30:08,418 what happens if I go into my store and say, hey, I want to change my song "In Your 619 00:30:08,419 --> 00:30:10,320 Eyes" from being 99 cents, 620 00:30:10,319 --> 00:30:11,990 because no one's heard of it before, 621 00:30:11,990 --> 00:30:15,509 to 9 cents. Okay. So 622 00:30:15,509 --> 00:30:19,859 if I go thought my list of songs I say, oh, here it is, I'll change it's cost 623 00:30:19,859 --> 00:30:22,240 to be 9 cents. 624 00:30:22,240 --> 00:30:27,609 Now, unless I go through all of my albums and find for every album go though 625 00:30:27,609 --> 00:30:31,209 every song that's listed on the album and see if I can find that same song 626 00:30:31,210 --> 00:30:35,769 duplicated, I'm going to create an inconsistency in my data. What I really want to have is say, 627 00:30:35,769 --> 00:30:39,569 hey, there's only one object that is that song. And 628 00:30:39,569 --> 00:30:43,269 if that song happens to be a song that's sold individually, or it's a song that's 629 00:30:43,269 --> 00:30:47,539 both in my list of songs and on some albums, there's only one object ever that 630 00:30:47,539 --> 00:30:50,339 I refer to for that song, which means, 631 00:30:50,339 --> 00:30:55,349 I never create the second object out here for that same song. What I do, is when 632 00:30:55,349 --> 00:30:57,389 I'm creating the album "Soul," 633 00:30:57,390 --> 00:31:01,870 and someone tells me, oh, it's got the song, "In Your Eyes," on it, I say, hey does 634 00:31:01,869 --> 00:31:04,139 that already exist in my store. 635 00:31:04,140 --> 00:31:09,450 If it does exist in my store, I'm going to add that object to my ArrayList. 636 00:31:09,450 --> 00:31:13,319 I'm not going to create a new object, which means each song only ever gets 637 00:31:13,319 --> 00:31:14,409 created once, but 638 00:31:14,410 --> 00:31:17,190 it can potentially get added to multiple ArrayLists. 639 00:31:17,190 --> 00:31:19,900 And it's the same single underlying object 640 00:31:19,900 --> 00:31:21,980 that has multiple references to it. 641 00:31:21,980 --> 00:31:25,049 Why is that cool? That's cool because now, when I come along and a whole bunch of 642 00:31:25,049 --> 00:31:26,990 people start listening to "In Your Eyes," and I'm like, 643 00:31:26,990 --> 00:31:29,628 Peter Gabriel, he just deserves a lot more money, 644 00:31:29,628 --> 00:31:33,949 we're going to make this $9.99. It's $9.99 everywhere 645 00:31:33,950 --> 00:31:35,788 by changing it once. 646 00:31:35,788 --> 00:31:39,329 And that's the real key to large-scale software engineering. You think about 647 00:31:39,329 --> 00:31:42,438 not only reusing - you remember for a long time we talked about having methods 648 00:31:42,439 --> 00:31:46,629 that you reuse and how you generalize your methods, this is about reusing your 649 00:31:46,628 --> 00:31:48,990 data. Thinking about your data, sort of, 650 00:31:48,990 --> 00:31:53,140 if it's only one thing, exists in one place ,and everything refers to it. Okay. 651 00:31:53,140 --> 00:31:56,179 So any questions about that idea? 652 00:31:56,179 --> 00:32:01,080 This is what we refer to as a shallow copy, 653 00:32:01,079 --> 00:32:02,809 because what you're getting, 654 00:32:02,809 --> 00:32:06,470 after you've created that song once, when you want to add that song somewhere else, 655 00:32:06,470 --> 00:32:09,558 you're just setting a reference to it, you're creating a shallow copy, there's 656 00:32:09,558 --> 00:32:10,629 only one copy. 657 00:32:10,630 --> 00:32:13,850 The thing we did before, where we actually created a whole separate 658 00:32:13,849 --> 00:32:17,980 structure, is referred to as a deep copy. 659 00:32:17,980 --> 00:32:20,970 And sometimes, deep copies make sense 660 00:32:20,970 --> 00:32:24,610 in some particular cases. Most of the time they actually, well, I won't say mot of the 661 00:32:24,609 --> 00:32:26,649 time, they don't, it depends on the application, 662 00:32:26,650 --> 00:32:28,778 but most of the time what you'll actually be using 663 00:32:28,778 --> 00:32:31,769 is your friend, the shallow copy. Okay. 664 00:32:31,769 --> 00:32:36,329 So what does that actually look like if we try to turn that into some code? Well, 665 00:32:36,329 --> 00:32:39,339 what does that mean in the application? Let me show you what that means in the 666 00:32:39,339 --> 00:32:41,889 application. So we're going to add some songs. We're going to go through another example. All 667 00:32:41,890 --> 00:32:44,020 right, let me add the song 668 00:32:44,019 --> 00:32:48,559 and I'll just abbreviate, "In Your Eyes," Peter Gabriel, $1.99. 669 00:32:48,559 --> 00:32:52,750 Then I'm going to add "Ramble On," oops, 670 00:32:52,750 --> 00:32:55,099 "Ramble On," Led Zeppelin, 671 00:32:55,099 --> 00:32:58,469 and we'll make that, oh, I don't know, $2.99. Okay. 672 00:32:58,470 --> 00:33:01,210 Now, at this point I have two songs. Now, I'm going to add an album. 673 00:33:01,210 --> 00:33:05,990 So I add a particular album and the album I'm going to add is "Soul," by Peter Gabriel and it says enter a 674 00:33:05,990 --> 00:33:10,450 song name. It's going to have "In Your Eyes" on it. And it asks me because the unique 675 00:33:10,450 --> 00:33:13,528 identifier is both the song and the band name, it still needs to ask me for the 676 00:33:13,528 --> 00:33:16,419 band name, and the band name I give it is Peter Gabriel. 677 00:33:16,419 --> 00:33:19,330 And it says, hey, that song is already in the store. 678 00:33:19,329 --> 00:33:22,849 It's just letting you know, hey, I found that song in my store, so when I add it to 679 00:33:22,849 --> 00:33:27,528 the album, I'm adding that same object that's also in my store to the album. 680 00:33:27,528 --> 00:33:29,579 And then you could say, well, there's other stuff on there like there happens 681 00:33:29,579 --> 00:33:33,378 to be a tune called, "Red Rain," which is also by Peter Gabriel, 682 00:33:33,378 --> 00:33:37,218 and you know it's a fine tune, but let's just say it's 1 cent, 683 00:33:37,219 --> 00:33:38,490 okay. 684 00:33:38,490 --> 00:33:41,009 And it says new song to add to the store. 685 00:33:41,009 --> 00:33:42,450 What did it do here? 686 00:33:42,450 --> 00:33:46,519 What it did in this case, it says, hey, you want to have a new song called "Red 687 00:33:46,519 --> 00:33:47,759 Rain," 688 00:33:47,759 --> 00:33:49,349 by Peter Gabriel. 689 00:33:49,349 --> 00:33:52,730 That song costs 1 cent, you want to add it to your album. 690 00:33:52,730 --> 00:33:56,929 Well, if you want to add it to your album, it's also a song that I'm going to see in the store. 691 00:33:56,929 --> 00:33:59,759 So it actually adds it to the store 692 00:33:59,759 --> 00:34:04,579 and adds it to the album. And there's still only one copy of that object ever. 693 00:34:04,579 --> 00:34:08,619 It just needs to make sure that when it creates a new song to add to an album that's 694 00:34:08,619 --> 00:34:09,938 not already in the store, 695 00:34:09,938 --> 00:34:12,369 it adds it to the store, as well as to the album. 696 00:34:12,369 --> 00:34:17,518 If the song already exists in the store then it just adds a reference to the album. Okay. 697 00:34:17,518 --> 00:34:20,208 That's the critical idea here. All right. 698 00:34:20,208 --> 00:34:23,978 So now, if we sort of list - I'll hit enter quit - 699 00:34:23,978 --> 00:34:28,468 and if we list all the songs, right, the song "Red Rain" has now been added to the store and 700 00:34:28,469 --> 00:34:31,499 costs 1 cent. And if I list all the albums 701 00:34:31,498 --> 00:34:36,838 that are sold by Peter Gabriel, and if I list all the songs on that album, 702 00:34:36,838 --> 00:34:40,449 it has the songs "In Your Eyes" and "Red Rain" so it matches the picture that I 703 00:34:40,449 --> 00:34:44,019 think. That's why having a piece of paper, where you draw pictures, is useful. 704 00:34:44,018 --> 00:34:46,579 Because you look at what you're application is doing and you say, 705 00:34:46,579 --> 00:34:49,750 does it match what I actually think should be happening in my picture. And if 706 00:34:49,750 --> 00:34:50,179 doesn't, 707 00:34:50,179 --> 00:34:51,779 then you know one of two things is wrong. 708 00:34:51,780 --> 00:34:55,070 Either your picture's wrong or your code that's supposed to be dealing with that 709 00:34:55,070 --> 00:34:55,760 picture is wrong. 710 00:34:55,760 --> 00:34:58,840 But in either case, you've already figured out a bug, even though the 711 00:34:58,840 --> 00:35:02,709 program hasn't crashed or anything, you just know there's an inconsistency. Okay. 712 00:35:02,708 --> 00:35:07,609 And so now, if I update the price for a song, like I update the song, "In Your Eyes," 713 00:35:07,610 --> 00:35:11,690 by Peter Gabriel, and I change it's price to, I just go crazy, no one's 714 00:35:11,690 --> 00:35:14,159 going to buy the song anymore, the price is updated. 715 00:35:14,159 --> 00:35:15,940 Now, if I list all the songs, 716 00:35:15,940 --> 00:35:20,088 that song is $999.99 in the store, 717 00:35:20,088 --> 00:35:24,438 and if I also list the songs on any album - 718 00:35:24,438 --> 00:35:25,920 five, 719 00:35:25,920 --> 00:35:27,838 so lower 720 00:35:27,838 --> 00:35:31,788 case, the price is also updated on each of the individual albums, because there's only 721 00:35:31,789 --> 00:35:33,249 one object. 722 00:35:33,248 --> 00:35:38,018 Okay. That's where the consistency comes in. That's why the consistency's key. Okay. 723 00:35:38,018 --> 00:35:40,588 So what does this actually look like in code? 724 00:35:40,588 --> 00:35:44,130 How do we do this? Let me show you what the actual application looks like for our 725 00:35:44,130 --> 00:35:47,259 little friend, the Flytune Store. Okay. So 726 00:35:47,259 --> 00:35:49,998 there's a bunch of stuff at the beginning that just asks for the user 727 00:35:49,998 --> 00:35:54,048 selection, basically print some stuff out to allow you to make a selection, 728 00:35:54,048 --> 00:35:57,150 and then gets you're selection for you. And then there's a big case statement that 729 00:35:57,150 --> 00:35:59,970 calls an appropriate method, depending what selection you made. 730 00:35:59,969 --> 00:36:02,519 So I'll go though some of the simple ones pretty quickly. You 731 00:36:02,519 --> 00:36:04,869 can list out all the songs carried in the store. 732 00:36:04,869 --> 00:36:07,740 In order to be able to do that, we need to keep track of how 733 00:36:07,739 --> 00:36:11,089 this information's actually stored, it's exactly in these data structures I just 734 00:36:11,090 --> 00:36:12,059 showed you. 735 00:36:12,059 --> 00:36:15,389 Song is kept track of in an ArrayList of songs and 736 00:36:15,389 --> 00:36:18,119 albums is kept track of in a HashMap that 737 00:36:18,119 --> 00:36:21,679 maps from the name of the album to the actual album data structure, itself. 738 00:36:21,679 --> 00:36:26,269 Okay. Any questions about that, hopefully, that's all clear. I will take off the hat. 739 00:36:26,268 --> 00:36:29,998 So how do we print these things out? To list all the songs, we just go through 740 00:36:29,998 --> 00:36:31,268 our ArrayList 741 00:36:31,268 --> 00:36:32,858 up to its size, 742 00:36:32,858 --> 00:36:36,348 and this is why you want to think of data structure as your needed guide, because 743 00:36:36,349 --> 00:36:37,869 you're going a journey. 744 00:36:37,869 --> 00:36:41,190 At any given point, when you're dealing with a data structure, you want to think, 745 00:36:41,190 --> 00:36:43,470 what is the type I'll dealing with right now? 746 00:36:43,469 --> 00:36:47,059 What does that mean? It means, when I want to print something out, what I need is a 747 00:36:47,059 --> 00:36:48,168 string that prints out. 748 00:36:48,168 --> 00:36:49,429 How do I get a string? 749 00:36:49,429 --> 00:36:51,719 If I started at songs, 750 00:36:51,719 --> 00:36:54,510 songs is an ArrayList. I don't have a string I can print out. 751 00:36:54,510 --> 00:36:57,160 But from an ArrayList I can get an individual element. 752 00:36:57,159 --> 00:37:00,369 When I get an individual element of that ArrayList, what do I have? I still don't 753 00:37:00,369 --> 00:37:02,688 have a string I have a song. 754 00:37:02,688 --> 00:37:07,279 What can I ask the song for? I can ask to get the string version of the song 755 00:37:07,280 --> 00:37:09,459 and I have a string to print out. Okay. 756 00:37:09,458 --> 00:37:12,598 So you always want to think of it as you're going on a journey. 757 00:37:12,599 --> 00:37:16,259 Where do you start your journey? You're journey starts at the data structures you 758 00:37:16,259 --> 00:37:19,278 have available to you. In this case, we have a data structure called songs, another 759 00:37:19,278 --> 00:37:21,079 data structure called albums, 760 00:37:21,079 --> 00:37:22,608 that's what's available to us. 761 00:37:22,608 --> 00:37:26,980 And what we want to do is go from that starting point through a series of steps 762 00:37:26,980 --> 00:37:29,990 to get to the thing that we actually care about at the end, hat little piece 763 00:37:29,989 --> 00:37:33,348 of data that we want to display or interact with somehow. 764 00:37:33,349 --> 00:37:34,939 So here's another example. 765 00:37:34,938 --> 00:37:38,049 If I want to list all the albums, how do I list all the albums? 766 00:37:38,050 --> 00:37:39,840 Well, to list all the albums, 767 00:37:39,840 --> 00:37:41,709 albums is a HashSet. 768 00:37:41,708 --> 00:37:45,338 So in order to do something with a HashSet I need to say, hey, I want an 769 00:37:45,338 --> 00:37:49,389 iterator over all the keys of that HashSet. So albums is the HashSet, I get 770 00:37:49,389 --> 00:37:51,099 the keys of the HashSet, 771 00:37:51,099 --> 00:37:52,440 which is a collection, 772 00:37:52,440 --> 00:37:56,369 and I get an iterator for that collection, which is an iterator over all the keys of 773 00:37:56,369 --> 00:37:57,950 the HashSet. 774 00:37:57,949 --> 00:38:01,399 And now, as long as my album iterator, which is just my iterator over the keys, 775 00:38:01,400 --> 00:38:04,170 has an element, what do I do? I start at albums. 776 00:38:04,170 --> 00:38:07,400 I need say I need to get a particular album. Okay. Get. 777 00:38:07,400 --> 00:38:09,168 Which album am I going to get? 778 00:38:09,168 --> 00:38:13,388 I'm going to get the album whose name is associated with the next elements of the 779 00:38:13,389 --> 00:38:16,079 iterator. Right, because it's an iterator over all the names of albums. 780 00:38:16,079 --> 00:38:18,949 So get, gives me a particular album. 781 00:38:18,949 --> 00:38:22,278 Then, when I have the particular album, I can call two strings on it to get the 782 00:38:22,278 --> 00:38:23,869 string form of the album. Okay, 783 00:38:23,869 --> 00:38:26,140 any questions about that? Because they're going to get even 784 00:38:26,139 --> 00:38:27,710 longer, 785 00:38:27,710 --> 00:38:30,809 so if there are any questions about sort of the chain of things we call. If 786 00:38:30,809 --> 00:38:34,278 it's making sense, the chain of things we call, nod your head. All right, 787 00:38:34,278 --> 00:38:37,518 and if it's not making sense, shake your head. 788 00:38:37,518 --> 00:38:40,958 And if it's kind of making sense, just keep looking and ask a question if a 789 00:38:40,958 --> 00:38:43,708 question comes to mind. All right. So 790 00:38:43,708 --> 00:38:46,569 how do I find a particular song? This is something where I'm going 791 00:38:46,570 --> 00:38:51,019 to use the helper method, so it's private to find a particular song. Songs, 792 00:38:51,019 --> 00:38:54,849 our unique identifier, is a combination of both the band name 793 00:38:54,849 --> 00:38:58,769 or the name of the song and the band name. So how do I check for that? I'm going to 794 00:38:58,768 --> 00:39:02,908 go through all my songs, it's an ArrayList so I can count through all the songs. 795 00:39:02,909 --> 00:39:04,409 Here's where things get long. 796 00:39:04,409 --> 00:39:09,088 How do I check to see if a song, that's actually in my data set, 797 00:39:09,088 --> 00:39:12,409 matches on its name with the name that's passed in? 798 00:39:12,409 --> 00:39:14,119 I start at songs, 799 00:39:14,119 --> 00:39:17,449 get the I song, and I have one particular song. 800 00:39:17,449 --> 00:39:21,939 For that particular object I get the song and name. Now, I have a string. I want to 801 00:39:21,940 --> 00:39:24,349 check to see if that string is equals 802 00:39:24,349 --> 00:39:27,079 to the name that's passed in. Okay. 803 00:39:27,079 --> 00:39:30,650 And I do the same thing with band names. Song, get the I song, get the band name 804 00:39:30,650 --> 00:39:34,139 of that song, and then check to see if that's equal to the band. And if both of 805 00:39:34,139 --> 00:39:35,139 these are equal, 806 00:39:35,139 --> 00:39:38,699 then, hey, I found the song, and so I'm going to return an index, which is the 807 00:39:38,699 --> 00:39:41,848 index location of that song in my ArrayList, and 808 00:39:41,849 --> 00:39:45,269 I can just break out of the four-loop, here. Because once I find it, I say, hey, I found 809 00:39:45,268 --> 00:39:48,048 that, I don't need to keep looking, so actually this is one of the rare cases where 810 00:39:48,048 --> 00:39:49,769 you'll see a break in a four-loop, 811 00:39:49,769 --> 00:39:52,159 is you don't need to finish the loop. You got to what you were looking for 812 00:39:52,159 --> 00:39:54,018 and get out of the loop. 813 00:39:54,018 --> 00:39:56,938 If you manage to get through this whole loop without ever finding something that 814 00:39:56,938 --> 00:39:59,000 matches on both, the name and the band, 815 00:39:59,000 --> 00:40:02,920 well, your index remains negative one. So you return negative one to indicate, hey, I didn't 816 00:40:02,920 --> 00:40:06,180 find it, because you know negative one's not a valid index for an ArrayList. 817 00:40:06,179 --> 00:40:09,978 So if you return it that means you didn't find a valid element. Okay. 818 00:40:09,978 --> 00:40:13,439 How do we use find song? Here's how add song works. Okay. 819 00:40:13,440 --> 00:40:15,909 When you want to thing about add song, you want to think about this property 820 00:40:15,909 --> 00:40:18,859 that we're only ever going to create an object once, 821 00:40:18,860 --> 00:40:22,019 and everything else is going to be references to that object. So the way add 822 00:40:22,019 --> 00:40:23,038 song is going to work 823 00:40:23,039 --> 00:40:26,819 is it's going to return a song object. Okay. 824 00:40:26,818 --> 00:40:29,579 And what it's going to do, is it's going to ask us for the name of a song, if 825 00:40:29,579 --> 00:40:33,669 the user enters blank line that means they want to stop adding songs so it just 826 00:40:33,668 --> 00:40:36,969 returns null to say, hey, you want to stop adding songs, I didn't create a new song, 827 00:40:36,969 --> 00:40:39,199 here's a null to indicate you are done. 828 00:40:39,199 --> 00:40:42,629 But if they don't impress enter quick, I also ask for a 829 00:40:42,630 --> 00:40:43,528 band name, 830 00:40:43,528 --> 00:40:46,239 and then I ask to find the song. Okay. 831 00:40:46,239 --> 00:40:49,309 I call that find song method I just wrote and I say, 832 00:40:49,309 --> 00:40:53,229 does that song exist. If the song exists, the song index is not going to be minus 833 00:40:53,228 --> 00:40:53,879 one. 834 00:40:53,880 --> 00:40:56,630 And that means, that song already exists in the store. 835 00:40:56,630 --> 00:41:00,519 So you told me to add a song that already existed in the store. So I'm not 836 00:41:00,519 --> 00:41:03,269 going to create a new song because it's already an object in the store 837 00:41:03,269 --> 00:41:03,699 that 838 00:41:03,699 --> 00:41:05,558 encapsulates all the information for that song, 839 00:41:05,559 --> 00:41:09,869 I will return to you a reference to that object, which means I just returned from 840 00:41:09,869 --> 00:41:11,910 the songs ArrayList 841 00:41:11,909 --> 00:41:17,178 whatever song happens to be at the index that that song actually lives at. Okay. 842 00:41:17,179 --> 00:41:20,289 So this just returns an actual object. It actually returns a reference. If you can, think of it 843 00:41:20,289 --> 00:41:22,679 as returning a pointer to the object. 844 00:41:22,679 --> 00:41:26,108 If I didn't find it in there, then, hey, I need to create the new song, right. It's 845 00:41:26,108 --> 00:41:29,558 sort of like "Red Rain" at the end. You wanted to add a song. It didn't exist in 846 00:41:29,559 --> 00:41:30,890 the store, 847 00:41:30,889 --> 00:41:34,618 let me get the price for that song. I'll create a new song object 848 00:41:34,619 --> 00:41:38,269 and now. Here's the funky thing, I will add that song to my ArrayList of 849 00:41:38,268 --> 00:41:40,358 songs for the whole store, 850 00:41:40,358 --> 00:41:43,958 write out to that the new song was added to the store, and I'll return that new 851 00:41:43,958 --> 00:41:46,798 song to you so you can do whatever you want with it. 852 00:41:46,798 --> 00:41:48,170 And so now, you might ask, 853 00:41:48,170 --> 00:41:49,019 okay, Marilyn, 854 00:41:49,018 --> 00:41:52,038 if I just added a song to the store I don't really care about doing anything 855 00:41:52,039 --> 00:41:54,519 with that song, why are you returning the song to me? 856 00:41:54,518 --> 00:41:58,688 And that's true. If I just add a song to the store, if that's all I care about, I 857 00:41:58,688 --> 00:42:00,358 ignore the return value. 858 00:42:00,358 --> 00:42:03,509 That's actually what I do up here, which is very funky. Right. 859 00:42:03,509 --> 00:42:07,019 If you want to add a song, I just call the add song method, 860 00:42:07,018 --> 00:42:10,778 it goes ahead and adds the song to the store, if it doesn't already exist, and it returns 861 00:42:10,778 --> 00:42:14,289 reference that song object. 862 00:42:14,289 --> 00:42:17,639 If all I'm doing is adding a song, I don't care I just ignore it. I don't assign it to 863 00:42:17,639 --> 00:42:20,380 anything, I just say, yeah, thanks for returning that object, that was fun, whatever, 864 00:42:20,380 --> 00:42:23,429 and just get rid of it. Okay. But 865 00:42:23,429 --> 00:42:28,068 the reason why I've written it this was is if I'm adding an album, 866 00:42:28,068 --> 00:42:30,880 what do I do? I ask for the name of the album, 867 00:42:30,880 --> 00:42:33,769 and I check to see if that album's already in the store. If the album's already in 868 00:42:33,769 --> 00:42:36,960 the store I'm not going to do anything because the album's already in the store. If the album's 869 00:42:36,960 --> 00:42:38,289 not already in the store, 870 00:42:38,289 --> 00:42:41,409 then I ask for the band name and I create a new album. 871 00:42:41,409 --> 00:42:46,159 And then I put that album in the store. So album is 872 00:42:46,159 --> 00:42:50,748 my HashMap. I put in that HashMap the name of the album is going to be the 873 00:42:50,748 --> 00:42:55,048 key and the actual album object is the object. So I add, you know, the album "Soul" 874 00:42:55,048 --> 00:42:56,199 to my 875 00:42:56,199 --> 00:42:57,438 HashMap. 876 00:42:57,438 --> 00:43:01,909 Now, I'm going to add all the songs. So I have a Y-loop that goes through and 877 00:43:01,909 --> 00:43:06,358 keeps adding songs until I get a null from add song to indicate that the user wanted to 878 00:43:06,358 --> 00:43:07,818 stop adding songs. 879 00:43:07,818 --> 00:43:11,128 But here's the funky part, every time the user adds a song, 880 00:43:11,128 --> 00:43:14,688 right, it comes along and says, hey, you want to create some new album? So let's say I 881 00:43:14,688 --> 00:43:17,918 actually want to create some new album over here when I create the album "Soul," 882 00:43:17,918 --> 00:43:20,668 so none of this stuff exists yet. Okay. 883 00:43:20,668 --> 00:43:24,469 So to create a new album, I say, hey, I want to create the album "Soul." It says, okay, that's fine, 884 00:43:24,469 --> 00:43:26,510 create an object for the album "Soul." 885 00:43:26,510 --> 00:43:30,460 It has the name "Soul," it's by Peter Gabriel. And it says, okay, what songs are on 886 00:43:30,460 --> 00:43:31,688 going to be in there? 887 00:43:31,688 --> 00:43:35,348 And it starts asking me for songs, because it's going to add them to my ArrayList 888 00:43:35,349 --> 00:43:35,970 in here. 889 00:43:35,969 --> 00:43:39,259 And so the first song I say is "In Your Eyes" in on that album. 890 00:43:39,259 --> 00:43:45,568 It goes and says, hey, find that song, it already exists. It returns a reference 891 00:43:45,568 --> 00:43:48,460 to that song, as a pointer that reference 892 00:43:48,460 --> 00:43:51,108 is what gets added to my ArrayList. 893 00:43:51,108 --> 00:43:54,858 Now, I go and ask for another song. Do you have any more songs? I say, yeah, there's another 894 00:43:54,858 --> 00:43:57,509 song. The song is called "Red Rain." 895 00:43:57,509 --> 00:44:01,920 When I go to create "Red Rain" it comes up here to add song, 896 00:44:01,920 --> 00:44:06,318 add song comes along, asks for the name and the band, it tries to find the song and says, 897 00:44:06,318 --> 00:44:08,018 hey, that song isn't already there, 898 00:44:08,018 --> 00:44:10,008 so I'm going to create a new song. 899 00:44:10,009 --> 00:44:15,318 It creates a new song called "Red Rain," by Peter Gabriel, 900 00:44:15,318 --> 00:44:16,400 has some price associate with it, 901 00:44:16,400 --> 00:44:19,229 and adds it 902 00:44:19,228 --> 00:44:22,468 to the list of songs for the store. 903 00:44:22,469 --> 00:44:26,318 And then it returns this object, which means it returns a reference to this 904 00:44:26,318 --> 00:44:27,068 object, 905 00:44:27,068 --> 00:44:30,208 and that reference to the object - oops, sorry this got blocked - all right, this is where it is 906 00:44:30,208 --> 00:44:34,048 creating a new song, and it adds the song to the store. Right, it adds it as a song, which is 907 00:44:34,048 --> 00:44:35,588 that ArrayList up there, 908 00:44:35,588 --> 00:44:37,619 and then it returns the object. 909 00:44:37,619 --> 00:44:40,759 So when it returns the object, I went too far, 910 00:44:40,759 --> 00:44:44,349 the add object does not know I add that song to the album. 911 00:44:44,349 --> 00:44:48,729 So this album, we're going to add a song, and the song we're going to add 912 00:44:48,728 --> 00:44:51,438 is that same object. It's "Red Rain." 913 00:44:51,438 --> 00:44:54,739 Okay. So that's the important thing to keep in mind. That object we only created once, 914 00:44:54,739 --> 00:44:58,199 and we passed around references to it or we can return references to it, 915 00:44:58,199 --> 00:45:01,219 and assign them other places. And that's how you get consistency in a much bigger 916 00:45:01,219 --> 00:45:02,668 date structure. Now, 917 00:45:02,668 --> 00:45:05,150 there are a bunch of other things we could do in here. I won't go through all the 918 00:45:05,150 --> 00:45:08,789 excruciating details down here. But we can list the songs on an album, we can 919 00:45:08,789 --> 00:45:10,210 update the song's price. 920 00:45:10,210 --> 00:45:14,429 And by updating a song's price, all we do is we ask for the song and the band, we 921 00:45:14,429 --> 00:45:17,309 find the song in the data set if it existed. If it doesn't exist we just 922 00:45:17,309 --> 00:45:18,710 say, hey, it's not in the store, 923 00:45:18,710 --> 00:45:20,099 and if it does exist 924 00:45:20,099 --> 00:45:24,640 then we read in it's price, and then for the songs in the store, we find the song 925 00:45:24,639 --> 00:45:26,719 at that index and set its price. 926 00:45:26,719 --> 00:45:30,529 And we know that whatever other albums contain that particular song, if we 927 00:45:30,530 --> 00:45:32,480 happen to update the price over here 928 00:45:32,480 --> 00:45:35,619 to you know, $6.99, 929 00:45:35,619 --> 00:45:39,338 we only update it once and all the places that refer to it automatically will see 930 00:45:39,338 --> 00:45:42,958 the updated version, because they point to the same object. Okay. 931 00:45:42,958 --> 00:45:45,578 Any questions about that? 932 00:45:45,579 --> 00:45:49,079 So I know it's a lot of complexity to kind of deal with a big data structure like 933 00:45:49,079 --> 00:45:49,710 this. 934 00:45:49,710 --> 00:45:53,030 But now it's one of those things like, now you're old enough to kind of 935 00:45:53,030 --> 00:45:56,970 see the big honking data structure. Because in the real world, when people think of 936 00:45:56,969 --> 00:46:00,079 software engineering the large, these are the kinds of things they need to 937 00:46:00,079 --> 00:46:03,289 worry about and that's where the complexity comes in. It's keeping track of 938 00:46:03,289 --> 00:46:04,588 all your objects 939 00:46:04,588 --> 00:46:08,018 and thinking about what objects you actually need to design and build, in 940 00:46:08,018 --> 00:46:11,008 order to actually build an application that's kind of successful to keep track 941 00:46:11,009 --> 00:46:13,798 of and makes thing consistent with all the data you have. 942 00:46:13,798 --> 00:46:17,139 So any other questions? Uh huh. [Inaudible]. 943 00:46:17,139 --> 00:46:23,400 Oh, can you use the mic, please? Sorry. 944 00:46:23,400 --> 00:46:26,519 So in this application, do all the songs and albums, 945 00:46:26,518 --> 00:46:30,618 they're also, I guess, singles, or - cause the albums are never priced, right? It's just the 946 00:46:30,619 --> 00:46:34,160 individual songs Right. So the albums don't have a price. You could imagine the cost for an album 947 00:46:34,159 --> 00:46:36,839 is the total of all the songs on the album. 948 00:46:36,840 --> 00:46:39,340 Or you could actually do something funky. Like this is one of those places you can 949 00:46:39,340 --> 00:46:43,320 make a policy decision and say, an album is 90 percent of the cost of all the 950 00:46:43,320 --> 00:46:47,519 songs on it. And then all the individual song prices can change and any time you just say, 951 00:46:47,519 --> 00:46:50,780 what's the price of the album, total up all the prices of the songs, and take 90 952 00:46:50,780 --> 00:46:54,909 percent of that. So it also allows for very dynamic album pricing. Thank you. All 953 00:46:54,909 --> 00:46:57,599 righty. If there's any more questions, come on up. Otherwise I'll see you on 954 00:46:57,599 --> 00:46:57,879 Wednesday.