Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Note:
- Please find the below LoginApplication class and coresponding output for three sample runs
- Please revert back in case of any doubts or anything needs to be changed
- Program:
- package com.chegg;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Scanner;
- public class LoginApplication {
- // Global String variable to store input file name
- public static final String INPUT_FILE_NAME = "userinfo.txt";
- // Global HashMap to store user name and password
- public static HashMap<String, String> userNamePasswordMap = new HashMap<String, String>();
- // Global HashMap to store user name and full name
- public static HashMap<String, String> userNameFullnameMap = new HashMap<String, String>();
- public static void main(String[] args) {
- //This method will read input text file and create user name & password map
- //Also create user name & full name map
- readInputFile ();
- //Scanner input to read user input data
- Scanner input = new Scanner(System.in);
- //Variable to store attempt count
- int attemptCount = 1;
- //Iterating three times for user name and password
- while (attemptCount<=3) {
- System.out.print("Login: ");
- String userName = input.nextLine();//Getting user name
- System.out.print("Password: ");
- String password = input.nextLine();//Getting password
- //Checking weather user name exits and coresponding password is matching
- if (userNamePasswordMap!= null & userNamePasswordMap.get(userName) != null
- && userNamePasswordMap.get(userName).equals(password)) {
- //if matching then getting full name based on user name
- String fullName = userNameFullnameMap.get(userName);
- System.out.println("Welcome "+fullName);//Displaying welcome message
- break;//breaking the loop
- } else if (attemptCount <3){
- //Displaying message for wrong user name or password
- System.out.println("Either the username or password is incorrect. You have "+(3-attemptCount)+" more attempt.");
- }
- attemptCount ++;//increasing attempt count for wrong user name or password
- }
- //If 3 attempts over, displaying message
- if (attemptCount >3) {
- System.out.println("Sorry. Incorrect login. Please contact the system administrator.");
- }
- input.close();//Clossing input scanner
- }
- /**
- * This method will read line by line of input text file to get each phone
- * number. Then checking each phone number weather valid or in-valid. After
- * that translated valid phone number to its numeric equivalent
- */
- public static void readInputFile() {
- BufferedReader inputReader = null;
- try {
- // create file object by file name
- File file = new File(INPUT_FILE_NAME);
- // creating input reader to read text file
- inputReader = new BufferedReader(new FileReader(file));
- String line = "";
- // Iterating each line of input text file to get each user info
- while ((line = inputReader.readLine()) != null) {
- String[] lineArray = line.split(" ");//creating string array using space seperator
- String userName = lineArray[lineArray.length-2];//getting user name from array
- String password = lineArray[lineArray.length-1];//getting password from array
- String fullName = "";
- userNamePasswordMap.put(userName, password);//adding user name & password in map
- //getting full name
- for (int i=0;i<lineArray.length-2;i++) {
- fullName = fullName + lineArray[i]+" ";
- }
- userNameFullnameMap.put(userName, fullName.trim());//adding user name & fullname in map
- }
- } catch (FileNotFoundException foe) {
- foe.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (inputReader != null) {
- inputReader.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- Output:
- Sample Run 1
- Sample Run 2
- Sample Run 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement