Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Encapsulates a bank account and lets the balance be manipulated.
- */
- public class BankAccount2
- {
- private String name;
- private Date date;
- private double balance = 0;
- private static int numOfAccounts = 0;
- /**
- * The name of the bank to which all accounts belong.
- */
- public static String BANK_NAME;
- /**
- * Create a bank account that is a copy of the passed account
- * @param otherAccount The bank account to be copied.
- */
- public BankAccount2(BankAccount2 otherAccount)
- {
- this.name = otherAccount.name;
- this.balance = otherAccount.balance;
- this.date = otherAccount.date;
- }
- /**
- * Creates an account with the passed account holder name,
- * balance and opening date. The passed balance must be at least 0.
- * @param name The name of the account holder.
- * @param balance The initial balance of this account.
- * @param date The date the account was created.
- * @throws IllegalArgumentException if passed balance < 0
- */
- public BankAccount2(String name, double balance, Date date)
- {
- if(balance < 0)
- throw new IllegalArgumentException();
- this.name = name;
- this.balance = balance;
- this.date = date;
- }
- /**
- * Creates an account with the passed account holder name,
- * balance and date information. The passed balance must be at least 0.
- * @param name The name of the account holder.
- * @param balance The initial balance of this account.
- * @param month the month account was created
- * @param day the day account was created
- * @param year the year account was created
- * @throws IllegalArgumentException if passed balace < 0
- */
- public BankAccount2(String name, double balance, String month,
- int day, int year)
- {
- if(balance < 0)
- throw new IllegalArgumentException();
- this.name = name;
- this.balance = balance;
- Date date = new Date(month, day, year);
- this.date = date;
- }
- /**
- * Returns the name of the account holder.
- * @return The name of the account holder.
- */
- public String getName()
- {
- return this.name;
- }
- /**
- * Return the balance of this account
- * @return The balance of this account
- */
- public double getBalance()
- {
- return this.balance;
- }
- /**
- * Returns the number of accounts
- * @return The number of accounts.
- */
- public static int getNumberOfAccounts()
- {
- return numOfAccounts;
- }
- /**
- * Return the date this account was created.
- * @return The date this account was created.
- */
- public Date getDate()
- {
- return this.date;
- }
- /**
- * Deposits the passed amount into the account.
- * @param amount The amount to deposit.
- * @return true if the deposit is successful, false otherwise.
- */
- public boolean deposit(double amount)
- {
- if(amount > 0)
- {
- this.balance += amount;
- return true;
- }
- else
- {
- return false;
- }
- }
- /**
- * Withdraw the passed amount from this account.
- * @param amount - The amount to withdraw.
- * @pre 0 < amount <= balance
- */
- public void withdraw(double amount)
- {
- if(amount < 0)
- {
- this.balance -= amount;
- }
- }
- /**
- * Change the account creation date to the passed date.
- * @param newDate The new creation date.
- */
- public void setDate(Date newDate)
- {
- this.date = newDate;
- }
- /**
- * Returns a string version of this bank account
- * @overrides toString in class java.lang.object
- * @return A string representation of this account
- */
- public String toString()
- {
- return BANK_NAME + " account: " + getBalance();
- }
- /**
- * Change the account holder name to the passed name.
- * @param name The new account holder name.
- */
- public void setName(String name)
- {
- this.name = name;
- }
- /**
- * Returns whether the passed account is "equal" to this bank account.
- * The accounts are considered equal if they have the same balance
- * and were opened on the same date.
- * @return true if otherAccount is equal to this account, false otherwise
- * @param otherAccount - The other bank account.
- */
- public boolean equals(BankAccount2 otherAccount)
- {
- return this.balance == otherAccount.balance &&
- this.date.equals(otherAccount.date);
- }
- /**
- * Returns whether the passed object is "equal" to this bank account.
- * The accounts are considered equal if they have the same balance
- * and were opened on the same date.
- * @return true if other is a bank account equal to this account, false otherwise
- * @param other The object to be compared with this account
- */
- public boolean equals(Object other)
- {
- if(other instanceof BankAccount2)
- {
- BankAccount2 otherAccount = (BankAccount2)other;
- return this.balance == otherAccount.balance &&
- this.date.equals(otherAccount.date);
- }
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement