jeudi 2 janvier 2020

Design ATM Machine with state design pattern

recently I've come across a question which is Design an ATM Machine using State Design Pattern an I'm new to design patterns but somehow I've completed the question and designed an ATM Machine using state design pattern in c#.

So, I just want to kindly tell me is there any optimization that can be done on this program, or this solution can be accepted. The current solution is running without any error.

Client Class which controls the state of ATM Machine.

public class Client
{
    public static void Main(string [] args)
    {
        AtmMachine atmMachine = new AtmMachine();

        atmMachine.enterPinAndWithdrawMoney();
        atmMachine.ejectDebitCard();
        atmMachine.insertDebitCardState();

        atmMachine.enterPinAndWithdrawMoney();
        atmMachine.insertDebitCardState();
        atmMachine.ejectDebitCard();      

        Console.ReadLine();
    }
}

ATM Machine Class

public class AtmMachine : IAtmMachineState
{
    private IAtmMachineState _state;
    public int _balance = 5000;
    public int _pin = 1234;
    public AtmMachine()
    {
        _state = new NoDebitCardState();
    }

    public IAtmMachineState GetMachineState()
    {
        return _state;
    }

    public void setAtmMachineState(IAtmMachineState state)
    {
        this._state = state;
    }

    public void ejectDebitCard()
    {
        _state.ejectDebitCard();        

        if(_state is HasDebitCardState)
        {
            _state = new NoDebitCardState();
            Console.WriteLine("State Changed");
            Console.WriteLine(_state.GetType().Name);
        }

    }

    public void enterPinAndWithdrawMoney()
    {       

        if(_state is HasDebitCardState)
        {
            _state.enterPinAndWithdrawMoney();
        }
    }

    public void insertDebitCardState()
    {        

        if(_state is NoDebitCardState)
        {
            _state = new HasDebitCardState();
            Console.WriteLine("State Changed");
            Console.WriteLine(_state.GetType().Name);
        }
    }
}

ATM Machine Interface

public interface IAtmMachineState
{
    void insertDebitCardState();
    void ejectDebitCard();
    void enterPinAndWithdrawMoney();

}

Has Debit Card State

class HasDebitCardState : IAtmMachineState
{
    AtmMachine atmMachine;
    public HasDebitCardState()
    {
        atmMachine = new AtmMachine();
    }
    public void ejectDebitCard()
    {
        Console.WriteLine("Debit card ejected successfully");
    }

    public void enterPinAndWithdrawMoney()
    {
        Console.WriteLine("Enter the Pin:");
        int pin = int.Parse(Console.ReadLine());
        if(atmMachine._pin==pin) 
        {
            Console.WriteLine("Enter the amount");
            int amount = int.Parse(Console.ReadLine());
            if(amount > atmMachine._balance) 
            {
                Console.WriteLine("Money can not be withdrawn because of low balance");
            }
            else
            {
                atmMachine._balance = atmMachine._balance - amount;
                Console.WriteLine("Please collect your cash");
                Console.WriteLine("Remaining Balance {0}", atmMachine._balance);
            }
        } else
        {
            Console.WriteLine("Incorrect Pin.");
        }
    }

    public void insertDebitCardState()
    {
        Console.WriteLine("Debit card is already there, so can not insert debit card");
    }
}

No Debit Card State

class NoDebitCardState : IAtmMachineState
{

    public void ejectDebitCard()
    {
        Console.WriteLine("No debit card inside atm");
    }

    public void enterPinAndWithdrawMoney()
    {
        Console.WriteLine("No debit card in ATM Machine, so you can not withdraw money");
    }

    public void insertDebitCardState()
    {
        Console.WriteLine("Debit Card Inserted");
    }
}

Aucun commentaire:

Enregistrer un commentaire