JAVA Write the e following class name Authenticator that has the following state
ID: 3701021 • Letter: J
Question
JAVA
Write the e following class name Authenticator that has the following states and behaviors.
Please Do NOT use Arraylist. Write an Array of Objects. User object has 3 String variables: userName, password, passwordHint
State
An array of type User (use a capacity of 100 — I would recommend using a class constant the way I did in the 06-Array class of Lecture 2).
An integer size
Behavior - Please WRITE CODE that:
1.) A constructor accepting a file name, that opens a Scanner on the file and reads in User objects
2.) A method named authenticate that accepts a username and password and attempts to authenticate them against the User array (by doing a search).
3.) Not finding the username in the array causes an exception to be thrown
4.) finding the username, but not matching the password (via verifyPassword) causes an exception with a different message to be thrown (this one with the password hint included).
5.) See below for the exact exception messages expected
6.) The return type of the method is void, i.e., the method returns nothing if the username and password are matched; otherwise an exception is thrown, as described above.
Your class is tested by a AuthenticatorApp class that reads in Users from a file using your read method, loads them into an array and prompts the keyboard for a login sequence (username/password).
For example, if the file users.data contains:
Explanation / Answer
Source Code:-
-----------------------
package com.samples;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Authenticator
{
String array[];
Authenticator(String filename) throws FileNotFoundException
{
array=new String[3];
//Path Name and Appending Filename
File file=new File("F:\Workspace_Luna\Challenging_Tasks\"+filename);
Scanner sc=new Scanner(file);
int pos = 0;
while(sc.hasNextLine())
{
array[pos]=sc.nextLine();
pos++;
}
sc.close();
}
void authenticate(String username, String password)
{
int flag=1,flag2=1;
for(int i=0;i<2;i++)
{
if(array[i].equals(username))
{
flag=0;
}
if(array[i].equals(password))
{
flag2=0;
}
}
if(flag2==1&&flag==1)
{
throw new NoSuchElementException("UserName and Password Not Exist");
}
else if(flag==1&&flag2==0)
{
throw new NoSuchElementException("UserName Not Found");
}
else if(flag==0&&flag2==1)
{
throw new NoSuchElementException("Password Not Matched");
}
else
{
System.out.println("Authenticated Successfully");
}
}
public static void main(String[] args) throws FileNotFoundException
{
Scanner input=new Scanner(System.in);
Authenticator obj=new Authenticator("users.data");
String Username,Password;
System.out.println("Enter The Username");
Username=input.next();
System.out.println("Enter Password");
Password=input.next();
obj.authenticate(Username,Password);
input.close();
}
}
Input file:-
-------------------
venkanna
Venky@547
Sample Output:-
-----------------------------
Enter The Username
venky
Enter Password
Venky@547
Exception in thread "main" java.util.NoSuchElementException: UserName Not Found
at com.samples.Authenticator.authenticate(Authenticator.java:43)
at com.samples.Authenticator.main(Authenticator.java:63)
Enter The Username
venkanna
Enter Password
Venky
Exception in thread "main" java.util.NoSuchElementException: Password Not Matched
at com.samples.Authenticator.authenticate(Authenticator.java:47)
at com.samples.Authenticator.main(Authenticator.java:63)
Enter The Username
venkanna
Enter Password
Venky@547
Authenticated Successfully
Enter The Username
venkanna
Enter Password
Venky@547
Authenticated Successfully
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.