Advertisement
tilahun1

Untitled

Jul 13th, 2022
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package test1;
  2.  
  3. import java.io.*;
  4. public class CheckingAccount
  5. {
  6. private double balance;
  7. private int number;
  8. public CheckingAccount(int number)
  9. {
  10. this.number = number;
  11. }
  12. public void deposit(double amount)
  13. {
  14. balance += amount;
  15. }
  16. public void withdraw(double amount) throws InsufficientFundsException
  17. {
  18. if(amount <= balance)
  19. {
  20. balance -= amount;
  21. System.out.println("Withdraw Done......");
  22. }
  23. else
  24. {
  25. double needs = amount - balance;
  26. throw new InsufficientFundsException(needs);
  27. } }
  28. public double getBalance()
  29. { return balance;
  30. }
  31. public int getNumber()
  32. {return number;
  33. } }
  34. =================================================================================================================
  35. package test1;
  36.  
  37. public class InsufficientFundsException extends java.lang.Exception
  38. {
  39. private double amount;
  40. public InsufficientFundsException(double amount)
  41. {
  42. this.amount = amount;
  43. }
  44. public double getAmount()
  45. {
  46. return amount;
  47. }
  48. }
  49. ==================================================================================================================
  50. package test1;
  51.  
  52. public class BankDemo
  53. {
  54. public static void main(String [] args)
  55. {CheckingAccount c = new CheckingAccount(101);
  56. System.out.println("Depositing $500...");
  57. c.deposit(500.00);
  58. try
  59. {
  60. System.out.println("\nWithdrawing $100...");
  61. c.withdraw(100.00);
  62. System.out.println("\nWithdrawing $600...");
  63. c.withdraw(600.00);
  64. }catch(InsufficientFundsException e)
  65. {
  66. System.out.println("Sorry, but you Balance is insufficent $"
  67. + e.getAmount());
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement