Advertisement
karlakmkj

Mini Project - Bank Account balance

Sep 6th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package bankaccount;
  2.  
  3. public class BankAccount {
  4.     private double balance;
  5.  
  6.     //Constructor
  7.     public BankAccount() {
  8.         balance = 0.0;
  9.     }
  10.    
  11.     //deposit method
  12.     public void deposit(double amount) {
  13.         balance = balance + amount;
  14.     }
  15.    
  16.     //withdraw method
  17.     public void withdraw(double amount) throws InsufficientFundException {
  18.         if(amount> balance) {
  19.             throw new InsufficientFundException("Insufficient balance. " + "Withdraw process could not be updated.");
  20.         }
  21.         balance = balance - amount;
  22.     }
  23.  
  24.     //getter
  25.     public double getBalance() {
  26.         return balance;
  27.     }
  28. }
  29. =================================================================================================================================
  30. package bankaccount;
  31.  
  32. public class InsufficientFundException extends Exception {
  33.     //must extend the parent class
  34.    
  35.     private String message;
  36.    
  37.     //Constructor
  38.     public InsufficientFundException(String message) {
  39.         this.message = message;
  40.     }
  41.  
  42.     public String getMessage() {
  43.         return message;
  44.     }
  45. }
  46. =================================================================================================================================
  47. package bankaccount;
  48.  
  49. import java.util.Scanner;
  50.  
  51. public class BankAccountTester {
  52.  
  53.     public static void main(String[] args) {
  54.         BankAccount account = new BankAccount();
  55.         Scanner input = new Scanner(System.in);
  56.         int choice;
  57.         do {
  58.             System.out.println("------BANK ACCOUNT MENU------");
  59.             System.out.println("1-Deposit");
  60.             System.out.println("2-Withdraw");
  61.             System.out.println("3-Show current balance");
  62.             System.out.println("4-Quit");
  63.             System.out.println("Select an option: ");
  64.             choice = input.nextInt();
  65.             switch (choice) {
  66.                 case 1:
  67.                     System.out.println("Deposit Amount: ");
  68.                     account.deposit(input.nextInt());
  69.                     System.out.println("Your current balance is: " + account.getBalance());
  70.                     break;
  71.                 case 2:
  72.                     System.out.println("Withdraw Amount: ");
  73.                     try {
  74.                         account.withdraw(input.nextInt());
  75.                         System.out.println("Your current balance is: " + account.getBalance());
  76.                     } catch (InsufficientFundException e) {
  77.                         System.out.println(e.toString());      
  78.                     }
  79.                     break;
  80.                 case 3:
  81.                     System.out.println("Current balance: " + account.getBalance());
  82.                     break;
  83.             }
  84.         } while(choice!=4);
  85.         System.out.println("Thank you for using our service. Goodbye!");
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement