0% found this document useful (0 votes)
11 views8 pages

PSOOPL - Experiment 9-2

The document outlines a Java program designed to handle banking transactions through an abstract class 'Account' and its subclass 'SavingAccount'. It includes methods for depositing, withdrawing, adding interest, and transferring funds, while implementing exception handling for various error scenarios. The program demonstrates object-oriented principles and emphasizes the importance of robust error management in financial applications.

Uploaded by

dhruvvbhalani
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)
11 views8 pages

PSOOPL - Experiment 9-2

The document outlines a Java program designed to handle banking transactions through an abstract class 'Account' and its subclass 'SavingAccount'. It includes methods for depositing, withdrawing, adding interest, and transferring funds, while implementing exception handling for various error scenarios. The program demonstrates object-oriented principles and emphasizes the importance of robust error management in financial applications.

Uploaded by

dhruvvbhalani
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/ 8

Name: Dhruv Bhalani

UID: 2024300019

Experiment No. 9

AIM: To handle the errors in Java

Program 1

PROBLEM There is an abstract class Account


STATEMENT :
Attribute:-

● Name

● Balance

● Acc_No

Method:-

● Deposit - abstract method

● withdraw - abstract method

● display - abstract method

Saving Account inherits the Account class and provides the implementation
for the methods accordingly

Saving Account class Attribute:-

● interestRate

● minBalance

Method

● addInterest: handle Arithmetic Exception

● transfer():

Note:

● Balance cannot be less than 0.

● In a Saving account if minBalance is set then for that the balance


cannot go less than that amount. If it goes, an error must be shown.

● let the user deposit to or withdraw from the account. For each

transaction, a message is displayed to indicate the status of the

transaction: successful or failed. In case of failure, the failure

reason is reported.

● The possible Exceptions are negative-amount-exception (in both

deposit and withdraw transaction) and insufficient-amount-

exception ( in withdraw transaction).

For the above scenario write an interactive program in Java. Also, show
output for different use cases.

PROGRAM: import java.util.Scanner;

class NegativeAmountException extends Exception {


public NegativeAmountException(String message) {
super(message);
}
}

class InsufficientAmountException extends Exception {


public InsufficientAmountException(String message) {
super(message);
}
}

abstract class Account {


protected String name;
protected double balance;
protected String accNo;

public Account(String name, double balance, String accNo) {


this.name = name;
this.balance = balance;
this.accNo = accNo;
}

abstract void deposit(double amount) throws


NegativeAmountException;

abstract void withdraw(double amount) throws


NegativeAmountException, InsufficientAmountException;

abstract void display();


}

class SavingAccount extends Account {


private double interestRate;
private double minBalance;

public SavingAccount(String name, double balance, String accNo,


double interestRate, double minBalance) {
super(name, balance, accNo);
this.interestRate = interestRate;
this.minBalance = minBalance;
}

@Override
void deposit(double amount) throws NegativeAmountException {
if (amount < 0)
throw new NegativeAmountException("Deposit failed: Amount
cannot be negative.");
balance += amount;
System.out.println("Deposit successful. Current Balance: " +
balance);
}

@Override
void withdraw(double amount) throws NegativeAmountException,
InsufficientAmountException {
if (amount < 0)
throw new NegativeAmountException("Withdraw failed:
Amount cannot be negative.");
if (balance - amount < minBalance)
throw new InsufficientAmountException("Withdraw failed:
Balance would go below minimum allowed (" + minBalance + ")");
balance -= amount;
System.out.println("Withdraw successful. Current Balance: " +
balance);
}

public void addInterest() {


try {
double interest = balance * (interestRate / 100);
balance += interest;
System.out.println("Interest added successfully. New Balance: "
+ balance);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception occurred while
adding interest.");
}
}

public void transfer(SavingAccount toAccount, double amount) {


try {
this.withdraw(amount);
toAccount.deposit(amount);
System.out.println("Transfer successful to account " +
toAccount.accNo);
} catch (Exception e) {
System.out.println("Transfer failed: " + e.getMessage());
}
}

@Override
void display() {
System.out.println("Account No: " + accNo);
System.out.println("Name: " + name);
System.out.println("Balance: " + balance);
System.out.println("Interest Rate: " + interestRate);
System.out.println("Minimum Balance: " + minBalance);
}
}

class BankSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

SavingAccount acc1 = new SavingAccount("Alice", 5000, "A101",


4.5, 1000);
SavingAccount acc2 = new SavingAccount("Bob", 3000, "B202",
4.0, 500);

System.out.println("Welcome to Bank System");


int choice;
do {
System.out.println("\n1. Display Account Info");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Add Interest");
System.out.println("5. Transfer");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
choice = sc.nextInt();

SavingAccount currentAcc = acc1;


switch (choice) {
case 1:
currentAcc.display();
break;
case 2:
System.out.print("Enter deposit amount: ");
double dep = sc.nextDouble();
try {
currentAcc.deposit(dep);
} catch (NegativeAmountException e) {
System.out.println(e.getMessage());
}
break;
case 3:
System.out.print("Enter withdraw amount: ");
double wdr = sc.nextDouble();
try {
currentAcc.withdraw(wdr);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 4:
currentAcc.addInterest();
break;
case 5:
System.out.print("Enter amount to transfer to Bob: ");
double amt = sc.nextDouble();
currentAcc.transfer(acc2, amt);
break;
case 6:
System.out.println("Thank you for using Bank System!");
break;
default:
System.out.println("Invalid option.");
}
} while (choice != 6);

sc.close();
}
}

RESULT:
CONCLUSION:

This Java program demonstrates the use of abstract classes and


inheritance to model a simple banking system with SavingAccount
functionality. It effectively handles user interactions, performs transactions
like deposit, withdraw, add interest, and transfer, and showcases robust
exception handling using multiple catch blocks for
ArrayIndexOutOfBoundsException, NullPointerException, and
ArithmeticException. Through this, it illustrates the importance of
defensive programming and structured object-oriented design in real-world
applications.

You might also like