For this lab, you will prepare a single python
file: account.py
. This
file should implement functionality for a bank savings account.
I am providing the following files for you:
account.py
: A file
for you work on.
bank.py
: This contains
the main program, which uses the Account
class you have created.
transactions.txt
: This contains a list of transactions, which the main
program uses
animals.py
: In this
example, two classes are created: Dog
and Cat
. Both classes have these methods:
__init__
: the constructor
__repr__
: returns a string
representation
speak
: prints the animal's sound
An object contains some data, and can perform some actions. The data are called instance variables, and the actions are called methods.
Every object needs the following methods:
__init__(self, param1, param2,
...):
This is the constructor method.
It must be named __init__
(notice the two underscores before and
after init
: they are mandatory).
The first parameter of every method is
always self
. There may also be other
parameters. For example, in the Dog
class,
the constructor:
def __init__(self, name, color): self._name = name self._color = colorthe first parameter is
self
, and there two
more, parameters, name
and color
, for a total of 3.
In the constructor, you will create the instance
variables: they are _name
and _color
(note the single
leading underscore).
In this case, we just copy the parameters into the instance variables, but we could also create other instance variables by giving them values.
__repr__(self)
: this method is also
needed. It should return a string, which describes the
object, and its instance variables (note
the double leading and trailing underscores:
they are mandatory).
In our case, we return a string that describes
our Dog
object.
__init__
and __repr__
, an object will have other methods.
In our case, we define the method speak(self)
.
class
, you can
use it in several ways:
Construction: To create an object, call its constructor.
For example, near the bottom of animals.py
,
you'll see Dog('Rover', 'brown')
.
The call to Dog(...)
simply calls
the __init__
method. It passes it 3
parameters: self
(which is passed in for
you), and the other two parameters, name
and color
.
In fact, all method calls use this convention: even
though the method requires a self
parameter, you should not pass self
, when
you call the method.
Printing: When you print an object, you need its string
version. This is happening in the last line
of animals.py
. The python interpreter
will, in this situation, call the __repr__
method for you.
All other methods: To call an object's method, just take
the variable that holds the object (in this
case, my_dog
), and call the method, after a
dot:
my_dog.speak()
.
Notice that, once again, the speak
method
expects a parameter self
, but you should
omit it; python will enter it for you.
account.py
, define a class
called Account
. The class should contain the
following instance variables:
_owner
: the person who owns the account (a
string)
_ID
: the account number (an int)
_balance
: how much is in the account (a float)
_interest_rate
: the percent APY of the
account (a float). For example, if the account earns
10%, _interest_rate
should be 10
.
_deposits
: the total money deposited into
the account (a float). This should initially be 0
.
_withdrawals
: the total money withdrawn from
the account (a float). This should initially be 0
.
_interest
: the total interest earned in
the account (a float). This should initially be 0
.
Account
should define the
following methods:
__init__(self, ID, owner, interest_rate)
:
the constructor, which initializes all the instance
variables.
deposit(self, amount)
: add
the amount
to the balance, and to the total
deposits.
withdraw(self, amount)
: subtract
the amount
from the balance, and add it to the
total withdrawals. But if an overdraft would occur, do
nothing.
earn_interest(self)
: compute the interest,
and add it to the balance, and to the total interest.
totals(self)
: return a list, with three
values: the total deposits, the total withdrawals, and the
total interest.
__repr__(self)
: return a string
representation of the account, 29 chars long, which
consists of three fields joined into a string:
_ID
, right-justified in 7 chars,
_owner
, left-justified in 10 chars,
_balance
, right-justified in 10
chars,
To join justified fields, use the format
function. For example, suppose you have two
variables, apple
and banana
. To
join them into one string, with the first one
left-justified in 10 chars, and the second right-justified
in 15 chars, call format
thus:
'{:<10} {:>15}'.format(apple, banana)
Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.
To turn your work in, go to
mycourses.unh.edu
,
find CS414, open the syllabus, and click on Lab 7. Then click
Submit Assignment, click Choose file,
then find your file account.py
, and upload it.
account.py
: edit and
submit this file.
bank.py
: main program,
which test your class.
transactions.txt
: data file usef by bank.py
animals.py
: example of
class definitions, and use of objects.