0% found this document useful (0 votes)
5 views

import java.util.Scanner;

The document contains a Java program that simulates a bank account with custom exceptions for handling insufficient balance, invalid withdrawal amounts, and exceeding daily withdrawal limits. It includes a main class that allows users to input withdrawal amounts and handles various exceptions accordingly. The program ensures that all transactions are validated and provides feedback to the user while maintaining a daily withdrawal limit.

Uploaded by

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

import java.util.Scanner;

The document contains a Java program that simulates a bank account with custom exceptions for handling insufficient balance, invalid withdrawal amounts, and exceeding daily withdrawal limits. It includes a main class that allows users to input withdrawal amounts and handles various exceptions accordingly. The program ensures that all transactions are validated and provides feedback to the user while maintaining a daily withdrawal limit.

Uploaded by

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

import java.util.

Scanner;

// Custom exception for insufficient balance


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

// Custom exception for invalid amount


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

// Custom exception for exceeding daily withdrawal limit


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

class BankAccount {
private double balance;
private double dailyWithdrawalLimit = 500.0;
private double totalWithdrawnToday = 0.0;

public BankAccount(double balance) {


this.balance = balance;
}

// Method to withdraw money


public void withdraw(double amount) throws InsufficientBalanceException,
InvalidAmountException, DailyLimitExceededException {
if (amount <= 0) {
throw new InvalidAmountException("Amount must be greater than zero.");
}
if (amount > balance) {
throw new InsufficientBalanceException("Insufficient balance in your
account.");
}
if (totalWithdrawnToday + amount > dailyWithdrawalLimit) {
throw new DailyLimitExceededException("Daily withdrawal limit exceeded.
You can withdraw only Rs. " + dailyWithdrawalLimit + " per day.");
}
balance = balance - amount;
totalWithdrawnToday = totalWithdrawnToday + amount;
System.out.println("Withdrawal successful. Remaining balance: " + balance);
}

public double getBalance() {


return balance;
}

public void resetDailyLimit() {


totalWithdrawnToday = 0.0;
}
}
public class main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount(1000.0);

try {
// Taking the user input
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();

//Attempting to withdraw the amount


account.withdraw(amount);
}
catch (InvalidAmountException e) {
System.out.println("Error: " + e.toString());
}
catch (InsufficientBalanceException e) {
System.out.println("Error: " + e.toString());
}
catch (DailyLimitExceededException e) {
System.out.println("Error: " + e.toString());
}
catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.toString());
}
finally {
//This block will get always executed
System.out.println("Thank you for using our banking service.");
scanner.close();
}
}
}

You might also like