Java作业——学籍管理系统

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/KevinWu93/article/details/50043549

JAVA课的期中作业,也花了点时间写了,既然写了就贴上来吧,说不定哪天回头看看会觉得现在写的代码是多么渣。

原题如下:

实验1 学籍管理系统

【实验类型】:综合型
【实验目的】:
1) 熟悉和掌握字符串、数组等两种高级数据类型;
2) 进一步应用Scanner类的对象,读取不同类型的数据;
3) 进一步训练自顶向下逐步求精策略,并应用于实际问题;
4) 在函数的参数传递中,传递String对象和数组对象。
【实验内容】:
设计一个简单的学生学籍管理程序,有如下功能:
1) 能输入10个学生信息,学生信息中包含学号,姓名,年龄,语文,数学,英语等内容;
2) 有统计功能,如统计个人的总分、平均分,以及整体的单科平均分、总分平均;
3) 按语文、数学、英语、年龄、学号等中的任一种对学生进行排序;
4) 输出所有人的具体信息;
5) 输入指定姓名或学号,输出符合条件的学生信息;
6) 以循环菜单的方式列出上述功能供用户选择;输入0则退出系统。
7) 注意:排序、统计、检索、打印等,必须用子程序单独实现,并以学生信息数组为参数。
提高1:数学、语文、英语均为成绩,考虑重用同一程序实施排序;
提高2:增强系统的健壮性,如判别分数、年龄是否合法,输入的学生信息数量等;
提高3:考虑系统的可交互性,即对要求用户输入信息格式必须给出友好的提示;在完成相关功能后给出结果信息;
提高4:排序后学生信息的增删改。

【实验提示】:
本实验可采用的基本框架结构如下:
class Student{
属性定义:如ID、name、……
构造函数、
属性的set、get方法:如setID()、getID()、……
}
}
class StudentGroup{
Student[] st=new Student[40]; //40人一个班
班级属性:如人数len、班级名称、……
【注】加入班级属性,可增强可扩充性,如以后的多班级处理
针对班级的功能操作:
如输入班级基本信息、排序、检索、修改个人信息等、……
}
}
class 系统应用类{
main(){
*系统功能菜单**
1. ……
……
0. 退出系统


请选择:
case 1:
功能1……

}

下面一一贴出代码,因为感觉必要的地方都加了注释,所以就不详细说明了。

import java.util.Scanner;

/**
 * 主菜单
 * 
 * @author KevinWu
 * 
 */
public class Main {
    static Config cf;
    static Student sInfo[];
    private static int ids;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Func f = new Func();
        Main m = new Main();
        cf = new Config();// 实例化对象同时导入保存的参数
        ids=Config.idp;
        sInfo = f.inputFromFile();// 导入之前的数据
        m.menuMain();

        // Student sInfo[]=f.setStudentInfo();

        // f.printStudentInfo(sInfo,"xh","jx");
        // f.printStudentInfo(sInfo,"xh","sx");
        // f.printStudentInfo(sInfo);
        // f.printStudentInfo(sInfo,"xh");
        // f.printStudentInfo(sInfo,"nl");
    }

    /*
     * 主菜单
     */
    private void menuMain() {
        boolean canInput = true;
        Scanner sc = new Scanner(System.in);
        while (canInput == true) {
            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("--------------------------");
            switch (sc.next()) {
            case "1":
                menu1();
                break;
            case "2":
                menu2();
                break;
            case "3":
                menu3();
                break;
            case "4":
                menu4();
                break;
            case "5":
                menu5();
                break;
            case "6":
                menu6();
                break;
            default:
                System.out.println("请输入正确的数字!");
                break;
            }
        }
        sc.close();
    }

    /*
     * 统计学生信息
     */
    private void menu5() {
        // TODO Auto-generated method stub
        Func f = new Func();
        System.out.println("请选择统计方式:");
        System.out.println("1、统计各科成绩总平均分");
        System.out.println("2、统计各科成绩总分");
        System.out.println("如果不进行以上操作请输入任意指令之外的数字退出本功能");
        Scanner sc = new Scanner(System.in);
        switch (sc.next()) {
        case "1":
            f.getAverage(sInfo);
            break;
        case "2":
            f.getCourseSum(sInfo);
            break;
        default:
            return;
        }
    }

