0% found this document useful (0 votes)
112 views2 pages

Bank Account Withdrawal System

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)
112 views2 pages

Bank Account Withdrawal System

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

import [Link].

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) {


[Link] = 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;
[Link]("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([Link]);
BankAccount account = new BankAccount(1000.0);

try {
// Taking the user input
[Link]("Enter amount to withdraw: ");
double amount = [Link]();

//Attempting to withdraw the amount


[Link](amount);
}
catch (InvalidAmountException e) {
[Link]("Error: " + [Link]());
}
catch (InsufficientBalanceException e) {
[Link]("Error: " + [Link]());
}
catch (DailyLimitExceededException e) {
[Link]("Error: " + [Link]());
}
catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
}
finally {
//This block will get always executed
[Link]("Thank you for using our banking service.");
[Link]();
}
}
}

Common questions

Powered by AI

The three custom exception classes - InsufficientBalanceException, InvalidAmountException, and DailyLimitExceededException - each serve distinct roles: InsufficientBalanceException ensures users cannot withdraw more than they have; InvalidAmountException prevents users from entering nonsensical or negative amounts; and DailyLimitExceededException enforces regulatory constraints on daily transactions. Together, they provide a robust mechanism for pristine user interaction, guiding users to adhere to financial best practices while enhancing the trustworthiness of the application as it reliably communicates with users about their account's transactional constraints .

The program uses the try-catch block to intercept specific exceptions such as InvalidAmountException, InsufficientBalanceException, and DailyLimitExceededException, each of which provides targeted feedback to users. This separation allows for clearer communication about what went wrong, directly addressing the user's input. This tailored approach likely improves user satisfaction by providing informative error messages, reducing confusion, and demonstrating the application's competency in managing errors .

When a user attempts to withdraw money, they input an amount into the system. The system then calls the 'withdraw' method of the 'BankAccount' class. If the entered amount is invalid (less than or equal to zero), the system throws an InvalidAmountException and notifies the user. If the amount exceeds the account balance, an InsufficientBalanceException is thrown. If the amount exceeds the daily withdrawal limit, a DailyLimitExceededException is triggered. Each exception provides a specific error message to the user. Finally, regardless of success or exception, a 'thank you' message is displayed, demonstrating the completion of the interaction .

Exception handling through specific custom exceptions like InsufficientBalanceException and InvalidAmountException enhances maintainability by abstracting error-handling logic and reducing code complexity. It allows developers to pinpoint sections of code responsible for errors quickly, making debugging more straightforward. The clear separation of concerns reduces the cognitive load when managing code changes or analyzing the impact of potential new features or bug fixes, as developers can focus on handling specific cases without dealing with generic errors that lack contextual information .

The 'resetDailyLimit' method improves user experience by resetting the daily withdrawal allowance, enabling users to start each day with a fresh limit, essential for users needing to transact up to the maximum amount specified regularly. Regarding system integrity, it ensures that withdrawal operations reflect the intended limits accurately, preventing fraudulent attempts to exceed limits by leveraging unchecked carry-overs from previous days. Implementing this method affirms the system's robustness and transparency in enforcing transaction rules .

The 'BankAccount' class enforces the daily withdrawal limit through a variable 'dailyWithdrawalLimit' set to 500.0, and tracks 'totalWithdrawnToday'. Each time a withdrawal is processed, it checks if the specified amount plus the already withdrawn amount for the day surpasses this limit. If it does, a DailyLimitExceededException is thrown. Additionally, a method 'resetDailyLimit' is provided to reset the 'totalWithdrawnToday', ensuring that this limit enforcement is accurate as each new day starts .

If the Scanner object is not closed, it may lead to resource leaks, as the underlying input stream remains open, possibly causing exhaustions or locking resources unnecessarily. The given code mitigates this risk by closing the Scanner object within a finally block, ensuring that it is closed regardless of whether the try block runs successfully or any exceptions are thrown, thus safeguarding resource management .

The use of custom exceptions in this banking application likely stems from a need to provide precise control over error handling, improved code readability, and maintainability. Custom exceptions like InsufficientBalanceException, InvalidAmountException, and DailyLimitExceededException allow the application to handle specific error scenarios more clearly than generic exceptions. This specificity helps developers quickly understand and solve issues and provides users with clear messages about what went wrong. Additionally, it aligns with object-oriented best practices by encapsulating exception logic within the context of banking operations .

The 'withdraw' method ensures the balance does not drop below zero by first checking if the withdrawal amount exceeds the current balance. If it does, the method throws an InsufficientBalanceException. Additionally, it checks for an invalid withdrawal amount (amount <= 0) by throwing an InvalidAmountException. Therefore, these exceptions prevent any changes that would cause a non-positive balance, thus maintaining the account’s integrity .

Custom exceptions such as InsufficientBalanceException, InvalidAmountException, and DailyLimitExceededException are used in the 'BankAccount' class to handle specific error states, such as attempting to withdraw an amount that exceeds the balance, an invalid withdrawal amount, or exceeding the daily withdrawal limit. These exceptions provide clear, descriptive messages that help in troubleshooting and ensure that the application handles errors gracefully, enhancing its reliability by preventing unexpected crashes and providing meaningful feedback to users .

You might also like