1 00:00:00,330 --> 00:00:04,710 The goal of this workbook was to quality control the item in machine classes, because if you're on 2 00:00:04,710 --> 00:00:05,700 the out the way it is. 3 00:00:11,990 --> 00:00:16,170 Some of the items have negative prices, negative quantities and blank names. 4 00:00:16,730 --> 00:00:21,380 Once again, these values don't make any sense and the caller should be forbidden from passing them. 5 00:00:25,060 --> 00:00:28,780 So when the item constructor, we're going to start by throwing an illegal argument exception, if the 6 00:00:28,780 --> 00:00:31,510 name is blank, so if name is equal to null. 7 00:00:34,550 --> 00:00:36,020 Or if the name is blank. 8 00:00:41,840 --> 00:00:42,710 Throw new. 9 00:00:44,030 --> 00:00:48,380 A legal argument, exception name cannot be null or blank. 10 00:00:53,520 --> 00:00:59,040 We're going to throw another exception if the price is less than zero, so if the price is less than 11 00:00:59,040 --> 00:01:02,400 zero, there are a new illegal argument exception. 12 00:01:09,040 --> 00:01:11,170 Price cannot be less than zero. 13 00:01:16,770 --> 00:01:20,760 We need to throw another exception if the quantity is less than zero. 14 00:01:26,720 --> 00:01:32,990 In which case will once again throw a new illegal argument, exception quantity cannot be less than 15 00:01:32,990 --> 00:01:33,530 zero. 16 00:01:43,890 --> 00:01:45,210 Katri around the code. 17 00:01:52,120 --> 00:01:55,150 The constructor forbids the caller from passing a legal values. 18 00:01:55,180 --> 00:01:55,780 That's good. 19 00:01:56,260 --> 00:01:58,900 This unchecked exception is forcing us to fix our code. 20 00:01:58,900 --> 00:02:01,300 So we're going to copy over the values from the workbook. 21 00:02:14,340 --> 00:02:15,330 Rerunning, running the app. 22 00:02:19,510 --> 00:02:20,140 And now we're good. 23 00:02:25,820 --> 00:02:30,740 Remember that centers are another place where the color may pass on illegal values, so we need to apply 24 00:02:30,740 --> 00:02:32,140 quality control there as well. 25 00:02:32,900 --> 00:02:37,220 So inside set name right through another illegal argument, exception, if the caller passes a name 26 00:02:37,220 --> 00:02:39,350 that is no or a blank. 27 00:02:44,800 --> 00:02:49,540 There are new legal argument, exception name cannot be null or blank. 28 00:02:59,550 --> 00:03:05,640 We'll do the same thing and set price if the culture passes a price that is less than zero, we'll throw 29 00:03:05,640 --> 00:03:06,600 a new illegal. 30 00:03:06,600 --> 00:03:07,500 Are you an exception? 31 00:03:11,840 --> 00:03:13,670 Price cannot be less than zero. 32 00:03:20,000 --> 00:03:27,350 Last one quantity, if they pass a quantity that is less than zero, throw a new illegal argument, 33 00:03:27,350 --> 00:03:30,680 exception quantity cannot be less than zero. 34 00:03:42,290 --> 00:03:47,150 Kate, now in the machine class, there is no need to throw a legal argument, exceptions in the constructor 35 00:03:47,480 --> 00:03:52,280 again, worst thing that could happen is a null being passed, but the output already throw a null pointer 36 00:03:52,280 --> 00:03:55,160 exception and the same can be said for set item. 37 00:03:58,140 --> 00:04:00,610 But what if the caller passes an invalid index here? 38 00:04:01,080 --> 00:04:05,430 Well, once again, the code would already throw an array index out of bounds exception. 39 00:04:05,850 --> 00:04:07,740 Same thing can be said about the center. 40 00:04:07,740 --> 00:04:10,740 So throwing another exception wouldn't make any sense. 41 00:04:11,640 --> 00:04:14,680 However, we can expect an illegal argument right here. 42 00:04:14,730 --> 00:04:18,149 There's a chance the caller may pass on an item with a quantity of zero. 43 00:04:18,149 --> 00:04:21,450 And in such a case, we need to index the item that they're interested in. 44 00:04:30,380 --> 00:04:32,800 And check if it's quantity is equal to zero. 45 00:04:38,670 --> 00:04:42,180 If that's the case, we need to throw an illegal argument exception. 46 00:04:49,240 --> 00:04:53,080 But says cannot dispense an item with a quantity of zero. 47 00:04:56,150 --> 00:04:58,230 That's all for a legal argument, exceptions. 48 00:04:58,250 --> 00:05:00,530 What about illegal state exceptions? 49 00:05:01,190 --> 00:05:05,990 Well, looking at this class, I can't really think of a scenario when a machine object can call a method 50 00:05:05,990 --> 00:05:06,680 at a bad time. 51 00:05:07,310 --> 00:05:13,190 Basically, the machine object would always be in a valid state to call any of its methods, and that's 52 00:05:13,190 --> 00:05:13,630 really it. 53 00:05:13,640 --> 00:05:18,620 You had to check points to the item in machine and classes, and every checkpoint forbids the caller 54 00:05:18,620 --> 00:05:20,810 from misusing the methods or constructor's.