package ATM_Example_OCL; 


public class ATM {
    
    private boolean  online = true;
    private BankCard insertedCard          = null;
    private boolean  customerAuthenticated = false;
    private int      wrongPINCounter       = 0;

     public void confiscateCard () {
        if ( !cardIsInserted() ) throw new RuntimeException ();

        insertedCard.makeCardInvalid ();
        insertedCard = null;
        customerAuthenticated = false;        
    }
    
        /**
         * @preconditions insertedCard <> null and not customerAuthenticated
         *                and not insertedCard.invalid
         * @postconditions if pin = insertedCard@pre.correctPIN 
         *     then customerAuthenticated
         *      else
         *         if wrongPINCounter@pre < 2 
         *             then wrongPINCounter = wrongPINCounter@pre + 1
         *             else insertedCard = null and insertedCard@pre.invalid
         *         endif
         *   endif 
         */
        public void enterPIN (int pin) {
        if ( !( cardIsInserted () && !customerIsAuthenticated () ) )
                                    throw new RuntimeException ();

        if ( insertedCard.pinIsCorrect ( pin ) ) {
            customerAuthenticated = true;
        } else {
            ++wrongPINCounter;
            if ( wrongPINCounter >= 3 ) confiscateCard ();
        }
    }
     
    private boolean cardIsInserted () {
        return insertedCard != null;
    }

    private boolean customerIsAuthenticated () {
        return customerAuthenticated;
    }

    private int getAccountNumber () {
        return insertedCard.getAccountNumber ();
    }
        
}