    /*
     * 设置系统参数菜单
     */
    private void menu6() {
        // TODO Auto-generated method stub
        Config cf = new Config();
        Func f = new Func();
        Scanner sc = new Scanner(System.in);
        System.out.println("当前系统参数:");
        System.out.println("学号起始值:" + ids);
        System.out.print("当前课程:");
        for (String c : Config.courses)
            System.out.print("\t" + c);
        System.out.println();
        System.out.println("1、修改学号起始值");
        System.out.println("2、修改课程信息");
        System.out.println("不做修改请输入任意值返回主菜单");
        switch (sc.next()) {
        case "1":
            cf.setIdp();
            cf.outputConfig();
            ids=Config.idp;
            sInfo = f.clear(sInfo);
            break;
        case "2":
            cf.setCourses();
            cf.outputConfig();
            sInfo = f.clear(sInfo);
            break;
        default:
            return;
        }
    }

    /*
     * 删除学生信息菜单
     */
    private void menu4() {
        // TODO Auto-generated method stub
        Func f = new Func();
        System.out.println("现有学生如下:");
        sInfo = f.serialPrint(sInfo);
        System.out.println("请输入要删除的学生对应的编号:");
        Scanner sc = new Scanner(System.in);
        sInfo = f.deleteSelectStudent(sInfo, sc.nextInt() - 1);
    }

    /*
     * 修改学生信息菜单 注:未做非法输入判断
     */
    private void menu3() {
        // TODO Auto-generated method stub
        Func f = new Func();
        System.out.println("现有学生如下:");
        sInfo = f.serialPrint(sInfo);
        System.out.println("请输入要修改的学生对应的编号(只允许修改课程成绩):");
        Scanner sc = new Scanner(System.in);
        int index = sc.nextInt();
        System.out.println("请输入要修改的课程成绩对应编号:");
        int j = 1;
        for (String c : Config.courses)
            System.out.print((j++) + "、" + c + "  ");
        System.out.println();
        int courseIndex = sc.nextInt();
        System.out.println("请输入【" + Config.courses[courseIndex - 1]
                + "】修改后的成绩:");
        float courseScore = sc.nextFloat();
        f.editSelectStudent(sInfo, index - 1, courseIndex - 1, courseScore);
    }

    /*
     * 查询学生信息菜单
     */
    private void menu2() {
        // TODO Auto-generated method stub
        Func f = new Func();
        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("8、按姓名查询学生信息(支持关键字查找)");
        System.out.println("如果不进行以上操作请输入任意指令之外的数字退出本功能");
        Scanner sc = new Scanner(System.in);
        switch (sc.next()) {
        case "1":
            f.printStudentInfo(sInfo, "xh");
            break;
        case "2":
            f.printStudentInfo(sInfo, "xh", "jx");
            break;
        case "3":
            f.printStudentInfo(sInfo, "nl");
            break;
        case "4":
            f.printStudentInfo(sInfo, "nl", "jx");
            break;
        case "5":
            System.out.println("请指定一门课程进行排序:");
            int i = 1;
            for (String c : Config.courses)
                System.out.print((i++) + "、" + c + "  ");
            System.out.println();
            f.printStudentInfo(sInfo, "cj", "sx",
                    Config.courses[sc.nextInt() - 1]);
            break;
        case "6":
            System.out.println("请指定一门课程进行排序:");
            int j = 1;
            for (String c : Config.courses)
                System.out.print((j++) + "、" + c + "  ");
            System.out.println();
            f.printStudentInfo(sInfo, "cj", "jx",
                    Config.courses[sc.nextInt() - 1]);
            break;
        case "7":
            System.out.println("请输入要查询的学号:");
            f.searchById(sInfo, sc.nextInt());
            break;
        case "8":
            System.out.println("请输入要查询的姓名:");
            f.searchByName(sInfo, sc.next());
            break;
        default:
            return;
        }
    }

    /*
     * 录入学生信息菜单
     */
    private void menu1() {
        // TODO Auto-generated method stub
        Func f = new Func();
        sInfo = f.addInputFromKeyboard(sInfo);
    }
}

/**
 * 学生类
 * 
 * @author KevinWu
 *
 */
public class Student {
    private int id;//学号
    private String name;//姓名
    private int age;//年龄
    private Course courseAndScore[];

    public Student(){
        this.id=Config.idp;
        courseAndScore=new Course[Config.courses.length];
    }


    public Course[] getCourseAndScore() {
        return courseAndScore;
    }


    public void setCourseAndScore(Course[] courseAndScore) {
        this.courseAndScore = courseAndScore;
    }


