0% found this document useful (0 votes)
25 views5 pages

CSP008 Practical 07

Uploaded by

vt23cs90
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)
25 views5 pages

CSP008 Practical 07

Uploaded by

vt23cs90
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/ 5

7. Develop a program for banking application with exception handling.

Handle the
exceptions in following cases:
a) Account balance <1000
b) Withdrawal amount is greater than balance amount
c) Transaction count exceeds 3
d) One day transaction exceeds 1 lakh.

import java.util.Scanner;
// Custom Exception Classes
class InsufficientBalanceException extends Exception
{
public InsufficientBalanceException(String message)
{

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

class TransactionLimitExceededException extends Exception


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

// Bank Account Class


class BankAccount
{
private String name;
private double balance;
private int transactionCount;
private double dailyTotal;
public BankAccount(String name, double balance)
{
this.name = name;
this.balance = balance;
this.transactionCount = 0;
this.dailyTotal = 0;
}
public void deposit(double amount) throws TransactionLimitExceededException,
DailyLimitExceededException {
if (transactionCount >= 3)
{
throw new TransactionLimitExceededException("Transaction count exceeded 3 per
day.");
}
if (dailyTotal + amount > 100000)
{
throw new DailyLimitExceededException("Daily transaction limit of ₹1 lakh
exceeded.");
}
balance += amount;
transactionCount++;
dailyTotal += amount;
System.out.println("Deposited ₹" + amount + ". New Balance: ₹" + balance);
}
public void withdraw(double amount) throws InsufficientBalanceException,
WithdrawalExceedsBalanceException,
TransactionLimitExceededException, DailyLimitExceededException
{
if (transactionCount >= 3)
{
throw new TransactionLimitExceededException("Transaction count exceeded 3 per
day.");
}
if (dailyTotal + amount > 100000)
{
throw new DailyLimitExceededException("Daily transaction limit of ₹1 lakh
exceeded.");
}
if (balance < 1000)
{
throw new InsufficientBalanceException("Account balance is less than ₹1000.");
}
if (amount > balance)
{
throw new WithdrawalExceedsBalanceException("Withdrawal amount exceeds
current balance.");
}
balance -= amount;
transactionCount++;
dailyTotal += amount;
System.out.println("Withdrawn ₹" + amount + ". New Balance: ₹" + balance);
}
public void resetDay()
{
transactionCount = 0;
dailyTotal = 0;
System.out.println("\nNew day started. Transaction count and daily total reset.\n");
}
}
// Main Class
public class Practical07
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
BankAccount account = new BankAccount("Manish", 50000.0);

while (true)
{
System.out.println("\nChoose an option:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Reset Day");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = 0;
try
{
choice = sc.nextInt();
} catch (Exception e)
{
System.out.println("Invalid input! Please enter a number.");
sc.next(); // clear invalid input
continue;
}
try
{
switch (choice)
{
case 1:
System.out.print("Enter deposit amount: ");
double depositAmount = sc.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = sc.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.resetDay();
break;
case 4:
System.out.println("Thank you for using our banking service!");
sc.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please select again.");
}
} catch (InsufficientBalanceException | WithdrawalExceedsBalanceException |
TransactionLimitExceededException | DailyLimitExceededException e) {
System.out.println("Transaction failed: " + e.getMessage());
} catch (Exception e) {
System.out.println("Something went wrong: " + e.getMessage());
}
}
}
}

You might also like