package atm;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PinCheck {
public String enterPin() {
String inputPinNo2 = "0000";
InputStreamReader keyInput = new InputStreamReader(System.in);
BufferedReader buffInput = new BufferedReader(keyInput);
System.out.print("Enter pin: ");
try {
inputPinNo2 = buffInput.readLine();
} catch (IOException ex) {
Logger.getLogger(PinCheck.class.getName()).log(Level.SEVERE, null, ex);
}
return inputPinNo2;
}
public static void main(String[] args) {
int pinRetries = 0;
String pinNo = "0907";
String inputPinNo = "0907";
do
{
PinCheck enterPinNo = new PinCheck();
inputPinNo = enterPinNo.enterPin();
if (!(pinNo.equals(inputPinNo)))
{
System.out.println("Wrong Pin");
pinRetries++;
}
else
{
System.out.println("Access granted");
pinRetries = 0;
break;
}
} while (pinRetries < 3);
if (pinRetries == 3)
{
System.out.println("Account blocked");
}
else
{
System.out.println("Welcome!");
}
}
}
There's my code for the PIN. It asks the user for a PIN. If it's correct, it displays "Welcome". If it's not, the same question is asked 3 times until the user gets the correct PIN, or its account gets blocked.
It's working, but I think it maybe too long or too inefficient. I'm new to Java programming. Can anybody comment and/or optimize this code? Please? Thank you very much.