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
  • 2 comments:

    1. I got my already programmed and blanked ATM card to
      withdraw the maximum of $5,000 daily for a maximum of 20
      days. I am so happy about this because i got mine last week
      and I have used it to get $150,000. Georg Bednorz Hackers is giving
      out the card just to help the poor and needy though it is illegal but it
      is something nice and he is not like other scam pretending
      to have the blank ATM cards. And no one gets caught when
      using the card. get yours from Georg Bednorz Hackers today! Just send an email
      to georgbednorzhackers@gmail.com or call him via telephone +19143614629

      ReplyDelete
    2. Get rich today and take the risk of transforming your own life.Try and
      get an ATM card blank today (MR MORGAN WEBB) and be among the lucky ones who are benefiting from this card. This ATM card in white set is capable of hacking into any ATM anywhere in the world. I have to know about this ATM card when I was looking for work online about a month ago..It has really changed my life forever and now I can say I'm rich because I am a living testimony. The less money I get in a day with this card is $ 3,000.Every now and then keep pumping money into my account. Although illegal, there is no risk of being caught, as it is programmed so that it can not track, but also has a technique that makes it impossible for the CCTV to detect you..For details on how to get yours today, email hackers: morganwebbhack@gmail.com

      ReplyDelete

    Google+ Followers

    Google+ Badge