CSP008 Practical 07
CSP008 Practical 07
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);
}
}
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());
}
}
}
}