Sagar D
Sagar D

Reputation: 2618

How to use callback mechanism?

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);
    }
}

Upvotes: 6

Views: 3624

Answers (3)

SandeepGodara
SandeepGodara

Reputation: 1528

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...

Upvotes: 6

kayahr
kayahr

Reputation: 22020

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.

Upvotes: 1

LhasaDad
LhasaDad

Reputation: 2143

Couple of points:

  1. callbacks are used to nontify an object you have reference to so somehow the credit card should have reference to the object to be called back to (and it should be passed in and held using type Callback
  2. you would then call the method using the object you were handed in your methods listed in the CredicCard class when its the right time. example in changePin call onPinChange on the object you were handed.

Upvotes: 0

Related Questions