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

Java Exception Handling in Banking System

The document discusses an exception handling program in Java. It defines a Demonetization Exception class and an Account class with deposit, withdraw, and balance methods. A Customer class is used to test the Account class methods based on user input.

Uploaded by

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

Java Exception Handling in Banking System

The document discusses an exception handling program in Java. It defines a Demonetization Exception class and an Account class with deposit, withdraw, and balance methods. A Customer class is used to test the Account class methods based on user input.

Uploaded by

PRIYANSHU KUMARI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exception handling

6. Design and implement a Java program for the following requirements:


a) An Exception class called Demonetization Exception which returns the statement that
says “Deposit of Old currency of (Rs_____) crosses Rs. 5,000 and cannot be Deposited”.
b) A class called ‘Account’ that creates account with 500 Rs minimum balance with
following methods.
i. deposit(amount, currencyType) method to deposit amount. This class should
handle “Demonetization Exception” and print the message defined in this Exception
class. If a currency type is “OLD” and the amount is greater than 5,000 then throw
the Demonetization Exception, otherwise update the balance.
ii. currBalance() method that displays balance amount in the account.
iii. withdraw(amount) method to withdraw amount and update the balance. Use proper
control structure to check Balance should not go less than 500.
c) A ‘Customer’ class that creates Account object and call the methods deposit(),
withdraw() and currBalance() based on the user choice.

import [Link];

class demonetizationException extends Exception


{
float amount;
demonetizationException(float amount)
{
[Link]=amount;
}

public String toString()


{
return "Deposit of old currency of Rs "+amount+" cross Rs5000 and
cannot be deposited";
}
}

class account
{
float balance;
account()
{
balance = 500;
}

void deposit(float amount,String currencytype)


{
String currency = [Link]();
try {
if ([Link]("OLD") && amount > 5000)
throw new demonetizationException(amount);
else
balance = balance + amount;
[Link]("Updated Balance is "+balance);
}
catch (demonetizationException e)
{
[Link](e);
}
finally {
[Link]("Your money has been deposited
successfully!!");
}
}

void withdraw(float amount)


{
if (amount>balance)
{
[Link]("Error: low balance");
}
balance = balance - amount;
[Link]("Updated Balance is "+balance);
}

void getBalance()
{
[Link]("Balance: "+balance);
}
}

public class Program6{


public static void main(String[] args)
{
int choice;
float amount;
String currencytype;
Scanner sc = new Scanner([Link]);
account obj = new account();

for(;;)
{
[Link]("1:Deposit");
[Link]("2:Withdraw");
[Link]("3:Balance");
[Link]("Enter your choice");
choice = [Link]();

switch (choice) {
case 1 -> {
[Link]("Enter the amount to deposit");
amount = [Link]();
[Link]("Currency Type");
currencytype = [Link]();
[Link](amount, currencytype);
}
case 2 -> {
[Link]("Enter the amount to withdraw");
amount = [Link]();
[Link](amount);
}
case 3 -> [Link]();
}
}
}
}
Output

Common questions

Powered by AI

Control structures in the withdraw method include a condition that checks whether the requested withdrawal amount exceeds the available balance. If true, it prints an error message, "Error: low balance." However, this implementation is limited in effectiveness, as it continues to deduct the amount from the balance regardless of exceeding the current balance threshold, allowing the balance to go negative. It lacks a branch to effectively prevent operations that would lead to the account falling below a minimum required balance after the deduction, which could be improved by adding a condition that ensures withdrawals leave a minimum balance .

Exception handling in the deposit method is implemented through a try-catch-finally construct. When depositing, if the currency is "OLD" and the amount exceeds Rs 5,000, a Demonetization Exception is thrown. This exception is caught in the catch block, and an appropriate error message is displayed. The use of the finally block to print "Your money has been deposited successfully!!" regardless of whether an exception occurs, might lead to misunderstandings since the statement is executed even when a deposit has failed due to the exception. Ideally, the finally block should be used for cleanup actions, and the program should clearly communicate the success or failure of operations .

The menu-driven interface in the main method facilitates user interaction by providing organized access to account operations. Advantages include ease of use for sequential task execution, clarity in operation selection, and reduced input errors due to direct navigation paths. However, drawbacks include limited flexibility for users familiar with command-based or automated workflows, potential for redundancy in code when expanding menu options, and scalability issues as the number of functions or operations grows. Also, continuous looping without a termination option could lead to inefficient resource usage .

When a user attempts to deposit an amount with a currency type that neither is "OLD" nor exceeds the Rs 5,000 limit, the system directly updates the balance by adding the deposited amount to the current balance. Following this update, a message "Updated Balance is [updated balance]" is displayed. The exception handling mechanism is only specifically triggered for "OLD" currency deposits exceeding the Rs 5,000 limit, so it does not affect other types or amounts of deposits .

Improvements to the account balance checking system could include enhancing the logic within the withdraw method to include a check that prevents the balance from falling below Rs 500 by not only checking if the withdrawal exceeds the available balance but also ensuring it does not bring the balance below the specified minimum. This can be implemented by adjusting the condition to: if ((balance - amount) < 500), which would ensure any transaction leading to such a scenario is declined before updating the balance. Additionally, the code could notify users about unsuccessful transactions caused by this issue and maintain a log for audit purposes .

The withdraw method in the Account class checks if the withdrawal amount exceeds the current balance and prints an error message "Error: low balance." However, it does not return or prevent the balance subtraction operation from occurring even if the error condition is met. This logic flaw allows the balance to go negative, failing the constraint that the balance should not fall below Rs 500 as specified in the problem statement .

The current implementation lacks validation checks to ensure the integrity and security of the inputs, which could lead to injection vulnerabilities or incorrect balance changes if manipulated by a malicious user. For example, without scrubbing or verifying input types and ranges, a user could attempt to input invalid or malicious data to affect system behavior adversely. Furthermore, the lack of transaction logs or multi-factor verification for substantial deposits/withdrawals can lead to operational and financial discrepancies without traceability, emphasizing the need for enhanced security protocols .

The program ensures that only an instance of the Account class is used to perform operations by instantiating a single Account object in the main method and using this object across all user operations via method calls. This design follows the principle of encapsulation, allowing controlled access and modifications to account data. Changing this requirement to allow multiple instances could lead to inconsistencies in account state, increased complexity in managing account interactions, and potential conflicts, especially if instances represent separate accounts within shared resources .

The Demonetization Exception impacts the deposit method by introducing a custom exception that specifically handles cases where old currency deposits exceed Rs 5,000. Its purpose is to prevent the deposit of old currency exceeding this limit, adhering to regulatory constraints regarding demonetized currency. When the method detects an old currency deposit exceeding Rs 5,000, it throws this custom exception, which is then caught in a try-catch block to provide a user-friendly error message, "Deposit of old currency of Rs [amount] crosess Rs 5,000 and cannot be deposited." The purpose is to enforce financial rules and inform users of transaction constraints in a structured manner .

The Customer class plays the role of creating an Account object and facilitating user interaction with the account through a menu-driven interface. It interacts with the Account class by providing options to deposit money, withdraw money, or view the current balance. Based on user input via a scanner, it calls the appropriate methods in the Account class, such as deposit(), withdraw(), or getBalance(), to perform the desired operations .

You might also like