In this video, I'm going to cover the solution to task for where we tie everything together and add
user interactivity.
So we're going to start by setting up a scanner.
I'm going to create an object of the scanner class.
And as always, we pass system, not into its constructor.
And then going to copy and paste the following from learn the parts.
Then I'll pick up the RO.
And spot.
All right, now, after the user picks a drink from the machine, we have to dispense it so machine
that dispense.
At the request of the row and spot.
And after we dispense a drink to the user, the quantity of that drink should decrease by one.
So we're going to print the updated machine print online.
I'll start off with a new line, plus the machine objects.
When your code.
OK, let's say I want to purchase a Barq's root beer.
That would be a real one, inducts one and it sells me a drink and now there's one left.
Great.
Now, what if only by more than one drink, I need to run this code in a loop and the easiest way to
approach this is to create a while loop that runs forever.
So while true.
I'll put all of this code inside.
Highlight it.
Press tab.
OK, I'm ready to run the code.
And I'm going to keep purchasing the Berkshire bear.
It's going to keep dispensing a drink, and every time it dispenses a drink, we need to say something
like enjoy your drink, press one to buy another.
Until that item runs out, it stops dispensing, in which case we need to tell the user we're out of
this item, press one to purchase another, and this should be very easy to implement because our method
dispense returns a boolean that confirms if that item was dispensed or not.
So we'll store the bullion that it returns and a variable called Dispensed.
And if that machine dispenses a drink, if dispensed is equal to true.
Will print the following message.
Otherwise, if the machine doesn't dispense a drink, then it could only be because that item ran out.
So we'll print this message.
And regardless of which block gets executed, if the user enters anything that isn't one, we need to
break the loop, so pick up the users answer scanned on extent.
And if their answer is not one.
Break the loop.
And I'm happy to say that now we're all done.
OK, run the code.
I'll purchase the berry, which is Zaccaro to index to.
I'll try purchasing it again, so I'll press one.
I'll choose the berry once more.
And it tells me that it's empty, I'll purchase another.
Let me get a Coca-Cola.
Wrote to eliminate one.
OK, now I'm satisfied I'll press zero and I'll break the lip.
All right, I purchased two drinks from the vending machine, ran out of Barry and one Coca-Cola remains.
But that being said, congratulations, this is the third application that we programmed using objects.
And notice how easy it is to read the code, your code becomes a lot more organized when you program
using objects.
And now that you know how to write high quality code, the next step is to write the reliable code code
that doesn't crash and we're going to accomplish that with exception handling.
I will see you in the next section.