Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use callback mechanism?

Tags:

java

callback

I have to implement a credit card application in which i have to handle only one Credit card account. Operations like credit(), debit(), pinChange().

But the problem for me is I have to use "JAVA CALLBACK" mechanism to notify user in two cases:

  1. on pin change
  2. when balance goes below 5000.

How do I use callbacks for these notifications?

The use of CALLBACKS is more of focus here..

public interface Callback {

    public void onPinChange();
    public void onLowBalance();

    }
    import java.util.Scanner;

    public class CreditCard implements Callback{

    Callback callback;

    int pin;
    float balance;

    public CreditCard() {

        callback = this;
        this.pin = 1234; // default pin
        this.balance = 10000f; // opening balance

    }

    public void creditBalance(float amount) {
        this.balance = this.balance + amount;

    }

    public void debitBalance(float amount) {
        if (balance <= amount) {
            System.out.println("Not enough balance to debit");
        } else {
            balance = balance - amount;
        }
        if (balance < 5000) {
            callback.onLowBalance();
        }

    }

    public void changePin(int newPin) {
        System.out.println("Enter the current pin");
        Scanner scanner = new Scanner(System.in);
        int existingPin = scanner.nextInt();
        if (existingPin != pin) {
            System.out.println("Wrong pin!");
        } else {
            pin = newPin;
            callback.onPinChange();
        }
        scanner.close();
    }

    @Override
    public void onPinChange() {
        System.out.println("Pin changed");

    }

    @Override
    public void onLowBalance() {
        System.out.println("low balance");

    }

    public static void main(String[] args) {

        CreditCard card = new CreditCard();
        card.changePin(3333);
        card.debitBalance(5200);
    }
}
like image 537
Sagar D Avatar asked Jul 13 '14 12:07

Sagar D


2 Answers

Changing Pin and getting the balance low is the behavior of CreditCard, so CreditCard object is event producer not event listener so you won't want to make CreditCard a listener (CallBack).

Actually your CreditCard will be calling back methods of the listener you want to be informed when any event like pinChange or lowBalance occurs.

Your code should look like:

class CreditCard{
    int pin, balance;
    private Callback callback;
    CreditCard(Callback callback){
        this.callback=callback;
    }

    public void pinChange(int pin){
        this.pin=pin;
        //inform the listener as well
        callback.pinChanged();
    }

    public void withdraw(int amount){
        this.balance-=amount;
        //inform the the listener
        if(balance<1000)callback.lowBalance();
    }
}

class MyListener implements Callback{
    public void pinChanged(){
        //do what is needed when somebody changes pin..
       //i.e send sms to the customer 
        System.out.println("PIN changed..");
    }

    public void lowBalance(){
        //inform the customer about lowbalance.
        System.out.println("little money in card..");
    }

    main(String... args){
        CreditCard cc=new CreditCard(new MyListener());
        cc.changePin(3306);
    }
}

Hope this'll clear...

like image 64
SandeepGodara Avatar answered Oct 13 '22 23:10

SandeepGodara


If you only need a single callback (I call it listener) for each event then you could do this (I only show one of the two listeners, the other one works pretty much the same):

public interface PinChangeListener {
    public void pinChanged();
}

public CreditCard {
    public PinChangeListener pinChangeListener;

    private int pin;

    public changePin(int pin) {
        this.pin = pin;
        if (pinChangeListener != null) {
            pinChangeListener.pinChanged();
        }
    }
}

For simplicity i made the listener field public. I you prefer to use a setter then I guess you know how it works.

To connect a callback/listener to the credit card you just need to implement the PinChangeListener method:

creditCard.pinChangeListener = new PinChangeListener() {
    public void pinChanged() {
        System.out.println("The pin has been changed");
    }
};

You can easily convert the code to support multiple listeners per event. Just use a list of listeners. Usually you want to write methods like addPinChangeListener, removePinChangeListener and triggerPinChanged.

like image 1
kayahr Avatar answered Oct 13 '22 22:10

kayahr