From: David S. <ds...@us...> - 2006-02-15 22:56:12
|
Update of /cvsroot/junit/junit/junit/samples/money In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13390/junit/samples/money Modified Files: Money.java IMoney.java MoneyTest.java MoneyBag.java Log Message: Merged with branch, Kent will make final changes and launch. Index: Money.java =================================================================== RCS file: /cvsroot/junit/junit/junit/samples/money/Money.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Money.java 26 Apr 2002 23:54:30 -0000 1.3 +++ Money.java 15 Feb 2006 22:55:29 -0000 1.4 @@ -36,6 +36,7 @@ public String currency() { return fCurrency; } + @Override public boolean equals(Object anObject) { if (isZero()) if (anObject instanceof IMoney) @@ -47,7 +48,10 @@ } return false; } + @Override public int hashCode() { + if (fAmount == 0) + return 0; return fCurrency.hashCode()+fAmount; } public boolean isZero() { @@ -62,6 +66,7 @@ public IMoney subtract(IMoney m) { return add(m.negate()); } + @Override public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("["+amount()+" "+currency()+"]"); Index: IMoney.java =================================================================== RCS file: /cvsroot/junit/junit/junit/samples/money/IMoney.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- IMoney.java 26 Apr 2002 23:54:30 -0000 1.4 +++ IMoney.java 15 Feb 2006 22:55:29 -0000 1.5 @@ -37,6 +37,9 @@ public abstract IMoney subtract(IMoney m); /** * Append this to a MoneyBag m. + * appendTo() needs to be public because it is used + * polymorphically, but it should not be used by clients + * because it modifies the argument m. */ public abstract void appendTo(MoneyBag m); } \ No newline at end of file Index: MoneyTest.java =================================================================== RCS file: /cvsroot/junit/junit/junit/samples/money/MoneyTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- MoneyTest.java 9 Sep 2004 21:53:57 -0000 1.7 +++ MoneyTest.java 15 Feb 2006 22:55:29 -0000 1.8 @@ -14,6 +14,7 @@ public static void main(String args[]) { junit.textui.TestRunner.run(MoneyTest.class); } + @Override protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); Index: MoneyBag.java =================================================================== RCS file: /cvsroot/junit/junit/junit/samples/money/MoneyBag.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- MoneyBag.java 9 Sep 2004 21:53:57 -0000 1.5 +++ MoneyBag.java 15 Feb 2006 22:55:29 -0000 1.6 @@ -1,7 +1,7 @@ package junit.samples.money; -import java.util.Enumeration; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; /** * A MoneyBag defers exchange rate conversions. For example adding @@ -14,10 +14,10 @@ * A MoneyBag is represented as a list of Monies and provides * different constructors to create a MoneyBag. */ -class MoneyBag implements IMoney { - private Vector fMonies= new Vector(5); +public class MoneyBag implements IMoney { + private List<Money> fMonies= new ArrayList<Money>(5); - static IMoney create(IMoney m1, IMoney m2) { + public static IMoney create(IMoney m1, IMoney m2) { MoneyBag result= new MoneyBag(); m1.appendTo(result); m2.appendTo(result); @@ -33,22 +33,23 @@ return MoneyBag.create(s, this); } void appendBag(MoneyBag aBag) { - for (Enumeration e= aBag.fMonies.elements(); e.hasMoreElements(); ) - appendMoney((Money)e.nextElement()); + for (Money each : aBag.fMonies) + appendMoney(each); } void appendMoney(Money aMoney) { if (aMoney.isZero()) return; IMoney old= findMoney(aMoney.currency()); if (old == null) { - fMonies.addElement(aMoney); + fMonies.add(aMoney); return; } - fMonies.removeElement(old); - IMoney sum= old.add(aMoney); + fMonies.remove(old); + Money sum= (Money) old.add(aMoney); if (sum.isZero()) return; - fMonies.addElement(sum); + fMonies.add(sum); } + @Override public boolean equals(Object anObject) { if (isZero()) if (anObject instanceof IMoney) @@ -59,21 +60,17 @@ if (aMoneyBag.fMonies.size() != fMonies.size()) return false; - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) { - Money m= (Money) e.nextElement(); - if (!aMoneyBag.contains(m)) + for (Money each : fMonies) + if (! aMoneyBag.contains(each)) return false; - } return true; } return false; } private Money findMoney(String currency) { - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) { - Money m= (Money) e.nextElement(); - if (m.currency().equals(currency)) - return m; - } + for (Money each : fMonies) + if (each.currency().equals(currency)) + return each; return null; } private boolean contains(Money m) { @@ -81,12 +78,11 @@ if (found == null) return false; return found.amount() == m.amount(); } + @Override public int hashCode() { int hash= 0; - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) { - Object m= e.nextElement(); - hash^= m.hashCode(); - } + for (Money each : fMonies) + hash^= each.hashCode(); return hash; } public boolean isZero() { @@ -94,35 +90,31 @@ } public IMoney multiply(int factor) { MoneyBag result= new MoneyBag(); - if (factor != 0) { - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) { - Money m= (Money) e.nextElement(); - result.appendMoney((Money)m.multiply(factor)); - } - } + if (factor != 0) + for (Money each : fMonies) + result.appendMoney((Money) each.multiply(factor)); return result; } public IMoney negate() { MoneyBag result= new MoneyBag(); - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) { - Money m= (Money) e.nextElement(); - result.appendMoney((Money)m.negate()); - } + for (Money each : fMonies) + result.appendMoney((Money) each.negate()); return result; } private IMoney simplify() { if (fMonies.size() == 1) - return (IMoney)fMonies.elements().nextElement(); + return fMonies.iterator().next(); return this; } public IMoney subtract(IMoney m) { return add(m.negate()); } + @Override public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("{"); - for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) - buffer.append(e.nextElement()); + for (Money each : fMonies) + buffer.append(each); buffer.append("}"); return buffer.toString(); } |