import java.util.
Scanner;
// Custom exception for invalid amount
class InvalidAmountException extends Exception {
public InvalidAmountException(String message) {
super(message);
}
}
// Custom exception for insufficient funds
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
// BankAccount class
class BankAccount {
private String accountHolder;
private double balance;
public BankAccount(String accountHolder) {
this.accountHolder = accountHolder;
this.balance = 0.0;
}
public void deposit(double amount) throws InvalidAmountException {
if (amount <= 0) {
throw new InvalidAmountException("Deposit amount must be greater than zero.");
}
balance += amount;
System.out.println("Deposited: $" + amount);
}
public void withdraw(double amount) throws InvalidAmountException, InsufficientFundsException {
if (amount <= 0) {
throw new InvalidAmountException("Withdrawal amount must be greater than zero.");
}
if (amount > balance) {
throw new InsufficientFundsException("Insufficient balance for this withdrawal.");
}
balance -= amount;
System.out.println("Withdrawn: $" + amount);
}
public void checkBalance() {
System.out.println("Current balance: $" + balance);
}
}
// Main class
public class BankManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter account holder's name: ");
String name = scanner.nextLine();
BankAccount account = new BankAccount(name);
while (true) {
System.out.println("\n--- Menu ---");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
System.out.print("Choose an option (1-4): ");
int choice = scanner.nextInt();
try {
switch (choice) {
case 1:
System.out.print("Enter deposit amount: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.checkBalance();
break;
case 4:
System.out.println("Thank you for using the Bank Management System!");
scanner.close();
return;
default:
System.out.println("Invalid option. Please try again.");
}
} catch (InvalidAmountException | InsufficientFundsException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Unexpected error: " + e.getMessage());
}
}
}
}