Advertisement
GamerBhai02

6. Banking Exception

Jan 16th, 2025
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | Source Code | 0 0
  1. import java.util.*;
  2. class InsufficientFundsException extends Exception{
  3.     public InsufficientFundsException(String message){
  4.         super(message);
  5.     }
  6. }
  7. class BankAccount{
  8.     private String accountHolder;
  9.     private double balance;
  10.     public BankAccount(String accountHolder){
  11.         this.accountHolder = accountHolder;
  12.         this.balance = 0.0;
  13.     }
  14.     public BankAccount(String accountHolder, double initialDeposit){
  15.         this.accountHolder = accountHolder;
  16.         this.balance = initialDeposit;
  17.     }
  18.     public double checkBalance(){
  19.         return balance;
  20.     }
  21.     public void deposit(double amount){
  22.         if(amount>0){
  23.             balance+=amount;
  24.             System.out.println("Deposited $"+amount);
  25.         }else{
  26.             System.out.println("Amount must be positive");
  27.         }
  28.     }
  29.     public void withdraw(double amount) throws InsufficientFundsException{
  30.         if(amount<=0){
  31.             System.out.println("Amount must be positive");
  32.             return;
  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.     public void displayInfo(){
  41.         System.out.println("Account Holder's Name: "+accountHolder);
  42.         System.out.println("Account Balance: $"+balance);
  43.     }
  44. }
  45. public class BankingSystem{
  46.     public static void main(String[] args){
  47.         Scanner read = new Scanner(System.in);
  48.         System.out.println("Enter Account Holder's name:");
  49.         String name = read.nextLine();
  50.         BankAccount account = new BankAccount(name);
  51.         int choice=-1;
  52.         do{
  53.             System.out.println("Banking System\n1. Check Balance\n2. Deposit Money\n3. Withdraw Money\n4. Display Account Information\n5. Exit\nEnter your choice:");
  54.             try{
  55.             choice = read.nextInt();
  56.             switch(choice){
  57.                 case 1:
  58.                     System.out.println("Current Balance: $"+account.checkBalance());
  59.                     break;
  60.                 case 2:
  61.                     System.out.print("Enter the amount to deposit: ");
  62.                     double depAmount = read.nextDouble();
  63.                     account.deposit(depAmount);
  64.                     break;
  65.                 case 3:
  66.                     System.out.print("Enter the amount to withdraw: ");
  67.                     double witAmount = read.nextDouble();
  68.                     account.withdraw(witAmount);
  69.                     break;
  70.                 case 4:
  71.                     account.displayInfo();
  72.                     break;
  73.                 case 5:
  74.                     System.out.print("Exited");
  75.                     break;
  76.             }
  77.             }catch(InsufficientFundsException e){
  78.                 System.out.println("Error: "+e.getMessage());
  79.             }catch(InputMismatchException e){
  80.                 System.out.println("Invalid input, please enter valid number");
  81.                 read.next();
  82.             }
  83.         }while(choice!=5);
  84.         read.close();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement