Bank Accounts
Imagine that you are a software engineer at a top-tier bank. The bank is working to improve their backend system, and your task is to design and implement new classes to represent different types of bank accounts.
The bank offers two types of accounts: checking and savings. Both types of accounts allow customers to deposit and withdraw money. But each type of account has its own characteristics. Checking accounts have an overdraft limit, which allows customers to withdraw money beyond their current balance up to the overdraft limit. Savings accounts earn interest based on an annual percentage yield.
Your task is to create three Python classes:
BankAccount
, CheckingAccount
, and SavingsAccount
. All of them are initialized with a starting balance
amount.- The
BankAccount
class should have two methods:deposit
andwithdraw
. Thedeposit
method should increase the balance by the amount of the deposit. Thewithdraw
method should decrease the balance by the amount of the withdrawal. Both should print the final balance after the operation in theBalance: <X>
format. If there aren’t sufficient funds, the program should printInsufficient funds.
.
- The
CheckingAccount
class should inherit fromBankAccount
and add an additional attribute:overdraft_limit
. Thewithdraw
method inCheckingAccount
should be overridden to allow customers to withdraw money beyond their current balance up to the overdraft limit. In case of exceeding the overdraft limit, the program should printWithdrawal exceeds overdraft limit.
.
- The
SavingsAccount
class should also inherit fromBankAccount
. It should add an additional initialization attribute:annual_percentage_yield
, and add a methodcalculate_returns
that calculates the returns for saving money based on theannual_percentage_yield
. Thecalculate_returns
method should print the returns in theReturns: X
format.
The balance in any account should never become negative (except for the
CheckingAccount
where it can go up to the negative overdraft limit).Input | Output |
check = CheckingAccount(500, 100); check.deposit(200); check.withdraw(800); saving = SavingsAccount(500, 0.05); saving.calculate_returns() | Balance: 700
Balance: -100
Returns: 25 |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB