JAVA实现——学生管理系统

实现一个学生信息管理系统:
1.学生的信息至少包括:身份证号、学号、姓名、成绩
2.学生的身份证号、学号、姓名可在对象诞生时初始化,也可以在对象诞生后赋值;
3.包含如下功能:
(1)学生信息存储:从控制台输入学生的信息并存储,按学号从小到大排序
(2)查询学生信息:根据输入的身份证号或学号查询到对应的学生信息并输出
(3)排序:可根据学生的学号从小到大排序,也可以根据学生的成绩从高到低排序
(4)插入:插入一个学生信息(从控制台输入),插入后按学号从小到大排序
(5)删除:根据身份证号或学号删除对应的学生信息

不多说,代码如下:

import com.sun.org.apache.regexp.internal.RE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 实现一个学生信息管理系统
 * User: Mr.Tang
 * Date: 2022-11-09
 * Time: 10:21
 */

/**
 * 运用 ArrayLiat 类,可调整大小的数组的实现List接口。以及一些类里面的方法
 * ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。ArrayList 继承了 AbstractList ,
 * 并实现了 List 接口。
 *
 * boolean add(E element)
 * 将指定的元素追加到此列表的末尾。
 * void add(int index, E element)
 * 在此列表中的指定位置插入指定的元素
 * void clear()
 * 从列表中删除所有元素
 * boolean contains(Object o)
 * 如果此列表包含指定的元素,则返回 true
 * E get(int index)
 * 返回此列表中指定位置的元素。
 * public int size()
 * 返回集合中的元素个数
 * public boolean remove(Object o)
 * 删除指定的元素,返回删除是否成功
 * public E remove(int index)
 * 删除指定索引处的元素,返回被删除的元素
 * public E set(int index,E element)
 * 修改指定索引处的元素,返回被修改的元素
 *
 */

class Student{
    private String Identity;
    //身份证号
    private String ID;
    //学号
    private String name;
    //姓名
    private Double point;
    //分数


    public String getIdentity() {
        return Identity;
    }

    public void setIdentity(String Identity) {
        this.Identity = Identity;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPoint() {
        return point;
    }

    public void setPoint(Double point) {
        this.point = point;
    }

    public Student(){

    }

    public Student(String Identity, String ID, String name, Double point) {
        this.Identity = Identity;
        this.ID = ID;
        this.name = name;
        this.point = point;
    }

    @Override
    public String toString() {
        return "Student{" +
                Identity + ',' +
                ID + ',' +
                name + ',' +
                point +
                '}';
    }
}
public class StudentInformationSystem {

    public static void menu(){
        System.out.println("============================");
        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("输入6:显示学生信息");
        System.out.println("输入7:清空学生信息");
        System.out.println("输入0:退出管理系统");
        System.out.println("============================");
        System.out.println("请输入:");
    }

