学生管理系统 简单 文件输入输出流

package Demo;
//能基本实现学生信息的增删改查,并含有以下功能:

1、将数组存储改为文本文件存取方式。

 
  

2、数据查询功能可以按照记录的顺序号查询。

 
  

3、修改学生信息可以增加指定学生记录的某项内容。

 
  

          1、修改姓名;2、修改年龄;3、修改性别;4、修改成绩;提示输入学生学号,再输入相关的修改内容。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class StudentMangager1 {
    static int n=0;
    static Student[] a1=null;
    public static void main(String[]args) throws IOException
    {    
        n=4;
        a1=new Student[n];
        a1[0]=new  Student("20153307","wzw",18,true,100);
        a1[1]=new  Student("20153306","wz",18,true,99);
        a1[2]=new  Student("20153304","w",19,true,95);
        a1[3]=new  Student("20153303","wzwh",19,true,50);
        
        try{
            PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
            write(a1,out);
            out.close();
            BufferedReader in=new BufferedReader(new FileReader("newFile.txt"));
            Student[] a2=read(in);
            in.close();
            for(Student e:a2)
                System.out.println(e);
        }
        catch(IOException exception)
        {
            exception.printStackTrace();
        }
        char ch='y';
        while(ch=='y')
        {
            
            System.out.println("***************************************************************");
            System.out.println("\t\t石家庄铁道大学软件工程系学生信息管理系统 ");
            System.out.println("***************************************************************");
            System.out.println("\t\t\t\t1、 遍历输出学生信息:");
            System.out.println("\t\t\t\t2、 新学生信息录入:");
            System.out.println("\t\t\t\t3、 删除学生信息:");
            System.out.println("\t\t\t\t4、 修改学生信息:");
            System.out.println("\t\t\t\t5、 查询学生信息:");
            System.out.println("\t\t\t\t6、 退出:");
            Scanner scan=new Scanner(System.in);
            int sc5=scan.nextInt();
            switch(sc5)
            {
            case 1:
            {
                showStudent();
                break;
            }
            case 2:
            {
                System.out.println("请输入需要添加的学生信息");
                String sc=scan.next();
                String sc1=scan.next();
                int sc2=scan.nextInt();
                boolean sc3=scan.nextBoolean();
                double sc4=scan.nextDouble();
                
                addStudent(sc,sc1,sc2,sc3,sc4);
                break;
            }
            case 3:
            {
                System.out.println("请输入需要删除的的学生的学号");
                String sc=scan.next();
                deleteStudent(sc);
                break;
            }
            case 4:
            {
                System.out.println("请输入需要修改的方式:1:全部修改,2修改指定内容");
                int sc6=scan.nextInt();
                if(sc6==1)
                {
                    System.out.println("请输入要修改的学生信息");
                    String sc=scan.next();
                    String sc1=scan.next();
                    int sc2=scan.nextInt();
                    boolean sc3=scan.nextBoolean();
                    double sc4=scan.nextDouble();
                    updateStudent(sc,sc1,sc2,sc3,sc4);
                }
                else
                {
                    updateStudent();
                }
                break;
            }
            case 5:
            {
                System.out.println("请输入要查询的方式:1:按顺序号查询,2按学号查询");
                int sc=scan.nextInt();
                if(sc==1)
                {
                    System.out.println("请输入要查询的顺序号");
                    int sc2=scan.nextInt();
                    selectStudent1(sc2);
                }
                else
                {
                    System.out.println("请输入需要查询的学生的学号");
                    String sc1=scan.next();
                    selectStudent(sc1);
                }
                
                break;
            }
            case 6:
            {
                ch='x';
                break;
            }
            }
        }
    }
    
    public static void write(Student[] a,PrintWriter out)
    {
         
              // write number of employees
              out.println(a.length);

              for (Student e : a)
                 e.writeStudent(out);
           
    }
    public static Student[] read(BufferedReader in)throws IOException
    {
        int n=Integer.parseInt(in.readLine());
        for(int i=0;i<n;i++)
        {
            a1[i]=new Student();
            a1[i].readStudent(in);
        }
        return a1;
    }
    
    static void showStudent() throws IOException
    {
        BufferedReader in=new BufferedReader(new FileReader("newFile.txt"));
        Student[] a2=read(in);
        in.close();
        for(int i=0;i<n;i++)
        System.out.println(a1[i].getStunumber()+a1[i].getName()+a1[i].getAge()+a1[i].getSex()+a1[i].getScore());
        
    }
    static void addStudent(String nu,String n1,int s1,boolean b,double s) throws IOException
    {
        
        int m=0;
        for(int i=0;i<n;i++)
        {
            if(a1[i].getStunumber().equals(nu))
            {
                System.out.println("已存在这个人");
                m++;
            }
        }
        if(m==0)
        {
            Student[]a2=new Student[n+1];
            for(int i=0;i<n;i++)
            {
                a2[i]=a1[i];
            }
            n++;
            a1=new Student[n];
            a2[n-1]=new Student(nu,n1,s1,b,s);
            PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
            write(a2,out);
            out.close();
            System.out.println("添加成功");
        }
    }
    static void deleteStudent(String nu) throws IOException
    {

        int m=0,z=0;
        for(int i=0;i<n;i++)
        {
            if(a1[i].getStunumber().equals(nu))
            {
                z=i;
                m++;
                System.out.println("删除成功");
            }
        }
        if(m==0)
        {
            System.out.println("不存在这个人");
        }
        else{
            Student[]a2=new Student[n-1];
            for(int j=z;j<n-1;j++)
            {
                a1[j]=a1[j+1];
            }
            n--;
            for(int i1=0;i1<n;i1++)
            {
                a2[i1]=a1[i1];
            }
            a1=new Student[n];
            PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
            write(a2,out);
            out.close();
        }
    }
    static void updateStudent(String nu,String n1,int s1,boolean b,double s) throws IOException
    {
        int m=0,z = 0;
        for(int i=0;i<n;i++)
        {
            if(a1[i].getStunumber().equals(nu))
            {
                z=i;
                m++;
                
            }
        }
        if(m==0)
        {
            System.out.println("不存在这个人");
        }
        else{
            a1[z]=new Student(nu,n1,s1,b,s);
            System.out.println("修改成功");
            Student[] a2=new Student[n];
            for(int i1=0;i1<n;i1++)
            {
                a2[i1]=a1[i1];
            }
            a1=new Student[n];
            PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
            write(a2,out);
            out.close();
        }
    }
    static void updateStudent() throws IOException{
        System.out.println("请选择修改的类型:1、修改姓名;2、修改年龄;3、修改性别;4、修改成绩;");
        Scanner scan=new Scanner(System.in);
        int m=0,z=0;
        int sc=scan.nextInt();
        if(sc==1)
        {
            System.out.println("请先输入需要修改人的学号");
            String sc1=scan.next();
            for(int i=0;i<n;i++)
            {
                if(sc1.equals(a1[i].getStunumber()))
                {
                    m++;
                    z=i;
                }
            }
            if(m==0)
            {
                System.out.println("修改失败,不存在这个人");
            }
            else
            {
                System.out.println("请输入新的姓名");
                String sc2=scan.next();
                a1[z].setName(sc2);
                System.out.println("修改成功");
                Student []a2=new Student[n];
                for(int i=0;i<n;i++)
                {
                    a2[i]=a1[i];
                }
                a1=new Student[n];
                PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
                write(a2,out);
                out.close();
            }
        }
        else if(sc==2)
        {
            System.out.println("请先输入需要修改人的学号");
            String sc1=scan.next();
            for(int i=0;i<n;i++)
            {
                if(sc1.equals(a1[i].getStunumber()))
                {
                    m++;
                    z=i;
                }
            }
            if(m==0)
            {
                System.out.println("修改失败,不存在这个人");
            }
            else
            {
                System.out.println("请输入新的年龄");
                int sc2=scan.nextInt();
                a1[z].setAge(sc2);
                System.out.println("修改成功");
                Student []a2=new Student[n];
                for(int i=0;i<n;i++)
                {
                    a2[i]=a1[i];
                }
                a1=new Student[n];
                PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
                write(a2,out);
                out.close();
            }
        }
        else if(sc==3)
        {

            System.out.println("请先输入需要修改人的学号");
            String sc1=scan.next();
            for(int i=0;i<n;i++)
            {
                if(sc1.equals(a1[i].getStunumber()))
                {
                    m++;
                    z=i;
                }
            }
            if(m==0)
            {
                System.out.println("修改失败,不存在这个人");
            }
            else
            {
                System.out.println("请输入新的性别");
                Boolean sc2=scan.nextBoolean();
                a1[z].setSex(sc2);
                System.out.println("修改成功");
                Student []a2=new Student[n];
                for(int i=0;i<n;i++)
                {
                    a2[i]=a1[i];
                }
                a1=new Student[n];
                PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
                write(a2,out);
                out.close();
            }
        }
        else{

            System.out.println("请先输入需要修改人的学号");
            String sc1=scan.next();
            for(int i=0;i<n;i++)
            {
                if(sc1.equals(a1[i].getStunumber()))
                {
                    m++;
                    z=i;
                }
            }
            if(m==0)
            {
                System.out.println("修改失败,不存在这个人");
            }
            else
            {
                System.out.println("请输入新的成绩");
                double sc2=scan.nextDouble();
                a1[z].setScore(sc2);
                System.out.println("修改成功");
                Student []a2=new Student[n];
                for(int i=0;i<n;i++)
                {
                    a2[i]=a1[i];
                }
                a1=new Student[n];
                PrintWriter out =new PrintWriter(new FileWriter("newFile.txt"));
                write(a2,out);
                out.close();
            }
        }
    }
    static void selectStudent(String nu)
    {
        int m=0,z=0;
        for(int i=0;i<n;i++)
        {
            if(a1[i].getStunumber().equals(nu))
            {
                z=i;
                m++;
            }
        }
        if(m==0)
        {
            System.out.println("查询失败,不存在这个人");
        }
        else{

            System.out.println("查询成功");
            System.out.println(a1[z].getStunumber()+a1[z].getName()+a1[z].getAge()+a1[z].getSex()+a1[z].getScore());
        }
    }
    static void selectStudent1(int z){
        if(z>n-1)
        {
            System.out.println("查询失败,不存在这个人");
        }
        else{
            z=z-1;
            System.out.println(a1[z].getStunumber()+a1[z].getName()+a1[z].getAge()+a1[z].getSex()+a1[z].getScore());
        }
    }
}
class Student {
    public Student(){}
    public String toString()
    {
        return getClass().getName()
                +"[stunumber="+stunumber
                +",name="+name
                +",age="+age
                +",sex="+sex
                +",score="+score
                +"]";
    }
    private String stunumber;
    private String name;
    private int age;
    private boolean sex;
    private double score;
    void setStunumber(String t){
        stunumber = t;
    }
    public void writeStudent(PrintWriter out) {
        // TODO Auto-generated method stub
        out.println(stunumber+"|"+name+"|"+age+"|"+sex+"|"+score);
    }
    public void readStudent(BufferedReader in)throws IOException
    {
        String s=in.readLine();
        StringTokenizer t = new StringTokenizer(s, "|");
        stunumber=t.nextToken();
        name=t.nextToken();
        age=Integer.parseInt(t.nextToken());
        sex=Boolean.parseBoolean(t.nextToken());
        score=Double.parseDouble(t.nextToken());
    }
    String getStunumber(){
        return stunumber;
    }
    String getName(){
        return name;
    }
    int getAge(){
        return age;
    }
    double getScore(){
        return score;
    }
    boolean getSex(){
        return sex;
    }
    void setName(String t){
        name = t;
    }
    
    void setAge(int t){
        age = t;
    }
    
    void setSex(boolean t){
        sex = t;
    }
    
    void setScore(double t){
        score = t;
    }
    
    
    Student(String anumber,String aname,int aage,boolean asex,double ascore){
        stunumber = anumber;
        name = aname;
        age = aage;
        sex = asex;
        score = ascore;
    }
}

 

转载于:https://www.cnblogs.com/qingtianxt/p/6117767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值