    private void initCourseAndScore() {
        // TODO Auto-generated method stub
        courseAndScore=new Course[Config.courses.length];
        for(int i=0;i<Config.courses.length;i++){
            courseAndScore[i]=new Course(Config.courses[i], 0);
        }   
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 toString() {
        String coursesS="";//课程成绩字符串
        for(int i=0;i<Config.courses.length;i++){
            //coursesS=courseAndScore[i].toString();
            coursesS=coursesS+"\t "+courseAndScore[i].getCourseName()+":"
                     +courseAndScore[i].getCourseScore();
        }
        return "学号:" + id + "\t姓名:" + name + "\t年龄:" + age
                + " \t课程成绩:"+coursesS;
    }
    public boolean equals(Student obj){
        if(obj.id==this.id&&obj.name.equals(this.name)){
            return true;
        }
        return false;

    }
}
/**
 * 课程类
 * 
 * @author KevinWu
 * 
 */
public class Course {
    private String courseName;// 课程名称
    private float courseScore;// 课程成绩

    public Course(String courseName, float courseScore) {
        this.courseName = courseName;
        this.courseScore = courseScore;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public float getCourseScore() {
        return courseScore;
    }

    public void setCourseScore(float courseScore) {
        this.courseScore = courseScore;
    }

    public String toString() {
        return "Course [courseName=" + courseName + ", courseScore="
                + courseScore + "]";
    }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 方法类
 * 
 * @author KevinWu
 * 
 */
public class Func {
    private static final String FILE_NAME = "info.txt";// 备份信息的文件名称

    public Func() {

    }

    /*
     * 当系统参数改变后,需要把所有的学生数据都清空,避免数据混淆
     */
    public Student[] clear(Student[] sInfo) {
        File file = new File(FILE_NAME);
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        return null;
    }

    /*
     * 设置学生信息
     */
    public Student[] setStudentInfo() {
        Student[] sInfo = null;
        File file = new File(FILE_NAME);
        if (file.isFile() && file.exists()) {
            // Scanner sc = new Scanner(System.in);
            // System.out.println("检测到已存在之前备份的文件,是否从文件中输入?Y/N");
            // String select = sc.next();
            // if (select.equals("Y") || select.equals("y")) {
            sInfo = inputFromFile();
            // } else {
            // sInfo = inputFromKeyboard();
            // }
            // sc.close();
        } else {

            // sInfo = inputFromKeyboard();
        }
        return sInfo;
    }

    /*
     * 输出文件备份
     */
    private void oupPuttoFile(Student[] sInfo) {
        // TODO Auto-generated method stub
        FileOutputStream fs = null;
        try {
            fs = new FileOutputStream(new File(FILE_NAME));
            PrintStream p = new PrintStream(fs);
            for (int i = 0; i < sInfo.length; i++) {
                p.println(sInfo[i].getId());
                p.println(sInfo[i].getName());
                p.println(sInfo[i].getAge());
                for (int j = 0; j < sInfo[i].getCourseAndScore().length; j++) {
                    p.println(sInfo[i].getCourseAndScore()[j].getCourseScore());
                }
            }
            System.out.println("已成功备份学生信息文件");
            p.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("文件操作异常,未备份数据!");
            e.printStackTrace();
        }
    }

    /*
     * 从键盘输入
     */
    public Student[] addInputFromKeyboard(Student[] sInfos) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加的学生数量:");
        int count = sc.nextInt();
        Student sInfo[];
        // 应对第一次添加时,文件为空情况
        if (sInfos != null)
            sInfo = new Student[count + sInfos.length];
        else
            sInfo = new Student[count];
        for (int i = 0; i < count; i++) {
            sInfo[i] = new Student();
            System.out.println("请输入学号为 " + (Config.idp++ ) + " 的学生姓名:");
            sInfo[i].setName(sc.next());
            System.out.println("请输入 " + sInfo[i].getName() + " 同学的年龄:");
            boolean tage = false;// 判断年龄是否规范标志
            while (tage == false) {
                int age = sc.nextInt();
                if (age > 0) {
                    sInfo[i].setAge(age);
                    tage = true;
                } else {
                    System.out.println("请输入正确的年龄!!!");
                    System.out.println("请重新输入 " + sInfo[i].getName()
                            + " 同学的年龄:");
                }
            }
            Course c[] = new Course[Config.courses.length];
            for (int j = 0; j < Config.courses.length; j++) {
                System.out.println("请输入 " + sInfo[i].getName() + " 同学的 "
                        + Config.courses[j] + " 成绩");
                boolean tscore = false;// 判断成绩是否规范标志

                while (tscore == false) {
                    float score = sc.nextFloat();
                    if (score > 0 && score <= 100) {
                        c[j] = new Course(Config.courses[j], score);
                        tscore = true;
                    } else {
                        System.out.println("请输入正确的成绩!!!");
                        System.out.println("请重新输入 " + sInfo[i].getName()
                                + " 同学的 " + Config.courses[j] + " 成绩");
                    }
                }
                sInfo[i].setCourseAndScore(c);
            }
        }
        // 复制之前的数组元素进去
        // 非第一次添加才需要链接
        if (sInfos != null) {
            for (int i = count; i < count + sInfos.length; i++) {
                sInfo[i] = new Student();
                sInfo[i] = sInfos[i - count];
            }
        }
        oupPuttoFile(sInfo);
        return sInfo;
    }

    /*
     * 从文件中输入
     */
    public Student[] inputFromFile() {
        ArrayList<Student> sList = new ArrayList<Student>();// 未知学生个数,所以用动态数组存储
        File f = new File(FILE_NAME);
        if (f.exists()) {
            try {
                Scanner in = new Scanner(new File(FILE_NAME));
                while (in.hasNextLine()) {
                    Student s = new Student();
                    int tempID = Integer.parseInt(in.nextLine());
                    if (tempID > Config.idp)
                        Config.idp = tempID + 1;
                    s.setId(tempID);
                    s.setName(in.nextLine());
                    s.setAge(Integer.parseInt(in.nextLine()));
                    int i = 0;
                    Course c[] = new Course[Config.courses.length];
                    for (String cname : Config.courses) {
                        c[i++] = new Course(cname, Float.parseFloat(in
                                .nextLine()));
                    }
                    s.setCourseAndScore(c);
                    sList.add(s);// 存进一个学生信息
                }
                in.close();
                Student[] sInfo = (Student[]) sList.toArray(new Student[sList
                        .size()]);// 把动态数组转成数组

                return sInfo;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("打开文件失败!");
                e.printStackTrace();
            }
        }
        return null;
    }

    /*
     * 打印学生信息 sortFunc值缺省时就按照学号升序排序 sortFunc参数示例:
     * 按学号升序排序参数示例{"xh","sx"},降序为{"xh","jx"}
     * 按年龄升序排序参数示例{"nl","sx"},降序为{"nl","jx"} 按课程成绩升序排序参数示例{"cj","sx","语文"}
     */
    public void printStudentInfo(Student[] sInfo, String... sortFunc) {
        if (sInfo != null) {
            if (sortFunc.length < 1) {
                sInfo = sortById(sInfo);
            } else if (sortFunc.length == 1) {
                switch (sortFunc[0]) {
                case "xh":
                    sInfo = sortById(sInfo);
                    break;
                case "nl":
                    sInfo = sortByAge(sInfo);
                    break;
                }
            } else if (sortFunc.length == 2 && !sortFunc[0].equals("cj")) {
                int order = 0;// 作为参数传进去,0时升序,1时降序
                if (sortFunc[1].equals("jx"))
                    order = 1;
                switch (sortFunc[0]) {
                case "xh":
                    sInfo = sortById(sInfo, order);
                    break;
                case "nl":
                    sInfo = sortByAge(sInfo, order);
                    break;
                }
            } else if (sortFunc.length == 2 && sortFunc[0].equals("cj")) {
                sInfo = sortByCourseScore(sInfo, sortFunc[1]);
            } else if (sortFunc.length == 3) {
                int order = 1;// 默认成绩从高到低排序,要从低到高排序,order值传0
                if (sortFunc[1].equals("sx"))
                    order = 0;
                sInfo = sortByCourseScore(sInfo, sortFunc[2], order);
            } else {
                System.out.println("未找到排序方法,请检查参数是否正确!");
            }
            for (int i = 0; i < sInfo.length; i++) {
                System.out.println(sInfo[i].toString());
            }
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
    }

    /*
     * 按照学号排序 order值缺省就按学号从小到大排序(升序)
     */
    private Student[] sortById(Student[] sInfo, int... order) {
        if ((order.length > 0 && order[0] == 0) || order.length < 1) {
            System.out.println("按学号升序排序如下:");
            // 从小到达排序
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getId() > sInfo[j].getId()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        } else if (order.length > 0 && order[0] == 1) {
            System.out.println("按学号降序排序如下:");
            // 从小到达排序
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getId() < sInfo[j].getId()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        } else {
            System.out.println("排序方法中参数有误,未对学号进行排序");
        }
        return sInfo;
    }

    /*
     * 按照年龄排序 order缺省就按年龄从小到大排序
     */
    private Student[] sortByAge(Student[] sInfo, int... order) {
        if ((order.length > 0 && order[0] == 0) || order.length < 1) {
            System.out.println("按年龄升序排序如下:");
            // 从小到达排序
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getAge() > sInfo[j].getAge()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        } else if (order.length > 0 && order[0] == 1) {
            System.out.println("按年龄降序排序如下:");
            // 从小到达排序
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getAge() < sInfo[j].getAge()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        } else {
            System.out.println("排序方法中参数有误,未按照年龄对数据进行排序");
        }
        return sInfo;
    }

    /*
     * 按照课程成绩排序 order缺省就按成绩从高到低排序
     */
    private Student[] sortByCourseScore(Student[] sInfo, String courseName,
            int... order) {
        int courseIndex = 0;// 课程对应的数组下标
        for (; courseIndex < Config.courses.length; courseIndex++) {
            if (courseName.equals(Config.courses[courseIndex]))
                break;
        }
        // 成绩从高到低排
        if ((order.length > 0 && order[0] == 1) || order.length < 1) {
            System.out.println("按成绩从高到低排序如下:");
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getCourseAndScore()[courseIndex]
                            .getCourseScore() < sInfo[j].getCourseAndScore()[courseIndex]
                            .getCourseScore()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        } else if (order.length > 0 && order[0] == 0) {
            System.out.println("按成绩从低到高排序如下:");
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getCourseAndScore()[courseIndex]
                            .getCourseScore() > sInfo[j].getCourseAndScore()[courseIndex]
                            .getCourseScore()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }
        }
        return sInfo;
    }

    /*
     * 编号输出学生信息
     */
    public Student[] serialPrint(Student sInfo[]) {
        if (sInfo != null) {
            // 先按照学号升序排序排好
            for (int i = 0; i < sInfo.length - 1; i++) {
                for (int j = i + 1; j < sInfo.length; j++) {
                    if (sInfo[i].getId() > sInfo[j].getId()) {
                        Student temps = new Student();
                        temps = sInfo[i];
                        sInfo[i] = sInfo[j];
                        sInfo[j] = temps;
                    }
                }
            }

            for (int i = 0; i < sInfo.length; i++) {
                System.out.println("【" + (i + 1) + "】\t    "
                        + sInfo[i].toString());
            }
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
        return sInfo;
    }

    /*
     * 修改学生信息 参数为对应学生编号(数组中真实下标)
     */
    public Student[] editSelectStudent(Student sInfo[], int index,
            int courseIndex, float courseScore) {
        if (sInfo != null) {
            sInfo[index].getCourseAndScore()[courseIndex]
                    .setCourseScore(courseScore);
            oupPuttoFile(sInfo);
            System.out.println("修改成功");
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
        return sInfo;
    }

    /*
     * 删除学生信息 参数为学生对应编号(数组中真实下标)
     */
    public Student[] deleteSelectStudent(Student sInfo[], int index) {
        if (sInfo != null) {
            Student newSInfo[] = new Student[sInfo.length - 1];
            int i = 0, j = 0;
            for (; i < sInfo.length; i++) {
                if (index == i)
                    continue;
                newSInfo[j++] = sInfo[i];
            }
            oupPuttoFile(newSInfo);
            System.out.println("删除成功");
            return newSInfo;
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
        return null;
    }

    /*
     * 指定学号查询学生信息(普通查询)
     */
    public void searchById(Student sInfo[], int sId) {
        if (sInfo != null) {
            int count = 0;
            for (Student s : sInfo) {
                if (s.getId() == sId) {
                    System.out.println(s.toString());
                    count++;
                }
            }
            System.out.println("查询到" + count + "条记录");
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
    }

    /*
     * 指定姓名查询(可以通过关键字查找)
     */
    public void searchByName(Student sInfo[], String sName) {
        if (sInfo != null) {
            int count = 0;
            for (Student s : sInfo) {
                if (s.getName().indexOf(sName) >= 0) {
                    System.out.println(s.toString());
                    count++;
                }
            }
            System.out.println("查询到" + count + "条记录");
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
    }

    /*
     * 统计各科总平均分
     */
    public void getAverage(Student sInfo[]) {
        if (sInfo != null) {
            float sumScore = 0.0f;
            System.out.println("各科平均分如下");
            for (int i = 0; i < Config.courses.length; i++) {
                sumScore = getOneCourseSum(sInfo, Config.courses[i]);
                System.out.println(Config.courses[i] + "的平均分为:" + sumScore
                        / sInfo.length);
            }
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
    }

    /*
     * 得到总分
     */
    public void getCourseSum(Student sInfo[]) {
        if (sInfo != null) {
            float sumScore = 0.0f;
            System.out.println("各科总分如下:");
            for (int i = 0; i < Config.courses.length; i++) {
                sumScore = getOneCourseSum(sInfo, Config.courses[i]);
                System.out.println(Config.courses[i] + "的总分为:" + sumScore);
            }
        } else
            System.out.println("不存在任何学生信息,请先添加信息!");
    }

    /*
     * 得到一科总分
     */
    private float getOneCourseSum(Student sInfo[], String Course) {
        float sumScore = 0.0f;
        for (int i = 0; i < Config.courses.length; i++) {
            for (Student s : sInfo) {
                if (s.getCourseAndScore()[i].getCourseName().equals(Course)) {
                    sumScore += s.getCourseAndScore()[i].getCourseScore();
                }
            }
        }
        return sumScore;
    }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 处理系统参数
 * 
 * @author KevinWu
 *
 */
public class Config {
    private static final String CONFIG_FILE_NAME="config.txt";
    //以下参数作为默认 参数
    public static int idp=1308095001;//id统计器,用于自动生成id

    public static String courses[]={"语文","数学","英语"};//课程

    public Config(){
        inputConfig();
    }
    /*
     * 设置起始学号
     */
    public void setIdp(){
        System.out.println("请输入您要设置的起始学号:");
        Scanner sc=new Scanner(System.in);
        idp=sc.nextInt();
        System.out.println("设置成功");
    }
    /*
     * 设置课程
     */
    public void setCourses(){
        System.out.println("请输入您要设置的课程(以-1完成输入):");
        Scanner sc=new Scanner(System.in);
        ArrayList<String> arr=new ArrayList<String>();
        String receiver="";
        while(sc.hasNext()&&!(receiver=sc.next()).equals("-1")){
            arr.add(receiver);
        }
        courses=arr.toArray(new String[arr.size()]);
        System.out.println("设置成功");
    }


    /*
     * 导入设置的参数
     */
    private void inputConfig(){
        File file = new File(CONFIG_FILE_NAME);
        if (!file.isFile() && !file.exists()) return;
        try {
            ArrayList<String> c=new ArrayList<String>();
            Scanner in = new Scanner(file);
            idp=Integer.parseInt(in.nextLine());
            while (in.hasNextLine()) {
                c.add(in.nextLine());
            }
            courses=c.toArray(new String[c.size()]);
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("导入参数出错,将使用默认参数");
            e.printStackTrace();
        }
    }

    /*
     * 导出设置的参数
     */
    public void outputConfig(){
        FileOutputStream fs = null;
        try {
            fs = new FileOutputStream(new File(CONFIG_FILE_NAME));
            PrintStream p = new PrintStream(fs);
            p.println(idp);
            for(String c1:courses)p.println(c1);
            p.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

运行后主界面效果图如下:
主界面

增加学生信息界面:
录入信息

录入信息后会保存到文件(一开始只想应付下作业并不想搞那么复杂,只是后来懒得一次一次输信息,直接从文件输入了。。。):
文件

查询功能菜单如下:
查询

按语文课成绩排序输出:
排序输出

可以按学号或姓名关键字查询信息,学号查询如下:
按学号查询

修改成绩信息功能如下:
修改成绩

删除菜单如下:
删除菜单

删除后查询学生信息:
删除后再查询一下

统计功能如下:
统计平均分

最后是修改系统参数功能:
修改参数

在参数修改中可以动态修改当前课程信息、添加信息的起止学号等,当然也没做那么完善,为了方便,防止数据错乱,每修改了信息就会删除原有学生信息,需要重新添加,如修改了学号起始值和课程信息后如下:
修改课程信息后

对应这个课程信息的添加过程如下:
添加过程

查询信息如下(屏幕宽度有限):
查询信息

最后,可以看看工程项目结构:
项目结构

就这样吧。

猜你喜欢

转载自blog.csdn.net/KevinWu93/article/details/50043549