Example #1
0
        private readonly int _KeyLength; // readonly will mean that the value for this field must be either set here (where it is declared) or in the constructor. After that, the value cannot be changed.

        public LockDriver()
        {
            // Set up the fields/properties with values
            // A standard KeyLock
            // 1) Build a key pattern for the lock
            int[] keySequence = { 5, 7, 2, 3, 5 };
            _KeyLength = keySequence.Length;

            List <KeyPin> key = new List <KeyPin>();

            key.Add(new KeyPin(5));
            key.Add(new KeyPin(7));
            key.Add(new KeyPin(2));
            key.Add(new KeyPin(3));
            key.Add(new KeyPin(5));

            _TheLock = new KeyLock(key);
            // Test of my lock
            _TheLock.Lock();
            if (!_TheLock.IsLocked)
            {
                throw new System.Exception("ERROR - Faulty lock");
            }
            _TheLock.Unlock(keySequence);
            if (_TheLock.IsLocked)
            {
                throw new System.Exception("ERROR - Can't unlock with correct key sequence");
            }
            // Display the key sequence for this lock
            DisplayKeySequence(keySequence);
        }
Example #2
0
        private static int PickLock(AbstractLock theLock, int keyLength)
        {
            int count = 0;

            int[] keySequence = new int[keyLength];
            while (theLock.IsLocked)
            {
                count++;
                bool noMoreNumbers = IncrementKeySequence(keySequence);
                if (noMoreNumbers)
                {
                    count = -1;
                    break;
                }
                else
                {
                    DisplayKeySequence(keySequence);
                    theLock.Unlock(keySequence);
                }
            }
            return(count);
        }