1 00:00:00,820 --> 00:00:05,620 Now we can add the second action, searching a car, we're almost done building that dealership up so 2 00:00:05,620 --> 00:00:09,120 far we've added everything from the requirements except for the search action. 3 00:00:09,880 --> 00:00:12,910 The customer is going to tell you what kind of car they want and their budget. 4 00:00:13,270 --> 00:00:18,580 And so the search action needs to search through all the cars in the dealership and find one that suits 5 00:00:18,580 --> 00:00:19,210 the customer. 6 00:00:22,220 --> 00:00:25,370 All right, so in this lesson, we're going to add the search car action. 7 00:00:28,310 --> 00:00:30,890 We need to add a search action to the dealership class. 8 00:00:33,360 --> 00:00:37,830 So inside at a method called search, the method is going to return a string that tells the customer 9 00:00:37,830 --> 00:00:38,970 what the dealership found. 10 00:00:41,150 --> 00:00:43,790 And it needs to parameters make and a budget. 11 00:00:49,430 --> 00:00:52,520 And for now, return, sorry, we couldn't find any cars. 12 00:01:08,880 --> 00:01:10,630 Now a customer is looking for a dodge. 13 00:01:10,650 --> 00:01:14,520 Their budget is a thousand, so have the dealership perform a search for a dodge. 14 00:01:15,800 --> 00:01:17,150 Dealership, not search. 15 00:01:26,600 --> 00:01:29,540 All right, we'll print the result of the search, the return value. 16 00:01:36,150 --> 00:01:41,130 And I want to run the code and make sure everything works as it's supposed to just for testing purposes. 17 00:01:51,490 --> 00:01:54,700 All right, for now, it returns a fixed message as expected. 18 00:01:57,800 --> 00:01:59,930 And we need to add some logic into the action. 19 00:02:01,730 --> 00:02:06,860 So we're going to make a for loop that runs through every car in the dealership, we can use the autocomplete 20 00:02:06,860 --> 00:02:10,639 feature and it's going to create a fault that runs through the nearest the right, in this case, our 21 00:02:10,639 --> 00:02:11,120 field. 22 00:02:13,920 --> 00:02:18,330 And even though we don't need to in this case, I like to always did this keyword when referring to 23 00:02:18,330 --> 00:02:21,630 a field anyways for each car object, the loop runs through. 24 00:02:21,640 --> 00:02:27,960 We need to check if the make matches the one you passed in, and we need to check if the price is equal 25 00:02:27,960 --> 00:02:30,240 to or lower than the customer's budget. 26 00:02:31,990 --> 00:02:34,150 I hope this makes you think of the and operator. 27 00:02:35,620 --> 00:02:37,660 So if they stock cars. 28 00:02:42,420 --> 00:02:46,200 We'll get the make and we'll check if it's equal to the make being passed in. 29 00:02:53,400 --> 00:02:56,190 And if that same car's price. 30 00:03:05,850 --> 00:03:08,310 Is less than or equal to the person's budget. 31 00:03:14,400 --> 00:03:17,130 If that condition is true, that means we found a car for them. 32 00:03:20,240 --> 00:03:22,100 Before we had any more code, let's run it. 33 00:03:36,360 --> 00:03:43,710 Henare Code Crash's, this is great news because we get to fix our code, it says null pointer, which 34 00:03:43,710 --> 00:03:46,650 means we're trying to access or cull something from a note. 35 00:03:47,130 --> 00:03:49,920 If you read the error, it tells you exactly where this happens. 36 00:03:50,190 --> 00:03:50,910 In Maine. 37 00:03:51,150 --> 00:03:55,110 I called dealership search at line 19 of Maine. 38 00:03:57,180 --> 00:04:00,930 This runs code in the dealership class crashing at line 25. 39 00:04:04,350 --> 00:04:07,560 Now, your line numbers might be different, but here is the line that crashed. 40 00:04:09,810 --> 00:04:15,570 When I is zero or one, we can safely index the first and second objects and extract values from them. 41 00:04:22,650 --> 00:04:28,070 But when I used to we're indexing the third element, which is no, and if you try to access or call 42 00:04:28,090 --> 00:04:30,660 something from a null, the code is going to crash. 43 00:04:31,320 --> 00:04:33,540 This is what we call a null pointer exception. 44 00:04:33,900 --> 00:04:39,900 If you try accessing or dot syntax, something from a null Java is going to crash and throw an exception. 45 00:04:40,900 --> 00:04:45,620 Now, one of the objectives no, and you can't dot syntax anything from another object. 46 00:04:45,970 --> 00:04:48,340 So what we'll do is if the object is no. 47 00:05:00,830 --> 00:05:03,770 We can use continue to skip that run in the loop. 48 00:05:20,950 --> 00:05:25,750 All right, so as the loop runs, if the object is not the first condition is going to execute and skip 49 00:05:25,750 --> 00:05:29,830 that run in the loop in this case, it's going to skip the run when the index is to. 50 00:05:34,810 --> 00:05:39,040 All right, let's finish our code, if we find the car that matches what the customer wants, then this 51 00:05:39,040 --> 00:05:40,270 condition executes. 52 00:05:40,270 --> 00:05:42,640 In this case, we're going to return a message to the user. 53 00:05:45,180 --> 00:05:46,470 Will return new line. 54 00:05:46,650 --> 00:05:48,210 We found one in spot. 55 00:05:53,580 --> 00:05:56,910 I whatever the index is, when this condition turns true. 56 00:06:07,640 --> 00:06:10,070 And then we'll print the two string of the car object. 57 00:06:19,200 --> 00:06:19,920 Newline. 58 00:06:22,620 --> 00:06:23,550 Are you interested? 59 00:06:37,410 --> 00:06:39,510 I still can't find any cars for our customer. 60 00:06:39,630 --> 00:06:44,190 The customer is looking for a dodge with a budget of eight thousand, but we're only selling one dodge 61 00:06:44,190 --> 00:06:46,140 and it's worth eight thousand five hundred. 62 00:06:49,260 --> 00:06:52,470 So the customer decides to change their budget to 10000. 63 00:07:00,350 --> 00:07:04,490 And it finds the McCarren spot one nice found the MCCA. 64 00:07:07,540 --> 00:07:09,940 First, the condition is false and nothing happens. 65 00:07:18,950 --> 00:07:23,870 But once the loop gets to inducts one, the condition is true and it returns a string that tells the 66 00:07:23,870 --> 00:07:25,190 customer what we found. 67 00:07:33,070 --> 00:07:38,440 This is great, you built a dealership application that manages cells and searches for cars and you 68 00:07:38,440 --> 00:07:40,960 program the entire thing using objects. 69 00:07:45,030 --> 00:07:50,610 In this lesson, you had a desert correction, you built an entire project by planting it around objects, 70 00:07:50,610 --> 00:07:52,140 fields and actions.