You've just been hired.
Congratulations.
But first, there's the matter of coffee.
We all know that programmers can't work unless they're loaded up on coffee.
So your company has asked you to create the code for a coffee machine.
Now, you very cleverly go online and look at the other coffee machines, and you notice that it only
costs $230 to buy a coffee machine,
but you're not going to cheat,
you're going to create everything from scratch.
And luckily, we don't actually have to build the hardware because we're programmers, right?
But we're going to use this real life coffee machine as the inspiration for our virtual coffee machine,
noting the features and the capabilities.
So what does it say?
I found some wonderful graphics on here, which tells me that there are three hot flavors, which I'm
guessing are these three buttons.
It's coins operate, which I think they mean it's coin operated, and there's not much else that it
can do.
Let's take this example and we're going to make a digital version of it.
We're going to create our own coffee machine.
And the first step we need to replicate is the ability to make three hot flavors.
So the flavors we're going to make are one Espresso, two Latte, and three Cappuccino.
Here are the recipes for these three types of drinks,
each of them requires a different quantity of water, a different quantity of coffee, and a different
quantity of milk.
And they also each have a different price.
We're going to be modeling all this data in our coffee machine program as well.
But luckily for you, in the starting code, I've already included all of this data inside a dictionary,
so you don't have to remember this, although it might help if you're at home and you decide that you
really wanted a latte instead of your usual black coffee.
Now, in addition, the coffee machine has some resources that it has to manage.
So it starts out with 300 ml of water in the tank, 200 ml of milk, and 100g of coffee.
The second feature of our coffee machine is that it's coin operated.
So we're going to be using American coins.
And they have four types of coins the Penny, the Nickel, the Dime, and the Quarter.
They used to also have the Dollar as a coin,
but a few years ago they stopped minting that.
So we're not going to count it,
and it probably means one less if statement for us.
Notice what each of these coins are worth.
The penny is worth a cent, the nickel worth $0.05, and the dime $0.10, and the quarter is a quarter
of a dollar.
These represented in decimal values would look something like this.
Now here's the important part,
let's think about what our program requirements are if we break it down.
One we need our coffee machine to be able to print a report.
It needs to be able to tell us what resources it has left, how much water has left, how much milk,
et cetera.
So let's say that we wanted a report on all the resources that the machine has.
All we have to do is type report and we can see all the resources we have.
Now, in addition to being able to print a report, we also want to be able to check that the resources
are sufficient when the user orders a drink.
Now let's say that the user decided to order a latte.
It asks me to insert some coins and then it gives me the change and it gives me the latte.
But now if I check the report, I can see that I've only got 100 ml of water left,
and I know from the previous slide that a latte and a cappuccino is going to need more than that amount
of water.
So let's see what happens if I go ahead and order a cappuccino.
It says, "Sorry, there's not enough water."
So our program is able to look through all the resources that the machine has, checks it against the
recipe of the drink that we're trying to make, and tells the user if there is insufficient water or
if there's insufficient milk,
and as long as one of the resources is insufficient, then it can't make the drink and it gives the
feedback to the user.
Now, as you saw before, our program also needs to be able to process coins because our machine is
coin operated, so no fancy contactless payment or pay with your Apple Watch,
none of that.
We've only got coins.
When we order something, it should ask us to insert coins, and it's going to ask for the quantity
of each type of coin.
So let's say that I insert one of each.
Then in this case, there's actually not enough money to cover my drink, and it refunds the money and
doesn't give me a drink.
But on the other hand, if I do insert enough money, then it should be able to calculate how much money
all of these coins are worth and then calculate the amount of change based on the cost of my drink,
and then it should hand me my drink, and tell me to enjoy.
So in addition to being able to process the four types of coins, calculating the actual monetary value
based on the number of coins, it should also check that the transaction is successful, that the user
didn't try to hoodwink us by not giving enough coins and asking for a drink.
So if they haven't inserted enough coins, then we're just going to refund them and tell them, "Sorry,
that's not enough money" and not give them their drink.
But if the transaction was successful, then we're going to make the coffee,
and in the process of making the coffee, we're going to have to deduct the resources.
Notice how every time we make a drink say, in this time we made a latte,
and previous to the latte we had 300ml of milk.
But after the latte, when we asked for the report, you can see that the water has been reduced, the
milk has been reduced, the coffee has been reduced, and the money has been put into the coffers.
This program, even though it seems simple, just a simple digital version of a coffee machine, it
actually has quite a few requirements.
Now I've created a detailed program specification for you as a PDF file, which you can download in
the Course Resources,
and this goes into a lot more details on each of those points.
For example, what should the prompt print in the beginning of the program, and then how to turn the
machine off, how to print the report, and how each of those points should work in detail.
Go ahead and download this and make sure you read each of the sections and test it out in the final
working version of the project.
Once you're ready, you can go ahead and head over to the starting version of the code, and I want
you to create a new project using PyCharm and call it CoffeeMachine.
And then create a new file inside your project called main.py, and then paste all of the starting code
into your main.py.
And now you're going to code inside this file.
And once you're ready, you're going to click Run,
and you'll be able to run this main.py down here,
and this will act as your console.
So if I go ahead and take some of these program requirements, I can put them in as TODOs.
To create a TODO you have to follow the syntax.
First is a pound sign #,
and then we write TODO in all caps.
And notice how that's just changed color just now.
And now you can see in the TODO tab it's found one TODO item.
Now we can write TODO Number 1,
and this is to print a report of all the coffee machine resources.
Now let's say that I created a TODO somewhere else,
right?
Like all the way up here.
"Check that the resources are sufficient to make the drink order."
And notice how they're completely in different places, in a different order,
but every time I create one of these TODOs using this format, it will get picked up in the TODO tab
and you can go ahead and see where they live.
So it's found two items inside our main.py.
And you can see that this one is TODO: 1.
This one is TODO: 2.
And when you click on them they'll take you to the correct places in your code.
Use this to break down the problem into smaller problems that you can solve one by one, just as you've
done before, and try to see if you can complete this project.
Just a word of warning.
This project is quite ambitious, so give this problem at least an hour to work on it and make sure
that you satisfy all the criteria that's set out in the program requirements, and also that your program
works exactly the same as the final version of the Coffee Machine.