Hi all , I'm trying to generate a custom event, like your acount balance goes below certain limit, then, it will generate an event. I'll just paste the code below, I guess what i did is quite fair, but couldnt work out. If anyone can help me ? ///// INTERFACE BALANCELISTENER.JAVA package event; import java.util.*; import event.BankEvent; public interface BalanceListener extends EventListener { public void lowFundEvent(BankEvent be); }
///// BANKEVENT.JAVA package event; import java.util.*; import event.Account; public class BankEvent extends EventObject { public BankEvent(Account acc) { super(acc); } } //// CLASS ACCOUNT.JAVA package event; import event.BalanceListener; import event.BankEvent; public class Account { private int acctId; private int balance;
Hi Vikram! You are on the right track. Your interface seems ok. However your other three classes need some changes to work better. Lets start with the BankEvent class. You need to include more information into this class because you have not named it lowBalanceEvent. Therefore, the easiest thing to do is to include a String explaining what in the bank went wrong.
Next we turn to your Account class. This one needs a few more methods and the balance check should be moved to the deduct method because that is where it is important. The methods addBalanceListener and removeBalanceListener should not really do any work (as you have tried to do in your addBalanceListener). They should just add or remove from the listener vector member variable so we can keep track of who is listening.
And finally the Transaction class which sets up the account and drains it to a low level. You should add the listener before we make any withdrawals since that is the purpose. Also the lowFundEvent method gets simpler because of the message String that BankEvent now contains.
With the changes made above the app works pretty well! Good Luck, Manfred.