Java学习-简单的用户管理系统

Java学习-简单的用户管理系统

码农世界 2024-05-27 前端 74 次浏览 0个评论
用户管理系统
  • 实现基本的用户类,用于抽象出用户的基本信息(编号,姓名,性别,电话,邮件)–Student
  • 实现用户工具类,用于实现用户的增、删、改、查–StudentUtils
  • 测试类,用于测试使用–StudentTest
    //Student类
    public class Student {
        private int id;
        private String username;
        private String sex;
        private String telephone;
        private String email;
        public Student(int id, String username, String sex, String telephone, String email) {
            this.id = id;
            this.username = username;
            this.sex = sex;
            this.telephone = telephone;
            this.email = email;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public String getTelephone() {
            return telephone;
        }
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        @Override
        public String toString() {
            return id +"\t    " + username + "\t  " + sex +"\t  " + telephone +"\t\t" + email ;
        }
    }
    //StudentUtils类
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    //基于饿汉式单例模式实现类的单例创建
    public class StudentUtils {
        private static StudentUtils studentUtils = new StudentUtils();
        private static List stuList = new ArrayList<>();
        private static int count = 0;
        public static StudentUtils getInstance() {
            return studentUtils;
        }
        private StudentUtils(){
        }
        //显示信息输出
        public void print() {
            System.out.println("-------------用户信息管理系统-------------");
            System.out.println("              1.添加用户");
            System.out.println("              2.修改用户");
            System.out.println("              3.删除用户");
            System.out.println("              4.用户列表");
            System.out.println("              5.退    出");
            System.out.println("              请选择(1-5)");
        }
    	//添加用户信息
        public void addStu() {
            String username = getInput("请输入用户名");
            String sex = getInput("请输入性别");
            String telephone = getInput("请输入电话");
            String email = getInput("请输入邮件");
            Student stu = new Student(++count,username,sex,telephone,email);
            stuList.add(stu);
        }
        //修改用户信息
        public void updateStu() {
            String sid = getInput("请输入要修改用户的编号");
            int stuId = Integer.parseInt(sid) - 1;
            Student stu = stuList.get(stuId);
            String username = getInput("请输入用户名--" + stu.getUsername());
            String sex = getInput("请输入性别--" + stu.getSex());
            String telephone = getInput("请输入电话--" + stu.getTelephone());
            String email = getInput("请输入邮件--" + stu.getEmail());
            stu.setId(stu.getId());
            stu.setUsername(username);
            stu.setSex(sex);
            stu.setEmail(email);
            stuList.set(stuId,stu);
        }
        //删除用户
        public void delStu() {
            String sid = getInput("请输入要删除用户的编号");
            int stuId = Integer.parseInt(sid) - 1;
            if (stuId + 1 > count) {
                System.out.println("不存在的用户");
                return;
            }
            stuList.remove(stuId);
            System.out.println(sid + "号用户删除成功");
            this.printStuList();
        }
        //输出用户列表信息
        public void printStuList() {
            System.out.println("--------------用户列表--------------");
            System.out.printf("编号\t姓名\t性别\t电话\t\t邮件");
            System.out.println();
            if (count == 0) {
                System.out.println("目前还没有用户");
                return;
            }
            for (Student stu : stuList) {
                System.out.printf(stu.toString());
                System.out.println();
            }
        }
        public String getInput(String promptInfo) {
            System.out.println(promptInfo);
            Scanner scanner = new Scanner(System.in);
            String info = scanner.next();
            return info;
        }
    }
    //StudentTest类
    import java.util.Scanner;
    public class StudentTest {
        private static  boolean flag = true;
        public static void main(String[] args) {
            StudentUtils studentUtils = StudentUtils.getInstance();
            while (flag) {
                studentUtils.print();
                Scanner scanner = new Scanner(System.in);
                String inputSelect = scanner.next();
                try {
                    int i = Integer.parseInt(inputSelect);
                    if (i <= 0 || i > 5) {
                        System.out.println("请输入1-5之间的数字");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("请输入1-5之间的数字");
                }
                switch (inputSelect) {
                    case "1":
                        studentUtils.addStu();
                        break;
                    case "2":
                        studentUtils.updateStu();
                        break;
                    case "3":
                        studentUtils.delStu();
                        break;
                    case "4":
                        studentUtils.printStuList();
                        break;
                    case "5":
                        flag = false;
                        break;
                }
            }
        }
    }
    

转载请注明来自码农世界,本文标题:《Java学习-简单的用户管理系统》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,74人围观)参与讨论

还没有评论,来说两句吧...

Top