Now we can add the second action, searching a car, we're almost done building that dealership up so
far we've added everything from the requirements except for the search action.
The customer is going to tell you what kind of car they want and their budget.
And so the search action needs to search through all the cars in the dealership and find one that suits
the customer.
All right, so in this lesson, we're going to add the search car action.
We need to add a search action to the dealership class.
So inside at a method called search, the method is going to return a string that tells the customer
what the dealership found.
And it needs to parameters make and a budget.
And for now, return, sorry, we couldn't find any cars.
Now a customer is looking for a dodge.
Their budget is a thousand, so have the dealership perform a search for a dodge.
Dealership, not search.
All right, we'll print the result of the search, the return value.
And I want to run the code and make sure everything works as it's supposed to just for testing purposes.
All right, for now, it returns a fixed message as expected.
And we need to add some logic into the action.
So we're going to make a for loop that runs through every car in the dealership, we can use the autocomplete
feature and it's going to create a fault that runs through the nearest the right, in this case, our
field.
And even though we don't need to in this case, I like to always did this keyword when referring to
a field anyways for each car object, the loop runs through.
We need to check if the make matches the one you passed in, and we need to check if the price is equal
to or lower than the customer's budget.
I hope this makes you think of the and operator.
So if they stock cars.
We'll get the make and we'll check if it's equal to the make being passed in.
And if that same car's price.
Is less than or equal to the person's budget.
If that condition is true, that means we found a car for them.
Before we had any more code, let's run it.
Henare Code Crash's, this is great news because we get to fix our code, it says null pointer, which
means we're trying to access or cull something from a note.
If you read the error, it tells you exactly where this happens.
In Maine.
I called dealership search at line 19 of Maine.
This runs code in the dealership class crashing at line 25.
Now, your line numbers might be different, but here is the line that crashed.
When I is zero or one, we can safely index the first and second objects and extract values from them.
But when I used to we're indexing the third element, which is no, and if you try to access or call
something from a null, the code is going to crash.
This is what we call a null pointer exception.
If you try accessing or dot syntax, something from a null Java is going to crash and throw an exception.
Now, one of the objectives no, and you can't dot syntax anything from another object.
So what we'll do is if the object is no.
We can use continue to skip that run in the loop.
All right, so as the loop runs, if the object is not the first condition is going to execute and skip
that run in the loop in this case, it's going to skip the run when the index is to.
All right, let's finish our code, if we find the car that matches what the customer wants, then this
condition executes.
In this case, we're going to return a message to the user.
Will return new line.
We found one in spot.
I whatever the index is, when this condition turns true.
And then we'll print the two string of the car object.
Newline.
Are you interested?
I still can't find any cars for our customer.
The customer is looking for a dodge with a budget of eight thousand, but we're only selling one dodge
and it's worth eight thousand five hundred.
So the customer decides to change their budget to 10000.
And it finds the McCarren spot one nice found the MCCA.
First, the condition is false and nothing happens.
But once the loop gets to inducts one, the condition is true and it returns a string that tells the
customer what we found.
This is great, you built a dealership application that manages cells and searches for cars and you
program the entire thing using objects.
In this lesson, you had a desert correction, you built an entire project by planting it around objects,
fields and actions.