    public static boolean isUesd(ArrayList<Student> array,String id){
        //遍历进行比较,有相同返回true,否则返回fales
        boolean flg = false;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getID().equals(id)){
                flg = true;
                break;
            }
        }
        return flg;
    }
    public static void addStudent(ArrayList<Student> array){
        //存储(添加)学生信息

        Scanner sc = new Scanner(System.in);
        //后面要通过学号进行信息查找,所以要求学号不能重复
        String id ;
        while (true) {
            System.out.println("请输入学号:");
            id = sc.nextLine();
            //定义一个判断方法
            boolean flg = isUesd(array, id);
            if (flg) {
                System.out.println("学号已被使用,请重新输入!");
            } else {
                break;      //学号有效,跳出循环
            }
        }
        System.out.println("请输入身份证号:");
        String identity = sc.nextLine();
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生分数:");
        Double p = sc.nextDouble();

        //赋值给到学生
        Student stu = new Student();
        stu.setIdentity(identity);
        stu.setID(id);
        stu.setName(name);
        stu.setPoint(p);

        //将学生对象给到集合中
        array.add(stu);
        System.out.print("添加成功!");
    }
    public static void findStudent(ArrayList<Student> array){
        //根据学号查找学生
        Scanner sc = new Scanner(System.in);
        System.out.println("输入查找学生的学号:");
        String id = sc.nextLine();

        //判断学号是否存在并查找输出
        int flg = -1;   //定义一个判断标志,因为数组下标不会为-1.所以将flg初始化为-1
        Student s = new Student();
        for (int i = 0; i < array.size(); i++) {
            s = array.get(i);
            if(s.getID().equals(id)){
                flg = i;
                break;
            }
        }
        if (flg == -1){
            System.out.println("学号不存在!");
        }else{
            System.out.println("找到该生!");
            System.out.println("身份证号\t\t学号\t\t姓名\t\t分数");
            System.out.println(s.getIdentity()+"\t\t\t"+s.getIdentity()+"\t\t"+s.getName()+"\t\t"+s.getPoint());
        }


    }
    public static void sortStudent(ArrayList<Student> array){
        //根据成绩进行排序(从高到低)
        
        array.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (int)(o2.getPoint() - o1.getPoint());
            }
        });
        System.out.println("排序成功!");
    }
    public static void insertStudent(ArrayList<Student> array){
        //插入学生信息
        Scanner sc = new Scanner(System.in);

        System.out.println("输入要插入的位置:");
        int a = sc.nextInt();

        //后面要通过学号进行信息查找,所以要求学号不能重复
        String id ;
        while (true) {
            System.out.println("请输入学号:");
            id = sc.nextLine();
            //定义一个判断方法
            boolean flg = isUesd(array, id);
            if (flg) {
                System.out.println("学号已被使用,请重新输入!");
            } else {
                break;      //学号有效,跳出循环
            }
        }
        System.out.println("请输入身份证号:");
        String identity = sc.nextLine();
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生分数:");
        Double p = sc.nextDouble();

        //赋值给到学生
        Student stu = new Student();
        stu.setIdentity(identity);
        stu.setID(id);
        stu.setName(name);
        stu.setPoint(p);

        //将学生对象给到集合中
        array.add(a,stu);
        System.out.print("成功插入学生信息!");

    }
    public static void  dleteStudent(ArrayList<Student> array){
        //删除指定学生信息
        Scanner sc = new Scanner(System.in);

        System.out.println("输入要删除的学生姓名:");
        String name = sc.nextLine();
        for (int i = 0; i < array.size(); i++) {
            if(array.get(i).getName().equals(name)){
                array.remove(i);
                break;
            }
        }
        System.out.println("成功删除"+name+"的信息!");
    }
    public static void showStudent(ArrayList<Student> array){
        //展示所有学生信息
        System.out.println("身份证号\t\t学号\t\t姓名\t\t分数");
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getIdentity()+"\t\t\t"+s.getIdentity()+"\t\t"+s.getName()+"\t\t"+s.getPoint());

        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> array = new ArrayList<Student>();
        menu();
        while (sc.hasNextInt()){
            int input = sc.nextInt();
            if (input == 0){
                break;
            }
            switch (input){
                case 1:
                    //存储学生信息
                    addStudent(array);
                    break;
                case 2:
                    //查询学生信息
                    findStudent(array);
                    break;
                case 3:
                    //排序
                    sortStudent(array);
                    break;
                case 4:
                    //插入
                    insertStudent(array);
                    break;
                case 5:
                    //删除
                    dleteStudent(array);
                    break;
                case 6:
                    //显示学生信息
                    showStudent(array);
                    break;
                case 7:
                    //清空学生信息
                    array.clear();
                    System.out.println("成功清空所有信息!");
                    break;
                default:
                    System.out.println("输入无效!");
                    break;
            }
            System.out.println();
            System.out.println();
            System.out.println("array:"+array);
            menu();
        }
        System.out.println("成功退出学生管理系统!");
    }
}

  • 0
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值