Java 将信息传递给构造函数

Java passing information to constructor

我这里有一个工作程序,它需要用户输入(密码)并检查条件(是否为 8 个字符,包含一个大写字母、一个小写字母和一个数字),然后将信息写入文件。

它编译并运行,但在进一步查看作业说明后,我必须使用一个对象来调用这些方法。这让我失望了。我想我会使用这样的语句: password.PasswordCheck(pwd);但我不确定这将如何与我所拥有的或放在哪里一起工作。

目前我的 toString 方法负责构建字符串以向用户显示信息,并通过调用可接受的方法来完成。

我必须做些什么来改变这个,以便一切都使用对象来调用?

非常感谢!

import java.util.Scanner;   
import java.io.*;           

public class PasswordCheck //create the class
{
       private boolean isEightCharacters;  //true if has 6 characters, false if                                                                                  doesn't
       private boolean hasOneUppercase;  //true if has 1 uppercase character, false if doesn't
       private boolean hasOneLowercase;  //true if has 1 lowercase character, false if doesn't
       private boolean hasOneDigit;      //true if has 1 digit, false if doesn't
       public boolean isPasswordValid;  //true if all other conditions are true, false otherwise
       private String password;         //global variable declaring string

       //create default constructor
       public PasswordCheck(){

          isEightCharacters = false;    //default value
          hasOneUppercase = false;      //default value
          hasOneLowercase = false;      //default value
          hasOneDigit = false;          //default value
          isPasswordValid = false;      //default value

       }

       //create constructor with string as input
       public PasswordCheck(String pwd){
          password = pwd;   //set password to the variable being passed in
          setEightCharacters();   //call the method
          setOneUppercase();      //call the method
          setOneLowercase();      //call the method
          setOneDigit();          //call the method
          setPasswordValid();     //call the method            

       }

       //create methods to change attributes/fields above
       public void setEightCharacters(){

           if (password.length() >= 8)  //if statement to validate password length
          {
             isEightCharacters = true;  //if passes condition test, set to true
          }   
          else  //if not 8 characters
          {
             isEightCharacters = false;    //set boolean to false
          }
       } 

       public void setOneUppercase(){   //method to check if password contains uppercase letter

         int i;    //declare count variable
         char ch;  //declare char variable
         boolean hasUpper = false;   //declare and initialize boolean to false

         for ( i = 0; i < password.length(); i++ ) {   //begin loop to read through string

             ch = password.charAt(i);   //sets ch to character in position of string
             if (Character.isUpperCase(ch))   //tests to see if character is uppercase
             {    
                 hasUpper = true;    //sets boolean to true
             }

         }

             if(hasUpper == true)    
             {
                hasOneUppercase = true;
             } 
             else 
             {
                hasOneUppercase = false;
             }
       }

       public void setOneLowercase(){   //method to check if password contains lowercase letter

         int i;    //declare count variable
         char ch;  //declare char variable
         boolean hasLower = false;   //declare and initialize boolean

         for ( i = 0; i < password.length(); i++ ) {   //begin loop

             ch = password.charAt(i);
             if (Character.isLowerCase(ch))   //tests to see if string contains lowercase letter
             {    
                 hasLower = true;
             }

         }

             if(hasLower == true)
             {
                hasOneLowercase = true;
             } 
             else 
             {
                hasOneLowercase = false;
             }
       }

       public void setOneDigit(){    //method to see if string contains a number

         int i;    //declares the count variable
         char ch;  //declares the char variable
         boolean hasNumber = false;     //declares and intializes the boolean variable

         for ( i = 0; i < password.length(); i++ ) {   //begin loop

             ch = password.charAt(i);
             if (Character.isDigit(ch)) //tests to see if character is a number
             {    
                 hasNumber = true;
             }

         }

             if(hasNumber == true)
             {
                hasOneDigit = true;
             } 
             else 
             {
                hasOneDigit = false;
             }
       }

