EDIT: FML! MY implementation of hashcode had a lowercase c. -.-
I've been trying to learn TDD and have been following the 'By Example' book by Kent Beck; it's very good!
However, I can't seem progress because a value is returning null when I access a hashtable. I've run a debug session and the object with the value is clearly there yet the result is null.
The code to build and access is:
public void addRate(String from, String to, int rate){
this.rates.put(new Pair(from, to), new Integer(rate));
}
from and to are "GBP" and "USD". Also verified by debug.
Test case calling the above:
@Test
public void testreduceMoneyDifferentCurrency(){
Bank bank = new Bank();
bank.addRate("GBP", "USD", 2);
Money result = bank.reduce(Money.gbpound(2), "USD");
assertEquals(Money.dollar(1), result);
}
The reduce method in bank calls the method rate:
public Money reduce(Bank bank, String to){
int rate = bank.rate(this.currency, to);
return new Money(this.amount / rate, to);
}
Which is where the issue is:
public int rate(String from, String to){
if (from.equals(to)) return 1;
Integer rate = (Integer) this.rates.get(new Pair(from, to));
return rate.intValue();
}
The first line copes with USD -> USD conversions etc.
The Pair object is 2 strings built to be used as a key.
I've not used has tables a great deal but I can't see what the issue is, I know for certain that the values are in the hashtable but 'rate' is always returning a null value.
I can't see the wood for the trees. :) Could someone point me in the right direction please?