package P9_10; /** * Simulates a coin * * @author Thomas Zaki * 11/05/08 */ public class Coin implements Comparable<Coin> { private String name; private double value; /** * Creates a new Coin with a specified name and value * * @param n a name * @param v a value */ public Coin(String n, double v) { name = n; value = v; }//end Constructor /** * Returns the name of the Coin * * @return the name */ public String getName() { return name; }//end getName /** * Returns the value of the Coin * * @return the value */ public double getValue() { return value; }//end getValue /** * returns the name of the Coin */ public String toString() { return this.getName(); } /** * Overrides <code>Object</code>'s <code>.equals(Object)</code> method * * @param other the other <code>Coin</code> to compare this <code>Coin</code> to * @return true if they are exactly equal, otherwise false */ public boolean equals(Coin other) { boolean result = false; if(other.getName().equals(this.getName()) && other.getValue() == this.getValue()) result = true; return result; } /** * Implements the inherited method <code>.comapreTo(Coin)</code> * * @param that the <code>Coin</code> to compare this one to * @return 0 if equal, -1 if this <code>Coin</code> is "less than" the other, * and 1 if this <code>Coin</code> is "greater than" the other */ public int compareTo(Coin that) { int result = 0; if(this.value > that.value) result = 1; else if(this.value < that.value) result = -1; else if(this.name.compareTo(that.name) == 1) result = 1; else if(this.name.compareTo(that.name) == -1) result = -1; return result; } }//end Coin class