       public void setPasswordValid(){     //method to test is all conditions are met for a valid password

          if (isEightCharacters && hasOneUppercase && hasOneLowercase && hasOneDigit == true) //if all true
          {
             isPasswordValid = true;
          }
          else
          {
             isPasswordValid = false;
          }
       }



       //create a method to check ok or missing, pass in one of the booleans
       public String acceptable(boolean x) {

          String var="";
            if (x == true)
             var = "OK";
            else
             var = "Missing";

          return var;    //return statement
       }

       //create a method to validate all conditions
       public String validate(boolean x) {

          String valid="";
             if (x == true)
                valid = "\nPassword is valid.";
             else
                valid = "\nPassword is not valid.";

          return valid;     //return statement
       }

       // toString creates a string with the output
       public String toString(){
          String stringToReturn="";  //declare the string to return

          stringToReturn += validate(isPasswordValid)+
          "\n\n8 characters:  "+ acceptable(isEightCharacters)+ "\n\n1 uppercase:  "+ acceptable(hasOneUppercase)
          +"\n\n1 lowercase:  "+acceptable(hasOneLowercase)+"\n\n1 digit:  "+acceptable(hasOneDigit);

          return stringToReturn;
       }


       public static void main(String[] args) throws IOException
        {
          //variables
          String filename = "passwordCheck.txt";
          String pwd;

          //Welcome message
          System.out.println("Welcome to the Password Check Program!\n");

          //Create scanner object for keyboard input
          Scanner keyboard = new Scanner(System.in);

          //prompt user for the password
          System.out.println("Please enter the password to verify: ");
          pwd = keyboard.nextLine(); //stores the input

          //create object using String as password
          PasswordCheck password=new PasswordCheck(pwd);  //remember to change PasswordCheck based upon new name of class/filename

          //display the validated information
          System.out.println(password);  //uses toString() method for display

          //make sure file does not exist
          File file = new File(filename);
          if (file.exists())
          {
             System.out.println("\nFile " + filename + " already exists. Output not written.");

             System.exit(0);
          }

          //Open the file
          PrintWriter outputFile = new PrintWriter(file);

          //Write the password to the file
          outputFile.println(password);

          //Close the file
          outputFile.close();
          System.out.println("\nOutput written to " + filename);

        }
    }

我认为它希望你制作一个对象密码,并在构造函数中使其满足密码很可能只有一个字符串变量可以接受的条件,然后你可以这样检查它是否正确

根据我对你问题的理解,我认为你需要使用对象的引用变量来调用其中的方法。因此,不要在构造函数中调用所有方法,而是在 main 方法中调用它们,如下所示。

更多信息: 通过不调用构造函数内的所有方法,您可以自由地在主方法中仅调用您想要调用的那些方法。例如,在任何应用程序中,您希望您的密码至少有 8 个字符长,并且不一定有数字、lowercase/uppercase 字母表,那么您可以只调用一个所需的方法。

让构造器像这样

public PasswordCheck(String pwd){
          password = pwd;   //set password to the variable being passed in         
       }

然后在你的main方法中

PasswordCheck password=new PasswordCheck(pwd);
password.setEightCharacters();   //call the method
password.setOneUppercase();      //call the method
password.setOneLowercase();      //call the method
password.setOneDigit();          //call the method
password.setPasswordValid();

我的解释是你需要将所有的密码验证方法作为PasswordCheck的实例方法。

所以像

  public PasswordCheck(){
      private String password;

      public PasswordCheck(String pwd){        
          password = pwd
      } 

      // Below you have all your verification methods
      public void isEightCharacters(){
         if (password.length() >= 8){
             System.out.println("Valid");
         }   
         else {
             System.out.println("invalid"); 
         }

      }
      // etc.etc.

   }

然后在您的主要方法中执行如下操作:

PasswordCheck password=new PasswordCheck(pwd);   
// Call all your verification method
password.isEightCharacters();
// etc.etc.