0% found this document useful (0 votes)
17 views3 pages

F018 Aditya Sharma Oop Lab 07

The document presents a Java implementation of a banking system with custom exceptions for insufficient funds and invalid amounts. It includes a BankAccount class with methods for depositing and withdrawing money, along with error handling for invalid transactions. A demo class demonstrates the functionality of the BankAccount class, showcasing deposits, withdrawals, and exception handling.

Uploaded by

Assassin Hackers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

F018 Aditya Sharma Oop Lab 07

The document presents a Java implementation of a banking system with custom exceptions for insufficient funds and invalid amounts. It includes a BankAccount class with methods for depositing and withdrawing money, along with error handling for invalid transactions. A demo class demonstrates the functionality of the BankAccount class, showcasing deposits, withdrawals, and exception handling.

Uploaded by

Assassin Hackers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

OOP LAB 07

Name: Aditya Sharma Roll Number: F018


Sem/Batch: Sem 8/F1 Date: 11/04/2025

class InsufficientFundsException extends Exception {​


public InsufficientFundsException(String message) {​
super(message);​
}​
}​

class InvalidAmountException extends Exception {​
public InvalidAmountException(String message) {​
super(message);​
}​
}​

// BankAccount class​
class BankAccount {​
private final String accountNumber;​
private final String holderName;​
private double balance;​

public BankAccount(String accountNumber, String holderName, double
initialBalance) {​
this.accountNumber = accountNumber;​
this.holderName = holderName;​
this.balance = initialBalance;​
}​

public void deposit(double amount) {​
if (amount <= 0) {​
System.out.println("Deposit amount must be greater than zero.");​
return;​
}​
balance += amount;​
System.out.println("Deposited: $" + amount + " | New Balance: $" +
balance);​
}​

public void withdraw(double amount) throws InsufficientFundsException,
InvalidAmountException {​
if (amount <= 0) {​
throw new InvalidAmountException("Withdrawal amount must be greater
than zero.");​
}​

if (amount > balance) {​
throw new InsufficientFundsException("Insufficient funds. Available
balance: $" + balance);​
}​

balance -= amount;​
System.out.println("Withdrawn: $" + amount + " | Remaining Balance: $" +
balance);​
}​

public double getBalance() {​
return balance;​
}​

public String getAccountDetails() {​
return "Account Number: " + accountNumber + ", Holder: " + holderName +
", Balance: $" + balance;​
}​
}​

class BankingSystemDemo {​
public static void main(String[] args) {​
BankAccount account = new BankAccount("123456", "Alice", 500.00);​
System.out.println(account.getAccountDetails());​

account.deposit(200);​

try {​
account.withdraw(100);​
account.withdraw(700); // This will throw InsufficientFundsException​
account.withdraw(-50); // This will throw InvalidAmountException​
} catch (InsufficientFundsException | InvalidAmountException e) {​
System.out.println("Exception: " + e.getMessage());​
}​

System.out.println("Final Balance: $" + account.getBalance());​
}​
}

You might also like