Bank Account Without Overdrafts
Implement a class BankAccount for an account whose balance may never drop below zero. Write these methods:
BankAccount(opening)opens the account with a balance ofopening.deposit(amount)addsamountto the balance and returns the new balance.withdraw(amount)takesamountout of the balance and returnstruewhen the balance is at leastamount. Otherwise the withdrawal is rejected: the balance does not change and it returnsfalse.get_balance()returns the current balance.count_rejected()returns how many withdrawals have been rejected so far.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
BankAccount(50)
withdraw(80) returns false
deposit(30) returns 80
withdraw(80) returns true
withdraw(1) returns false
get_balance() returns 0
count_rejected() returns 2
Taking 80 from 50 is rejected. After the deposit the balance is exactly 80, so that withdrawal works and leaves 0. Taking 1 from 0 is the second rejection.
Constraints
0 <= opening <= 10001 <= amount <= 500in every deposit and withdrawal- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Grid With Row and Column AddsBeginner
- Guest Book SignaturesBeginner
- Odometer With a Trip MeterBeginner
- Panel of Light SwitchesBeginner
- Rectangle on the TableBeginner
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner