маскировка пароля в окне консоли и рекурсивный вызов функции в Java - PullRequest
1 голос
/ 09 февраля 2011

следующий код Java выполняется в окне консоли в среде разработки DR.java. У меня есть следующие две проблемы, пожалуйста, помогите мне, друзья.

  1. Можно ли сделать маскировку пароля ?? я много пробовал гуглить, но ни один не работал для меня (следует использовать только консольное окно для исключения).
  2. Когда я вызываю «GetLoginDetails ();» внутри метода «ShowAdminMainMenuFun (String EmpName)» отображается ошибка ([строка: 148] Ошибка: необработанный тип исключения java.io.IOException). я думал о создании рекурсивной функции, но она не сработала, вы можете исправить код и отправить его обратно.

благодарю друзей

{
import java.io.*;
import java.awt.*;
import java.io.Console;
import java.util.Scanner;

/*
UserLoginAuthentiction UserLoginAuthentictionObj = new UserLoginAuthentiction();
UserLoginAuthentictionObj.GetLoginDetails();
*/

class UserLoginAuthentiction
{
  static String EmployeeID,Password;
  public static byte resultRole = 0;
  public static void main(String[] args) throws Exception 
  {
    GetLoginDetails();   // it works well here but not works when called againin the following code
  }
  static void GetLoginDetails() throws IOException
  {
    Scanner sc = new Scanner(System.in);
    byte resultRole = 0;
    byte CountForLogin = 0;
    System.out.println("Totally 3 attempts ");  

    do
    {

      if(CountForLogin<3){
      System.out.print("\nEnter User Name:");
      EmployeeID = sc.nextLine();

      System.out.print("Enter Password :");



      Password = sc.nextLine();



      resultRole = ValidateUserIDAndPassword(EmployeeID,Password);
      // if result is zero then the login is invalid,
      // for admin it is one , 
      // for quality analyser it is 2 
      // for project developer it is 3 
      // for developer it is 4
      if(resultRole==0)
      {
        System.out.println("Username & Password does not match ");    
        System.out.print("Retry ::");
        CountForLogin++;
        if(CountForLogin>2)
        {System.out.println("ur attempts are over is locked");}

      }
      else
      {
        System.out.println("here t should call the appropriate employe function");

        GetRoleAndAssignFun(EmployeeID,resultRole);
        break;
      }
      }
    }while(resultRole==0);
  }
  static byte ValidateUserIDAndPassword(String EmployeeID,String Password)
  {

    byte resultRole = 0;

    if((EmployeeID.equals("tcs"))&&(Password.equals("tcs")))
    {
       resultRole = 1;

    }

    /*

     Code for checking the arraylist and returning the validations
     this method should return the roles of the users password

     */

    return resultRole;
  }
  static void GetRoleAndAssignFun(String EmpName ,int EmpRole)
  {   
    // System.out.println("  ");  
    switch(EmpRole)
    {
      case 1:
        System.out.println(" hi " +EmpName+ " u are logged in as admin ");
        ShowAdminMainMenuFun(EmpName);
        break;
      case 2:
        System.out.println(" hi " +EmpName+ " u are logged in as QA ");
        //  QualityAnalyserMainMenu(EmpName);
        break;
      case 3:
        System.out.println(" hi " +EmpName+ " u are logged in as PM ");
        //  ProjectMAnagerMainMenu(EmpName);
        break;
      case 4:
        System.out.println(" hi " +EmpName+ " u are logged in as DEVeloper ");
        //  DeveloperMainMenu(EmpName);
        break;
      default:
        //  System.out.println(EmpName +" You dont have any roles asigned ");
        break;
    }       
  } 


  public static void ShowAdminMainMenuFun(String EmpName)
  {   
    Scanner sc = new Scanner(System.in);
    int loop_option=0;
    do
    {

      //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

      System.out.println(" Hi "+ EmpName + " you can perform task ussing the menu given below");

      System.out.println("press the appropriate option only");
      System.out.println("1.Create New Employe Profile ");
      System.out.println("2.See Employee's Profile ");
      System.out.println("3. LogOut ");

      System.out.println("Enter the Option u need:");               
      int  option = sc.nextInt();
      switch(option)
      {
        case 1:
          System.out.println("1.Creating New Employe Profile");
          //CreateNewEmployeeProfile();
          break;
        case 2:
          System.out.println("2.See Employee's Profile ");
          //  ViewEmployeeProfile();
          break;
        case 3:
          System.out.println("3. LogOut");
          System.out.println("Do u want to continue logging out  ?? If yes Press 1  ..");
          boolean ConformLogout = false;

          ConformLogout = sc.nextBoolean();
          if(ConformLogout)
          {
            **GetLoginDetails();**   //**** error is here */ how can i call this function please help me
          }
          else
          {

          }
          //   LogOut();
          break;        
        default :
          System.out.println(" You .. ");
      }
      System.out.println("Do u want to continue to main menu ?? Press 1 to continue..");
      loop_option = sc.nextInt();
    }while(loop_option==1);     
  }  


}
}

1 Ответ

1 голос
/ 09 февраля 2011

Относительно вашего первого вопроса,

Можно ли сделать пароль маскировка?

Вы можете использовать java.io.Console класс, чтобы скрыть пароль в окне консоли.

...