File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How can you generate your own events? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can you generate your own events?" Watch "How can you generate your own events?" New topic
Author

How can you generate your own events?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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;

public Account(int acctId,int balance)
{
this.acctId = acctId;
this.balance = balance;
}
public void addBalanceListener(BalanceListener bl)
{
if(this.balance < 5000)
{
bl.lowFundEvent(new BankEvent(this));
}
}
public void deductAmount(int amount)
{
balance = balance - amount;
System.out.println("Account : deductAmount : "+balance);
}
}
////// TRANSACTION.JAVA
package event;
import java.util.*;
import event.*;
public class Transaction implements BalanceListener
{
private Account ac1;

public Transaction()
{
ac1 = new Account(11,10000);
ac1.deductAmount(6000);
ac1.addBalanceListener(this);
}
public static void main(String[] args)
{
Transaction tr = new Transaction();
}
public void lowFundEvent(BankEvent be)
{
System.out.println(be.getSource().getClass().getName());
System.out.println("Event is raised");
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can you generate your own events?