1 00:00:00,150 --> 00:00:05,070 This video is going to cover the solution for part five based on the requirements we can define for 2 00:00:05,070 --> 00:00:07,010 fields private. 3 00:00:07,380 --> 00:00:09,000 Long time stamp. 4 00:00:10,730 --> 00:00:18,530 Private string ID, by the way, the reason this is a long is because time stamp values can get really 5 00:00:18,530 --> 00:00:23,630 large and then we'll say private double amounts. 6 00:00:29,690 --> 00:00:34,820 And now the type field can be one of two values, withdraw and deposit. 7 00:00:35,150 --> 00:00:39,110 So what I'm going to do is create an enum named type. 8 00:00:42,640 --> 00:00:46,570 With two constants withdraw and deposit. 9 00:00:49,700 --> 00:00:51,530 And Harold, declare a private field. 10 00:00:53,850 --> 00:00:56,760 Of type, type, named type. 11 00:00:59,620 --> 00:01:04,420 Now, a lot of constructor public transaction they receives for parameters. 12 00:01:05,200 --> 00:01:07,480 On second thought we can just auto generate this. 13 00:01:14,060 --> 00:01:14,810 All right. 14 00:01:16,040 --> 00:01:18,650 We'll add a copy constructor by copying this one. 15 00:01:21,530 --> 00:01:26,450 And instead of receiving values from four different parameters, we're going to receive values from 16 00:01:26,450 --> 00:01:29,480 a source object transaction source. 17 00:01:30,420 --> 00:01:38,550 This duct tape equals sauce, duct tape, source timestamp, source that I.D. and sauce that amounts. 18 00:01:41,200 --> 00:01:44,740 OK, now we're going to auto generate the getters and setters. 19 00:01:46,000 --> 00:01:46,660 Pretty easy. 20 00:01:47,990 --> 00:01:51,960 And now back on learn the parts you go here. 21 00:01:53,950 --> 00:01:58,000 Task number six was to convert the time stem field into a debt. 22 00:01:58,580 --> 00:02:01,870 Accordingly, if we scroll back to the requirements. 23 00:02:07,540 --> 00:02:11,980 Remember, you should always write unit tests for logic that is considered meaningful. 24 00:02:12,400 --> 00:02:18,070 So inside of the test folder, we're going to create a file named transaction. 25 00:02:19,280 --> 00:02:19,970 Tests. 26 00:02:23,800 --> 00:02:26,740 And I believe there was something on there in the part we had to import. 27 00:02:26,770 --> 00:02:27,820 Let me just have a look. 28 00:02:28,390 --> 00:02:32,290 And indeed, we did will import the following snippet. 29 00:02:35,450 --> 00:02:37,850 We'll use autocomplete to get rid of these errors. 30 00:02:42,160 --> 00:02:42,440 OK. 31 00:02:42,460 --> 00:02:43,030 We should be good. 32 00:02:45,120 --> 00:02:52,140 OK, now what we want to do is unit test our capability of converting this long value into a date. 33 00:02:52,590 --> 00:02:53,970 So I'm going to create a unit test. 34 00:02:59,440 --> 00:03:00,280 Public void. 35 00:03:01,640 --> 00:03:02,870 Correct date test. 36 00:03:08,550 --> 00:03:15,650 And now, if we go to any Web site that converts long values into a date, we can convert this one time 37 00:03:15,690 --> 00:03:21,000 stamp to human dates and we get back January 7th, 2019. 38 00:03:21,630 --> 00:03:25,350 So that means that we need to assert assert equals. 39 00:03:27,540 --> 00:03:30,090 We're expecting a value of, oh, seven. 40 00:03:31,240 --> 00:03:32,870 01 2019. 41 00:03:42,810 --> 00:03:47,220 When we call transaction date return dates. 42 00:03:50,870 --> 00:03:55,900 A method that does not exist yet, so back here inside of transaction Java. 43 00:03:56,300 --> 00:03:58,250 We're going to write code to make the test fail. 44 00:03:58,700 --> 00:04:02,540 So I'm going to write public string return dates. 45 00:04:04,570 --> 00:04:05,920 For now, just return high. 46 00:04:08,930 --> 00:04:10,640 And if you go back here. 47 00:04:14,480 --> 00:04:17,709 Figured the test fails now we just got to write code to make it pass. 48 00:04:17,980 --> 00:04:22,270 I left you a lot of hands on during the part, so if you were to look at the documentation, the date 49 00:04:22,270 --> 00:04:30,940 constructor returns a date based on the number of milliseconds since 1970, but our long value represents 50 00:04:30,940 --> 00:04:36,730 the number of seconds since 1970, so all we got to do is say. 51 00:04:38,600 --> 00:04:42,620 Dates date will create a new object of the debt class. 52 00:04:46,310 --> 00:04:51,050 For now will pass in the time stamp, which is the number of seconds since 1970. 53 00:04:51,350 --> 00:04:54,680 But remember, it's expecting the number of milliseconds. 54 00:04:55,010 --> 00:04:57,650 So what we're going to have to do is multiply by a thousand. 55 00:04:59,400 --> 00:05:04,440 So now we have our data object, we just got to format it into a string and we've already done this 56 00:05:04,440 --> 00:05:05,700 like two sections ago. 57 00:05:05,940 --> 00:05:11,790 So I'm going to skip the explanation and we'll go ahead and just create a new object of the simple date 58 00:05:11,800 --> 00:05:12,780 format class. 59 00:05:14,670 --> 00:05:21,210 And we're going to format our date to be in the form day, month, year. 60 00:05:22,680 --> 00:05:27,060 And using this format, we can format our date into a string. 61 00:05:28,550 --> 00:05:29,210 Perfect. 62 00:05:29,650 --> 00:05:31,070 Let's run our unit test. 63 00:05:32,550 --> 00:05:37,050 Go back to transactional tests, run the test. 64 00:05:39,560 --> 00:05:40,970 And we get an error. 65 00:05:42,030 --> 00:05:42,840 Expected. 66 00:05:43,910 --> 00:05:47,240 Negative 2013, but was. 67 00:05:48,630 --> 00:05:54,990 Oh, seven, oh, this is seven minus the one minus 2019. 68 00:05:55,320 --> 00:05:56,490 This should be a string. 69 00:05:57,670 --> 00:05:58,870 Well, we run our tests. 70 00:06:01,610 --> 00:06:02,240 And we're good. 71 00:06:05,130 --> 00:06:11,220 OK, now Task nine was to override equals and hash code, you can just generate them. 72 00:06:14,240 --> 00:06:15,470 Calls and hash code. 73 00:06:15,560 --> 00:06:16,130 OK. 74 00:06:18,300 --> 00:06:20,280 Import the object class. 75 00:06:25,140 --> 00:06:30,510 And now we want to make sure our objects are sortable from lowest time stamp to highest timestamp. 76 00:06:30,870 --> 00:06:34,380 And in order to sort our objects, they need to be comparable. 77 00:06:34,770 --> 00:06:41,850 That is, every transaction needs to implement the comparable interface implements comparable. 78 00:06:45,960 --> 00:06:51,930 And because we implemented the interface, we're signing a contract of behavior that forces us to override 79 00:06:51,930 --> 00:06:54,090 every method inside this interface. 80 00:06:54,540 --> 00:06:58,080 There is only one method, you know it very well, it's compared to. 81 00:07:03,880 --> 00:07:09,760 Compared to expect an integer, that integer is the result from comparing double dot compare. 82 00:07:10,670 --> 00:07:17,090 From comparing the timestamp of the current object, this timestamp against the timestamp of the one 83 00:07:17,090 --> 00:07:17,960 being passed in. 84 00:07:20,790 --> 00:07:26,520 This is going to be useful once we have hundreds of transactions inside of an array, the array is going 85 00:07:26,520 --> 00:07:32,970 to use this method to compare it to transaction objects at a time and based on each comparison, it's 86 00:07:32,970 --> 00:07:36,740 just going to know how to sort from lowest to highest anyways. 87 00:07:36,810 --> 00:07:39,120 Final task is to override to string. 88 00:07:39,840 --> 00:07:41,370 So we'll just generate it. 89 00:07:44,010 --> 00:07:46,480 But we're not going to use this format. 90 00:07:47,480 --> 00:07:49,520 Just copy the one from lowering the parts. 91 00:07:51,450 --> 00:07:52,470 Copy this over. 92 00:07:57,390 --> 00:07:59,490 And here will say. 93 00:08:00,750 --> 00:08:03,660 Return dates that start return date. 94 00:08:04,370 --> 00:08:10,440 OK, the final step is to always apply quality control in case the data being entered is faulty. 95 00:08:10,470 --> 00:08:11,850 You just never know. 96 00:08:13,060 --> 00:08:14,830 There's three hundred rows. 97 00:08:15,010 --> 00:08:16,840 One of the lines could be bad. 98 00:08:17,290 --> 00:08:22,750 We want to throw an unchecked exception, which basically forces the caller to fix their code and anticipate 99 00:08:22,750 --> 00:08:27,500 such a scenario instead of just passing it in that way, they're not sending over bad data. 100 00:08:28,840 --> 00:08:29,860 So we'll go back here. 101 00:08:31,700 --> 00:08:39,980 Now, time stamp is always valid because positive timestamp means seconds since 1970, and a negative 102 00:08:39,980 --> 00:08:43,190 timestamp is the time that comes before 1970. 103 00:08:43,490 --> 00:08:44,270 So that's fine. 104 00:08:44,270 --> 00:08:49,010 No matter what value we get, it shouldn't be null or blank. 105 00:08:49,010 --> 00:08:57,170 So our quality control that will say if ID is equal to known or indeed is blank. 106 00:08:59,250 --> 00:09:04,830 And also, the amount can't be negative, because the type, whether it's withdrawal or the opposite, 107 00:09:05,160 --> 00:09:09,600 it's going to determine whether we subtract or reduce the amount from the account balance. 108 00:09:09,900 --> 00:09:11,730 So we need to quality control that as well. 109 00:09:12,520 --> 00:09:14,040 Amount cannot be negative. 110 00:09:18,140 --> 00:09:22,250 In such a case, we need to throw a new illegal argument exception. 111 00:09:23,800 --> 00:09:25,450 Invalid params. 112 00:09:28,140 --> 00:09:32,490 We'll do the same thing inside the centres inside of it. 113 00:09:33,060 --> 00:09:34,410 We're going to make sure that the ID. 114 00:09:36,340 --> 00:09:37,360 It's not equal to no. 115 00:09:39,230 --> 00:09:40,190 Or if it's blank. 116 00:09:41,420 --> 00:09:45,680 In that case, we want to throw a new illegal argument exception. 117 00:09:48,100 --> 00:09:49,950 Invalid I.D.. 118 00:09:52,790 --> 00:09:59,480 And we'll do the same thing inside of amounts, if amount is smaller than zero. 119 00:10:01,020 --> 00:10:01,830 There are new. 120 00:10:03,640 --> 00:10:06,310 A legal argument exception. 121 00:10:08,720 --> 00:10:10,520 Invalid amount. 122 00:10:12,180 --> 00:10:12,620 That's it. 123 00:10:12,840 --> 00:10:14,460 Moving on to part six.