编程语言
首页 > 编程语言> > JAVA学生管理系统(简单版)

JAVA学生管理系统(简单版)

作者:互联网

本程序实现了学生信息的增删改查等基本功能。其中为了更好的保存信息,本程序以文件的形式来保存信息。
首先,编写了一个Student类来存放学生信息,生成get、set方法以及tostring()方法;

 import java.text.DecimalFormat;
    public class Student {
    //学生信息
    private final String name;    
    private final String num;      
    private double math;   
    private double English;  
    private double web;      
    private double sum;     
    private double average;  
    private final DecimalFormat format;   //数字格式化类
    public Student(String name, String num, double math, double english, double web) {
        this.name = name;
        this.num = num;
        this.math = math;
        this.English = english;
        this.web = web;
        this.sum = (math+english+web);
        format = new DecimalFormat("####.###");  //定义了数字格式,保留三位小数(多余位数直接舍去)
        this.average = Double.parseDouble(format.format(this.sum/3));
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", num='" + num + '\'' +
                ", math=" + math +
                ", English=" + English +
                ", web=" + web +
                ", sum=" + sum +
                ", average=" + average +
                '}';
    }`
    //get方法
    public String getName() {
        return name;
    }
    public String getNum() {
        return num;
    }
    public double getMath() {
        return math;
    }

    public double getEnglish() {
        return English;
    }

    public double getWeb() {
        return web;
    }

    public double getSum() {
        return sum;
    }

    public double getAverage() {
        return average;
    }
    //set方法
    public void setMath(double math) {
        this.math = math;
    }

    public void setEnglish(double english) {
        English = english;
    }

    public void setWeb(double web) {
        this.web = web;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }

    public void setAverage(double average) {
        this.average = Double.parseDouble(format.format(average));
    }
    }

接着为学生信息处理编写一个类,其中利用了设计模式中的单例模式,并且采取LinkedList结构来存放新建的学生信息,方便应对未知的学生数量;
对于文件的读取,在程序运行时读入文件信息,在程序关闭时将链表中的信息存入文件中,这样可以避免重复对文件进行操作;避免了I/O抖动的情况;

import java.io.*;
import java.util.LinkedList;

public class InformationIO  {
    private static LinkedList<Student> message = null;   //存放学生信息
    public InformationIO()  {
        if(message == null) {
            message = new LinkedList<>();
            try {//文件原内容读入
                File file = new File("message.txt");
                if(file.exists()){     //文件存在时读入数据
                    BufferedReader buff = new BufferedReader(new FileReader("message.txt"));//打开文件
                    String row ;
                    while((row = buff.readLine()) != null){ //文件中存有内容时读入
                        String []str = row.split(";");    //可能会出现数组越界(本例将此种情况忽略)
                        message.add(new Student(str[0],str[1],Double.parseDouble(str[2]),Double.parseDouble(str[3]),Double.parseDouble(str[4])));
                    }
                    buff.close(); //缓存流关闭
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //添加学生信息
    public static boolean addMessage(String name, String num, double math, double english, double web){
        for (Student stud:message) {                       //防止学号重复
            if(stud.getNum().equalsIgnoreCase(num)){
                return false;
            }
        }
        message.add(new Student(name,num,math,english,web));//添加学生信息
        return true;
    }
   //查看所有学生成绩
    public static  void printMessage(){
        for (Student temp:message) {   //遍历链表
            System.out.print("姓名:"+temp.getName()+"  学号:"+temp.getNum() +"  高数:"+temp.getMath()
                    +"  大学英语:"+temp.getEnglish()+"  网页设计:"+temp.getWeb() + "   总成绩:" + temp.getSum()
                    +"  平均成绩"+temp.getAverage()+'\n');}
    }
    //返回链表长度,方便日后调用
    public static int returnSize(){
        return message.size();
    }
    //删除学生成绩,此处将学号作为关键字,避免姓名重复
    public static boolean delMessage(String key){
       int i = 0;
       int size = message.size();
        for (;i<size;++i) {
            if(message.get(i).getNum().equalsIgnoreCase(key)) {           //删除指定数据即退出查找
                message.remove(i);
                  break;
            }
        }
        return i != size;
    }
    //修改学生成绩
    public static boolean setMessage(String num,double math,double English,double web){
        for (Student student:message) {
            if(student.getNum().equalsIgnoreCase(num)){   //与已有信息进行比较
                student.setMath(math);
                student.setEnglish(English);
                student.setWeb(web);
                student.setSum(math+English+web);
                student.setAverage(student.getSum()/3);
                return true;
            }
        }
        return false;     //信息不存在
    }
    //将已有学生信息升序排序
    public  static void shortMessage_Min(int choose){
        switch(choose){
            case 1:{
                sort_Max_1(message);
            }break;
            case 2:{
                sort_Max_2(message);
            }break;
            case 3:{
                sort_Max_3(message);
            }break;
            case 4:{
                sort_Max_4(message);
            }break;
            case 5:{
                sort_Max_5(message);
            }break;
            case 6:{
                sort_Max_6(message);
            }break;
        }
    }
    //将已有学生信息降序排序
    public static void shortMessage_Max(int choose){
        switch(choose){
            case 1:{
                sort_Min_1(message);
            }break;
            case 2:{
                sort_Min_2(message);
            }break;
            case 3:{
                sort_Min_3(message);
            }break;
            case 4:{
                sort_Min_4(message);
            }break;
            case 5:{
                sort_Min_5(message);
            }break;
            case 6:{
                sort_Min_6(message);
            }break;
        }
    }
    //输出成绩不合格的学生信息
    public static void printDmessage(String key){
        boolean flag = false;
        switch (key){
            case "math":{
                for (Student stud:message) {
                    if(stud.getMath()<60){
                        System.out.print("姓名:"+stud.getName()+"  学号:"+stud.getNum() +"  高数:"+stud.getMath()
                                +"  大学英语:"+stud.getEnglish()+"  网页设计:"+stud.getWeb() + "   总成绩:" + stud.getSum()
                                +"  平均成绩"+stud.getAverage()+'\n');}
                    flag = true;
                    }
                if(!flag) {
                    System.out.println("该学科无成绩不及格者!");
                }
            }break;
            case "English":{
                for (Student stud:message) {
                    if(stud.getEnglish()<60){
                        System.out.print("姓名:"+stud.getName()+"  学号:"+stud.getNum() +"  高数:"+stud.getMath()
                                +"  大学英语:"+stud.getEnglish()+"  网页设计:"+stud.getWeb() + "   总成绩:" + stud.getSum()
                                +"  平均成绩"+stud.getAverage()+'\n');}
                    flag = true;
                }
                if(!flag) {
                    System.out.println("该学科无成绩不及格者!");
                }
            }break;
            case "web":{
                for (Student stud:message) {
                    if(stud.getWeb()<60){
                        System.out.print("姓名:"+stud.getName()+"  学号:"+stud.getNum() +"  高数:"+stud.getMath()
                                +"  大学英语:"+stud.getEnglish()+"  网页设计:"+stud.getWeb() + "   总成绩:" + stud.getSum()
                                +"  平均成绩"+stud.getAverage()+'\n');}
                    flag = true;
                }
                if(!flag) {
                    System.out.println("该学科无成绩不及格者!");
                }
            }break;
            case "sum":{
                for (Student stud:message) {
                    if(stud.getSum()<60){
                        System.out.print("姓名:"+stud.getName()+"  学号:"+stud.getNum() +"  高数:"+stud.getMath()
                                +"  大学英语:"+stud.getEnglish()+"  网页设计:"+stud.getWeb() + "   总成绩:" + stud.getSum()
                                +"  平均成绩"+stud.getAverage()+'\n');}
                    flag = true;
                }
                if(!flag) {
                    System.out.println("该学科无成绩不及格者!");
                }
            }break;
            case "average":{
                for (Student stud:message) {
                    if(stud.getAverage()<60){
                        System.out.print("姓名:"+stud.getName()+"  学号:"+stud.getNum() +"  高数:"+stud.getMath()
                                +"  大学英语:"+stud.getEnglish()+"  网页设计:"+stud.getWeb() + "   总成绩:" + stud.getSum()
                                +"  平均成绩"+stud.getAverage()+'\n');}
                    flag = true;
                }
                if(!flag) {
                    System.out.println("该学科无成绩不及格者!");
                }
            }break;
            default:
                System.out.println("输入信息无效!");
        }
    }
    //文件内容写入
    public static void writeFile(){
        File file = new File("message.txt");
        String row;
        try{
          if(!file.exists()){  //文件不存在时创建文件
             file.createNewFile();
          }
          BufferedWriter write = new BufferedWriter(new FileWriter("message.txt"));
            for (Student std:message) {
                row =std.getName()+";"+std.getNum()+";"+std.getMath()+";"+std.getEnglish()+";"
                        +std.getWeb()+";"+std.getSum()+";"+std.getAverage();//将链表内容进行拼接存到文件中,方便保存;分号的作用是为了读入时方便拆分数据;
                write.write(row);
                write.newLine();
            }
            write.close();    //关闭缓存流
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /*自定义排序函数(选择排序)
    以下结构都采取选择排序的方法,用来实现不同关键字的排序(略显啰嗦)*/
    private static void sort_Max_1(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(Double.parseDouble(info.get(j).getNum()) >Double.parseDouble(stud_max.getNum())){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);
        }
    }
    private static void sort_Max_2(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getMath() > stud_max.getMath()){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);

        }
    }
    private static void sort_Max_3(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getEnglish() > stud_max.getEnglish()){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);

        }
    }
    private static void sort_Max_4(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getWeb() > stud_max.getWeb()){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);

        }
    }
    private static void sort_Max_5(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getSum() > stud_max.getSum()){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);

        }
    }
    private static void sort_Max_6(LinkedList<Student> info){
        Student stud_max;   //中间变量
        Student temp;
        int index_max;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_max = info.get(0);
            index_max = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getAverage()> stud_max.getAverage()){
                    stud_max = info.get(j);
                    index_max = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_max);
            info.set(index_max,temp);

        }
    }

    private static void sort_Min_1(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(Integer.parseInt(info.get(j).getNum()) <Integer.parseInt(stud_min.getNum())){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);
        }
    }
    private static void sort_Min_2(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getMath()< stud_min.getMath()){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);

        }
    }
    private static void sort_Min_3(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getEnglish()< stud_min.getEnglish()){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);

        }
    }
    private static void sort_Min_4(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getWeb()< stud_min.getWeb()){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);

        }
    }
    private static void sort_Min_5(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getSum()< stud_min.getSum()){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);

        }
    }
    private static void sort_Min_6(LinkedList<Student> info){
        Student stud_min;   //中间变量
        Student temp;
        int index_min;
        int size = info.size();
        for (int i = size-1; i >0; i--){
            stud_min = info.get(0);
            index_min = 0;
            for (int j = 1; j <= i; j++) {
                if(info.get(j).getAverage()< stud_min.getAverage()){
                    stud_min = info.get(j);
                    index_min = j;
                }
            }
            temp = info.get(i);
            info.set(i,stud_min);
            info.set(index_min,temp);

        }
    }
}

最后为创建了一个简单的工具类,将系统所需要的简单界面以及事件简单的包装了一下;

import java.util.Scanner;

public final class Extrance {
    private Extrance(){}
    public static void UI(){
        System.out.println("*********************学生成绩管理系统**************************");
        System.out.println("***  1.录入学生成绩      2.全体学生成绩       3.删除学生成绩    ***");
        System.out.println("***  4.修改学生成绩  5. 查看不及格学生信息 6.将学生成绩增序排序   ***");
        System.out.println("***  7.将学生成绩降序排序                     0.退出程序      ***");
        System.out.println("************************************************************");
    }
    public static void UI_back(){
        System.out.println("************************************************************");
        System.out.println("***                   感谢使用本系统!                      ***");
        System.out.println("************************************************************");
    }
    public static void start(int key) {
        new InformationIO();
        Scanner in = new Scanner(System.in);
        switch (key) {
            case 0:{//退出系统并将信息写入文件中
                InformationIO.writeFile();
                Extrance.UI_back();
                System.exit(0);
            }break;
            case 1: {
                while(true) {
                    System.out.println("请依次输入姓名,学号,高数,大学英语,网页设计成绩并按回车确认: ");
                    if(InformationIO.addMessage(in.next(),in.next(),in.nextInt(),in.nextInt(),in.nextInt())){
                        System.out.println("保存成功!");
                    }else{
                        System.out.println("学号已经存在!");
                    }
                    System.out.println("是否继续添加<Y/N>");
                    if(in.next().equalsIgnoreCase("N")){
                        System.out.println("录入结束");
                        break;
                    }
                }
            }break;
            case 2: {//查询全体学生成绩
                if(InformationIO.returnSize() != 0){
                    InformationIO.printMessage();
                }else{
                    System.out.println("当前成绩为空!");
                }
            }break;
            case 3: {//删除学生成绩
                System.out.print("请输入删除学生的学号:");
                if(InformationIO.delMessage(in.nextLine())){
                    System.out.println("删除成功!");
                }else{
                    System.out.println("学号不存在或输入错误!");
                }
            }break;
            case 4:{//修改学生成绩
                System.out.println("请输入要修改学生的学号以及成绩(高数、大学英语、web设计)用分号隔开");
                String str[] = in.nextLine().split(";");
               if(InformationIO.setMessage(str[0],Double.parseDouble(str[1]),Double.parseDouble(str[2]),Double.parseDouble(str[3]))){
                   System.out.println("修改成功!");
               }else{
                   System.out.println("学号不存在");
               }
            }break;
            case 5:{//查看不及格学生信息
                System.out.print("请输入要查询的学科(math、English、web、sum、average): ");
                InformationIO.printDmessage(in.nextLine());
            }break;
            case 6:{//将学生信息增序排序
                System.out.print("请输入关键字(1.num、2.math、3.English、4.web、5.sum、6.average): ");
                InformationIO.shortMessage_Min(in.nextInt());
            }break;
            case 7:{//将学生信息降序排序
                System.out.print("请输入关键字(1.num、2.math、3.English、4.web、5.sum、6.average): ");
                InformationIO.shortMessage_Max(in.nextInt());
            }break;
            default:
                System.out.println("选项错误,请输入正确的选项!");
        }
    }
}

最后,将代码整合在入口函数(main)中:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Extrance.UI();   //操作菜单
        Scanner in = new Scanner(System.in);   //读入数据
        while (true) {
            Extrance.start(in.nextInt());   ///选项处理函数
        }
    }
}

此为作者学习记录,其中难免有不足之处,请多多指教!

标签:info,JAVA,管理系统,int,max,min,学生,stud,get
来源: https://blog.csdn.net/Travrller/article/details/112065228