1 00:00:00,330 --> 00:00:02,870 There are two more classes we need to write up in this video. 2 00:00:02,880 --> 00:00:04,050 We'll do the shopping cart. 3 00:00:04,560 --> 00:00:09,720 The shopping cart is identified by the items that it carries and the number of items can increase or 4 00:00:09,720 --> 00:00:11,300 decrease in the shopping cart. 5 00:00:11,340 --> 00:00:15,390 So right there, we know we need to make an array list that can store item objects. 6 00:00:19,700 --> 00:00:24,230 And as always, if a class has failed, you need to apply the big three steps, you need to take instructor, 7 00:00:24,230 --> 00:00:25,160 a getter and a setter. 8 00:00:25,760 --> 00:00:28,850 The instructor is public shares the same name as a class. 9 00:00:30,780 --> 00:00:35,850 And it receives no parameters because in the beginning, your card is going to be empty, we're just 10 00:00:35,850 --> 00:00:39,870 going to set the field equal to a new era list that can store item objects. 11 00:00:46,100 --> 00:00:49,190 OK, now we want to get her there, receives a one parameter. 12 00:00:55,030 --> 00:00:57,730 The index of the item object they're trying to access. 13 00:01:01,160 --> 00:01:06,140 And this letter is going to return a new copy of the item object at the request that inducts. 14 00:01:12,060 --> 00:01:15,030 Now, we went out a centre that receives two parameters. 15 00:01:18,770 --> 00:01:21,170 The index of the element you want to update. 16 00:01:22,350 --> 00:01:25,410 And an item object that we're going to update the element with. 17 00:01:27,030 --> 00:01:29,970 Then we're going to get the element at the requested index. 18 00:01:33,480 --> 00:01:37,170 And set it equal to a new copy of the item object being Pastan. 19 00:01:38,250 --> 00:01:42,650 And that's all for the big three steps, if you feel like it's getting repetitive, then that's good. 20 00:01:42,660 --> 00:01:44,030 It means you're getting the hang of it. 21 00:01:44,490 --> 00:01:45,920 Now we're going to add actions. 22 00:01:45,930 --> 00:01:48,120 One thing we can do is add an item to the card. 23 00:01:48,120 --> 00:01:53,700 So all credit boolean method named ADD it receives one parameter and item object. 24 00:01:54,970 --> 00:02:01,240 And the duck comment tells us we can add an item only if it wasn't already added, I'm going to ignore 25 00:02:01,240 --> 00:02:02,440 this condition for now. 26 00:02:02,470 --> 00:02:03,950 I'll come back to it at the end. 27 00:02:04,450 --> 00:02:07,390 For now, I'm just going to add the item to the items list. 28 00:02:16,130 --> 00:02:17,030 And return true. 29 00:02:24,010 --> 00:02:27,310 The next action that it can perform is to remove an item from a cart. 30 00:02:27,520 --> 00:02:33,310 This method is void called remove, and it receives one parameter, the name of the item you want to 31 00:02:33,310 --> 00:02:33,730 remove. 32 00:02:36,520 --> 00:02:40,780 So what we'll do is create a far loop that runs through the size of the array list. 33 00:02:49,540 --> 00:02:53,560 And inside the loop, we're going to get the item object at every index I. 34 00:02:56,840 --> 00:03:01,790 We'll get the name field of that item and we'll check if it equals the name of the item, though, as 35 00:03:01,790 --> 00:03:02,360 Pastan. 36 00:03:04,100 --> 00:03:07,820 And if that's the case, then we're going to remove that item from the array list. 37 00:03:17,990 --> 00:03:22,550 The last method is check out this one return to string that resembles a receipt. 38 00:03:33,240 --> 00:03:38,280 And the receipt needs to include three things, the subtotal, which means the price before tax, the 39 00:03:38,280 --> 00:03:40,670 tax they have to pay and the total. 40 00:03:41,370 --> 00:03:46,110 And instead of creating three separate variables, I'd rather create an array with ALENKA three. 41 00:03:50,610 --> 00:03:54,990 And so the idea is that the first element is going to be the subtotal, the second element, the tax 42 00:03:54,990 --> 00:03:57,000 and the third element is going to be the total. 43 00:04:05,410 --> 00:04:11,020 So during each run, I'm going to update the first element, the subtotal and add to the price of the 44 00:04:11,020 --> 00:04:12,390 item inside the cart. 45 00:04:27,890 --> 00:04:33,820 So that should give us the price of every item before tax, the tax rate is 13 percent. 46 00:04:33,830 --> 00:04:36,920 So we'll set the second element equal to the first one. 47 00:04:43,110 --> 00:04:44,400 Times point one three. 48 00:04:49,140 --> 00:04:54,720 And now the total is equal to the subtotal plus the tax, so we'll set the third element equal to the 49 00:04:54,720 --> 00:04:56,400 subtotal plus tax. 50 00:05:01,350 --> 00:05:07,560 All right, now we have the total subtotal end tax so we can return a receipt, just copy over the format 51 00:05:07,560 --> 00:05:08,340 from the article. 52 00:05:19,460 --> 00:05:21,920 And replace the placeholder is where we need to. 53 00:05:35,500 --> 00:05:40,930 Kate, now our final task before we can test our code is add it to string method that returns a nicely 54 00:05:40,930 --> 00:05:43,500 formatted string of every item inside the cart. 55 00:05:43,960 --> 00:05:46,660 As always, public string to string. 56 00:05:51,210 --> 00:05:54,040 I'm going to create a string named temp for temporary. 57 00:05:54,570 --> 00:05:57,840 It's going to be an empty string for now and we'll just return it's. 58 00:06:02,280 --> 00:06:05,580 First, don't create a loop that runs through the size of the array list. 59 00:06:12,460 --> 00:06:17,770 And during each run, I'm going to get the two string of the item object at the index eye and I'm going 60 00:06:17,770 --> 00:06:20,440 to add the string to the temp variable that we're returning. 61 00:06:21,070 --> 00:06:25,660 And after adding the two string of an item, I'm only going to add one line to separate it from the 62 00:06:25,660 --> 00:06:27,310 next two string that gets added. 63 00:06:30,780 --> 00:06:33,860 OK, now we're going to test our code before we do anything else. 64 00:06:36,090 --> 00:06:38,880 I'll have to create a shopping cart object inside Main. 65 00:06:47,850 --> 00:06:52,830 I'm going to add four items to the shopping cart, so I'll go back to the article, I'll copy them over. 66 00:06:55,370 --> 00:06:56,810 And print the cards. 67 00:07:16,420 --> 00:07:22,690 And by printing the cart, it prints a nicely formatted string of every item inside the cart, but it 68 00:07:22,690 --> 00:07:26,280 seems we added the salary twice for our purposes. 69 00:07:26,290 --> 00:07:29,000 We only want to allow the user to put one of each item. 70 00:07:29,380 --> 00:07:31,000 We can't have any duplicates. 71 00:07:32,520 --> 00:07:38,490 Well, thankfully, every aerialist has a contained method, the method receives an object, it returns 72 00:07:38,490 --> 00:07:43,260 true if one of the elements inside the array list equals the object they were passing in. 73 00:07:49,280 --> 00:07:54,590 So what I'm going to do is check if the items array list already contains the item being Pastan. 74 00:08:01,390 --> 00:08:02,890 Then I'm going to return false. 75 00:08:05,730 --> 00:08:09,000 Otherwise, the item will get added and will return true. 76 00:08:10,130 --> 00:08:14,310 And now you might think, OK, we're good contains is going to behave exactly like we want it to. 77 00:08:14,540 --> 00:08:18,500 It's going to check if the array list already contains an object before adding it. 78 00:08:19,220 --> 00:08:21,550 Yeah, let's run the code and see what happens. 79 00:08:36,570 --> 00:08:39,240 First, it adds the celery, OK, that's fine. 80 00:08:42,669 --> 00:08:49,270 But then we get the exact same Sellery object, its fields are identical to the one already in the array 81 00:08:49,270 --> 00:08:53,380 list, the array list and our eyes already contains that object. 82 00:08:59,030 --> 00:09:05,270 But it gets added anyway, so why isn't this condition evaluating to true, why doesn't it recognize 83 00:09:05,270 --> 00:09:08,480 that the array list already contains a salary object? 84 00:09:09,020 --> 00:09:13,240 The short answer is we need to help the contains method achieve its goal. 85 00:09:13,700 --> 00:09:17,990 So before we move on to part four, you need to learn about the equals method.