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
No comments:
Post a Comment