java BufferedReader/Writer作业

实现一个登录注册功能
把注册成功的用户信息写入到硬盘中

package com.wxhl.jq0802;
import java.io.*;
import java.util.HashSet;
import java.util.Scanner;

class User{
    String name;
    String passwd;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public User(String name,String passwd){
        this.name = name;
        this.passwd = passwd;
    }
    
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        User user = (User)obj;
        return (user.name.equals(this.name));
    }
}

public class Demo01 {
    
    static HashSet<User> set = new HashSet <User>();
    static File userFile = new File(System.getProperty("user.home")+"/Desktop/user.txt");
    
    public static void main(String[] args) throws IOException {
        readFromFile();
        while(true){
            System.out.println("请选择功能");
            System.out.println("1.登录 2.注册 3.退出");
            int sel = new Scanner(System.in).nextInt();
            switch(sel){
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            case 3:
                exit();
                break;
            default:
                System.out.println("没有改该选项,请重新选择");
                break;
            }
            
        }
    }
    
    /**
     * 登录*/
    public static void login(){
        System.out.println("用户名:");
        String name = new Scanner(System.in).nextLine();
        System.out.println("密码:");
        String passwd = new Scanner(System.in).nextLine();
        for(User user :set){
            if(user.getName().equals(name) ){
                if(user.getPasswd().equals(passwd)){
                    System.out.println("登录成功");
                    return;
                }
                else{
                    System.out.println("密码错误");
                    return;
                }
            }
        }
        System.out.println("用户不存在");
    }
    
    /**
     * 注册*/
    public static void register (){
        while(true){
            System.out.println("请输入以下信息:");
            System.out.println("用户名:");
            String name = new Scanner(System.in).nextLine();
            System.out.println("密码:");
            String passwd = new Scanner(System.in).nextLine();
            
            if(set.add(new User(name,passwd))){
                System.out.println("注册成功");
                break;
            }else{
                System.out.println("用户名已存在");
            }
        }
    }
    
    /**
     * 从文件中读取数据*/
    public static void readFromFile() throws IOException{
        BufferedReader bufReader = new BufferedReader(new FileReader(userFile));
        String content = null;
        while((content=bufReader.readLine())!=null){
            String str[] = content.split(" ");
            set.add(new User(str[0],str[1]));
        }
        bufReader.close();
    }
    
    /**
     * 退出程序*/
    public static void exit() throws IOException {
        BufferedWriter bufWriter = new BufferedWriter(new FileWriter(userFile,true));
        
        for(User user :set){
            String name = user.getName();
            String passwd = user.getPasswd();
            String str = name+" "+passwd;
            bufWriter.write(str);
            bufWriter.newLine();
        }
        bufWriter.close();
        System.exit(0);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,131评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,071评论 19 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,986评论 9 468
  • 你有遇到人生中最灰暗的时光吗?灰暗的时光我相信很多人有遇到过。 记得小的时侯的我是一个非常自卑的人...
    陈志阳阅读 535评论 0 0
  • 1/我不太能想到我今后该干嘛。怎么办爷爷? ~~~ 你好。爷爷看到你的来信了。 不知你的性别,年龄如何,关于你的疑...
    MABEL梅阅读 421评论 0 1

友情链接更多精彩内容