This Java program simulates a simple bank account management system. It allows users to deposit and withdraw money from a bank account while handling custom exceptions for invalid transactions.
Main
class to start the program.
d
to deposit money.w
to withdraw money.q
to quit the program.
The program uses a custom exception class MyCustomException
to handle invalid transactions:
import java.util.Scanner;
class MyCustomException extends {
public MyCustomException(String ) {
}
}
class BankAccount {
private double balance;
public BankAccount() {
}
public double getBalance() {
return balance;
}
public void deposit(double amount) throws MyCustomException {
if (amount <= 0) {
}
balance += amount;
System.out.println("Deposit successful. New balance: " + balance);
}
public void withdraw(double amount) throws {
if (amount > balance) {
}
balance += amount;
System.out.println("Withdrawal successful. Remaining balance: " + balance);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount(500);
while (true) {
System.out.println("\nEnter 'd' to deposit, 'w' to withdraw, or 'q' to quit:");
if (choice.equalsIgnoreCase("q")) {
System.out.println("Exiting program. Final balance: " + account.getBalance());
break;
}
try {
}
} catch (MyCustomException e) {
System.out.println("Banking Exception: " + e.getMessage());
}
}
scanner.close();
}
}
Below are examples of the expected output when running the program:
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
d
Enter deposit amount: 200
Deposit successful. New balance: 700.0
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
w
Enter withdrawal amount: 100
Withdrawal successful. Remaining balance: 600.0
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
d
Enter deposit amount: -50
Banking Exception: Deposit amount must be greater than zero.
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
w
Enter withdrawal amount: 1000
Banking Exception: Insufficient funds. Attempted to withdraw: 1000.0
Please enter a valid withdrawal amount.
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
q
Exiting program. Final balance: 600.0