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 set = new HashSet ();
    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);
    }
}

你可能感兴趣的:(java BufferedReader/Writer作业)