BankAccount.java /**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private int accountNumber;
private double balance;
/**
Constructs a bank account with a zero balance
@param anAccountNumber the account number for this account
*/
public BankAccount(int anAccountNumber)
{
accountNumber = anAccountNumber;
balance = 0;
}
/**
Constructs a bank account with a given balance
@param anAccountNumber the account number for this account
@param initialBalance the initial balance
*/
public BankAccount(int anAccountNumber, double initialBalance)
{
accountNumber = anAccountNumber;
balance = initialBalance;
}
/**
Gets the account number of this bank account.
@return the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
Use the following file:
BankAccountTester.java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
A program to test hash codes of bank accounts.
*/
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount acct1 = new BankAccount(1729, 10000);
BankAccount acct2 = new BankAccount(1730, 3400);
BankAccount acct3 = new BankAccount(1731, 100);
BankAccount acct4 = new BankAccount(1729, 98000);
System.out.println(acct2.equals(acct3));
System.out.println("Expected: false");
System.out.println(acct1.equals(acct4));
System.out.println("Expected: true");
Set<BankAccount> accounts = new HashSet<BankAccount>();
accounts.add(acct1);
accounts.add(acct2);
accounts.add(acct3);
accounts.add(acct4);
System.out.println(accounts.size());
System.out.println("Expected: 3");
}
}