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;
this.balance = 10000f;
}
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);
}
}
source
share