基于Java的学生信息管理系统

该系统包括管理员和学生两类用户,管理员需登录后可管理学生信息,如查询、添加、删除和修改。系统使用ArrayList存储和操作数据,包含Student和Admin实体类,以及模拟数据的Datas类,AdminDao和StudentDao分别处理管理员和学生数据的CRUD操作。
摘要由CSDN通过智能技术生成

基于Java的学生信息管理系统

需求
1.管理员 学生
2.管理员 —— 账户 密码
3.学生 —— 学号 姓名 成绩 班级
4.管理员需要登录才能管理学生的信息
5.管理学生的信息——查询 添加 删除 修改

用ArrayList来对数据进行操作

Student类

package sm.entity;

import java.util.Objects;

public class Student {
    private String studentId; //学号
    private String name;      //姓名
    private int age;          //年龄
    private String classType;    //班级

    public Student(){
        super();
    }

    public Student(String studentId, String name, int age, String classType) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.classType = classType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(studentId, student.studentId);
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    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 getClassType() {
        return classType;
    }

    public void setClassType(String classType) {
        this.classType = classType;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentId='" + studentId + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", classType='" + classType + '\'' +
                '}';
    }
}

Admin类

package sm.entity;

import java.util.Objects;

public class Admin {
    private String account; //账户
    private String password; //密码

    public Admin() {
        super();
    }

    public Admin(String account, String password) {
        this.account = account;
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Admin admin = (Admin) o;
        return Objects.equals(account, admin.account) &&
                Objects.equals(password, admin.password);
    }

}

Datas类(因为没有连接数据库,用datas来存一些初始数据)

package sm.data;

import sm.entity.Admin;
import sm.entity.Student;

import java.util.ArrayList;

//模拟数据库 预先存储一些数据
public class Datas {
    //管理员数据容器
    public static ArrayList<Admin> admins = new ArrayList<Admin>();
    //学生数据容器
    public static ArrayList<Student> students = new ArrayList<Student>();

    //进行一些初始化
    static {
        admins.add(new Admin("123","123"));
        students.add(new Student("20202B5058","张三",20,"软件工程2001"));
        students.add(new Student("20202B5059","张四",21,"计算机科学与技术2001"));
        students.add(new Student("20202B5060","张五",22,"电子信息工程2001"));
        students.add(new Student("20202B5061","张六",23,"通信工程2001"));
    }
}

AdminDao(管理员的一些操作)

package sm.dao;

import sm.data.Datas;
import sm.entity.Admin;

import java.util.ArrayList;

public class AdminDao {
    private static ArrayList<Admin> admins = Datas.admins;

    public boolean query(String account,String password){
        return query(new Admin(account,password));
    }

    private boolean query(Admin loginAdmin) {
        if (admins.isEmpty()){
            return false;
        }
        for (Admin admin:admins
             ) {
            if(admin.equals(loginAdmin)) {
                return true;
            }
        }
        return false;
    }
}

StudentDao类

package sm.dao;

import sm.data.Datas;
import sm.entity.Student;

import java.util.ArrayList;
import java.util.Comparator;

//对学生数据进行的增删改查
public class StudentDao {
    private static ArrayList<Student> students = Datas.students;

    //查询并按学号打印所有学生信息
    public void queryAll(){
        if(students.isEmpty()){
            System.out.println("暂无学生信息");
        }else{
            students.sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getStudentId().compareTo(o2.getStudentId());
                }
            });
            for (Student student:students
                 ) {
                System.out.println(student.toString());
            }
        }
    }

    //按照学号查找学生
    public boolean queryByStudentID(String studentId){
        if(students.isEmpty()){
            return false;
        }
        for (Student student:students
             ) {
            if(student.getStudentId().equals(studentId)){
            return true;
            }
        }
        return false;
    }

    //添加一个学生
    public boolean addStudent(Student student){
        return  students.add(student); //不带角标添加元素,默认添加尾部
    }

    //删除一个学生
    public boolean deleteStudent(String StudentID){
        for (int i = 0; i < students.size();i++){
            if (students.get(i).getStudentId().equals(students));
            students.remove(i);
            return true;  //说明删掉了
        }
        return false;     //说明没找到
    }

    //根据id修改学生信息
    public boolean changeByStudentID(String oriStudentID,Student newStudent){ //传入原始id 以及 学生信息
        int index = -1;
        for (int i = 0;i < students.size();i++){
            if (students.get(i).getStudentId().equals(oriStudentID)){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }
        students.get(index).setStudentId(newStudent.getStudentId());
        students.get(index).setName(newStudent.getName());
        students.get(index).setAge(newStudent.getAge());
        students.get(index).setClassType(newStudent.getClassType());
        return true;
    }

}

