import java.util.*; /**** * * This is a simple class to manage valuable assets. It illustrates how * multiple interfaces can be used in an interesting and effective way. * * This class is derived from the BankAccountManager class. The difference * is that the AssetManager class maintains a list of any kind of valuable * asset, not just bank accounts. For example, things like can be valuable * assets, as long the defining class implements the getValue method. See * Lecture Notes Week 4 for further discussion. * */ public class AssetManager { /** The list of valuable assets */ private ArrayList assets = new ArrayList(); /** * Add the given asset to the list. */ public void add(Valuable asset) { assets.add(asset); } /** * Remove the given asset from the list. */ public void remove(BankAccount account) { assets.remove(account); } /** * Return the total value of all the assets in the list. Compare this * method with BankAccount.getTotalValue. In the bank account case, only * the sum of bank accounts could be computed. Here the total sum of all * kinds of assests can be computed, using the asset-specific * implementations of the Valuable.getValue methods. */ public double getTotalValue() { double worth = 0; for (Valuable asset : assets) { worth += asset.getValue(); } return worth; } /** * Return the string representation of this, one asset per line, with each * line indented three spaces. The string format for each asset is defined * by the asset-specific toString methods. */ public String toString() { String returnValue = ""; for (Valuable asset : assets) { returnValue += " " + asset + "\n"; } return returnValue; } /** * Sort this' account list, based on the comparator defined in the * asset-specific implementations of Valuable.compareTo. * * UNFORTUNATELY, the implementation of sort in BankAccountManager doesn't * work here, even though all implementations of Valuable also implement * Comparable. The worse part, the implementation won't even compile, * which is why the call to Collections.sort is commented out. * * QUESTION: What can we do to fix this? * * HINTS: (1) The problem is not here, but elsewhere in the classes that * implement Comparable. (2) This is not a totally easy question to * answer, even though the solution is simple and pretty obvious when you * see it. */ public void sort() { // Collections.sort(assets); } }