日期:2014-05-20  浏览次数:20639 次

看看谁能有更简单的方法,大牛们出招吧!!!!!!

今天比较闲暇,写了一个小程序,就是一个登录相关的

从键盘中输入,用main方法接收,user:abc 和password:123

当输入错误时,应该再次输入用户名和密码,知道正确为止。

以下是我的实现方式,我想看看谁有更简单的没有,欢迎指点:

LoginEntity
Java code


public class LoginEntity {

    private String name;
    private String password;
    
    public LoginEntity(String name,String password){
        this.name = name;
        this.password = password;
    }
    
    public boolean validate(){
        if(name.equals("abc")&&password.equals("123")){
            return true;
        }else{
            return false;
        }
            
    }
}



Operate
Java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Operate {

    private String str[];
    
    public Operate(String str[]){
        this.str = str;
        if(str.length!=2){
            System.out.println("输入的参数不正确");
            System.exit(1);            //JVM运行终止,设置一个非0的参数即可
        }
    }
    
    //用户再次输入
    public static void inputTest(){
            
            System.out.println("请再次输入用户名和密码");
                    
            String str[] = new String[2];
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            for (int i = 0; i < str.length; i++) {
                try {
                    str[i] = br.readLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            
        }
    
    public boolean getInfo(){
        if(new LoginEntity(this.str[0], this.str[1]).validate()){
            return true;
        }else{
            return false;
        }
    }
}



Login
Java code
package mystudy.j2se.DemoLogin02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Login {

    public static void main(String[] args) {
        
        System.out.println("请输入用户名和密码");
        
        String str[] = new String[2];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        for (int i = 0; i < str.length; i++) {
            try {
                str[i] = br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        Operate o = new Operate(str);
        
        if(!o.getInfo()){
            System.out.println("登录失败");
            
            o.inputTest();
            
        }else{
            System.out.println("登陆成功");
        }
        
    }
    
    
}



谢谢大家指点, 看看有木有 更简单的

------解决方案--------------------
public boolean validate(){
if(name.equals("abc")&&password.equals("123")){
return true;
}else{
return false;
}

 }
 public boolean getInfo(){
if(new LoginEntity(this.str[0], this.str[1]).validate()){
return true;
}else{
return false;
}
 }
这两处的判断有点像画蛇添足。
直接return name.equals("abc")&&password.equals("123");
return new LoginEntity(this.str[0], this.str[1]).validate();
------解决方案--------------------