Assignment 4 (Oop) .PDF - 20241130 - 233241 - 0000
Assignment 4 (Oop) .PDF - 20241130 - 233241 - 0000
23104A0023
EXTC A
ASSIGNMENT 4
You are tasked with writing a Java program to simulate a simple bank account system. The
program should handle exceptions related to common banking operations such as deposits,
withdrawals, and balance inquiries. The system should allow a user to perform operations on
a bank account, with proper exception handling for scenarios such as insufficient balance,
invalid account number, and invalid deposit amounts.
CODE:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
super(message);
}
super(message);
super(message);
// BankAccount class
class BankAccount {
this.accountNumber = accountNumber;
this.balance = 0.0;
return accountNumber;
}
return balance;
}
if (amount <= 0) {
throw new InvalidDepositAmountException("Deposit amount must be greater than zero.");
}
balance += amount;
}
public void withdraw(double amount) throws InsufficientBalanceException {
}
balance -= amount;
System.out.println("Withdrew: " + amount);
class Bank {
throw new InvalidAccountException("Account number " + accountNumber + " does not exist.");
return account;
// Main class
while (true) {
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
switch (choice) {
case 1:
System.out.print("Enter account number: ");
break;
case 2:
try {
account.deposit(depositAmount);
System.out.println("New balance: " + account.getBalance());
case 3:
try {
account.withdraw(withdrawalAmount);
System.out.println("New balance: " + account.getBalance());
}
break;
case 4:
try {
} catch (InvalidAccountException e) {
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
}
}