In: Computer Science
## Problem Description
You are writing a small section of an ATM machine where you accept a string that is a password and accept it or reject it. If the user tries more than three times with a bad password the card is blocked and no password will be accepted until the **resetFailedAttemptCounter** method is called. The password is provided at creation of the AtmPin. Security is a priority!
The class to implement this is referred to as **AtmPin** and consists of the following public methods:
**public AtmPin(String actualCode)** - Creates an AtmPin object with the provided code as the actual PIN
**public boolean verifyPinCode(String code)** - Accepts a string which represents the user's entry of their pin code. If the string matches the actual code provided in the constructor and the account is not blocked then the method returns true. Returns false when the code is bad OR when **isAccountLocked**() is true due to too many consecutive bad verification attempts. If an invalid code was passed or a valid code, but the account is blocked, the system will increment the number of bad password attempts.
**public boolean lastAttemptFailed()** - Returns true if the last invocation of **verifyPinCode** was unsuccessful. Underlying state is updated every time a password is passed to **verifyPinCode**. This method DOES NOT do the verification of the code, but simply is a record of whether the last attempt was successful. Note that you can submit a good password but have it rejected by the system since the account is locked (**verifyPinCode** returns false).
**public boolean isAccountLocked()** - If *more than* three consecutive attempts are made then the password is not accepted (**verifyPinCode** returns false) until **resetFailedAttemptCounter** is called.
**public void resetFailedAttemptCounter()** - Resets the number of failed attempts to zero.
Your **Program.java** should contain code to test your **AtmPin** object.
**Requirements:**
- You may not hard-code the pin in the AtmPin class, but must provide it as a constructor argument
- DO NOT STORE the user's code when attempting to verify
- **verifyPinCode** returns false when
- code is bad
- OR
- when **isAccountLocked**() is true due to too many consecutive bad verification attempts
- **lastAttempt** failed should not do any comparison of actual pin to the tested pin (i.e. you cannot store the user's pin)
## Getting Started
Create the following files in the folder and add the appropriately named classes to them
1. Program.java
2. AtmPin.java
Thanks for the question, here are the two program classes you will be needing. Program is a driver class, and I wrote some test cases to check that the AtmPin class is working correctly.
Hope this helps, let me know for any help with any other questions..
=============================================================================
public class AtmPin {
private String
actualCode;
private int
attempts;
private final int
MAX_ATTEMPTS =3;
private boolean
accountLocked;
private boolean
lastAttemptFailed;
public AtmPin(String
actualCodel) {
this.actualCode =
actualCodel;
attempts=0;
accountLocked = false;
lastAttemptFailed = false;
}
public boolean
verifyPinCode(String code){
if(attempts<MAX_ATTEMPTS
&& !accountLocked){
attempts+=1;
if(actualCode.equals(code)){
lastAttemptFailed=false;
return true;
}
}
if(attempts==MAX_ATTEMPTS){
accountLocked=true;
}
lastAttemptFailed =true;
return
false;
}
public boolean
lastAttemptFailed(){
return
lastAttemptFailed;
}
public boolean
isAccountLocked() {
return
accountLocked;
}
public void
resetFailedAttemptCounter(){
attempts=0;
lastAttemptFailed =false;
accountLocked=false;
}
}
=============================================================================
public class Program {
public static void main(String[] args) {
AtmPin pin = new AtmPin("1234");
System.out.println("Attempt 1: Trying to enter atm pin with incorrect pin");
pin.verifyPinCode("1233");
System.out.println("pin.isAccountLocked() = " + pin.isAccountLocked());
System.out.println("pin.lastAttemptFailed() = " + pin.lastAttemptFailed());
System.out.println("\nAttempt 2: Trying to enter atm pin with incorrect pin");
pin.verifyPinCode("1244");
System.out.println("pin.isAccountLocked() = " + pin.isAccountLocked());
System.out.println("pin.lastAttemptFailed() = " + pin.lastAttemptFailed());
System.out.println("\nAttempt 3: Trying to enter atm pin with incorrect pin");
pin.verifyPinCode("1224");
System.out.println("pin.isAccountLocked() = " + pin.isAccountLocked());
System.out.println("pin.lastAttemptFailed() = " + pin.lastAttemptFailed());
System.out.println("\nAttempt 4: Trying to enter atm pin with incorrect pin");
pin.verifyPinCode("1222");
System.out.println("pin.isAccountLocked() = " + pin.isAccountLocked());
System.out.println("pin.lastAttemptFailed() = " + pin.lastAttemptFailed());
System.out.println("\nResetting the counter now..");
pin.resetFailedAttemptCounter();
System.out.println("\nAttempt 5: Trying to enter atm pin with correct pin");
pin.verifyPinCode("1234");
System.out.println("pin.isAccountLocked() = " + pin.isAccountLocked());
System.out.println("pin.lastAttemptFailed() = " + pin.lastAttemptFailed());
}
}