Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. public class Account implements AccountInterface {
  2. private long accountNumber;
  3. private double balance;
  4.  
  5. public Account(long accountNumber) {
  6. this.accountNumber = accountNumber;
  7. this.balance = 0;
  8. }
  9.  
  10. @Override
  11. public long getNumber() {
  12. return this.accountNumber;
  13. }
  14.  
  15. @Override
  16. public double getBalance() {
  17. return this.balance;
  18. }
  19.  
  20. protected void updateBalance(double amount) {
  21. this.balance+=amount;
  22. }
  23.  
  24. @Override
  25. public void deposit(double amount) {
  26. if(amount <= 0) {
  27. throw new NegativeAmountException("The amount to deposit should be positive!");
  28. }
  29. this.balance+=amount;
  30. }
  31.  
  32. @Override
  33. public void withdraw(double amount) {
  34. if(amount <= 0) {
  35. throw new NegativeAmountException("The amount to withdraw should be positive!");
  36. }
  37. if (this.balance >= amount) {
  38. this.balance-=amount;
  39. }
  40. else {
  41. throw new NegativeBalanceException("You don't have that much balance to withdraw!");
  42. }
  43. }
  44. }
  45.  
  46.  
  47. public class OverdraftAccount extends Account {
  48. private double overdraftAmount;
  49.  
  50. public OverdraftAccount(long accountNumber, double overdraftAmount) {
  51. super(accountNumber);
  52. this.overdraftAmount=overdraftAmount;
  53. }
  54.  
  55. @Override
  56. public void withdraw(double amount) {
  57. if(amount < 0) {
  58. throw new NegativeAmountException("The amount to withdraw should be positive!");
  59. }
  60.  
  61. double currentBalance = this.getBalance();
  62. double minBalanceAllowed = currentBalance + overdraftAmount;
  63.  
  64. if(minBalanceAllowed >= amount) {
  65. this.updateBalance(-amount);
  66. }
  67. else {
  68. throw new ExceededOverdraftException("Not enough overdraft for this withdraw!");
  69. }
  70. }
  71. }
  72.  
  73. public class RestrictedWithdrawAccount extends Account {
  74. private double withdrawRestriction;
  75.  
  76. public RestrictedWithdrawAccount(long accountNumber,double withdrawRestriction) {
  77. super(accountNumber);
  78. this.withdrawRestriction=withdrawRestriction;
  79. }
  80.  
  81. @Override
  82. public void withdraw(double amount) {
  83. if(amount > this.withdrawRestriction) {
  84. throw new ExceedesWithdrawRestrictionException("You have exceeded your withdraw restriction!");
  85. }
  86. super.withdraw(amount);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement