我写的Java学生成绩管理系统源代码

这是一个Java编写的学生成绩管理系统,包括增加、查找、删除、修改、排序等功能。用户通过学号进行操作,程序读取并保存数据到data.txt文件。提供友好的命令行交互界面。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

/*
 * Created on 2005-1-11
 */

/**
 * @author RangWei
 * @since 1.0
 *
 * TODO 学生成绩管理系统
 * 通过学号查找,修改,删除数据
 *
 */
public class LittleProgram
{
    static boolean isDelete = true;
    static boolean isFind = true;
    public static void main(String [] args)//主方法,程序从这里开始运行 
    throws IOException,NumberNotFoundException
    {
        int choice=-1;
        do{
            LittleProgram lp = new LittleProgram();
            System.out.println();
            System.out.println("/t####################################");
            System.out.println();
            System.out.println("/t/t Java学生成绩管理系统1.1");
            System.out.println("/t/t请用学号查找,修改,删除数据");
            System.out.println();
            System.out.println("/t####################################/n");
            System.out.print("1.增加数据:/n"+
                    "2.查找数据:/n"+
                    "3.删除数据:/n"+
                    "4.清除所有数据:/n"+
                    "5.把数据全部打印到屏幕/n"+
                    "6.把成绩按学号排序/n"+
                    "7.修改数据/n"+
                    "8.统计已记录成绩学生数/n"+
                    "9.关于作者/n"+
                    "0.退出程序./n" +
                    "输入:");
            BufferedReader in =                           //从终
                new BufferedReader(                       //端接
                        new InputStreamReader(System.in));//收数
            String inputLine = in.readLine();             //字选
            choice= Integer.valueOf(inputLine).intValue();//项;
            switch(choice)
            {
            case 1: {//1.增加数据                  
                String str = lp.inputData();
                lp.addData(str);
                System.out.println("增加数据成功.");
                timeOut(1);
            }break;
            case 2: {//2.查找数据
                long find = 0;
                System.out.print("请输入你要查找的学生学号:");
                BufferedReader inn =
                    new BufferedReader(
                            new InputStreamReader(System.in));
                String inputLi = inn.readLine();
                find = Integer.valueOf(inputLi).longValue();
                lp.findData(find);
               
                timeOut(2);
            }break;
            case 3: {//3.删除数据
                long deleteNumber = 0;
                System.out.print("请输入你想删除的同学的学号:");
                BufferedReader bf =
                    new BufferedReader (
                            new InputStreamReader(System.in));
                String inputL = bf.readLine();
                deleteNumber = Integer.valueOf(inputL).longValue();
                lp.deleteData(deleteNumber);
                if(isDelete)
                System.out.println("删除数据成功!");
                timeOut(1);
            }break;
            case 4: {  
                lp.clearData();//4.清除所有数据
                timeOut(1);
            }break;
            case 5: {
                print();//5.把数据全部打印到屏幕
                timeOut(2);
            }break;
            case 6: {
                lp.numSort();//6.把成绩按学号排序
                System.out.println("按照学号从小到大排序成功!/n"+
                "排序后:/n");
                print();
                timeOut(2);
            }break;
            case 7: {
                lp.rewrite();//7.修改数据
                timeOut(2);
            }break;
            case 8: {
                int count = lp.count();
                System.out.println("共有"+count+"个学生已经记录.");
                timeOut(2);
            }break;
            case 9: {
                System.out.print("/t/tRangWei/n"+
                        "/t/tshiep/n"+
                        "/t/tQQ:*********/n");
                timeOut(4);
            }break;
        }
        }while (choice != 0);
        System.out.println("Bye! ^-^");
        System.exit(0);
    }
    public String inputData()//从终端接收数据的方法,返回字符串
    throws IOException,NumberFormatException
    {
            System.out.print("请依次输入 :学号 姓名 性别 成绩/n" +
          "每项数据请用空格隔开:");
            String all = "";
        try{
            BufferedReader in =                               //从终
                new BufferedReader (                          //端接
                        new InputStreamReader(System.in));    //收数    
                String inputLine = in.readLine();             //据
                StringTokenizer str =                                          
                    new StringTokenizer(inputLine," ");//接收的数据用空格隔开,这个类用来提取每个字符串                         
                long num = Integer.valueOf(str.nextToken()).longValue();//学号          
                String name = (String)str.nextToken();                  //姓名
                String sex = (String)str.nextToken();                   //性别
                double mark = Integer.valueOf(str.nextToken()).doubleValue();//分数
                all = String.valueOf(num) +" , "+
                name +" , "+
                sex +" , "+
                String.valueOf(mark);//把所有的数据用" , "隔开然后在连起来放进字符串all           
        }catch (IOException e){}
         catch (NumberFormatException e){}
        return all;//返回字符串all
    }
    public void addData(String str)//增加数据的方法
    throws IOException
    {
        String s1 ="",s2="" ,s3= "";
        File file = new File("data.txt");
        if (file.exists())//如果文件data.txt存在
        {
        try{
            BufferedReader in =
                new BufferedReader(
                        new FileReader("data.txt"));
            while ((s1=in.readLine())!=null)
                s2+=s1+"/n";//把文件中的每行数据全部放进一个字符串s2                         
            s2+=str+"/n";   //再把s2于形参str相连放进s2
            BufferedReader in2 =                                  //把字符
                new BufferedReader(                               //串s2也
                        new StringReader(s2));                    //就是原
            PrintWriter out =                                     //文件+
                new PrintWriter(                                  //形参str(新输入的一行数据)
                        new BufferedWriter(                       //重新写进data.txt
                                new FileWriter("data.txt")));     //覆盖原来的数据
            while ((s3=in2.readLine())!= null)
            {
                out.println(s3);
            }
            out.close();
            //System.out.println("write data true.");
        }catch (IOException e){}
        }else{
            System.err.println("File /"data/" Missing!");
        }
    }
    public void clearData()//清除data.txt的所有数据的方法
    throws IOException
    {
        File file = new File("data.txt");
        if(file.exists())//如果文件在
        {
            try{
                PrintWriter out =
                    new PrintWriter(
                            new BufferedWriter(
                                    new FileWriter(file)));
                out.print("");//在文件data.txt里写进一个空字符,所以清除了原来的内容
                out.close();  //关闭文件
                System.out.println("clear data true!");
            }catch(IOException e){}
         }else{
                System.err.println("File /"data/" Missing!");
         }
    }
    public void deleteData(long deleteNumber)//删除某条数据
    throws IOException,FileNotFoundException
    {
        isDelete = true;
        try{
        DataMap mp = new DataMap();//生成一个自己编写的容器
        long j=0;
        String s1="",s2="",s3="";
            BufferedReader in =
                new BufferedReader(
                        new FileReader("data.txt"));
            while ((s1=in.readLine())!=null)
            {
                j=numberTokenizer(s1);
                mp.put(j,s1);
            }
            try{
                if(mp.containsKey( String.valueOf(deleteNumber).toString()))
                {
                    mp.remove(deleteNumber);
                }else
                    throw new NumberNotFoundException();
           Collection c = mp.values();
            Iterator iter = c.iterator();
            while(iter.hasNext())
            {
                s1 = (String)iter.next();
                s3 +=s1+"/n";
            }
            BufferedReader in2 =
                new BufferedReader(
                        new StringReader(s3));
            PrintWriter out =
                new PrintWriter(
                        new BufferedWriter(
                                new FileWriter("data.txt")));
            //System.out.println("delete No"+deleteNumber);
            while( (s1 = in2.readLine())!=null)
            {
                out.println(s1);
            }
            out.close();
            }catch (NumberNotFoundException e)
            {
                isDelete = false;
                System.out.println(deleteNumber+" no found :(");   
            }
        }catch(IOException e){}
    }
    public long numberTokenizer(String s)
    throws IOException
    {
        StringTokenizer st =
            new StringTokenizer(s," ");
        return Integer.valueOf((st.nextToken())).longValue();
    }
    public void findData(long find)//查找数据
    throws IOException,NumberNotFoundException
    {
        isFind = true;
        String s = "",findString ="";
        long i;
        DataMap dm = new DataMap();
        BufferedReader in =
            new BufferedReader(
                    new FileReader("data.txt"));
        while ((s=in.readLine())!=null)
        {
            i=numberTokenizer(s);
            dm.put(i,s);
        }
        //in.close();
        try{
            if(dm.containsKey( String.valueOf(find).toString()))
            {
                findString = dm.get(find);
                System.out.println("学号"+find+"学生的资料是:");
                System.out.println(findString);
            }else
                throw  new NumberNotFoundException();
        }catch (NumberNotFoundException e){
            System.out.println(find+" no found :(");
            isFind = false;
        }       
    }
    public static void print()//读取文本文件把数据打印到终端的方法
    throws IOException
    {
        try{
            BufferedReader in =
                new BufferedReader(
                        new FileReader("data.txt"));
            String read = "";
            while ((read = in.readLine())!=null)
                System.out.println(read);
        }catch(IOException e){}
    }
    public static void timeOut(double sec)//停顿短暂时间的一个方法完全可以不要这个功能
    {
        double seconds = sec;
        long t = System.currentTimeMillis()+(int)(seconds*1000);
        while ((System.currentTimeMillis())<t)
            ;
    }
    public void numSort()//按学号排序
    throws IOException
    {
        long i = 0;
        String s = "";
        try{
            DataArrayList dal = new DataArrayList();
            BufferedReader in =
                new BufferedReader(
                        new FileReader("data.txt"));
            while ((s=in.readLine())!=null)
            {
                i=numberTokenizer(s);
                dal.add(i);
            }
            Collections.sort(dal);
            DataMap dm = new DataMap();
            BufferedReader in2 =
                new BufferedReader(
                        new FileReader("data.txt"));
            while ((s=in2.readLine())!=null)
            {
                i=numberTokenizer(s);
                dm.put(i,s);
            }
            PrintWriter out =
                new PrintWriter (
                        new BufferedWriter(
                                new FileWriter("data.txt")));
            Iterator it = dal.iterator();
            long temp = 0;
            String tempStr = "";
            while (it.hasNext())
            {
                temp = Integer.valueOf((String)it.next()).longValue();
                tempStr = dm.get(temp);
                out.println(tempStr);
            }
            out.close();
        }catch(IOException e){}
    }
    public void rewrite()
    throws IOException,NumberNotFoundException
    {
        try{
            System.out.print("请输入你要修改的学生学号:");
            BufferedReader in =
                new BufferedReader (
                        new InputStreamReader(System.in));
            String inputLine = in.readLine();
            long num = Integer.valueOf(inputLine).longValue();
            findData(num);
            if(isFind)
            {
            deleteData(num);
            System.out.print("请重新输入该学生的资料:");
            String str = inputData();
            addData(str);
            System.out.println("rewrite true!");
            }           
        }catch(IOException e){}
        catch(NumberNotFoundException e){}
    }
    public int count()
    throws IOException
    {
       
        DataArrayList dal = new DataArrayList();
        try{
            String s = "";
            long i =0;
            BufferedReader in =
                new BufferedReader(
                        new FileReader("data.txt"));
            while ((s=in.readLine())!=null)
            {
                i=numberTokenizer(s);
                dal.add(i);
            }
        }catch(IOException e){}
        return dal.size();
    }
}
/*
 *
 * @author RangWei
 * TODO 这是个我们写的一个容器,继承公共类HashMap
 * 大概的功能就相当一个数组
 *
 */
class DataMap extends HashMap//一个存储数据的Map                 
{
    public void put(long i,String str)//把学号和数据放进这个Map
    {                                 //以后一个学号(key)对应的是一个人的数据(value)
        put(String.valueOf(i).toString(),str);
    }
    public void remove(long i)//接收学号,然后删除学号(key)和它对应的数据(value)
    {
        remove(String.valueOf(i).toString().toString());
    }
    public String get(long i)//接收一个学号,然后返回这个key对应的value
    {
        String s = String.valueOf(i).toString();
        if (!containsKey(s))
        {
            System.err.println("Not found Key: "+s);
        }
        return (String)get(s);
    }
}
/*
 *
 * @author RangWei
 *
 * TODO 这个类继承ArrayList
 * 用来按数字排序,在用学号排序时要用到它
 *
 */
class DataArrayList extends ArrayList
{
    public void add(long num)
    {
        String numToString = String.valueOf(num).toString();
        add(numToString);
    }
}
/*
 *
 * @author RangWei
 *
 * TODO 增加的一个Exception,主要是在文件里没有要找
 * 的学号就抛出
 *
 */
class NumberNotFoundException extends Exception
{                                                          
    public NumberNotFoundException()
    {}
}

学生成绩信息管理系统涉及到学生、教师、系统管理员、班级、学生成绩、课程。设置一个系统管理员对系统进行管理。所有用户需输入账号、密码登录进入系统;管理员进入系统后可对学生、老师、班级、课程进行增删改查操作;学生进入系统,查看成绩、查看和修改自己的信息;老师进入系统后,对自己这门课程的学生设置课程成绩、查看和修改自己的信息,查看学生的信息和成绩、以及统计分析学生的成绩; 管理员为班级设置年级,为年级设置课程,为班级的每门课程设置老师,为学生设置班级。一个年级有多门课程(语文、数学、外语等等),班级的每门课程只能有一名老师,一个老师可以有多门课程;老师选择自己这门课程为该课程的学生登记成绩。老师可以查看其他老师的信息(可以当成是老师的通讯录),查看本课程学生的信息和成绩;学生可以查看班级其他同学的信息(可以看成是班级的同学录)。 考试分为两种,一种是年级统考,一种是平时考试。年级统考需要管理员事先添加一次年级统考,考试成绩出来后,老师进入系统选择该次考试为学生登记成绩。平时考试则是班级平时的考试,老师添加考试信息,登记成绩。成绩统计分析则是针对年级统考进行分析,主要涉及各学科分数名次,总分名次。 技术实现 系统环境:Windows开发工具:IDEAJava版本:JDK 1.8服务器:Tomcat 1.8数据库:MySQL 5.X系统采用技术:Servlet+Jsp+Jdbc+H-ui+EasyUI+jQuery+Html+Css+Ajax 系统功能系统主要分为三种用户角色,分别是管理员、老师以及学生,其具体功能如下: - 管理员   学生信息管理、教师信息管理、年级信息管理、班级信息管理、课程信息管理、考试信息管理,系统参数设置 - 老师   教学管理、教师个人信息维护、教师通讯录管理 - 学生考试成绩查询、学生通讯录、学生个人信息维护 运行截图 登录界面: 管理员界面: 考试列表:  成绩统计: 学生信息管理: 教师信息管理: 年级、班级信息管理:  系统设置: 教师界面:  成绩登记:  教师通讯录: 个人信息:  学生界面: 学生成绩查询: 班级通讯录: 学生个人信息:              
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值