In: Computer Science
I need to add this checkpoint to an existing code that i have in python
Checkpoint 1:
Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and SavingsAccount and test them to ensure all the methods work. You should also verify that BankAccount still behaves correctly. You will need to extend the __init__ and __str__ routines from your BankAccount class.
The BankAccount, CheckingAccount, SavingsAccount, and BankAccountTester should all be separate files. You will need to import the BankAccount into the SavingsAccount, CheckingAccount, and BankAccountTester. You will need to import the BankAccount into the SavingsAccount and CheckingAccount classes.
To set default values for a parameter, you say paramater_name = default_value.
In testing you will need to create an instance of the BankingAccount, and test all the methods, deposit and withdraw, and try to withdraw more money than is in the account. You must also create instances of the checking account testing the same methods.
Existing code:
class BankAccount(): def __init__(self, name, id, ssn, balance=0.0): self.__name = name self.__account_id = id self.__ssn = ssn self.__balance = balance def deposit(self, amount): if amount <= 0: print('Error: Deposit amount ${} is in negative'.format(amount)) return else: self.__balance += amount print('Success: Amount ${} deposited succesfully') def withdraw(self, amount): if amount <= 0: print('Error: Withdraw amount ${} is in negative'.format(amount)) return else: if amount <= self.__balance: self.__balance -= amount print('Success: Amount ${} withdrawn succesfully') else: print('Error: Insufficient funds.') def __str__(self): return 'Name: {}, Account ID: {}, SSN: {}, Current Balance: ${:.2f}'.format( self.__name, self.__account_id, self.__ssn, self.__balance )
=========================================================================
from BankAccount import BankAccount def main(): account = BankAccount('John Doe', 1234, '234-789-1221', 1000) print(account) print('Depositing $100 into account') account.deposit(100) print(account) print('Withdrawing $950 from account') account.withdraw(950) print(account) print('Withdrawing $400 from account') account.withdraw(400) print(account) main()
====================================================================
Since no use of the newly added variables are mentioned, I just implemented new 2 classes and added new variable interest rate to bankaccount class. let me know If you need any features to be implemented.
class BankAccount: def __init__(self, name, id, ssn, balance=0.0,interest_rate=0.0): self.__name = name self.__account_id = id self.__ssn = ssn self.__balance = balance self.__interest_rate = interest_rate def deposit(self, amount): if amount <= 0: print('Error: Deposit amount ${} is in negative'.format(amount)) return else: self.__balance += amount print('Success: Amount ${} deposited succesfully'.format(amount)) def withdraw(self, amount): if amount <= 0: print('Error: Withdraw amount ${} is in negative'.format(amount)) return else: if amount <= self.__balance: self.__balance -= amount print('Success: Amount ${} withdrawn succesfully'.format(amount)) else: print('Error: Insufficient funds.') def __str__(self): return 'Name: {}, Account ID: {}, SSN: {}, Current Balance: ${:.2f}'.format( self.__name, self.__account_id, self.__ssn, self.__balance )
# Subclass Checking Account class CheckingAccount(BankAccount): def __init__(self, name, id, ssn, balance=0.0,interest_rate=0.0,per_check_fee = False, allow_overdraft = True): super().__init__(name, id, ssn, balance,interest_rate) self.__per_check_fee = per_check_fee self.__allow_overdraft = allow_overdraft def __str__(self): return '{}, Per Check Fee: {}, Overdraft Allowed: {}'.format( super().__str__(), self.__per_check_fee, self.__allow_overdraft) # Subclass Savings Account class SavingsAccount(BankAccount): def __init__(self, name, id, ssn, balance=0.0, interest_rate=0.0, transactions_per_month=5): super().__init__(name, id, ssn, balance, interest_rate) self.__transactions_per_month = transactions_per_month def __str__(self): return '{}, Transactions Per Month: {}'.format( super().__str__(), self.__transactions_per_month)
Test Code
# This is my package structure. change it according to yours
from Bank.BankAccount import CheckingAccount,SavingsAccount
def main(): account = CheckingAccount('John Doe', 1234, '234-789-1221', 1000,6.5,True,False) print(account) print('Depositing $100 into account') account.deposit(100) print(account) print('Withdrawing $950 from account') account.withdraw(950) print(account) print('Withdrawing $400 from account') account.withdraw(400) print(account) account = SavingsAccount('Jane Doe', 1234, '234-789-1120', 1050, 7.5,10) print('Depositing $100 into account') account.deposit(850) print(account) print('Withdrawing $950 from account') account.withdraw(950) print(account) print('Withdrawing $400 from account') account.withdraw(400) print(account) main()