How to use callback mechanism?

I need to implement a credit card application in which I have to process only one credit card account. Type of operation credit(), debit(), pinChange().

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

  • when changing contacts
  • with a balance below 5000.

How to use callbacks for these notifications?

Using CALLBACKS here is more.

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);
    }
}
+4
source share
3 answers

Pin - CreditCard, CreditCard , , CreditCard (CallBack).

, , , - , pinChange lowBalance.

:

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

, ...

+4

( ) , ( , ):

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

. , , , .

/ , PinChangeListener:

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

. . , addPinChangeListener, removePinChangeListener triggerPinChanged.

+1

:

  • , , , ( Callback
  • , , , CredicCard, . changePin onPinChange , .
0

Source: https://habr.com/ru/post/1548249/


All Articles