
Patrick B. answered 08/15/21
Math and computer tutor/teacher
import java.util.Scanner;
import java.lang.Character;
import java.lang.Exception;
class InputMismatchException extends Exception
{
public InputMismatchException(String errorMessage)
{
super(errorMessage);
}
}
class _RuntimeException extends Exception
{
public _RuntimeException(String errorMessage)
{
super(errorMessage);
}
}
class PinCodeChecker
{
protected String pinNumber;
protected Scanner scanner;
public PinCodeChecker()
{
pinNumber = "5276";
scanner = new Scanner(System.in);
}
int CheckPinNumber(String strPinNumber) throws InputMismatchException,_RuntimeException
{
int iReturn=0;
int nStrLen = strPinNumber.length();
if (nStrLen==4)
{
for (int iLoop=0; iLoop<nStrLen; iLoop++)
{
char curChChar = strPinNumber.charAt(iLoop);
System.out.println(" cur char is " + curChChar);
if (Character.isDigit(curChChar))
{
if (pinNumber.charAt(iLoop)!=curChChar)
{
iReturn=-4; //return(iReturn);
throw (new _RuntimeException(" pincode mismatch "));
}
}
else
{
iReturn=-2; //return(iReturn);
throw( new InputMismatchException("invalid pincode character"));
}
}
}
else
{
System.out.println("4 digit pin # expected ");
iReturn = -1;
}
return(iReturn);
}
public void Go()
{
boolean done_flag = false;
int iReturn=0;
while (!done_flag)
{
done_flag=true;
System.out.print(" Please input the pin # :>");
String strPinNumber = scanner.nextLine();
try
{
iReturn = CheckPinNumber(strPinNumber);
}
catch (InputMismatchException ex)
{
done_flag=false;
iReturn=-2;
System.out.println(" Please input only digits... Please try again !!! ");
}
catch (_RuntimeException ex)
{
done_flag=false;
iReturn=-4;
System.out.println(" Invalid pin #... Please try again !!! ");
}
if (iReturn==0)
{
System.out.println("Pin # is correct !!! " + pinNumber);
done_flag=true;
break;
}
else
{
done_flag=false;
}
} //while
}
public static void main(String args[])
{
PinCodeChecker x = new PinCodeChecker();
x.Go();
}
}