Build a BankAccount class that keeps its balance private and lets other code change it only through methods that check the rules. This is encapsulation: the object protects its own data.
Private balance. Mark the balance field private double balance, so only the methods of BankAccount can touch it.
What the class must have (Java):
public BankAccount(double initialBalance): creates an account that starts with the given balance.public void deposit(double amount): addsamountto the balance. Ifamountis zero or negative, do nothing (no error, the balance stays the same).public boolean withdraw(double amount): ifamountis zero or negative, or larger than the balance, change nothing and returnfalse. Otherwise subtract it from the balance and returntrue. Withdrawing exactly the whole balance is allowed (the balance becomes 0).public double getBalance(): returns the current balance. It is the only way for outside code to read it.
The starter prints the balance with (int)account.getBalance(): the (int) cast drops the .0 so the output is Balance: 120, not Balance: 120.0.
The driver. The driver at the end of the starter code is already written and uses your class. It runs these checks:
class Solution {
public void mainFunction() {
BankAccount account = new BankAccount(100);
account.deposit(50);
account.withdraw(30);
account.withdraw(200);
System.out.println("Balance: " + (int)account.getBalance());
account.deposit(-20);
account.deposit(0);
account.withdraw(-5);
System.out.println("Balance: " + (int)account.getBalance());
if (account.withdraw(120)) {
System.out.println("Withdrew all");
}
System.out.println("Balance: " + (int)account.getBalance());
if (!account.withdraw(1)) {
System.out.println("Rejected");
}
}
}
Walk through it: start with 100, deposit 50 (150), withdraw 30 (120), withdraw 200 is refused (still 120). Then deposit -20, deposit 0 and withdraw -5 are all ignored (still 120). Withdrawing 120 succeeds and empties the account, so the balance is 0, and withdrawing 1 more is refused.
Expected output:
Balance: 120
Balance: 120
Withdrew all
Balance: 0
Rejected
Both
deposit and withdraw start with a guard that rejects amount <= 0. Write the guard first, then the change to the balance.
private double balance;, then in the constructor balance = initialBalance;.
if (amount <= 0 || amount > balance) return false;, then balance -= amount; and return true;.
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
if (amount <= 0) return;
balance += amount;
}
public boolean withdraw(double amount) {
if (amount <= 0 || amount > balance) return false;
balance -= amount;
return true;
}
public double getBalance() {
return balance;
}
}
class Solution {
public void mainFunction() {
BankAccount account = new BankAccount(100);
account.deposit(50);
account.withdraw(30);
account.withdraw(200);
System.out.println("Balance: " + (int)account.getBalance());
account.deposit(-20);
account.deposit(0);
account.withdraw(-5);
System.out.println("Balance: " + (int)account.getBalance());
if (account.withdraw(120)) {
System.out.println("Withdrew all");
}
System.out.println("Balance: " + (int)account.getBalance());
if (!account.withdraw(1)) {
System.out.println("Rejected");
}
}
}
Free plan: 30 tutor messages. Premium is unlimited.
