Our first cash register still has it's original number of 20's, we have not
modified the first one, we've only changed the contents of the second one.
Let's take a look at the memory addresses.
As you can see, the two cash register objects, exist in different places in
computer memory. Let's go back and continue working on our
class so that we have methods get_total, add, and remove defined.
Here's the header for method get_total. Again, the first parameter is self, it
will refer to the cash register whose total is being asked for.
We create a cash register with these values.
We expect the total to be $190.00 dollars.
Self refers to an instance of cash register, yet total returns an int.
This function will return the total amount of cash in the register.
Each loonies is worth $1.00 dollar, each toonies is worth $2.00 dollars.
Each five dollar bill is worth five dollars, ten dollar bill is worth ten,
and every 20 dollar bill is worth 20 dollars.
When we learn our program again, register.gettotal produces 190 just like
we wanted. And then we're told there is no attribute
Add, in a cash register object, let's define one.
Method add has self as the first parameters, as always, and then the
number of items to be added and the denomination.
For example, we might call register.add, and ask to add two Toonies to the cash
register. Means that when we examine Toonies, we
should see seven. We can also add one to the tens, and then
when we examine that we should see six. Self will refer to a cash register
object, count to inet and denomination to a string.
This method doesn't return anything. And then we'll add count items of
denomination to the register. And we're going to spell out that the
dem, denomination must be one of loonies, toonies, fives, tens, and twenties.
We're going to write an int statement to determine the new kind of denomination
that is being added to the cache register.
If we're adding loonies, then we'll add count to self.loonies.
Likewise, if we've been told to add toonies, then we will increment toonies
by count. Copying and pasting to save time, we will
deal with fives, tens, and twenties in the same way.
Method removed is going to look remarkably like method add.
So we will copy and paste it. We need to fix the name, the description
and the doc tests. We start with five to reason, remove two
of them. We expect there to be three.
When we start with five ten's and remove one of them.
We expect there to be four. Rather than adding to the appropriate
instance variable, we want to subtract. Scrolling to the top, we see that we're
defining a class. The indentation indicates that
underscores init is a method inside this class.
We also have a getTotal method, and an add method and a remove method.
Looking at our main program, that's all the methods that we use.
When we run this, we see that we get exactly the values that we were hoping
for. We've defined a new type, and we can use
it just like any other type that comes with Python.