Question: Your task is to write a class that simulates a bank account. Customers can deposit and withdraw funds. If sufficient funds are not available for

Your task is to write a class that simulates a bank account. Customers can deposit and withdraw funds. If sufficient funds are not available for withdrawal, a $10 overdraft penalty is charged. At the end of the month, interest is added to the account. The interest rate can vary every month.

Get an informal list of the responsibilities of your objects. Here is their list:

deposit funds

withdraw funds

add interest

get account balance

Specify the public interface. By this, the author means the methods that users of our class will want to have available to them. This specification will get us started in writing the code for the class. The public interface consists of the method declarations without any code to make them actually work. That will come a little later.

We'll start by creating a program named bankaccount.py in WingIDE.

Next, enter this code that identifies our program as a class definition and gives the class a name:

class BankAccount :

For the deposit and withdraw methods, we'll need a parameter that specifies how much they want to work with.

def deposit(self, amount) :

def withdraw(self, amount) :

The problem statement says that the interest rate can vary each month, so that will be a parameter for the third method addInterest:

def addInterest(self, rate) :

The constructor will be used when a new bank account object is created. We'll give the user the option of specifying an initial balance but we'll also have a default value in case they don't:

def __init__(self, initialBalance = 0.0) :

By convention, put this constructor declaration before the other methods in the class.

The last thing to define is the getBalance method:

def getBalance(self) :

Note that we haven't addressed the particulars for any of these methods. We'll do that once we get everything planned out.

Next, we need to document the class, the constructor, and our methods so that users of our class will know what they do and what the parameters mean. Add the following comments at the top of your code:

## A bank account has a balance that can be changed by deposits and withdrawals.

#

Add the following comments before the constructor:

## Constructs a bank account with a given balance.

# @param initialBalance the initial account balance (default = 0.0)

#

Next, add this comment before the deposit method:

## Deposits money into this account.

# @param amount the amount to deposit

#

Add this comment before the withdrawal method:

## Makes a withdrawal from this account, or charges a penalty if

# sufficient funds are not available.

# @param amount the amount of the withdrawal

#

And this comment before the addInterest method:

## Adds interest to this account.

# @param rate the interest rate in percent

#

Finally, add this for the getBalance method:

## Gets the current balance of this account.

# @return the current balance

#

As a check, your program should look like this:

## A bank account has a balance that can be changed by deposits and

# withdrawals.

#

class BankAccount :

## Constructs a bank account with a given balance.

# @param initialBalance the initial account balance (default = 0.0)

#

def __init__(self, initialBalance = 0.0) :

## Deposits money into this account.

# @param amount the amount to deposit

#

def deposit(self, amount) :

## Makes a withdrawal from this account, or charges a penalty if

# sufficient funds are not available.

# @param amount the amount of the withdrawal

#

def withdraw(self, amount) :

## Adds interest to this account.

# @param rate the interest rate in percent

#

def addInterest(self, rate) :

## Gets the current balance of this account.

# @return the current balance

#

def getBalance(self) :

The next step is to determine the instance variables. This is the information we need to maintain for a BankAccount object. The only thing we'll consider is the current account balance, although we could add other items such as the customer's name, address, phone number, e-mail address, the date the account was created, etc.

Add the following line of code within the definition of the constructor after line 8 (be sure to indent the code when you type it in):

self._balance = initialBalance

Now we can add code for the rest of the methods. The easiest one to start with is the getbalance method whose only job is to return the current account balance:

return self._balance

The next one to tackle is the deposit method that increases the balance by the amount the customer gives to the bank for safe-keeping:

self._balance = self._balance + amount

The add interest method is a little more complex. We'll use the rate parameter to determine the amount of interest and then add that amount to the customer's balance. Recall that interest rates are actually percentages so we'll divide that parameter by 100 before we use it:

amount = self._balance * rate /100.0

self._balance = self._balance + amount

The last method to implement is the withdrawal method. Note that the problem description includes a proviso that a bank fee of $10 is to be deducted if the customer attempts to take out more money than they have. We won't worry about a negative balance if the customer has less than $10 in their account.

PENALTY = 10.0

if amount > self._balance :

self._balance = self._balance - PENALTY

else :

self._balance = self._balance - amount

This completes the coding portion of the problem. Before we declare victory and move on, we must test our class first. Be sure to save your work in a file named bankaccount.py.

Create a new program named BankAccountTest.py that contains the following lines of code:

from bankaccount import BankAccount #import the class definition

harrysAccount = BankAccount(1000.0) #instantiate a BankAccount object

print("%.2f" % harrysAccount.getBalance())

print("Expected: 1000.00")

harrysAccount.deposit(500.0)

print("%.2f" % harrysAccount.getBalance())

print("Expected: 1500.00")

harrysAccount.withdraw(2000.0) #note that we don't have $2000

print("%.2f" % harrysAccount.getBalance())

print("Expected: 1490.00")

harrysAccount.addInterest(1) #1% interest should be $14.90

print("%.2f" % harrysAccount.getBalance())

print("Expected: 1504.90")

Run this program. We expect it will work but if it doesn't you'll have to track down the error and fix it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!