主类

package sm.main;

import sm.dao.AdminDao;
import sm.dao.StudentDao;
import sm.entity.Student;

import java.util.Scanner;

/*需求
 1.管理员 学生
 2.管理员 —— 账户 密码
 3.学生 —— 学号 姓名 成绩 班级
 4.管理员需要登录才能管理学生的信息
 5.管理学生的信息
   查询
   添加
   删除
   修改*/
public class StudentManagement {
    private static StudentDao studentDao = new StudentDao();
    private static AdminDao adminDao = new AdminDao();
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入管理员账户:");
        String account = scanner.nextLine();
        System.out.print("请输入管理员密码:");
        String password = scanner.nextLine();

        if(adminDao.query(account,password)){
            System.out.println("登录成功");
            showMenu();
        }else{
            System.out.println("密码或账户名错误,登录失败");
        }
    }
    private static void showMenu(){
        while (true){
            System.out.println("======学生信息管理系统======");
            System.out.println("\t1.查询所有学生信息");
            System.out.println("\t2.根据学号查询学生");
            System.out.println("\t3.添加学生信息");
            System.out.println("\t4.删除学生信息");
            System.out.println("\t5.修改学生信息");
            System.out.println("\t6.退出系统");
            System.out.print("请输入你的选择:");
            int choice = Integer.parseInt(scanner.nextLine());
            if(choice == 1){
                studentDao.queryAll();
            }else if(choice == 2){
                System.out.println("请输入学生学号:");
                String id = scanner.nextLine();
                if(studentDao.queryByStudentID(id)){
                    System.out.println("该学生存在!");
                }else{
                    System.out.println("该学生不存在!");
                }
            }else if(choice == 3){
                System.out.println("请输入学生的学号:");
                String studentID = scanner.nextLine();

                if(studentDao.queryByStudentID(studentID)){
                    System.out.println("该学生学号已经存在,无法添加");
                    continue;
                }
                System.out.println("请输入学生的姓名:");
                String name = scanner.nextLine();

                System.out.println("请输入学生的年龄:");
                int age = Integer.parseInt(scanner.nextLine());

                System.out.println("请输入学生的班级:");
                String classType = scanner.nextLine();

                if(studentDao.addStudent(new Student(studentID,name,age,classType))){
                    System.out.println("添加成功!");
                }
            }else if(choice == 4){
                System.out.println("请输入要删除学生的学号:");
                String studentID = scanner.nextLine();

                if(studentDao.deleteStudent(studentID)){
                    System.out.println("删除成功");
                }else{
                    System.out.println("删除失败");
                }
            }else if(choice == 5){
                System.out.println("请输入要修改的学生学号");
                String studentID = scanner.nextLine();

                if(studentDao.queryByStudentID(studentID)){
                    System.out.println("请输入修改后学生的学号:");
                    String newID = scanner.nextLine();

                    System.out.println("请输入修改后学生的姓名:");
                    String name = scanner.nextLine();

                    System.out.println("请输入修改后学生的年龄:");
                    int age = Integer.parseInt(scanner.nextLine());

                    System.out.println("请输入修改后学生的班级:");
                    String classType = scanner.nextLine();

                    studentDao.changeByStudentID(studentID,new Student(newID,name,age,classType));
                    System.out.println("学生信息修改成功!");
                }else{
                    System.out.println("学生不存在,无法修改!");
                }
            }else{
                return;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值