skilletwaffles

lab 17.1

Mar 18th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public class CheckingAccount
  2. {
  3. private double myBalance;
  4. private String myAccountNumber;
  5.  
  6. public CheckingAccount()
  7. {
  8. myBalance = 0;
  9. myAccountNumber= "NEW";
  10. }
  11.  
  12. public CheckingAccount(double initialBalance, String acctNum)
  13. {
  14. if(initialBalance < 0)
  15. throw new IllegalArgumentException("negative initial balance");
  16. myBalance = initialBalance;
  17. myAccountNumber = acctNum;
  18. }
  19.  
  20. public double getBalance()
  21. {
  22. return myBalance;
  23. }
  24.  
  25. public void deposit(double amount)
  26. {
  27. if(amount < 0)
  28. throw new IllegalArgumentException("negative amount");
  29. myBalance = myBalance + amount;
  30. }
  31.  
  32. public void withdraw(double amount)
  33. {
  34. if(amount < 0 || amount > myBalance)
  35. throw new IllegalArgumentException("illegal amount");
  36. myBalance = myBalance - amount;
  37. }
  38. }
  39.  
  40.  
  41. import chn.util.*;
  42. public class CheckingTester
  43. {
  44. public static void main(String[] args)
  45. {
  46. CheckingAccount checking;
  47. ConsoleIO console = new ConsoleIO();
  48. double startBal=0;
  49. double dep = 0;
  50. double withdraw = 0;
  51.  
  52. /*final double INTEREST_RATE = 2.5;
  53. double interest;
  54.  
  55. interest = checking.getBalance() * INTEREST_RATE / 100;
  56. checking.deposit(interest);
  57.  
  58. System.out.println("balance after year 1 is $" + checking.getBalance());
  59.  
  60. interest = checking.getBalance() * INTEREST_RATE / 100;
  61. checking.deposit(interest);
  62.  
  63. System.out.println("Balance after year 2 is $" + checking.getBalance());
  64. */
  65.  
  66. System.out.println("");
  67. System.out.println("ErrorFreeChecking Test");
  68. System.out.print("Please enter the starting balance----> ");
  69. startBal = console.readDouble();
  70. try
  71. {
  72. checking = new CheckingAccount(startBal, "A123");
  73. }
  74. catch(IllegalArgumentException e)
  75. {
  76. System.out.println("Negative balance amount");
  77. System.out.println("");
  78. }
  79. System.out.println("");
  80. System.out.print("Please enter amount to deposit---->");
  81. dep = console.readDouble();
  82. try
  83. {
  84. checking.deposit(dep);
  85. }
  86. catch(IllegalArgumentException e)
  87. {
  88. System.out.println("negative amount");
  89. System.out.println("");
  90. }
  91. System.out.println("");
  92. System.out.print("Please enter amount to withdraw----->");
  93. withdraw = console.readDouble();
  94. checking.withdraw(withdraw);
  95.  
  96.  
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment