java用户登录注册

用户登录注册

案例需求

涉及知识点

面向对象,接口,Java序列化,HashMap,IO流

欢迎界面

public class BootStrap {
          
    
    public static void main(String[] args) {
          
    
        System.out.println("欢迎来到自动售货系统,请选择:"); 
        FrontMenu.showFirstLevelMeun(); 
    } 
}

一级目录

public class FrontMenu {
          
    
    public static void showFirstLevelMeun() {
          
    
        UserService userService = new UserServiceImp(); 
        System.out.println("1、注册    2、登录"); 
        Scanner sc = new Scanner(System.in); 
        String select = sc.nextLine();
		 //注册 
        if(select.equals("1")) {
          
   
            System.out.println("请输入用户名:"); 
            String username = sc.nextLine(); 
            System.out.println("请输入密码:"); 
            String password = sc.nextLine(); 
            System.out.println("请再次输入密码:"); 
            String password2 = sc.nextLine(); 
            if(password.equals(password2)) {
          
    
                boolean isExists = userService.isExists(username); 
                if(isExists) {
          
    
                    System.out.println("此用户名已存在"); 
                    showFirstLevelMeun(); 
                }else {
          
    
                    System.out.println("请输入地址:"); 
                    String address = sc.nextLine();
                    userService.regist(username,password,address); 
                } 
            }else {
          
    
                System.out.println("您输入的密码不一致"); 
                showFirstLevelMeun(); 
            } 
        }else if(select.equals("2")) {
          
    
            //登录 
            System.out.println("请输入用户名:"); 
            String username = sc.nextLine(); 
            System.out.println("请输入密码:"); 
            String password = sc.nextLine(); 
            boolean success = userService.login(username,password); 
            if(success) {
          
    
                System.out.println("登录成功"); 
            }else {
          
    
                System.out.println("用户名或者密码错误"); 
                showFirstLevelMeun(); 
            } 
        }else {
          
   
            System.out.println("您输入的选项不正确");
            showFirstLevelMeun();
        }
    }
}

登录注册功能交互

public interface UserService {
          
    
    /*
     * 判断用户名是否已经存在
     */
    boolean isExists(String username); 
    /*
     * 注册一个用户
     */ 
    void regist(String username, String password,String address); 
    /*
     * 登录
     */ 
    boolean login(String username, String password); }

实现接口中的具体方法

public class UserServiceImp implements UserService{
          
    
    UserDao userdao = new UserDaoImp();
    /*
     * 判断用户名是否存在
     */ 
    @Override 
    public boolean isExists(String username) {
          
    
        HashMap<String,User> userMap = userdao.loadUserData();
        return userMap.containsKey(username); 
    } 
    /**
     * 注册
     */ 
    @Override 
    public void regist(String username, String password,String address) {
          
    
        User user = new User(); 
        user.setUsername(username); 
        user.setPassword(password); 
        user.setAddress(address); 
        //调用dao将user数据添加到hashmap中 
        HashMap<String,User> userMap = userdao.loadUserData(); 
        //将新用户对象添加到userMap中 
        userMap.put(user.getUsername(), user); 
        //调用dao将添加后usermap保存到数据文件中 
        userdao.saveUserData(userMap); 
        System.out.println("注册成功"); 
        FrontMenu.showFirstLevelMeun(); 
    } 
    /*
     * 登录
     */ 
    @Override 
    public boolean login(String username, String password) {
          
    
        //调用dao将user数据添加到hashmap中 
        HashMap<String,User> userMap = userdao.loadUserData();
        if(userMap.containsKey(username)) {
          
    
            User user = userMap.get(username); 
            if(user.getPassword().equals(password)) {
          
    
                return true; 
            }else {
          
    
                return false; 
            } 
        }else {
          
    
            return false; 
        } 
    } 
}

程序中的用户数据与磁盘文件存储数据的接口

public interface UserDao {
          
    
    /**
     * 将数据文件中得数据读取到hashmap中
     */ 
    HashMap<String, User> loadUserData(); 
    /**
     * 将hashmap中的数据保存到数据文件中
     */ void saveUserData(HashMap<String, User> userMap); 
}

实现接口中的具体方法

public class UserDaoImp implements UserDao{
          
    /**
     * 将hashMap中的数据保存到用户数据文件中
     */ 
    @Override 
    public HashMap<String, User> loadUserData() {
          
    
        File file = new File("D:/user.data"); 
        try {
          
    
            if(!file.exists()) {
          
    
                HashMap<String, User> userMap = new HashMap<String, User>();
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); 
                oos.writeObject(userMap); 
                oos.close(); 
                return userMap; 
            } 
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            HashMap<String, User> userMap = (HashMap<String, User>)ois.readObject();
            ois.close(); 
            return userMap; 
        } catch (Exception e) {
          
    
            System.out.println("文件里没有对象"); 
            e.printStackTrace(); 
        } 
        System.out.println(3); 
        return null; 
    } 
    /*
     * 将hashMap中的数据保存文件中
     */ 
    @Override public void saveUserData(HashMap<String, User> userMap) {
          
         
        try {
          
    
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/user.data")); 
            oos.writeObject(userMap);
            oos.flush(); 
            oos.close(); 
        } catch (Exception e) {
          
    
            e.printStackTrace(); 
        } 
    } 
}

使用的user类

public class User implements Serializable{
          
    
    private String username; 
    private String password; 
    private String address; 
    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; 
    } 
    public String getAddress() {
          
    
        return address; 
    } 
    public void setAddress(String address) {
          
    
        this.address = address; 
    } 
}

注意事项

1.进行序列化的对象必须要实现Serializable接口
2.IO流的关流操作
3.HashMap的与序列化的相互转化细节,容易出现空指针异常
经验分享 程序员 微信小程序 职场和发展