持久化用户操作

本文介绍了一个简单的用户管理系统的设计与实现过程,包括用户注册、查询、修改和删除等功能,并使用Java进行编码实现。系统还具备基本的数据持久化功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用户进行持久化操作的界面及其代码

这里有2个类分别叫Reg 和User

package Timu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Reg {

    static Scanner sc=new Scanner(System.in);
    static ArrayList<User> list=new ArrayList<User>();
    public static void main(String[] args) {

        System.out.println("===========用户操作==============");
        File file=new File("F:\\user.list");
        if(file.exists()){
            try {
                ObjectInputStream ois=new ObjectInputStream(new FileInputStream("F:\\user.list"));
                Object obj=ois.readObject();
                if(obj instanceof ArrayList){
                    list = (ArrayList<User>) obj;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        a:while(true){

            System.out.println("请选择你需要进行的操作1。添加用户  2.查询用户 3.修改用户 4.删除用户 5.退出");
            int i=sc.nextInt();
            switch(i){
            case 1:
                zhuce();
                break;

            case 2:
                chaxun(); 
                break;
            case 3:
                xg();
                break;
            case 4:
                delete();
                break;
            case 5:
                System.out.println("退出当前操作,是否存档:y/n");
                String sf=sc.next();
                switch(sf){
                case "y":
                    try {
                        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("F:\\user.list"));
                        oos.writeObject(list);
                        oos.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break a;
                case "n":
                    break a;

                }
        }


        }
        System.out.println("===========退出界面==============");
    }

    public static User zhuce(){
        User u=new User();
        list.add(u);

        while(true){

            System.out.println("请输入用户名:");
            String userName=sc.next();
            if(userName.matches("^[^0-9][a-zA-Z0-9]{0,5}$")){
                u.setUserName(userName);
                break;
            //  System.out.println("用户名通过");
            }else{
                System.out.println("用户名不符合规则,请重新输入!(小提示:6位英文字母)");
                String userName2=sc.next();
            }


        }

        while(true){
            System.out.println("请输入密码");
            String pwd=sc.next();
            if(pwd.matches("^[a-zA-Z0-9]{6}$")){
                u.setPwd(pwd);
                break;
            //  System.out.println("密码设置成功!");
            }else{
                System.out.println("设置失败,密码必须为6位");
                String pwd2=sc.next();
            }


        }


        while(true){

            System.out.println("请输入你的手机号");
            String phone=sc.next();
            if(phone.matches("^[1]([3]|[5]|[7])([0-9]{9})$")){
                u.setPhone(phone);  
                break;
            }else{
                System.out.println("手机号输入格式有误");
                String phone2=sc.next();

            }
            //System.out.println(phone.matches("^[1]([3]|[5]|[7])([0-9]{9})$"));

        }

        while(true){

            System.out.println("请输入你的邮箱");
            String email=sc.next();
            if(email.matches("^[0-9a-zA-Z_]+[@][a-zA-Z0-9]+[.](([c][o][m])|([c][n]))$")){
                u.setEmail(email);
                break;
            }else{
                System.out.println("邮箱输入有误");
                String email2=sc.next();
            }


        }
        return u;
    }


    public static void chaxun(){
        //System.out.println();

            if(list.size()==0){
                System.out.println("查无此人");
            }else{
                for (User user : list) {
                    System.out.println(user);

                }

            }
    }
    public static void xg(){
        System.out.println("请输入要修改的用户名");
        String name=sc.next();
        User u=null;
        for (User user : list) {
            if(user.getUserName().equals(name)){
                u=user;
            }
        }

        if(u==null){
            System.out.println("没有该用户");
            return;
        }
        int index = list.indexOf(u);

        System.out.println("请输入修改后的姓名:");
        String name2 = sc.next();
        System.out.println("请输入修改后的密码:");
        String pwd = sc.next();

        System.out.println("请输入修改后的手机号:");
        String phone2 = sc.next();
        System.out.println("请输入修改后的邮箱:");
        String email2 = sc.next();


        list.set(index,new User(name2,pwd,phone2,email2) );

        System.out.println("修改成功");

    }

    public static void delete(){
        System.out.println("请输入要删除的用户");
        String d=sc.next();
        User u=null;
        for (User user : list) {
            if(user.getUserName().equals(d)){
                u=user;
            }
        }
        if(u==null){
            System.out.println("查无此人");
            return;
        }
        boolean b = list.remove(u);
        if(b){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }





    }

}

package Timu;

import java.io.Serializable;

public class User implements Serializable{
    private String userName;
    private String pwd;
    private String phone;
    private String email;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public User(String userName, String pwd, String phone, String email) {
        super();
        this.userName = userName;
        this.pwd = pwd;
        this.phone = phone;
        this.email = email;
    }
    public User() {
        super();
    }
    @Override
    public String toString() {
        return "Reg [姓名:" + userName + ", 密码:" + pwd + ", 手机号:" + phone + ", 邮箱:" + email + "]";
    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值