Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:04,770
In this video, I'm going to cover the solution to task for where we tie everything together and add
2
00:00:04,770 --> 00:00:05,990
user interactivity.
3
00:00:06,480 --> 00:00:08,230
So we're going to start by setting up a scanner.
4
00:00:08,280 --> 00:00:10,590
I'm going to create an object of the scanner class.
5
00:00:14,800 --> 00:00:18,370
And as always, we pass system, not into its constructor.
6
00:00:20,750 --> 00:00:23,660
And then going to copy and paste the following from learn the parts.
7
00:00:40,900 --> 00:00:42,130
Then I'll pick up the RO.
8
00:00:54,150 --> 00:00:54,960
And spot.
9
00:01:03,630 --> 00:01:08,400
All right, now, after the user picks a drink from the machine, we have to dispense it so machine
10
00:01:08,400 --> 00:01:09,330
that dispense.
11
00:01:13,470 --> 00:01:16,140
At the request of the row and spot.
12
00:01:17,450 --> 00:01:22,620
And after we dispense a drink to the user, the quantity of that drink should decrease by one.
13
00:01:23,270 --> 00:01:25,970
So we're going to print the updated machine print online.
14
00:01:28,410 --> 00:01:32,280
I'll start off with a new line, plus the machine objects.
15
00:01:35,960 --> 00:01:36,680
When your code.
16
00:01:47,090 --> 00:01:49,620
OK, let's say I want to purchase a Barq's root beer.
17
00:01:49,760 --> 00:01:55,140
That would be a real one, inducts one and it sells me a drink and now there's one left.
18
00:01:55,220 --> 00:01:55,970
Great.
19
00:01:59,710 --> 00:02:05,290
Now, what if only by more than one drink, I need to run this code in a loop and the easiest way to
20
00:02:05,290 --> 00:02:07,820
approach this is to create a while loop that runs forever.
21
00:02:08,440 --> 00:02:09,910
So while true.
22
00:02:16,180 --> 00:02:17,860
I'll put all of this code inside.
23
00:02:19,140 --> 00:02:19,680
Highlight it.
24
00:02:20,810 --> 00:02:21,620
Press tab.
25
00:02:27,720 --> 00:02:29,340
OK, I'm ready to run the code.
26
00:02:41,100 --> 00:02:43,110
And I'm going to keep purchasing the Berkshire bear.
27
00:02:45,610 --> 00:02:50,200
It's going to keep dispensing a drink, and every time it dispenses a drink, we need to say something
28
00:02:50,200 --> 00:02:52,630
like enjoy your drink, press one to buy another.
29
00:02:53,690 --> 00:02:59,750
Until that item runs out, it stops dispensing, in which case we need to tell the user we're out of
30
00:02:59,750 --> 00:03:05,780
this item, press one to purchase another, and this should be very easy to implement because our method
31
00:03:05,960 --> 00:03:11,540
dispense returns a boolean that confirms if that item was dispensed or not.
32
00:03:12,740 --> 00:03:16,700
So we'll store the bullion that it returns and a variable called Dispensed.
33
00:03:24,240 --> 00:03:28,680
And if that machine dispenses a drink, if dispensed is equal to true.
34
00:03:33,560 --> 00:03:35,150
Will print the following message.
35
00:04:01,480 --> 00:04:07,150
Otherwise, if the machine doesn't dispense a drink, then it could only be because that item ran out.
36
00:04:07,150 --> 00:04:08,650
So we'll print this message.
37
00:04:41,250 --> 00:04:47,490
And regardless of which block gets executed, if the user enters anything that isn't one, we need to
38
00:04:47,490 --> 00:04:51,510
break the loop, so pick up the users answer scanned on extent.
39
00:04:58,430 --> 00:05:00,500
And if their answer is not one.
40
00:05:04,310 --> 00:05:05,230
Break the loop.
41
00:05:06,770 --> 00:05:09,080
And I'm happy to say that now we're all done.
42
00:05:14,660 --> 00:05:15,920
OK, run the code.
43
00:05:36,740 --> 00:05:39,980
I'll purchase the berry, which is Zaccaro to index to.
44
00:05:44,470 --> 00:05:47,050
I'll try purchasing it again, so I'll press one.
45
00:05:49,620 --> 00:05:51,350
I'll choose the berry once more.
46
00:05:55,010 --> 00:05:58,160
And it tells me that it's empty, I'll purchase another.
47
00:06:00,000 --> 00:06:02,120
Let me get a Coca-Cola.
48
00:06:03,720 --> 00:06:04,980
Wrote to eliminate one.
49
00:06:08,290 --> 00:06:11,470
OK, now I'm satisfied I'll press zero and I'll break the lip.
50
00:06:14,410 --> 00:06:20,080
All right, I purchased two drinks from the vending machine, ran out of Barry and one Coca-Cola remains.
51
00:06:21,550 --> 00:06:26,320
But that being said, congratulations, this is the third application that we programmed using objects.
52
00:06:31,610 --> 00:06:37,280
And notice how easy it is to read the code, your code becomes a lot more organized when you program
53
00:06:37,280 --> 00:06:38,240
using objects.
54
00:06:40,820 --> 00:06:46,340
And now that you know how to write high quality code, the next step is to write the reliable code code
55
00:06:46,340 --> 00:06:50,590
that doesn't crash and we're going to accomplish that with exception handling.
56
00:06:51,200 --> 00:06:52,970
I will see you in the next section.
5195
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.