In this video, we're going to cover the solution for part one.
Each class that models an object was placed inside the model's folder, and the main class is by itself.
We'll start with task one, and that was to add all the necessary fields to the contact class, and
if you were to look back at the article, the fields were private string name.
Private string, phone number.
Private string birthdate.
In private and age.
And task to reminds us that if a class has failed, it needs to apply the big three steps, the first
step is to create a constructor.
It's only going to receive three parameters.
String name.
String phone number.
And string birthdate.
And you might be thinking, why not?
For while the birth date needs to automatically determine the person's age, it doesn't make sense to
ask the user how old they are if they have already given you their date of birth.
All right.
The constructor runs when you create a new object.
So we're going to set every field in that object equal to a parameter.
The star name, the stop phone number.
Equals phone number.
And they start birthdates.
OK, so how do we get the age from the birth date?
Back in the article, I left you a lot of hints on how to achieve this.
And the first step was to create an object of the simple date format class.
So we'll say formatter.
Is equal to a new object of the simple date format class.
And inside, we need to pass the format that we expect our dates to be in.
They're going to have the format of month, month, day, day.
Yea yea.
Yea, yea.
And make sure the EMS are upper case, because that's how the formatter interprets months, your next
instruction was the research to pass the string value intimidates, and I hope you're able to figure
it out.
We can call the pass method from the formatter object.
And what that's going to do is receive a tax value and from that string, it's going to return a date
object.
So I'm going to pass it the birth date string, and it's going to convert that string to an actual date
object and return it.
So we need to store the return value inside of a variable of type dates.
I'll call it two dates.
Because we're converting from a string to a date.
OK, now the pass method throws a checked exception, we know it's a checked exception because Javas
forcing us to catch it.
Do not try catch the failure inside the constructor, whoever is calling the constructor, it's their
job to handle the failure.
So we're going to throw the exception from the constructor throws pass exception.
So that whoever is calling the constructor, they're going to handle the failure, the constructor's
only job should be to update Fields', nothing else.
So going back in the article, the next step is to convert each date into the number of milliseconds
since 1970.
We can go to the documentation for the date class and look for a method that does this, as I scroll
down, you'll see that many methods are deprecated.
That's pretty crazy.
But interesting enough, this one isn't it returns the number of milliseconds since January 1970 up
until the specified date, and it returns it as a log.
That's perfect.
So I'll have to say long to Milli.
Is equal to to date and from we're going to get the time.
So this line converts the string to an actual date.
And this line converts the date to the number of milliseconds since 1970.
The last thing I want to do is get the current date and convert it to the number of milliseconds since
January 1970, then I can get the difference between the two in milliseconds, which is basically going
to be the person's age in milliseconds, and then we can easily convert from milliseconds to years.
So we'll say long def.
We can get today's date by just saying new dates, we're going to convert the current date to the number
of milliseconds since 1970 and from today's date, we can subtract the birth date.
So this is the number of milliseconds from 1970 until the person's birth, and this is the number of
milliseconds from 1970 until today.
So death is the person's age, but in milliseconds.
But before we can continue, I did all of this in separate steps to walk you through it.
I want to make the code shorter.
So I'm going to add this right here.
And now with just two lines, we're getting the person's age in milliseconds and the next instruction
was free to research how to use the time unit class to convert from milliseconds.
Today's.
Your research would have a junior right time unit.
Milliseconds, two days, and what that's going to do is take a value in milliseconds, in this case
death, and convert it to days.
So now we're going to get the person's age in days.
And then if we divide the number of days by three hundred and sixty five, we'll get their age in years.
But the age is an integer and the two days method returns along.
So the final step is to typecast this long to.
All right, we can finally test our code at the very top, you'll notice the line package models basically
tells us Java, hey, the contact class is inside of a separate folder.
So now inside main Java.
We can import the class from the models folder.
And we can also auto complete imports and TVs code just half way I.
And now inside the main method, we can create a new object of the contact class.
His name will be Alan Turing.
With the following phone number, just some random number.
And their birthdates.
OK, I'm also going to print the object, even though we don't have a to string, I'll just leave it
here for later.
And here we get an error that's because of the contact constructor can throw a pass exception pass exception
is a checked exception.
And if you want to call this constructor, Jebba forces you to try to run the code.
And if the code happens to fail, you need to catch the pass exception.
And we'll just print the message that comes out of it.
Then we're going to add a finally block, that prince.
Process completes finally is always the last thing that runs regardless of the results.
All right, now I'm going to go to break point and step into the constructor.
Here we can see the parameters that were passed in.
And this points to the current object that we just created, the stock names, that's the name field
of the current object, equal to the parameter, the phone number updates, the phone number field,
they stop birth date updates, the birth date field.
And we're just going to step over the formatter object.
I'm going to step over these function calls.
This is Alan Turing's age in milliseconds.
And in this last line, it's going to convert the age to year and update the field.
And just like that, we created an object of the contact class.
And if you keep stepping over the contact variable stores, a reference to the object we just created.
And finally, it's the last thing that runs, regardless of the result in this case, we know that the
process is complete.
Now, what happens if I change them month, something that doesn't make any sense?
I would expect the parties function to not be able to pass the state and throw an exception.
I'm going to relaunch the debugger.
But instead, it falsely interprets the input, it doesn't make sense for the H to be negative.
That's not good.
The past method should have thrown an exception, it should not have been able to pass the state and
the task was free to research how to stop the formatter from being lenient when it's passing the string,
the string that we pass and needs to strictly adhere to the format.
Month, month, day, day.
Yea yea yea yea that we specified.
I told you to look inside the documentation for a method that specifies whether the date passing should
be lenient, and I hope you are able to find the method lenient, which expects a boolean.
And if we go back to our code, we're going to set the leniency to false.
And now I expect that it's going to strictly adhere to the format we Pastan and it's not going to tolerate
anything else.
Relaunching the debugger.
I'm going to keep stepping into the constructor until we reach the pass function.
Which is going to fail and throw a pass exception.
Here we catch the checked exception and print its message.
In this case, it says that the date was unpassable, as always, finally is the last thing that runs
and whether the process succeeds or fails, we know that it's complete.
That's all for part one.