observer/1 to 1 relationship

Hi, I have a Bank class that has a 1 to 1 relationship with an InterestRate class and I have another class which should be observing any change to the interest rate.

My trouble is that I dont know how to just create one instance of the InterestRate class and be able to set the interest rate to a different value.

I would really appreciate any help.

Thanks

[381 byte] By [DP1000000a] at [2007-10-2 5:29:49]
# 1
Interest rate = new InterestRate();rate.setRate(0.07);
CeciNEstPasUnProgrammeura at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 2
Then:rate.addObserver(obs);rate.setRate(0.08);If you're looking for "test code". obs should trigger.
CeciNEstPasUnProgrammeura at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 3
It's probably best to make InterestRate immutable. This means a new InterestRate rate object is created each time the interest rate changes. A Bank object holds an InterestRate object in a variable. When this variable gets updated the Bank object notifies all listeners.
..u..j....a at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 4

I have created a new InterestRate object in Bank to link the two classes and initalise the interest rate, but I get an error:

Bank.java:210: setInterestRate(double) in InterestRate cannot be applied

to (InterestRate)

theInterestRate.setInterestRate(anIntRate);

^

theInterestRate being the initial setup in Bank to access methods inside the InterestRate class.

does anyone know where I'm going wrong?

DP1000000a at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 5
I answered this very same question about an hour ago. setInterestRate is declared to take a double, but you seem to put an InterestRate object as an argument instead.
CeciNEstPasUnProgrammeura at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks for all the help I have it working now.
DP1000000a at 2007-7-16 1:31:22 > top of Java-index,Java Essentials,Java Programming...