[英]How would I condense these methods?
我无法弄清楚如何使用单个isValidPassword
方法解决此问题,因此我将其分解为3个并使其正常工作。 有人可以向我解释如何使用我编写的代码,如果可能的话可以压缩它?
创建一个程序Password(密码),提示用户输入密码并确定密码有效还是无效。 应该有一个静态方法isValidPassword
来验证密码(字符串)并返回true或false。 用户只有3次尝试输入有效的密码。 有效的密码遵循以下三个规则:
- Must have at least eight characters.
- Must contain ONLY letters and digits.
- Must contain at least two digits.
使用程序对话框。 您必须为此程序使用String和Character类。 您不能使用正则表达式。 该程序只需要运行一次。
import javax.swing.JOptionPane;
public class Password {
public static void main(String[] args) {
int retryCount = 0;
while (retryCount < 3) {
retryCount++;
String password = JOptionPane.showInputDialog("Please Enter the Password");
if (CheckPassword(password)) {
JOptionPane.showMessageDialog(null, "Congrats! Correct Password!");
break;
}
else {
JOptionPane.showMessageDialog(null, "Sorry, Invalid Password, you tried " + retryCount + " times");
continue;
}
}
}
public static boolean CheckPassword(String password) {
if (password.length() >= 8 & CheckAlphanumeric(password) && Check2digits(password)) {
return true;
}
else {
return false;
}
}
public static boolean CheckAlphanumeric(String string) {
for (int i = 0; i < string.length(); i++) {
char x = string.charAt(i);
if (x < 0x30 || (x >= 0x3a && x <= 0x40) || (x > 0x5a && x <= 0x60) || x > 0x7a) {
return false;
}
}
return true;
}
public static boolean Check2digits(String string) {
int count = 0;
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (Character.isDigit(c)) {
count++;
}
if (count > 2) {
return true;
}
}
return false;
}
}
让我们快速回顾一下代码:
isPasswordValid()
除此之外; 有关如何针对“真实世界”用法增强设计的建议。 事实是:可以有许多不同的规则来使密码有效 。 这可能会随着时间而改变。 通常,您可能想告诉用户其输入与哪个规则冲突。 解决这些问题的一种选择:
public interface PasswordValidator {
public void checkPassword(String password) throws InvalidPasswordException;
}
然后您创建各种实现,例如
public class MinimumLengthValidator implements PasswordValidator {
@Override
public void checkPassword(String password) throws InvalidPasswordException {
if (password.lenght() < 8) {
throw new InvalidPasswordException("Given password is not long enough; expecting at least 8 characters");
}
}
然后,创建这些不同的验证器类的实例。 并将它们存储在某个数组中。 您对数组进行迭代,并单独调用每个验证。 当它失败时,您会直接收到一个异常,并向用户显示一条好消息。
如前所述:上面的内容仅供参考(只需键入以下内容,请注意输入错误)。
请看下面的代码,它可以满足您的所有要求-
这是纯Java类。 您可以从以下代码检查逻辑。
程序edit1-指定密码错误的原因。
public class Demo {
private final int passwordAttempts = 3;
private int countAttempts = 0;
private String password;
private int digitCounter = 0;
public Demo() {
init();
}
public static void main(String[] args) {
new Demo();
}
private String checkPassword(String pass) {
if (pass.length() >= 8) {
if (StringUtils.isAlphanumeric(pass)) {
char[] toCharArray = pass.toCharArray();
for (int i = 0; i < toCharArray.length; i++) {
char check = toCharArray[i];
if(Character.isDigit(check)){
digitCounter++;
}
}
if (digitCounter >= 1) {
return "success";
}else{
return "digit";
}
}else{
return "alphanumeric";
}
}else{
return "length";
}
}
private void init() {
while (countAttempts < passwordAttempts) {
password = JOptionPane.showInputDialog("Please Enter the Password");
digitCounter = 0;
String passwordResult = checkPassword(password);
if (passwordResult.equals("success")) {
JOptionPane.showInputDialog("yes you entered correct password...");
} else if(passwordResult.equals("length")){
JOptionPane.showInputDialog("password must contain atleast 8 characters...");
}else if(passwordResult.equals("alphanumeric")){
JOptionPane.showInputDialog("only alphanumeric password accept...");
}else if(passwordResult.equals("digit")){
JOptionPane.showInputDialog("password must contain atleast two digits...");
}
countAttempts++;
}
}
}
现在只需要用其他仅指定消息的方法替换JOptionPane.showInputDialog()方法,因为showInputDialog()方法会提示输入文本字段也输入值,我们不希望这样做。 我对挥杆一无所知。
根据您的评论-
此方法返回字符串值,例如成功,长度,字母数字,数字等。 下面我们将这个值与if if if上面的所有值进行比较。 每当它相等时,这意味着我们必须相应地显示长度消息,字母数字字符串消息,数字消息。
依赖-
我正在使用Maven。 以下依赖项需要添加到pom.xml文件中。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
如果您不了解maven,则只需从此处下载jar文件,并在项目的lib文件夹中提供即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.