189. Python Program to Build a Bank with Savings Accounts
HardInheritance
Overview
Put the whole chapter together: a class with __init__, methods that check and raise, __str__, and a child class that adds to it with inheritance.
Problem Statement
Write two classes.
BankAccount has an owner and a balance, and:
deposit(amount): raisesValueError("Amount must be positive")if the amount is 0 or less; otherwise adds it.withdraw(amount): raisesValueError("Amount must be positive")if the amount is 0 or less, andValueError("Not enough money")if it is more than the balance; otherwise takes it out.__str__: returns the owner and the balance with two decimals, likeSunny: 1500.00
SavingsAccount is a child of BankAccount with one more attribute, rate (a whole number, the interest in percent), and:
add_interest(): addsbalance * rate / 100to the balance.__str__: the parent's text, then the rate, likeSunny: 1500.00 (savings 5%)
Read the owner's name (it may have extra spaces around it), then the account type: basic, or savings and a rate, like savings 5. Then read the starting balance (it may have decimals), the number of commands, and one command per line: deposit 500, withdraw 75 or interest.
For each command, print the account if the command worked, or Error: and the message if it didn't. Asking a basic account for interest is an error too: Error: No interest on a basic account.
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
The owner, the type, the starting balance, the number of commands, then the commands. Example:
Sunny
savings 5
1000
4
deposit 500
withdraw 2000
interest
withdraw 75
Output
The output for that example:
Sunny: 1500.00 (savings 5%)
Error: Not enough money
Sunny: 1575.00 (savings 5%)
Sunny: 1500.00 (savings 5%)
The tests try 5 different inputs, with basic and savings accounts.