import java.util.*; public class BankAccount implements Cloneable, Comparable { private static int count = 0; private String name; // fields (replicated for each object) private int id; private double balance; private List transactions; public BankAccount(String name) { this.name = name; count++; this.id = count; balance = 0.0; transactions = new ArrayList(); } public BankAccount clone() { try { BankAccount copy = (BankAccount) super.clone(); // deep copy; copy the transactions list so they won't be shared copy.transactions = new ArrayList(transactions); return copy; } catch (CloneNotSupportedException e) { return null; // won't ever happen } } public int compareTo(BankAccount other) { if (name.equals(other.name)) { return id - other.id; } else { return name.compareTo(other.name); } } public String getName() { return name; } public int getID() { // return this account's id return id; } public void setName(String name) { this.name = name; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } public void printTransactions() { System.out.println("All transactions for " + this); for (String trans : transactions) { System.out.println(trans); } System.out.println(); } public String toString() { return name + " (id " + id + "), $" + balance; } }