Question
ACCOUNTS.PY: class SavingsAccount: # The __init__ method accepts arguments for the # account number, interest rate, and balance. def __init__(self, account_num, int_rate, bal): self.__account_num =
ACCOUNTS.PY:
class SavingsAccount: # The __init__ method accepts arguments for the # account number, interest rate, and balance. def __init__(self, account_num, int_rate, bal): self.__account_num = account_num self.__interest_rate = int_rate self.__balance = bal
# The following methods are mutators for the # data attributes.
def set_account_num(self, account_num): self.__account_num = account_num
def set_interest_rate(self, int_rate): self.__interest_rate = int_rate
def set_balance(self, bal): self.__balance = bal
# The following methods are accessors for the # data attributes.
def get_account_num(self): return self.__account_num
def get_interest_rate(self): return self.__interest_rate
def get_balance(self): return self.__balance
# The CD account represents a certificate of # deposit (CD) account. It is a subclass of # the SavingsAccount class.
class CD(SavingsAccount):
# The init method accepts arguments for the # account number, interest rate, balance, and # maturity date. def __init__(self, account_num, int_rate, bal, mat_date): # Call the superclass __init__ method. SavingsAccount.__init__(self, account_num, int_rate, bal)
# Initialize the __maturity_date attribute. self.__maturity_date = mat_date
# The set_maturity_date is a mutator for the # __maturity_date attribute.
def set_maturity_date(self, mat_date): self.__maturity_date = mat_date
# The get_maturity_date method is an accessor # for the __maturity_date attribute.
def get_maturity_date(self): return self.__maturity_date
ACCOUNT DEMO.PY:
import accounts
def main(): # Get the account number, interest rate, # and account balance for a savings account. print('Enter the following data for a savings account.') acct_num = input('Account number: ') int_rate = float(input('Interest rate: ')) balance = float(input('Balance: '))
# Create a SavingsAccount object. savings = accounts.SavingsAccount(acct_num, int_rate, \ balance)
# Get the account number, interest rate, # account balance, and maturity date for a CD. print('Enter the following data for a CD.') acct_num = input('Account number: ') int_rate = float(input('Interest rate: ')) balance = float(input('Balance: ')) maturity = input('Maturity date: ')
# Create a CD object. cd = accounts.CD(acct_num, int_rate, balance, maturity)
# Display the data entered. print('Here is the data you entered:') print() print('Savings Account') print('---------------') print('Account number:', savings.get_account_num()) print('Interest rate:', savings.get_interest_rate()) print('Balance: $', \ format(savings.get_balance(), ',.2f'), \ sep='') print() print('CD') print('---------------') print('Account number:', cd.get_account_num()) print('Interest rate:', cd.get_interest_rate()) print('Balance: $', \ format(cd.get_balance(), ',.2f'), \ sep='') print('Maturity date:', cd.get_maturity_date())
# Call the main function. main()
ASSIGNMENT:
Were going to take Programs 11-7 (accounts.py) and 11-8 (account
demo.py) and add to them two additional methods.
Begin by entering
accounts.py
and
account demo.py
Add to
accounts.py
the following two methods:
deposit(
amount), which adds an
amount
to the
balance
in
SavingsAccount
.
withdraw(
amount), which withdraws an
amount
from the
SavingsAccount balance.
Now add TO ACCOUNTS DEMO the following two questions:
After the
SavingsAccount
information and the
CD
information have been entered,
the user is asked the question: Do you want to make a Deposit to your Savings?
If the answer is Y, then the user is asked to type in the amount that is desired to be
deposited. It is here that you will employ your new
deposit()
method.
Next, the user is asked: Do you want to make a Withdrawal from your Savings?
If the answer is Y, then the user is asked to type in the amount that is desired to be
withdrawn.
This amount is withdrawn if and only if the
amount
is less than the current
balance
in
SavingsAccount
. It is here that we will employ your new
withdraw()
method.
Now were going to ask two more questions of the user in
ACCOUNTS DEMP.PY
Now the user is asked the question: Do you want to make a Deposit into your
CD?
If the answer is Y, then the user is asked to type in the amount that is desired to be
deposited. It is here that you will employ your new
deposit()
method once more.
Next, the user is asked: Do you want to make a Withdrawal from your CD?
If the answer is Y, then the user is asked to type in the amount that is desired to be
withdrawn.
This amount is withdrawn if and only if the
amount
is less than the current
balance
in
your
CD
. Again we will employ your new
withdraw()
method.
Note that we do not have to rewrite the two methods that we have made.
Because of Inheritance, your CD object can use these two methods on its own
balance with no further changes being necessary in the module:
accounts.py
.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started