Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make a class called GetDataException that subclasses the Exception class and con

ID: 3539477 • Letter: M

Question

Make a class called GetDataException that subclasses the Exception class and contains an error code of type int that can be 1, 2, 3 or 4.

                  1 => "username too short (less than 5)";

                  2 => "username too long (greater than 15)";

                  3 => "password too short (less than 8)"

                  4 => "pin is not 5 digits";

Make an appropriate constructor for this class and include a getter method to return the error code. Also, include a method called getError that returns a String and corresponds to one of the above descriptions of the error code.

Next, write a program that contains the following methods:

public static void validate() => this method only handles GetDataExceptions that it throws, it does not handle Exception objects that may be thrown. It prompts the user to enter a username, password and pin code in this sequence. If any error condition is met it immediately throws a GetDataException. In the catch block, inform the user of the error message and prompt the user to try again (Y/N). If the user does not choose Y, throw an Exception containing the message %u201CExit%u201D, otherwise take the user back to the point where he was at when the GetDataException was initially thrown. If more than 3 attempts have been made your catch block should throw an Exception containing the message %u201CBlocked%u201D.

Hint: use a switch statement inside a while loop.

Explanation / Answer

// START of GetDataException.java
public class GetDataException extends Exception
{
private String message = null;
private int error_code;
public GetDataException()
{
super();
}
public GetDataException(String message)
{
super(message);
this.message = message;
}
public GetDataException(int i)
{
//super(message);
error_code = i;
if(i==1)
message = "username too short (less than 5)";
else if(i==2)
message = "username too long (greater than 15)";
else if(i==3)
message = "password too short (less than 8)";
else if(i==4)
message = "pin is not 5 digits";
}
public int get_error_code()
{
return error_code;
}
public String getError()
{
return message;
}
}

// END of GetDataException.java


// START of DRIVER PROGRAM
import java.util.*;

class Driver3
{
public static void main(String[] args) throws Exception    
{
Scanner in = new Scanner(System.in);
char again='Y';
int max_attempts = 0;
int error = 1;
while(again=='Y'||again == 'y')
{
    try{
        validate(error);
        again='n';
        System.out.println("Your request is being processed");
       }
       catch(GetDataException e)
       {
       System.out.println(e.getError());
       error = e.get_error_code();
       max_attempts++;
       if(max_attempts > 3)
       {
       System.out.println("more than 3 attempts made ");
       System.out.println("Your account has been blocked. Please call customer service");
       again = 'n';
       }
       else
       {
       System.out.println("Do you want to try again (Y/N)");
       try
       {
       again = in.nextLine().charAt(0);
       if(again!='y' && again!='Y')
       throw new Exception("Exit");
       }
       catch(Exception exp)
       {
       System.out.println("Thank you for banking with us");
       again = 'n';
       }
       }
       }

      
}

}
public static void validate(int i) throws GetDataException
{
Scanner in = new Scanner(System.in);
switch(i)
{
case 1:
case 2:
{
System.out.println("Enter user name : ");
String user_name= in.next();
if(user_name.length() < 5)
throw new GetDataException(1);
else if(user_name.length() > 15)
throw new GetDataException(2);
}
case 3:
{
System.out.println("Enter Password : ");
String password = in.next();
if(password.length() < 8)
throw new GetDataException(3);
}
case 4:
{
System.out.println("Enter Pin Code : ");
String pin_code = in.next();
if(pin_code.length()!=5)
throw new GetDataException(4);
}
break;
}
}
}

// END of DRIVER PROGRAM.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote