The dealership clerk should have a two string method as well.
I showed you that two string makes it easy to print every field and the current object.
When you had to string to your class, Java internally calls it when you're printing the object.
When we print the dealership, it would be nice to display every car in the dealership.
So in this lesson, you're going to have to string method to the dealership class.
Like before, the two string method is going to be public and return to string value.
It's really important that you follow the signature exactly so that Gever recognizes it as the two string
method that it needs to call.
And this method is going to return a strong representation of every car and the dealership.
So first I'm going to set a temporary variable equal to an empty string and I'll return it's.
Then a for loop is going to run through the entire length of the car's array.
You can choose to put this or not.
It doesn't matter, Javin knows that you're referring to the field cars because it's the only thing
within scope.
But you know me, I like to always include this keyword when referring to a field.
And during each run, we're going to update the string to describe every parking spot, so I'm going
to set temp equal to itself, plus the words parking spot.
Followed by the current index that we're running through.
As well as a new line for aesthetic purposes.
Ultimately, this group is going to run three times and each time it's going to append the current parking
spot that we're running through.
For now, I'm going to test this code by printing the dealership object.
And as expected, printing an object automatically calls its two string method, in this case the two
string method for dealership runs and returns a string that describes every parking spot.
And now remember that each car object also has a two string method as well that describes all of its
fields'.
So during each run, right after we describe the current spot, we're going to add the two string value
of the current car that we're running through.
So we're going to update the string variable to include the two string description of each car object.
Once again, I'm also going to add a new line for aesthetic purposes.
So we should expect that during each run, the temp variable is going to update with the parking spot
and the two string description of the car parked at that spot.
It's going to repeat this three times until I restring.
Variable describes every parking spot and the car that's parked in it.
All right, let's run the code.
And we get a no, a exception, but remember that the third object in the car is array is No.
The for loop effectively grabs the string representation of the first and second objects.
However, the third spot in the area is no, and you can't access or call anything from Intel, Java
is going to crash.
So inside the loop, we need to check if an object is null before attempting to use its two string,
if the current element that the loop is running.
There was no.
Then we're going to update the string to say empty for that parking spot.
Otherwise, if there is an object at that element, we can append its two string.
That's the run your code.
And this is great, the first two elements aren't empty, but the one in index two is.
By printing the object, Jova calls the two string method, which returns a string that effectively
displays every car in the array.
All right, we're officially done programming the car and dealership class, there's still one little
thing we have to fix, but in the end we blueprinted every field action and we added the necessary getters,
setters and to strength.
And now we can effectively program using car and dealership objects.
All of these options are going to come together and form a beautiful application once we add user input.
So for now, let's remove all the function calls.
In this lesson, you added a two string method to the dealership class, the tutoring method for dealership
effectively returns every object in the car's array.