fMonies.addElement(sum);
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof MoneyBag) {
MoneyBag aMoneyBag= (MoneyBag)anObject;
if (aMoneyBag.fMonies.size() != fMonies.size())
return false;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (!aMoneyBag.contains(m))
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;
}
return null;
}
private boolean contains(Money m) {
Money found= findMoney(m.currency());
if (found == null) return false;
return found.amount() == m.amount();
}
public int hashCode() {
int hash= 0;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Object m= e.nextElement();
hash^= m.hashCode();
}
return hash;
}
public boolean isZero() {
return fMonies.size() == 0;
}
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));
}
}
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());
}
return result;
}
private IMoney simplify() {
if (fMonies.size() == 1)
return (IMoney)fMonies.elements().nextElement();
return this;
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); )
buffer.append(e.nextElement());
buffer.append("}");
return buffer.toString();
}
public void appendTo(MoneyBag m) {
m.appendBag(this);
}
}
文章来源于领测软件测试网 https://www.ltesting.net/