1 00:00:00,390 --> 00:00:02,820 In this video, we're going to cover the solution for part one. 2 00:00:05,260 --> 00:00:10,720 Each class that models an object was placed inside the model's folder, and the main class is by itself. 3 00:00:12,630 --> 00:00:17,880 We'll start with task one, and that was to add all the necessary fields to the contact class, and 4 00:00:17,880 --> 00:00:22,140 if you were to look back at the article, the fields were private string name. 5 00:00:23,050 --> 00:00:25,150 Private string, phone number. 6 00:00:28,140 --> 00:00:30,540 Private string birthdate. 7 00:00:36,200 --> 00:00:38,350 In private and age. 8 00:00:40,020 --> 00:00:45,840 And task to reminds us that if a class has failed, it needs to apply the big three steps, the first 9 00:00:45,840 --> 00:00:47,420 step is to create a constructor. 10 00:00:47,700 --> 00:00:49,710 It's only going to receive three parameters. 11 00:00:49,710 --> 00:00:50,580 String name. 12 00:00:53,030 --> 00:00:54,260 String phone number. 13 00:00:58,930 --> 00:01:00,670 And string birthdate. 14 00:01:07,040 --> 00:01:08,350 And you might be thinking, why not? 15 00:01:08,360 --> 00:01:14,480 For while the birth date needs to automatically determine the person's age, it doesn't make sense to 16 00:01:14,480 --> 00:01:17,990 ask the user how old they are if they have already given you their date of birth. 17 00:01:18,710 --> 00:01:19,130 All right. 18 00:01:19,580 --> 00:01:21,800 The constructor runs when you create a new object. 19 00:01:21,800 --> 00:01:24,860 So we're going to set every field in that object equal to a parameter. 20 00:01:25,190 --> 00:01:27,800 The star name, the stop phone number. 21 00:01:30,920 --> 00:01:32,060 Equals phone number. 22 00:01:35,220 --> 00:01:36,990 And they start birthdates. 23 00:01:43,440 --> 00:01:46,290 OK, so how do we get the age from the birth date? 24 00:01:47,100 --> 00:01:50,330 Back in the article, I left you a lot of hints on how to achieve this. 25 00:01:50,580 --> 00:01:54,990 And the first step was to create an object of the simple date format class. 26 00:01:56,330 --> 00:01:57,560 So we'll say formatter. 27 00:02:06,480 --> 00:02:10,289 Is equal to a new object of the simple date format class. 28 00:02:14,380 --> 00:02:18,530 And inside, we need to pass the format that we expect our dates to be in. 29 00:02:19,090 --> 00:02:22,760 They're going to have the format of month, month, day, day. 30 00:02:23,200 --> 00:02:24,010 Yea yea. 31 00:02:24,010 --> 00:02:24,670 Yea, yea. 32 00:02:26,470 --> 00:02:32,110 And make sure the EMS are upper case, because that's how the formatter interprets months, your next 33 00:02:32,110 --> 00:02:37,180 instruction was the research to pass the string value intimidates, and I hope you're able to figure 34 00:02:37,180 --> 00:02:37,610 it out. 35 00:02:40,670 --> 00:02:43,520 We can call the pass method from the formatter object. 36 00:02:49,320 --> 00:02:53,940 And what that's going to do is receive a tax value and from that string, it's going to return a date 37 00:02:53,950 --> 00:02:54,600 object. 38 00:02:58,850 --> 00:03:04,190 So I'm going to pass it the birth date string, and it's going to convert that string to an actual date 39 00:03:04,190 --> 00:03:05,330 object and return it. 40 00:03:05,780 --> 00:03:09,520 So we need to store the return value inside of a variable of type dates. 41 00:03:13,410 --> 00:03:14,910 I'll call it two dates. 42 00:03:16,330 --> 00:03:18,510 Because we're converting from a string to a date. 43 00:03:21,140 --> 00:03:26,300 OK, now the pass method throws a checked exception, we know it's a checked exception because Javas 44 00:03:26,300 --> 00:03:27,440 forcing us to catch it. 45 00:03:29,210 --> 00:03:34,880 Do not try catch the failure inside the constructor, whoever is calling the constructor, it's their 46 00:03:34,880 --> 00:03:36,220 job to handle the failure. 47 00:03:36,740 --> 00:03:41,270 So we're going to throw the exception from the constructor throws pass exception. 48 00:03:43,540 --> 00:03:48,490 So that whoever is calling the constructor, they're going to handle the failure, the constructor's 49 00:03:48,490 --> 00:03:51,250 only job should be to update Fields', nothing else. 50 00:03:55,120 --> 00:03:59,740 So going back in the article, the next step is to convert each date into the number of milliseconds 51 00:03:59,740 --> 00:04:00,880 since 1970. 52 00:04:02,580 --> 00:04:08,280 We can go to the documentation for the date class and look for a method that does this, as I scroll 53 00:04:08,280 --> 00:04:10,500 down, you'll see that many methods are deprecated. 54 00:04:10,530 --> 00:04:11,520 That's pretty crazy. 55 00:04:12,030 --> 00:04:18,510 But interesting enough, this one isn't it returns the number of milliseconds since January 1970 up 56 00:04:18,510 --> 00:04:22,280 until the specified date, and it returns it as a log. 57 00:04:22,290 --> 00:04:23,130 That's perfect. 58 00:04:31,320 --> 00:04:33,870 So I'll have to say long to Milli. 59 00:04:36,010 --> 00:04:39,730 Is equal to to date and from we're going to get the time. 60 00:04:42,020 --> 00:04:45,320 So this line converts the string to an actual date. 61 00:04:49,210 --> 00:04:53,770 And this line converts the date to the number of milliseconds since 1970. 62 00:05:00,800 --> 00:05:05,690 The last thing I want to do is get the current date and convert it to the number of milliseconds since 63 00:05:05,690 --> 00:05:11,900 January 1970, then I can get the difference between the two in milliseconds, which is basically going 64 00:05:11,900 --> 00:05:17,630 to be the person's age in milliseconds, and then we can easily convert from milliseconds to years. 65 00:05:20,670 --> 00:05:22,140 So we'll say long def. 66 00:05:24,870 --> 00:05:30,870 We can get today's date by just saying new dates, we're going to convert the current date to the number 67 00:05:30,870 --> 00:05:36,930 of milliseconds since 1970 and from today's date, we can subtract the birth date. 68 00:05:39,500 --> 00:05:44,690 So this is the number of milliseconds from 1970 until the person's birth, and this is the number of 69 00:05:44,690 --> 00:05:47,020 milliseconds from 1970 until today. 70 00:05:47,360 --> 00:05:50,630 So death is the person's age, but in milliseconds. 71 00:05:55,680 --> 00:05:59,380 But before we can continue, I did all of this in separate steps to walk you through it. 72 00:05:59,610 --> 00:06:00,840 I want to make the code shorter. 73 00:06:03,240 --> 00:06:04,650 So I'm going to add this right here. 74 00:06:10,320 --> 00:06:14,820 And now with just two lines, we're getting the person's age in milliseconds and the next instruction 75 00:06:15,060 --> 00:06:19,540 was free to research how to use the time unit class to convert from milliseconds. 76 00:06:19,740 --> 00:06:20,520 Today's. 77 00:06:25,950 --> 00:06:28,500 Your research would have a junior right time unit. 78 00:06:33,720 --> 00:06:39,720 Milliseconds, two days, and what that's going to do is take a value in milliseconds, in this case 79 00:06:39,720 --> 00:06:42,690 death, and convert it to days. 80 00:06:43,620 --> 00:06:45,930 So now we're going to get the person's age in days. 81 00:06:47,100 --> 00:06:52,590 And then if we divide the number of days by three hundred and sixty five, we'll get their age in years. 82 00:06:57,980 --> 00:07:01,500 But the age is an integer and the two days method returns along. 83 00:07:01,730 --> 00:07:04,640 So the final step is to typecast this long to. 84 00:07:15,220 --> 00:07:20,500 All right, we can finally test our code at the very top, you'll notice the line package models basically 85 00:07:20,500 --> 00:07:23,830 tells us Java, hey, the contact class is inside of a separate folder. 86 00:07:24,460 --> 00:07:26,030 So now inside main Java. 87 00:07:26,260 --> 00:07:28,720 We can import the class from the models folder. 88 00:07:34,300 --> 00:07:37,960 And we can also auto complete imports and TVs code just half way I. 89 00:07:45,020 --> 00:07:49,160 And now inside the main method, we can create a new object of the contact class. 90 00:07:50,310 --> 00:07:51,960 His name will be Alan Turing. 91 00:07:57,980 --> 00:08:00,620 With the following phone number, just some random number. 92 00:08:06,130 --> 00:08:07,510 And their birthdates. 93 00:08:12,920 --> 00:08:17,300 OK, I'm also going to print the object, even though we don't have a to string, I'll just leave it 94 00:08:17,300 --> 00:08:17,930 here for later. 95 00:08:19,240 --> 00:08:25,300 And here we get an error that's because of the contact constructor can throw a pass exception pass exception 96 00:08:25,300 --> 00:08:26,470 is a checked exception. 97 00:08:26,680 --> 00:08:30,820 And if you want to call this constructor, Jebba forces you to try to run the code. 98 00:08:35,289 --> 00:08:38,919 And if the code happens to fail, you need to catch the pass exception. 99 00:08:45,500 --> 00:08:47,710 And we'll just print the message that comes out of it. 100 00:08:54,290 --> 00:08:56,570 Then we're going to add a finally block, that prince. 101 00:08:59,960 --> 00:09:04,880 Process completes finally is always the last thing that runs regardless of the results. 102 00:09:07,140 --> 00:09:10,260 All right, now I'm going to go to break point and step into the constructor. 103 00:09:18,880 --> 00:09:21,100 Here we can see the parameters that were passed in. 104 00:09:22,140 --> 00:09:28,050 And this points to the current object that we just created, the stock names, that's the name field 105 00:09:28,050 --> 00:09:32,970 of the current object, equal to the parameter, the phone number updates, the phone number field, 106 00:09:33,420 --> 00:09:36,090 they stop birth date updates, the birth date field. 107 00:09:36,540 --> 00:09:39,330 And we're just going to step over the formatter object. 108 00:09:39,840 --> 00:09:42,120 I'm going to step over these function calls. 109 00:09:42,960 --> 00:09:45,240 This is Alan Turing's age in milliseconds. 110 00:09:45,810 --> 00:09:50,210 And in this last line, it's going to convert the age to year and update the field. 111 00:09:50,820 --> 00:09:54,450 And just like that, we created an object of the contact class. 112 00:09:55,700 --> 00:10:00,290 And if you keep stepping over the contact variable stores, a reference to the object we just created. 113 00:10:04,030 --> 00:10:07,810 And finally, it's the last thing that runs, regardless of the result in this case, we know that the 114 00:10:07,810 --> 00:10:09,010 process is complete. 115 00:10:13,610 --> 00:10:16,660 Now, what happens if I change them month, something that doesn't make any sense? 116 00:10:18,090 --> 00:10:22,530 I would expect the parties function to not be able to pass the state and throw an exception. 117 00:10:23,280 --> 00:10:24,780 I'm going to relaunch the debugger. 118 00:10:38,940 --> 00:10:43,990 But instead, it falsely interprets the input, it doesn't make sense for the H to be negative. 119 00:10:44,010 --> 00:10:44,730 That's not good. 120 00:10:50,280 --> 00:10:55,470 The past method should have thrown an exception, it should not have been able to pass the state and 121 00:10:55,470 --> 00:11:00,450 the task was free to research how to stop the formatter from being lenient when it's passing the string, 122 00:11:01,230 --> 00:11:04,830 the string that we pass and needs to strictly adhere to the format. 123 00:11:04,830 --> 00:11:06,570 Month, month, day, day. 124 00:11:06,630 --> 00:11:08,850 Yea yea yea yea that we specified. 125 00:11:10,310 --> 00:11:15,080 I told you to look inside the documentation for a method that specifies whether the date passing should 126 00:11:15,080 --> 00:11:20,330 be lenient, and I hope you are able to find the method lenient, which expects a boolean. 127 00:11:27,970 --> 00:11:31,480 And if we go back to our code, we're going to set the leniency to false. 128 00:11:42,430 --> 00:11:47,770 And now I expect that it's going to strictly adhere to the format we Pastan and it's not going to tolerate 129 00:11:47,770 --> 00:11:48,670 anything else. 130 00:11:51,590 --> 00:11:52,940 Relaunching the debugger. 131 00:11:58,340 --> 00:12:01,790 I'm going to keep stepping into the constructor until we reach the pass function. 132 00:12:08,720 --> 00:12:11,020 Which is going to fail and throw a pass exception. 133 00:12:11,940 --> 00:12:14,850 Here we catch the checked exception and print its message. 134 00:12:17,850 --> 00:12:23,520 In this case, it says that the date was unpassable, as always, finally is the last thing that runs 135 00:12:23,520 --> 00:12:27,310 and whether the process succeeds or fails, we know that it's complete. 136 00:12:27,780 --> 00:12:28,920 That's all for part one.