0

按照以下 UML 图中的说明设计一个名为 Password2 的类: 在此处输入图像描述

  • 密码必须至少包含七个字符
  • 密码仅由字母和数字组成
  • 密码必须至少包含三位数字

+Password2 (ps: String)输入密码的构造

+verifyPassword2():void调用其他支持方法

-verifyLength():boolean根据密码长度返回真/假

-verifyValidCharacters():boolean根据有效/无效字符返回真/假

-verifyNumberOfDigits(): boolean根据位数返回真/假

+printReport():void打印如下指定的摘要报告

这是我到目前为止所拥有的:

public class Password2 {
private boolean rule1;
private boolean rule2;
private boolean rule3;
private String ps;

public Password2(){
}

 public Password2(String ps){
     this.ps = ps;
 }

 public void verifyPassword2(){
     //Verify the password is the right length
     rule1 = verifyLength();
     rule2 = verifyValidCharacters();
     rule3 = verifyNumberOfDigits();
}    
private boolean verifyLength(){
         if (ps.length() >= 8){
             return true;
        return false;
    }
     //Verify if the password have the right characters
private boolean verifyValidCharacters(){
    if (ps.matches("^[A-Za-z1-9]$"){
        return true;}
    return false;    
    }
     //Verify if the password contains at least 2 digits
private boolean verifyNumberOfDigits(){
    int digit = 0;
    for (int i = 0; i = <ps.length(); i++){
        char letter = password.charAt(i);
        if(Character.isDigit(letter)){
                digit++;
                    }
         if (digits >= 2){
            return true;{
                    }
         return false
         }
     }

 public void printReport(){
     String Report;
     Report = "\t+++++++++++++++++ Password Report ++++++++++++++++++++\n";
     Report += "\tLebron Broadnax\n";
     Report += "\tCS1302\n";
     Report += "\t6/11/201\n3";
     Report += "\t+++++++++++++++++++++++++++++++++++++++++++++++++++++";

 }
 }

方法不在其他方法中这就是我发布的方式

4

1 回答 1

1

改进提示之一:

//Verify if the password have the right characters
private boolean verifyValidCharacters()
{
     if (ps.matches("^[A-Za-z1-9]$")
         return true;

    return false;
}

可以改为:

private boolean verifyValidCharacters()
{
     return ps.matches("^[A-Za-z1-9]$");
}

既然你想返回true它是否匹配,false如果它不匹配,这正是matches(regex)它所做的。

类似的逻辑可以应用于:

  private boolean verifyNumberOfDigits()
  {
       int digit = 0;

       for (int i = 0; i = <ps.length(); i++)
       {
            char letter = password.charAt(i);

            if(Character.isDigit(letter))
                digit++;
       }            

       return (digit >= 2);             
 }

此外,您的方法不应在您的verifyPassword2方法中声明。

您应该将您的问题发布到:codereview.stackexchange

于 2013-06-12T17:53:59.137 回答