模拟账号登录

  • 创建用户类(User), 定义用户名(username) 和密码(password)属性. 创建本地文件, 写入几个用户名密码,以"," 分割
  • 模拟登录, 启动系统, 提示请您输入用户名密码, 如果成功,提示登录成功,
  • 如果用户名不存在,提示用户注册, 输入 y 进入注册, 请用户输入用户名密码,对比本地文件,如果用户名不重复则注册成功,否则提示失败,并重新注册
  • 如果是密码错误,提示用户重置密码, 输入y, 进入修改密, 两次密码一致, 表示修改成功,并保存到本地文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
    public static void test3_1() throws IOException {
        String path = "users.txt";
        File file = new File(path);
        Scanner sc = new Scanner(System.in);
        if(!file.exists()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 3; i++) {
                System.out.println("请输入用户名:");
                String name = sc.next();
                sb.append(name);
                sb.append("=");
                System.out.println("请输入密码:");
                String password = sc.next();
                sb.append(password);
                sb.append(",");
            }
            
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(sb.toString());
            bw.close();
        } else {
            // 模拟登录
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str;
            StringBuilder sb = new StringBuilder();
            while((str=br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            String[] strings = sb.toString().split(",");
            HashMap hm = new HashMap<>();
            for(String s : strings) {
                String[] s1 = s.split("=");
                User user = new User(s1[0], s1[1]);
                hm.put(user.getUsername(), user.getPassword());
            }
            System.out.println("请输入登录用户名:");
            String user = sc.next();
            if(hm.containsKey(user)) {
                // 用户存在,输入密码
                System.out.println("请输入密码:");
                String password = sc.next();
                if(hm.get(user).equals(password))
                    System.out.println("登录成功");
                else {
                    // 密码错误
                    resetPwd(hm,user);
//                  System.out.println(hm.get(user)+"用户是否要重置密码,重置请按y");
//                  String reset = sc.next(); // 重置
//                  if("y".equals(reset)) {
//                      resetPwd(hm, user);
//                  }
                }
            } else {
                // 用户不存在
                userRegister(hm);
            }
            System.out.println(hm);
            localSave(hm, file);
        }
        sc.close();
    }
    public static void localSave(HashMap hm, File file) throws IOException {
        //保存到本地
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        Set keySet = hm.keySet();
        StringBuilder sb = new StringBuilder();
        for (String key : keySet) {
            sb.append(key);
            sb.append("=");
            sb.append(hm.get(key));
            sb.append(",");
        }
        bw.write(sb.toString());
        bw.close();
        System.out.println("保存本地成功!");
    }
    public static void userRegister(HashMap hm) {
        System.out.println("用户不存在,是否要注册用户,注册请按y");
        Scanner sc = new Scanner(System.in);
        String register = sc.next();
        if("y".equals(register)) {
            // 用户注册
            System.out.println("请输入新用户名:");
            String newUser = sc.next();
            System.out.println("请输入密码:");
            String newPwd = sc.next();
            if(!hm.containsKey(newUser)) {
                hm.put(newUser, newPwd);
                System.out.println("恭喜用户注册成功!");
            } else  {
                System.out.println("用户名已存在,请重新输入");
                userRegister(hm);
            }
        } else {
            // 用户注册
            userRegister(hm);
        }
        sc.close();
    }
    // 密码重置
    public static HashMap resetPwd(HashMap hm, String user) {
        System.out.println(user+"用户是否要重置密码,重置请按y");
        Scanner sc = new Scanner(System.in);
        String reset = sc.next(); // 重置
        if("y".equals(reset)) {
            // 重置密码
            System.out.println("请输入新密码:");
            String pwd = sc.next();
            System.out.println("请再次输入新密码:");
            String renewPwd = sc.next();
            if(pwd.equals(renewPwd)) {
                System.out.println("恭喜用户,重置密码成功!");
                hm.put(user, pwd);
            }else {
                System.out.println("密码输入错误,请重新输入:");
                resetPwd(hm, user);
            }
        } else {
            resetPwd(hm, user);
        }
        sc.close();
        return hm;
    }

你可能感兴趣的:(模拟账号登录)