%==============================================================
%=====           The "COIN MACHINE" Algorithm             =====
%==============================================================
%===== The "Coin Machine" Will Take British Coins of The  =====
%===== Following Values: 5p, 10p, 20p, and 50p. It Will   =====
%===== Accept Each Coin, and When The Total Is Exactly    =====
%===== 60p It Will Dispence A Ticket. In Any State It Can =====
%===== Tell The Value of The Coin Inserted And It Can     =====
%===== Reject The Coin If It Does Not Recognise It (or If =====
%===== It Would Exceed The 60p Total). Of Course, machine =====
%===== Is Able To Accept The Coins In Any Order.          =====
%==============================================================

function CoinMachineFSM()

clear
clc

Input =  input('The Coin Machine Can Accept: 5p - 10p - 20p - 50p Coins OR 9 To Exit: ');
Total = 0;
Change = 0;
InputCounter = 1;

while (Input ~= 9)
    if (InputCounter ~= 1)
        Input =  input('The Coin Machine Can Accept: 5p - 10p - 20p - 50p Coins OR 9 To Exit: ');
    end
    if (Total < 60)
        if (Input == 5)
            Total = Total + Input;
        elseif (Input == 10)
            Total = Total + Input;
        elseif (Input == 20)
            Total = Total + Input;
        elseif (Input == 50)
            Total = Total + Input;
        else
            display('- Invalid Coin Denomination -');
        end
        display('Your Balance So Far:');
        display(Total);
    end
    if (Total >= 60)
        Change = Total - 60;
        display('*** Ticket Dispenced ***');
        display('Your Change are:');
        display(Change);
        break;
    end
    InputCounter = InputCounter + 1;
end
end