package BookDigitByDigit;
public class CombinationLock{
private boolean isOpen;
private int digit1;
private int digit2;
private int digit3;
private int thirdToLast;
private int secondToLast;
private int last;
/** Constructor: Creates a lock with the given 3 digit combination.
* values less than 100 are given leading zeros */
public CombinationLock (int combination){
digit3 = combination % 10;
combination = combination / 10;
digit2 = combination % 10;
combination = combination / 10;
digit1 = combination % 10;
isOpen = true;
last = -1;
secondToLast = -1;
thirdToLast = -1;
}
/** Query Method: Returns if the door is unlocked or not */
public boolean isOpen () {
return isOpen;
}
/** Mutator: locks the lock */
public void close () {
isOpen = false;
last = -1;
secondToLast = -1;
thirdToLast = -1;
}
/** Mutator: Enter a digit of the combination and it unlocks
* the lock if the 3 digits of the combination are entered in
* the right order*/
public void enter (int digit) {
thirdToLast = secondToLast;
secondToLast = last;
last = digit;
if (thirdToLast == digit1 &&
secondToLast == digit2 &&
last == digit3)
isOpen = true;
}
}