AlgoMaster Logo
AlgoMasterDesign Bank Accounteasy

Design Bank Account

easy

Design a BankAccount class that tracks a single account balance and enforces its own deposit and withdrawal rules.

Implement the BankAccount class:

  • BankAccount(String accountNumber, String ownerName) creates an account with the given number and owner. The balance starts at 0.
  • boolean deposit(double amount) adds amount to the balance and returns true. If amount is not greater than zero, it changes nothing and returns false.
  • boolean withdraw(double amount) subtracts amount from the balance and returns true. If amount is not greater than zero, or the balance is smaller than amount, it changes nothing and returns false.
  • double getBalance() returns the current balance.
  • String getSummary() returns the account as "<ownerName> (<accountNumber>) | Balance: $<balance>", with the balance formatted to exactly two decimal places and no thousand separators. An account owned by John Doe with number ACC-123 and a balance of 500 gives "John Doe (ACC-123) | Balance: $500.00".

The balance must not be reachable or assignable from outside the class. Every rule above has to be enforced by the object itself.

Example 1:

Input:

Output:

Explanation:

  • BankAccount account = new BankAccount("ACC-123", "John Doe"); // balance is 0
  • account.deposit(1000); // returns true, balance is 1000
  • account.getBalance(); // returns 1000.0
  • account.withdraw(500); // returns true, balance is 500
  • account.getBalance(); // returns 500.0
  • account.withdraw(1000); // returns false, 1000 is more than the balance, nothing changes
  • account.getSummary(); // returns "John Doe (ACC-123) | Balance: $500.00"
Example 2:

Input:

Output:

Explanation: A negative deposit, a zero deposit, and a withdrawal from an empty account are all rejected, so the balance stays at 0.

Constraints:
  • 1 <= accountNumber.length <= 20
  • 1 <= ownerName.length <= 50
  • -10^6 <= amount <= 10^6
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Encapsulation

    Full marks when accountNumber, ownerName and balance are all private (or the language's closest equivalent) and there is no setter that lets a caller assign the balance directly. Lose points for public or package-visible mutable state, or for a setBalance method that bypasses the deposit and withdraw rules.

  • Rule placement

    Full marks when the amount validation lives inside deposit and withdraw so the object protects its own balance. Lose points if the checks are missing, duplicated across methods in a way that could drift, or clearly written to satisfy specific test values rather than the stated rule.

  • Structure and naming

    Full marks for a single cohesive class with descriptive names and no dead code. Lose points for unused fields, stray debug printing, or logic unrelated to a bank account.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new BankAccount("ACC-123", "John Doe")null
deposit(1000)true
getBalance()1000
withdraw(500)true
getBalance()500
withdraw(1000)false
getSummary()"John Doe (ACC-123) | Balance: $500.00"

A deposit of 1000 succeeds. Withdrawing 500 succeeds and leaves 500. Withdrawing 1000 from a balance of 500 fails and changes nothing.

Run checks these cases. Submit also runs a larger hidden set.