Advertisement
Niloy007

OOP Lab Assignment

Dec 11th, 2019
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. package BankAccount;
  2.  
  3. class InvalidBalanceException extends Exception {
  4.     public InvalidBalanceException(String msg) {
  5.         super(msg);
  6.     }
  7. }
  8.  
  9. class Account {
  10.     double balance;
  11.  
  12.     Account(double balance) throws InvalidBalanceException {
  13.         credit(balance);
  14.     }
  15.  
  16.     public void debit(double amount) throws InvalidBalanceException {
  17.         if((balance - amount) < 0) {
  18.             throw new InvalidBalanceException("The account balance can’t be less than zero");
  19.         } else {
  20.             balance -= amount;
  21.         }
  22.     }
  23.  
  24.     public void credit (double amount) throws InvalidBalanceException {
  25.         if(amount < 0) {
  26.             throw new InvalidBalanceException("The account balance can’t be less than zero");
  27.         } else {
  28.             balance += amount;
  29.         }
  30.     }
  31. }
  32.  
  33.  
  34. public class Bank {
  35.     public static void main(String[] args) {
  36.         Account account;
  37.         try {
  38.             account = new Account(1000);
  39.             account.credit(500);
  40.             System.out.println(account.balance);
  41.             account.debit(500);
  42.             System.out.println(account.balance);
  43.             account.debit(2000);
  44.             System.out.println(account.balance);
  45.         } catch (InvalidBalanceException e) {
  46.             System.out.println(e.getMessage());
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement