package type.lib; /** * This class implements only part of type.lib.Investment. * It is used as an example to illustrate aggregation. * * @author Franck van Breugel */ public class Investment { private double bookValue; private Stock stock; private int quantity; /** * Initializes this investment with the given book value, stock and quantity. * * @param bookValue the book value of this investment. * @param stock the stock of this investment. * @param quantity the quantity of this investment. */ public Investment(double bookValue, Stock stock, int quantity) { this.setBookValue(bookValue); this.setStock(stock); this.setQuantity(quantity); } /** * Returns the book value of this investment. * * @return the book value of this investment. */ public double getBookValue() { return this.bookValue; } /** * Sets the book value of this investment to the given book value. * * @param bookValue the new book value of this investment. */ public void setBookValue(double bookValue) { this.bookValue = bookValue; } /** * Returns the stock of this investment. * * @return the stock of this investment. */ public Stock getStock() { return this.stock; } /** * Sets the stock of this investment to the given stock. * * @param stock the new stock of this investment. */ private void setStock(Stock stock) { this.stock = stock; } /** * Returns the quantity of this investment. * * @return the quantity of this investment. */ public int getQuantity() { return this.quantity; } /** * Sets the quantity of this investment to the given quantity. * * @param quantity the new quantity of this investment. */ public void setQuantity(int quantity) { this.quantity = quantity; } /** * Tests if this investment is the same as the given object. The two are * considered equal if the stocks they hold are equal (as defined by the * Stock's equals() method) or both are null and the two book values are * equal to the nearest cent. * * @param object an object. * @return true if if this investment is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { final double ONE_CENT = 0.01; Investment other = (Investment) object; equal = ((this.getStock() == null && other.getStock() == null) || (this.getStock() != null && other.getStock() != null && this.getStock().equals(other.getStock()))) && Math.abs(this.getBookValue() - other.getBookValue()) < ONE_CENT; } else { equal = false; } return equal; } // other methods such as hashCode and toString should be implemented as well }