Saturday, 1 February 2014

State Design pattern - Implementation [ATM]



Click here to watch in Youtube : https://www.youtube.com/watch?v=97AmoYlWxrI

 Click the below Image to Enlarge
State Design pattern - Implementation [ATM]

State Design pattern - Implementation [ATM] - Class Diagram


AtmMachineState.java

public interface AtmMachineState
{

public void insertDebitCard();

public void ejectDebitCard();

public void enterPinAndWithdrawMoney();
}

HasDebitCardState .java

public class HasDebitCardState implements AtmMachineState
{


@Override
public void insertDebitCard()
{
System.out.println("Debit Card is already there,So you cannot insert the Debit Card ...");

}

@Override
public void ejectDebitCard()
{

System.out.println("Debit Card is ejected...");
}

@Override
public void enterPinAndWithdrawMoney()
{
System.out.println("Pin number has been entered correctly and money has been withdrawn...");

}
}

NoDebitCardState.java

public class NoDebitCardState implements AtmMachineState
{

@Override
public void insertDebitCard()
{
System.out.println("DebitCard inserted ....");

}

@Override
public void ejectDebitCard()
{

System.out.println("No Debit Card in ATM Machine slot, so you cannot eject the Debit Card...");
}

@Override
public void enterPinAndWithdrawMoney()
{
System.out.println("No Debit Card in ATM Machine slot, so you cannot enter the pin and withdraw the money...");

}

}

AtmMachine.java

// This is like Context
public class AtmMachine implements AtmMachineState
{

private AtmMachineState atmMachineState;

public AtmMachine()
{
atmMachineState = new NoDebitCardState();
}

public AtmMachineState getAtmMachineState()
{
return atmMachineState;
}

public void setAtmMachineState( AtmMachineState atmMachineState )
{
this.atmMachineState = atmMachineState;
}

@Override
public void insertDebitCard()
{
atmMachineState.insertDebitCard();

/*
* Debit Card has been inserted so internal state of ATM Machine
* has been changed to 'hasDebitCardState'
*/

if( atmMachineState instanceof NoDebitCardState )
{

AtmMachineState hasDebitCardState = new HasDebitCardState();
setAtmMachineState(hasDebitCardState);
System.out.println("ATM Machine internal state has been moved to : "
               + atmMachineState.getClass().getName());
}

}

@Override
public void ejectDebitCard()
{
atmMachineState.ejectDebitCard();

/*
* Debit Card has been ejected so internal state of ATM Machine
* has been changed to 'noDebitCardState'
*/
if( atmMachineState instanceof HasDebitCardState )
{

AtmMachineState noDebitCardState = new NoDebitCardState();
setAtmMachineState(noDebitCardState);
System.out.println("ATM Machine internal state has been moved to : "
               + atmMachineState.getClass().getName());
}

}

@Override
public void enterPinAndWithdrawMoney()
{
atmMachineState.enterPinAndWithdrawMoney();

}

}

Client.java

public class Client
{

public static void main( String[] args )
{
/*
* Initially ATM Machine in 'noDebitCardState'
*/
AtmMachine atmMachine = new AtmMachine();

System.out.println("ATM Machine Current state : "
               + atmMachine.getAtmMachineState().getClass().getName());

System.out.println();
atmMachine.enterPinAndWithdrawMoney();
atmMachine.ejectDebitCard();
atmMachine.insertDebitCard();

System.out.println("\n*******************************************************");

/*
* Debit Card has been inserted so internal state of ATM Machine
* has been changed to 'hasDebitCardState'
*/
System.out.println("\nATM Machine Current state : "
               + atmMachine.getAtmMachineState().getClass().getName());
System.out.println();

atmMachine.enterPinAndWithdrawMoney();
atmMachine.insertDebitCard();
atmMachine.ejectDebitCard();
System.out.println("\n*******************************************************");

/*
* Debit Card has been ejected so internal state of ATM Machine
* has been changed to 'noDebitCardState'
*/

System.out.println("\nATM Machine Current state : "
               + atmMachine.getAtmMachineState().getClass().getName());

}

}

Output

ATM Machine Current state : NoDebitCardState

No Debit Card in ATM Machine slot, so you cannot enter the pin and withdraw the money...
No Debit Card in ATM Machine slot, so you cannot eject the Debit Card...
DebitCard inserted ....
ATM Machine internal state has been moved to : HasDebitCardState

*******************************************************

ATM Machine Current state : HasDebitCardState

Pin number has been entered correctly and money has been withdrawn...
Debit Card is already there,So you cannot insert the Debit Card ...
Debit Card is ejected...
ATM Machine internal state has been moved to : NoDebitCardState

*******************************************************

ATM Machine Current state : NoDebitCardState

See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [TV Remote]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [Vending Machine]
  • State Design pattern - Implementation [Project]
  • State Design pattern - Implementation [LED TV]
  • State Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge