Advertisement
GamerBhai02

Exceptional Handling

Nov 11th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | Source Code | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. // Custom exception for insufficient funds
  4. class InsufficientFundsException extends Exception {
  5.  public InsufficientFundsException(String message) {
  6.  super(message);
  7.  }
  8. }
  9. // Class to represent a bank account
  10. class BankAccount1 {
  11.  private String accountHolder;
  12.  private double balance;
  13.  // Constructor to initialize account holder's name and initial balance
  14.  public BankAccount1(String accountHolder) {
  15.  this.accountHolder = accountHolder;
  16.  this.balance = 0.0; // Initial balance set to zero
  17.  }
  18.  // Method to deposit money
  19.  public void deposit(double amount) {
  20.  if (amount <= 0) {
  21.  System.out.println("Deposit amount must be positive.");
  22.  return;
  23.  }
  24.  balance += amount;
  25.  System.out.println("Deposited: $" + amount);
  26.  }
  27.  // Method to withdraw money
  28.  public void withdraw(double amount) throws InsufficientFundsException {
  29.  if (amount <= 0) {
  30.  System.out.println("Withdrawal amount must be positive.");
  31.  return;
  32. 33
  33.  }
  34.  if (amount > balance) {
  35.  throw new InsufficientFundsException("Insufficient funds for withdrawal.");
  36.  }
  37.  balance -= amount;
  38.  System.out.println("Withdrawn: $" + amount);
  39.  }
  40.  // Method to check balance
  41.  public double checkBalance() {
  42.  return balance;
  43.  }
  44.  // Method to get account holder's name
  45.  public String getAccountHolder() {
  46.  return accountHolder;
  47.  }
  48. }
  49. // Main class to manage the bank account transactions
  50. public class BAM {
  51.  public static void main(String[] args) {
  52.  Scanner scanner = new Scanner(System.in);
  53.  // Get account holder's name and create the account
  54.  System.out.print("Enter account holder's name: ");
  55.  String name = scanner.nextLine();
  56.  BankAccount1 account = new BankAccount1(name);
  57.  int choice = -1;
  58.  do {
  59.  // Display menu
  60.  System.out.println("\n--- Bank Account Management ---");
  61.  System.out.println("1. Check Balance");
  62.  System.out.println("2. Deposit Money");
  63.  System.out.println("3. Withdraw Money");
  64.  System.out.println("4. Exit");
  65.  System.out.print("Enter your choice: ");
  66. 34
  67.  try {
  68.  choice = scanner.nextInt(); // Get user's menu choice
  69.  switch (choice) {
  70.  case 1:
  71.  System.out.println("Current Balance: $" + account.checkBalance());
  72.  break;
  73.  case 2:
  74.  System.out.print("Enter amount to deposit: $");
  75.  double depositAmount = scanner.nextDouble();
  76.  account.deposit(depositAmount);
  77.  break;
  78.  case 3:
  79.  System.out.print("Enter amount to withdraw: $");
  80.  double withdrawAmount = scanner.nextDouble();
  81.  account.withdraw(withdrawAmount);
  82.  break;
  83.  case 4:
  84.  System.out.println("Exiting...");
  85.  break;
  86.  default:
  87.  System.out.println("Invalid choice. Please try again.");
  88.  }
  89.  } catch (InsufficientFundsException e) {
  90.  // Handle insufficient funds exception
  91.  System.out.println("Error: " + e.getMessage());
  92.  } catch (InputMismatchException e) {
  93.  // Handle invalid input (non-integer or non-double values)
  94.  System.out.println("Invalid input. Please enter a number.");
  95.  scanner.next(); // Clear the invalid input
  96.  }
  97.  } while (choice != 4);
  98.  scanner.close();
  99.  }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement