【BigData】Java基础_登录注册实现

需求描述

实现简单的登录注册功能,要求以下几点:

1.注册时对两次输入的密码进行验证

2.检查注册的用户名是否已经存在

代码实现

代码结构图:

 

 package cn.test.logan.day07.login;

import java.util.Scanner;

import cn.test.logan.day07.login.service.UserService;
import cn.test.logan.day07.login.service.UserServiceImpl;

/**
 * 用户登录认证,主要实现以下功能:
 * 1.根据选择的菜单选项【1.登录  2.注册  quit.退出】,进行操作
 * 2.当用户选择登录,先验证用户是否存在,如果存在,则验证密码是否正确,如果用户不存在或者密码错误,统一返回"Sorry,用户名或密码错误"
 * 3.实现注册
 *   - 用户输入两次密码,验证两次密码是否一致
 *   - 查询注册账号是否存在,存在则提示
 * @author QIN
 *
 */
public class UserLogin {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        // 引用接口类型变量
        // UserService service = null;
        UserService service = new UserServiceImpl();
        
        
        boolean flag = true;
        while(flag) {
            System.out.println("1.登录;  2.注册; quit.退出");
            String line = sc.nextLine();
            switch(line) {
                case "1":
                    System.out.println("请输入您的用户名:");
                    String username = sc.nextLine();
                    System.out.println("请输入您的密码:");
                    String password = sc.nextLine();
                    // 调用登录方法进行登录
                    boolean status = service.login(username, password);
                    
                    if(status) {
                        System.out.println("您已经登录成功!");
                    }else {
                        System.out.println("Sorry,用户名或密码错误!");
                    }
                    break;
                case "2":
                    System.out.println("请输入你的用户名:");
                    String uname = sc.nextLine();
                    System.out.println("请输入你的密码:");
                    String pwd1 = sc.nextLine();
                    System.out.println("请确认你的密码:");
                    String pwd2 = sc.nextLine();
                    String res = service.regist(uname, pwd1, pwd2);    
                    switch(res) {
                        case "1":
                            System.out.println("你两次输入的密码不一致.");
                            break;
                        case "2":
                            System.out.println("你要注册的账号已经存在.");
                            break;
                        case "3":
                            System.out.println("你已经注册成功了!恭喜你.");
                            break;
                        default:
                            break;
                    }
                    break;
                case "quit":
                    System.out.println("程序已退出...........");
                    flag = false;
                    break;                
            }
        }
        
        
    }
}
UserLogin.java
package cn.test.logan.day07.login;

import java.util.HashMap;

import cn.test.logan.day07.login.pojo.User;

public class UserDatabase {
    public static HashMap<String,User> userMap = new HashMap<>();
    /**
     * 定义静态代码块,在类被jvm加载时,静态代码就行执行
     */
    static {
        User u1 = new User();
        u1.set("admin","1");
        
        User u2 = new User();
        u2.set("Logan","1");
        
        userMap.put(u1.getUsername(), u1);
        userMap.put(u2.getUsername(), u2);
    }
}
UserDatabase.java

service层

package cn.test.logan.day07.login.service;

/**
 * 定义两个方法:login、regist
 * @author QIN
 *
 */
public interface UserService {
    // 登录方法
    public boolean login(String username,String pwd);
    
    // 注册方法
    public String regist(String username,String pwd1,String pwd2);
    
}
UserService.java
package cn.test.logan.day07.login.service;

import cn.test.logan.day07.login.dao.UserDao;
import cn.test.logan.day07.login.dao.UserDaoImpl;
import cn.test.logan.day07.login.pojo.User;

public class UserServiceImpl implements UserService{
    
    // UserDao userdao = null;
    UserDao userdao = new UserDaoImpl();

    @Override
    public boolean login(String username, String pwd) {
        //调用DAO层,根据用户名查询返回一个用户对象
        User user = userdao.selectUser(username);
        
        if(user==null) {
            return false;
        }
        if(user.getPassword().equals(pwd)) {
            return true;
        }
        return false;
    }

    @Override
    public String regist(String username, String pwd1, String pwd2) {
        // 检查密码是否一致
        if(!pwd1.equals(pwd2)) {
            return "1";
        }
        // 检查用户已经存在则返回2
        boolean res = userdao.checkUser(username);
        if(res) {
            return "2";
        }
        
        // 
        User u = new User();
        u.set(username, pwd1);
        userdao.addUser(u);
        return "3";
    }
    
}
UserServiceImpl.java

dao层

package cn.test.logan.day07.login.dao;

import cn.test.logan.day07.login.pojo.User;

public interface UserDao {
    public User selectUser(String username);
    public boolean checkUser(String username);
    public void addUser(User user);
}
UserDao.java
package cn.test.logan.day07.login.dao;

import cn.test.logan.day07.login.UserDatabase;
import cn.test.logan.day07.login.pojo.User;

public class UserDaoImpl implements UserDao{
    
    /**
     * 根据一个用户名到数据库中查找该用户
     */
    @Override
    public User selectUser(String username) {
        User user = UserDatabase.userMap.get(username);
        return user;
    }
    
    /**
     * 检查数据库中是否已存在该用户
     */
    @Override
    public boolean checkUser(String username) {
        boolean containsKey = UserDatabase.userMap.containsKey(username);
        return containsKey;
    }
    
    /**
     * 将传入的user对象存入数据库
     */
    @Override
    public void addUser(User user) {
        UserDatabase.userMap.put(user.getUsername(), user);    
    }

}
UserDaoImpl.java

pojo层

package cn.test.logan.day07.login.pojo;

public class User {
    private String username;
    private String password;
    
    public void set(String username,String password) {
        this.username = username;
        this.password = password;
    }
    // 生成get与set
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}
User.java

 

posted @ 2019-12-24 23:24  OLIVER_QIN  阅读(466)  评论(0编辑  收藏  举报