Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. /**
  2. * Encapsulates a bank account and lets the balance be manipulated.
  3. */
  4. public class BankAccount2
  5. {
  6. private String name;
  7. private Date date;
  8. private double balance = 0;
  9. private static int numOfAccounts = 0;
  10. /**
  11. * The name of the bank to which all accounts belong.
  12. */
  13. public static String BANK_NAME;
  14.  
  15. /**
  16. * Create a bank account that is a copy of the passed account
  17. * @param otherAccount The bank account to be copied.
  18. */
  19. public BankAccount2(BankAccount2 otherAccount)
  20. {
  21. this.name = otherAccount.name;
  22. this.balance = otherAccount.balance;
  23. this.date = otherAccount.date;
  24. }
  25.  
  26. /**
  27. * Creates an account with the passed account holder name,
  28. * balance and opening date. The passed balance must be at least 0.
  29. * @param name The name of the account holder.
  30. * @param balance The initial balance of this account.
  31. * @param date The date the account was created.
  32. * @throws IllegalArgumentException if passed balance < 0
  33. */
  34. public BankAccount2(String name, double balance, Date date)
  35. {
  36. if(balance < 0)
  37. throw new IllegalArgumentException();
  38.  
  39. this.name = name;
  40. this.balance = balance;
  41. this.date = date;
  42. }
  43.  
  44. /**
  45. * Creates an account with the passed account holder name,
  46. * balance and date information. The passed balance must be at least 0.
  47. * @param name The name of the account holder.
  48. * @param balance The initial balance of this account.
  49. * @param month the month account was created
  50. * @param day the day account was created
  51. * @param year the year account was created
  52. * @throws IllegalArgumentException if passed balace < 0
  53. */
  54. public BankAccount2(String name, double balance, String month,
  55. int day, int year)
  56. {
  57. if(balance < 0)
  58. throw new IllegalArgumentException();
  59.  
  60. this.name = name;
  61. this.balance = balance;
  62. Date date = new Date(month, day, year);
  63. this.date = date;
  64. }
  65.  
  66. /**
  67. * Returns the name of the account holder.
  68. * @return The name of the account holder.
  69. */
  70. public String getName()
  71. {
  72. return this.name;
  73. }
  74.  
  75. /**
  76. * Return the balance of this account
  77. * @return The balance of this account
  78. */
  79. public double getBalance()
  80. {
  81. return this.balance;
  82. }
  83.  
  84. /**
  85. * Returns the number of accounts
  86. * @return The number of accounts.
  87. */
  88. public static int getNumberOfAccounts()
  89. {
  90. return numOfAccounts;
  91. }
  92.  
  93. /**
  94. * Return the date this account was created.
  95. * @return The date this account was created.
  96. */
  97. public Date getDate()
  98. {
  99. return this.date;
  100. }
  101.  
  102. /**
  103. * Deposits the passed amount into the account.
  104. * @param amount The amount to deposit.
  105. * @return true if the deposit is successful, false otherwise.
  106. */
  107. public boolean deposit(double amount)
  108. {
  109. if(amount > 0)
  110. {
  111. this.balance += amount;
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119.  
  120. /**
  121. * Withdraw the passed amount from this account.
  122. * @param amount - The amount to withdraw.
  123. * @pre 0 < amount <= balance
  124. */
  125. public void withdraw(double amount)
  126. {
  127. if(amount < 0)
  128. {
  129. this.balance -= amount;
  130. }
  131. }
  132.  
  133. /**
  134. * Change the account creation date to the passed date.
  135. * @param newDate The new creation date.
  136. */
  137. public void setDate(Date newDate)
  138. {
  139. this.date = newDate;
  140. }
  141.  
  142. /**
  143. * Returns a string version of this bank account
  144. * @overrides toString in class java.lang.object
  145. * @return A string representation of this account
  146. */
  147. public String toString()
  148. {
  149. return BANK_NAME + " account: " + getBalance();
  150. }
  151.  
  152. /**
  153. * Change the account holder name to the passed name.
  154. * @param name The new account holder name.
  155. */
  156. public void setName(String name)
  157. {
  158. this.name = name;
  159. }
  160.  
  161. /**
  162. * Returns whether the passed account is "equal" to this bank account.
  163. * The accounts are considered equal if they have the same balance
  164. * and were opened on the same date.
  165. * @return true if otherAccount is equal to this account, false otherwise
  166. * @param otherAccount - The other bank account.
  167. */
  168. public boolean equals(BankAccount2 otherAccount)
  169. {
  170. return this.balance == otherAccount.balance &&
  171. this.date.equals(otherAccount.date);
  172. }
  173.  
  174. /**
  175. * Returns whether the passed object is "equal" to this bank account.
  176. * The accounts are considered equal if they have the same balance
  177. * and were opened on the same date.
  178. * @return true if other is a bank account equal to this account, false otherwise
  179. * @param other The object to be compared with this account
  180. */
  181. public boolean equals(Object other)
  182. {
  183. if(other instanceof BankAccount2)
  184. {
  185. BankAccount2 otherAccount = (BankAccount2)other;
  186. return this.balance == otherAccount.balance &&
  187. this.date.equals(otherAccount.date);
  188. }
  189. else
  190. return false;
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement