Lev1

导航

永久储存信息(已完善)

package cn.vip;

import java.io.Serializable;

public class Users implements Serializable{
    private String name;
    private int age;
    private String gender;
    private String code;
    public Users() {
        super();
    }
    public Users(String name, int age, String gender, String code) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    
    
}
用户类
package cn.vip;
import java.io.*;
import java.util.*;
public class BackedGround {
    static Scanner sc = new Scanner(System.in);
    static Map<String, Users>map= new HashMap <String, Users>();
    //初始化信息
    static {
        Users u9 = new Users("nike1",25,"男","123456");
        Users u1 = new Users("zt1", 20, "男", "123456");
        Users u2 = new Users("zt2", 21, "男", "123456");
        Users u3 = new Users("zt3", 22, "男", "123456");
        Users u4 = new Users("zt4", 23, "男", "123456");
        Users u5 = new Users("zt5", 24, "男", "123456");
        Users u6 = new Users("zt6", 25, "男", "123456");
        Users u7 = new Users("zt7", 26, "男", "123456");
        Users u8 = new Users("zt8", 27, "男", "123456");
        map.put(u1.getName(), u1);
        map.put(u2.getName(), u2);
        map.put(u3.getName(), u1);
        map.put(u4.getName(), u4);
        map.put(u5.getName(), u5);
        map.put(u6.getName(), u6);
        map.put(u7.getName(), u7);
        map.put(u8.getName(), u8);
        map.put(u9.getName(), u9);
    }
    //调用保存的方法来把用户信息存到电脑
    public  void save(Users users) throws IOException, IOException {
        map.put(users.getName(), users);
        //序列化
        FileOutputStream fos = new FileOutputStream("F:\\javaclasszjl\\abc");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(map);
        oos.flush();
        oos.close();
        fos.close();
    }
    //用户登录
    public void Denglu() throws IOException, ClassNotFoundException {
        System.out.println("******************************************");
        System.out.println("请输入用户名:");
        String name = sc.next();
        System.out.println("请输入用户密码:");
        String code = sc.next();
        //反序列集合
        FileInputStream fis = new FileInputStream("F:\\javaclasszjl\\abc");
        ObjectInputStream ois = new ObjectInputStream(fis);
        @SuppressWarnings("unchecked")
        Map<String, Users>map = (Map<String,Users>)ois.readObject();
        if(map.get(name)!=null) {
            if(code.equals(map.get(name).getCode())) {
                System.out.println("登录成功,信息如下:");
                System.out.println(String.valueOf(map.get(name)));
            }else {
                System.out.println("密码错误");
            }
        }else {
            System.out.println("账号不存在");
        }
        ois.close();
        fis.close();
    }
    public void zhuce() throws IOException {
        System.out.println("请输入用户名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入性别:");
        String gender = sc.next();
        System.out.println("请输入密码:");
        String code = sc.next();
        Users un = new Users(name,age,gender,code);
        map.put(name, un);
        System.out.println("注册成功");
        save(un);
    }
    
}
后台类
package cn.vip;

import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        BackedGround bg = new BackedGround();
        bg.zhuce();
        bg.Denglu();
    }
}
测试类

上次的问题不能正确显示输入信息 输出ASCII码

在Users分装类加入toString 解决

分装一般这几个要写:有参构造、无参构造、set/get方法、toString重写方法

posted on 2019-08-04 20:48  Lev1  阅读(209)  评论(0编辑  收藏  举报