zoukankan      html  css  js  c++  java
  • Java——简单实现学生管理系统

    import java.io.*;
    import java.util.ArrayList;
    import java.util.Scanner;
    class MyObjectOutputStream extends ObjectOutputStream{
     public MyObjectOutputStream() throws IOException{
      super();
     }
     public MyObjectOutputStream(OutputStream out) throws IOException {
      super(out);
     }
     public void writeStreamHeader() throws IOException {
      return;
     }         //以上为解决对象流输入输出读写文件时的一些问题,重写库里的方法。
    }
    class Student implements Serializable {    //序列化这个类,只有成员变量无方法,作为结构体使用。
     long studentid = 1803050200;                  //学号,姓名,性别,年龄,备注。
     String studentname = "noname";
     String Sexual = "unknow";
     int age = 18;
     String ps = "unknow";
    }
    public class DemoSystem {          
     public static void main (String args []) {
      boolean end = true;
      String c ;
      c = "请选择系统功能项:"+" "+" "+"a.从文件中读入学生的基本信息"+" "+" "+"b.添加新学生的基本信息"+" "+" "+"c.学生基本信息显示"+" "+" "+"d.学生信息保存至文件"+" "+" "+"e.学生基本信息删除"+" "+" "+"f.学生基本信息的修改"+" "+" "+"g.学生基本信息查询"+" "+" "+" "+"1.按学号查询"+" "+" "+" "+"2.按姓名查询"+" "+" "+"h.退出系统"+" ";   //方便打印管理系统界面。
      Student stu[] = new Student[10];    //实例化“结构体”数组。
      ArrayList<Student> al = new ArrayList<Student>();   //泛型数组,方便保存。单纯的对象流输入输出,并不方便。
      for(int i = 0;i<10;i++) {       //初始化
       stu[i] = new Student();
      }
      Scanner sc = new Scanner(System.in);    //用户输入
      File f = new File("D:\javawork","stu1.txt");  //文件创建
      try {                 //因为文件读写可能出现异常,所以把语句放入try语句块内,方便捕捉异常
       while(end) {        //循环开始,end为前面设的布尔值,初始值为true
        System.out.println(c);   //打印管理系统菜单(界面)
        char d = sc.next().charAt(0);   //等待用户输入对应的菜单项字母
        switch(d) {       //匹配相应的用户输入的菜单字母,以执行其功能
        case 'a':         //从文件读取学生信息
         FileInputStream fileIn = new FileInputStream(f);   //初始化对象流。
         ObjectInputStream objectIn = new ObjectInputStream(fileIn);
         try {    //用try捕捉异常,可能出现找不到文件的现象
         ArrayList<Student> al1 =  (ArrayList<Student>)objectIn.readObject();  //用对象流读取,并打印学生信息
         for(int i = 0;i<10;i++) {
          stu[i] = al1.get(i);
          System.out.println("name:"+stu[i].studentname+"  Sexual:"+stu[i].Sexual+"  age:"+stu[i].age+"  id:"+stu[i].studentid+"  ps:"+stu[i].ps);
         }
         }
         catch(ClassNotFoundException e) {
          System.out.println(e);
         }
         objectIn.close();  //关闭对象流
         fileIn.close();
         break;
        case 'b':    //添加新学生功能
           int q = 0;
           int flag = 0;
           boolean end0 = true;
           String name = "noname";
           while(end0&&flag<10) {  //运用flag,以防数组已满,不能添加
            if(stu[flag].studentname.compareTo(name)==0) {
             q=flag;
             end0 = false;
            }
            flag++;
           }
         for(;q<10;q++) {
         if(stu[q].studentname.compareTo(name)==0) {
          System.out.println("请输入新学生姓名:");
          stu[q].studentname = sc.next();
          System.out.println("请输入新学生学号:");
          stu[q].studentid = sc.nextLong();
          System.out.println("请输入新学生年龄:");
          stu[q].age = sc.nextInt();
          System.out.println("请输入新学生性别:");
          stu[q].Sexual = sc.next();
          System.out.println("请输入对新学生的备注:"+" ");
          stu[q].ps = sc.next();
          System.out.println("添加新学生信息完毕!"+" ");
         }
         System.out.println("是否继续?yes/no");
         String anwser = sc.next();
         if(anwser.compareTo("yes")==0) {
          q=q;
         }
         if(anwser.compareTo("no")==0) {
          q = 10;
         }
         }
         break;
        case 'c':      //显示基本学生信息
         String s;
         for(int i = 0;i<10;i++) {
          s= "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;
          System.out.println(s);
         }
         break;
        case 'd':   //保存学生信息
         FileOutputStream fileOut = new FileOutputStream(f);
         ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
         if(f.length()<1) {
          for(int i=0;i<10;i++) {
           al.add(stu[i]);
          }
          objectOut.writeObject(al);
          objectOut.flush();
         }
         else {
          MyObjectOutputStream mos = new MyObjectOutputStream(fileOut);
          for(int i=0;i<10;i++) {
           al.add(stu[i]);
          }
          mos.writeObject(al);
          mos.flush();
         }
         objectOut.close();
         fileOut.close();
         break;
        case 'e':    //删除学生信息
         boolean start1= true;
         while(start1) {
         System.out.println("请输入你想操作的学号:");
         long si = sc.nextLong();
         for(int i = 0;i<10;i++) {
          if(stu[i].studentid==si) {
           String str1 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;
           System.out.println(str1);
           System.out.println(" "+"1.姓名"+" "+"2.性别"+" "+"3.年龄"+" "+"4.学号"+" "+"5.备注"+" ");
           System.out.println("请输入您要操作的序号:");
           int l = sc.nextInt();
           switch(l) {
           case 1:
            stu[i].studentname = "noname";
            break;
           case 2:
            stu[i].Sexual="unknow";
            break;
           case 3:
            stu[i].age=18;
            break;
           case 4:
            stu[i].studentid = 1803050200;
            break;
           case 5:
            stu[i].ps="unknow";
            break;
           default :
            System.out.println("输入序号错误!"); 
           } 
          }
         }
         System.out.println("是否继续删除学生信息?yes/no");
         String me1 = sc.next();
         if(me1.compareTo("yes")==0) {
          start1 = true;
         }
         if(me1.compareTo("no")==0) {
          start1 = false;
         }
         }
         break;
        case 'f':   //更改学生信息
         boolean end2 = true;
         System.out.println("请输入你想操作的学号:");
         long si1 = sc.nextLong();
         for(int i = 0;i<10;i++) {
          if(stu[i].studentid==si1) {
           String str2 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;
           System.out.println(str2);
           System.out.println(" "+"1.姓名"+" "+"2.性别"+" "+"3.年龄"+" "+"4.学号"+" "+"5.备注"+" ");
           System.out.println("请输入您要操作的序号:");
           int l1 = sc.nextInt();
           while(end2) {
            switch(l1) {
            case 1:
             System.out.println("请输入你更改后的姓名:");
             stu[i].studentname = sc.next();
             break;
            case 2:
             System.out.println("请输入你更改后的性别:");
             stu[i].Sexual = sc.next();
             break;
            case 3:
             System.out.println("请输入你更改后的年龄:");
             stu[i].age = sc.nextInt();
             break;
            case 4:
             System.out.println("请输入你更改后的学号:");
             stu[i].studentid = sc.nextLong();
             break;
            case 5:
             System.out.println("请输入你更改后的备注::");
             stu[i].ps = sc.next();
             break;
            default :
             System.out.println("输入序号错误!");
            }
            System.out.println("是否继续修改?yes/no");
            String an1=sc.next();
            if(an1.compareTo("yes")==0) {
             end2 = true;
            }
            if(an1.compareTo("no")==0) {
             end2 = false;
            }
           }
          }
         }
         break;
        case 'g':  //查询某个学生信息
         boolean end4 = true;
         while(end4) {
         System.out.println("(1).按学号查询"+" "+"(2).按姓名查询"+" "+"输入操作序号,继续"+" ");
         int l3 = sc.nextInt();
         if(l3==1) {
          System.out.println("请输入查询学号:");
          long stid2=sc.nextLong();
          for(int i = 0;i<10;i++) {
           if(stu[i].studentid==stid2) {
           String str3 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;
           System.out.println(str3);
           }
          }
         }
         if(l3==2) {
          System.out.println("请输入查询姓名:");
          String stna2 = sc.next();
          for(int i = 0;i<10;i++) {
           if(stna2.compareTo(stu[i].studentname)==0) {
            String str4 = "姓名:"+stu[i].studentname+"  性别:"+stu[i].Sexual+"  年龄:"+stu[i].age+"  学号:"+stu[i].studentid+"  备注:"+stu[i].ps;
            System.out.println(str4);
           }
          }
         }
         System.out.println("是否继续查询?yes/no");
         String an4 = sc.next();
         if(an4.compareTo("yes")==0) {
          end4 =true;
         }
         if(an4.compareTo("no")==0) {
          end4 =false;
         }
         }
         break;
        case 'h':
         System.out.println("退出系统!");
         end =false;
         break;
        default :
         System.out.println("输入的操作字母错误!");
         }
       }
      }
      catch(IOException e) {
       System.out.println(e.toString());
      }
     }
    }

  • 相关阅读:
    15 鼠标事件
    09 属性操作
    06 DOM操作之插入节点
    03 如何处理多个库$冲突的问题
    01 jquery引入
    08 千千音乐盒实现全选和反选
    03 衣服相册切换效果
    02 显示和隐藏图片
    01 图片切换
    派生
  • 原文地址:https://www.cnblogs.com/wbwhy/p/11703607.html
Copyright © 2011-2022 走看看