Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:03,240
The solution is going to cover tasks four and five of Part eight.
2
00:00:03,630 --> 00:00:06,420
We're going to start by defining a function named manage items.
3
00:00:06,780 --> 00:00:10,710
It's not going to return anything, nor will it receive any parameters.
4
00:00:14,480 --> 00:00:18,920
I'm going to start by creating a new object of the scanner class that can receive input from the system.
5
00:00:30,690 --> 00:00:34,410
And the next task to ultimately create an infinite loop while true.
6
00:00:35,100 --> 00:00:38,760
And if I go back to the article, it tells me to copy over the print statements.
7
00:00:55,900 --> 00:00:59,620
So I'll need to remove what I have here and call the manage items instead.
8
00:01:03,150 --> 00:01:09,610
Simple enough and then I need to let the user choose between adding removing from cart or checking items
9
00:01:09,610 --> 00:01:10,450
out from a cart.
10
00:01:11,110 --> 00:01:13,570
I'm sure there's a print statement to go along with the step.
11
00:01:13,780 --> 00:01:14,800
And there it is.
12
00:01:14,950 --> 00:01:15,340
Cool.
13
00:01:15,400 --> 00:01:16,570
I'll just copy it over.
14
00:01:21,900 --> 00:01:25,380
And then I need to pick up the response using scan dot next line.
15
00:01:34,000 --> 00:01:37,420
The next step is to compare the response against three possible cases.
16
00:01:41,700 --> 00:01:44,790
So I'll create a switch statement that compares response.
17
00:01:47,860 --> 00:01:49,090
Against Case A.
18
00:01:52,660 --> 00:01:53,500
Case be.
19
00:01:56,230 --> 00:01:56,830
Casey.
20
00:02:01,780 --> 00:02:04,030
If the user wants to add a car, will tell them.
21
00:02:06,810 --> 00:02:08,729
Choose an aisle between one and seven.
22
00:02:13,680 --> 00:02:15,960
Then we'll pick up the using the next ENT.
23
00:02:24,650 --> 00:02:27,410
But remember that in Java we start counting from zero.
24
00:02:27,830 --> 00:02:31,310
So the row is going to be whatever the user inputs minus one.
25
00:02:36,640 --> 00:02:36,970
Okay.
26
00:02:36,970 --> 00:02:41,170
So we're using next end and next line in the same loop and that can be problematic.
27
00:02:41,860 --> 00:02:45,670
We're likely to fall into the next line trap, which can cause a lot of bugs.
28
00:02:46,390 --> 00:02:51,580
So before we fall into the trap, I'm going to add a throwaway next line right after the next end.
29
00:02:55,930 --> 00:02:59,410
And the throwaway next line is going to get wasted by blank space.
30
00:02:59,410 --> 00:03:00,760
So it's not going to do anything.
31
00:03:03,400 --> 00:03:08,680
But it's going to prevent the other next lines that are actually useful from being wasted on blank space.
32
00:03:11,820 --> 00:03:12,050
Okay.
33
00:03:12,060 --> 00:03:15,240
So then we'll just ask the user to pick an item number between one and three.
34
00:03:19,110 --> 00:03:22,410
Once again, we'll pick the value up using scan to an extent.
35
00:03:27,190 --> 00:03:30,880
But we minus one because in code we actually start counting from zero.
36
00:03:33,940 --> 00:03:38,890
And once again, we'll add a throwaway next slide to prevent useful next lines from being wasted on
37
00:03:38,890 --> 00:03:39,730
blank space.
38
00:03:40,950 --> 00:03:41,270
Cool.
39
00:03:41,370 --> 00:03:43,260
So now we have the row and column.
40
00:03:43,260 --> 00:03:49,410
We can use the getter from store to get item to get the item at the requested row and column.
41
00:03:52,560 --> 00:03:55,980
And after getting the item, you can add it to the user shopping carts.
42
00:04:11,720 --> 00:04:16,990
Now back in the workbook, it tells us to print a message that tells the user add their item was successfully
43
00:04:16,990 --> 00:04:17,380
added.
44
00:04:21,430 --> 00:04:24,220
So I guess here, I'll have to put this in a separate variable.
45
00:04:36,610 --> 00:04:37,420
And there you go.
46
00:04:38,140 --> 00:04:38,710
Perfect.
47
00:04:40,000 --> 00:04:44,290
And don't forget to always add the break keyword at the end of each case in a switch statement.
48
00:04:47,570 --> 00:04:51,080
So after the user adds their item, the runtime jumps to here.
49
00:04:52,390 --> 00:04:55,180
Where we're going to print the updated shopping cart.
50
00:04:57,340 --> 00:05:00,730
I'm going to print two new lines, followed by the words shopping cart.
51
00:05:04,380 --> 00:05:07,470
Two more newlines followed by the object.
52
00:05:10,280 --> 00:05:11,670
I'm sure there are more stuff to print.
53
00:05:11,690 --> 00:05:12,920
Here, let's check the workbook.
54
00:05:18,670 --> 00:05:19,510
And there you go.
55
00:05:31,770 --> 00:05:37,170
We'll put a scanner next line to allow the user to pause and or something before they continue.
56
00:05:41,570 --> 00:05:42,420
Later on the code.
57
00:05:53,990 --> 00:05:56,960
I'll buy the crush aisle one item to.
58
00:06:02,020 --> 00:06:02,980
There is my coat.
59
00:06:02,980 --> 00:06:03,400
Cool.
60
00:06:05,720 --> 00:06:06,620
I'll continue.
61
00:06:09,740 --> 00:06:11,360
Then I'll buy some coriander.
62
00:06:20,570 --> 00:06:21,920
My current updates.
63
00:06:29,500 --> 00:06:30,730
Then I'll get a printer.
64
00:06:36,640 --> 00:06:38,380
And this is working really well.
65
00:06:41,240 --> 00:06:44,630
Now, what if I had the same item to the shopping cart like crush?
66
00:06:54,560 --> 00:06:55,270
And sweet.
67
00:06:55,310 --> 00:06:59,060
Our contains method effectively stops the user from doing so.
68
00:07:03,460 --> 00:07:09,280
But it doesn't make sense for it to print item was added to your cart when in fact nothing was added.
69
00:07:12,380 --> 00:07:15,950
So we can stop the runtime using control C for both Mac and Windows.
70
00:07:19,500 --> 00:07:24,930
In fact, our code, the ad method returns a boolean that tells us whether the item was added or not.
71
00:07:29,660 --> 00:07:30,620
So here, I'll say.
72
00:07:33,460 --> 00:07:36,940
So here, I'll check if the item wasn't added to the cart.
73
00:07:48,590 --> 00:07:51,170
That item is already in your shopping cart.
74
00:08:02,360 --> 00:08:02,720
Great.
75
00:08:02,720 --> 00:08:03,560
Let's test that.
76
00:08:06,390 --> 00:08:07,260
We run the code.
77
00:08:14,540 --> 00:08:15,440
I'll add Pepsi.
78
00:08:23,790 --> 00:08:24,960
I'll add Pepsi again.
79
00:08:27,030 --> 00:08:28,200
And it works beautifully.
80
00:08:42,659 --> 00:08:44,210
Now we can work on case B.
81
00:08:44,510 --> 00:08:46,610
In this case, the user wants to remove an item.
82
00:08:47,480 --> 00:08:49,520
So we'll ask them what they'd like to remove.
83
00:08:49,880 --> 00:08:51,680
Copy the print statement from the workbook.
84
00:09:00,010 --> 00:09:02,320
And then pick up the name using scandal and explain.
85
00:09:07,730 --> 00:09:09,140
And remove it from the cart.
86
00:09:29,460 --> 00:09:30,630
I'm going to add Pepsi.
87
00:09:38,910 --> 00:09:40,080
A lot of television.
88
00:09:43,640 --> 00:09:45,080
Then I removed television.
89
00:09:51,370 --> 00:09:53,860
And our shopping cart updates effectively sweep.
90
00:10:00,040 --> 00:10:02,770
An advocacy widget to print the receipt.
91
00:10:04,750 --> 00:10:07,720
And remember that cart checkout returns the receipt.
92
00:10:11,520 --> 00:10:14,250
So we can just call card checkout.
93
00:10:18,410 --> 00:10:19,850
And print the return value.
94
00:10:24,840 --> 00:10:25,350
And that's it.
95
00:10:26,680 --> 00:10:29,200
And now once they check out, that means they're done.
96
00:10:29,200 --> 00:10:30,880
So here we have to close scanner.
97
00:10:36,110 --> 00:10:41,810
And instead of breaking, we're going to return because break is only going to break the switch statement.
98
00:10:43,970 --> 00:10:47,210
Return breaks the entire function, and that includes the loop.
99
00:10:52,570 --> 00:10:56,260
And since the function is void, make sure nothing comes after return.
100
00:10:56,410 --> 00:10:57,390
Just put return.
101
00:10:58,680 --> 00:10:59,100
Cool.
102
00:10:59,340 --> 00:11:00,090
Run the code.
103
00:11:11,080 --> 00:11:12,340
I'll add a few items.
104
00:11:26,320 --> 00:11:27,010
Check out.
105
00:11:27,340 --> 00:11:28,600
And there is my receipt.
106
00:11:35,140 --> 00:11:37,600
Now what if the user enters an option that doesn't exist?
107
00:11:46,680 --> 00:11:49,440
It's a bit awkward that it displays the shopping carts.
108
00:11:56,120 --> 00:11:57,500
So inside default.
109
00:12:00,020 --> 00:12:04,970
If they are some random response, we're just going to restart the while loop is and continue.
110
00:12:16,040 --> 00:12:17,660
I'm just going to enter a bad value.
111
00:12:18,020 --> 00:12:18,770
Beautiful.
112
00:12:23,080 --> 00:12:24,400
Now I can just pick an item.
113
00:12:26,180 --> 00:12:26,840
Continue.
114
00:12:28,960 --> 00:12:29,860
I'll do it again.
115
00:12:44,000 --> 00:12:45,050
And nice.
116
00:12:45,530 --> 00:12:49,310
Where officially done building the shopping cart and this turned out really well.
117
00:12:49,760 --> 00:12:55,880
You created item objects, a cart object, a store object, and you glued them all together inside Main.
118
00:12:56,150 --> 00:12:59,750
And in the end, we were able to achieve this beautiful application.
119
00:13:00,530 --> 00:13:05,270
We're done building the application, but now we get the, quote unquote bullet proof fit.
120
00:13:05,720 --> 00:13:10,400
In other words, we want to make sure the application doesn't fail or crash no matter what.
121
00:13:10,910 --> 00:13:12,500
And we'll do that in part nine.
122
00:13:12,650 --> 00:13:13,340
See you in there.
10233
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.