Advertisement
GamerBhai02

3. Banking System

Jan 16th, 2025
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2. class BankAccount{
  3.     private String accountHolder;
  4.     private double balance;
  5.     public BankAccount(String accountHolder){
  6.         this.accountHolder = accountHolder;
  7.         this.balance = 0.0;
  8.     }
  9.     public BankAccount(String accountHolder, double initialDeposit){
  10.         this.accountHolder = accountHolder;
  11.         this.balance = initialDeposit;
  12.     }
  13.     public double checkBalance(){
  14.         return balance;
  15.     }
  16.     public void deposit(double amount){
  17.         if(amount>0){
  18.             balance+=amount;
  19.             System.out.println("Deposited $"+amount);
  20.         }else{
  21.             System.out.println("Amount must be positive");
  22.         }
  23.     }
  24.     public void withdraw(double amount){
  25.         if(amount>0 && amount<=balance){
  26.             balance-=amount;
  27.             System.out.println("Withdrawn $"+amount);
  28.         }else{
  29.             System.out.println("Amount must be greater then or equal to your balance");
  30.         }
  31.     }
  32.     public void displayInfo(){
  33.         System.out.println("Account Holder's Name: "+accountHolder);
  34.         System.out.println("Account Balane: $"+balance);
  35.     }
  36. }
  37. public class BankingSystem{
  38.     public static void main(String[] args){
  39.         Scanner read = new Scanner(System.in);
  40.         System.out.println("Enter Account Holder's name:");
  41.         String name = read.nextLine();
  42.         BankAccount account = new BankAccount(name);
  43.         int choice;
  44.         do{
  45.             System.out.println("Banking System\n1. Check Balance\n2. Deposit Money\n3. Withdraw Money\n4. Display Account Information\n5. Exit\nEnter your choice:");
  46.             choice = read.nextInt();
  47.             switch(choice){
  48.                 case 1:
  49.                     System.out.println("Current Balance: $"+account.checkBalance());
  50.                     break;
  51.                 case 2:
  52.                     System.out.print("Enter the amount to deposit: ");
  53.                     double depAmount = read.nextDouble();
  54.                     account.deposit(depAmount);
  55.                     break;
  56.                 case 3:
  57.                     System.out.print("Enter the amount to withdraw: ");
  58.                     double witAmount = read.nextDouble();
  59.                     account.withdraw(witAmount);
  60.                     break;
  61.                 case 4:
  62.                     account.displayInfo();
  63.                     break;
  64.                 case 5:
  65.                     System.out.print("Exited");
  66.                     break;
  67.                 default:
  68.                     System.out.print("Invalid Choice");
  69.             }
  70.         }while(choice!=5);
  71.         read.close();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement