Java-- simple implementation of student management system

the java.io. * Import;
Import of java.util.ArrayList;
Import java.util.Scanner;
class MyObjectOutputStream the extends the ObjectOutputStream {
 public MyObjectOutputStream () throws IOException {
  Super ();
 }
 public MyObjectOutputStream (the OutputStream OUT) throws IOException {
  Super (OUT );
 }
 public void the writeStreamHeader () throws IOException {
  return;
 } // to solve the above problems when the stream of objects to read and write the input and output files, overwriting library method.
}
Class Student sequence of the implements the Serializable {// this class, only the members of the non-variable method, as a structure.
 long studentid = 1803050200; // Student ID, name, sex, age, notes.
 StudentName = String "noname";
 String Sexual = "unknow";
 int Age = 18 is;
// easy print management system interface.   Student stu [] = new Student [ 10]; // instantiate array "structure."   ArrayList <Student> al = new ArrayList <Student> (); // generic array, easy to save. Simple object stream input and output, not convenient.   for (int i = 0; i <10; i ++) {// Initialization    STU [I] = new new Student ();   }   Scanner Scanner new new SC = (the System.in); // user input












  File f = new File ( "D : \\ javawork", "stu1.txt"); // Create file
  try {// file read and write because the possible exception, so the statements in the try block, easy to catch exceptions
   while (end) {// cycle start, end disposed in front of a Boolean value, the initial value to true
    System.out.println (C); // print management system menu (screen)
    char = D sc.next () the charAt. (0); // wait for user input letters corresponding to the menu item
    switch (d) {// menu letters match the corresponding user input to perform its functions
    case 'a': // student reads information from the file
     FileInputStream fileIn = new FileInputStream (f); // initialize the object stream.
     = New new objectIn the ObjectInputStream the ObjectInputStream (fileIn);
     try {// try to capture with abnormal phenomenon may occur not find the file
     ArrayList <Student> al1 = (ArrayList <Student>) objectIn.readObject (); // read with object flow take, and prints the student information
     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);
     }
     }
     the catch (a ClassNotFoundException E) {
      System.out.println (E);
     }
     objectIn.close (); // close the object stream
     fileIn.close ();
     BREAK;
    case 'b': // add new functionality student
       int Q = 0;
       int In Flag = 0;
       Boolean = END0 to true;
       String name = "noname";
       the while (END0 && In Flag <10) {// use flag, to prevent the array is full , not add
        IF (STU [In Flag] .studentname.compareTo (name) == 0) {
         Q = In Flag;
         END0 = to false;
        }
        flag++;
       }
     for (; Q <10; Q ++) {
     IF (STU [Q] .studentname.compareTo (name) == 0) {
      System.out.println ( "Please enter a new student's name:");
      STU [Q] .studentname sc.next = ();
      System.out.println ( "Please enter a new student number:");
      STU [q] = .studentid sc.nextLong ();
      System.out.println ( "Please enter a new age of the students: ");
      STU [q] = .age sc.nextInt ();
      System.out.println (" Please enter a new student sex: ");
      STU [q] = .Sexual sc.next ();
      System.out.println ( "Please enter comments for the new students:" + "\ the n-");
      STU [q] = sc.next .ps ();
      System.out.println ( "add new student information is complete!" + "\ n" );
     }
     System.out.println ( "whether to continue yes / NO?");
     String anwser = sc.next ();
     iF (anwser.compareTo("yes")==0) {
      q=q;
     }
     IF (anwser.compareTo ( "NO") == 0) {
      Q = 10;
     }
     }
     BREAK;
    Case 'C': // student information display basic
     String S;
     for (int I = 0; I <10; I ++) {
      S = "name:" + stu [i] .studentname + " gender:" + stu [i] .Sexual + " Age:" + stu [i] .age + " Student ID:" + stu [i] .studentid + " Remarks : "STU + [I] .ps;
      System.out.println (S);
     }
     BREAK;
    Case 'D': // save student information
     a FileOutputStream FILEOUT new new = a FileOutputStream (F);
     the ObjectOutputStream objectOut the ObjectOutputStream new new = (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 = "Name:" + stu [i] .studentname + " Sex:" + stu [i] .Sexual + " Age:" + stu [i] .age + " Student ID:" + stu [i] .studentid + " Remarks : "STU + [I] .ps;
       System.out.println (str1);
       .. System.out.println (" \ T "+" name. 1 "+" \ n "+" 2 gender "+" \ n ... "+" Age 3 "+" \ n "+" 4 Student ID "+" \ n "+" 5 Remarks "+" \ the n-");
       System.out.println (" Please enter your want to operate ID: ");
       int sc.nextInt L = ();
       Switch (L) {
       Case. 1:
        STU [I] .studentname =" noname ";
        BREAK;
       Case 2:
        STU [I] .Sexual =" unknow ";
        BREAK ;
       Case. 3:
        STU [I] = 18 is .age;
        break;
       case 4:
        stu[i].studentid = 1803050200;
        break;
       case 5:
        STU [i] = .ps "unknow";
        BREAK;
       default:
        System.out.println ( "! Enter the serial number error"); 
       } 
      }
     }
     System.out.println ( "whether to continue to delete student information yes / no?") ;
     String sc.next ME1 = ();
     IF (me1.compareTo ( "Yes") == 0) {
      Start1 = to true;
     }
     IF (me1.compareTo ( "NO") == 0) {
      Start1 = to false;
     }
     }
     BREAK;
    Case 'f': // change the student information
     boolean = end2 to true;
     System.out.println ( "Please enter the number you want to learn the operation:");
     Long SI1 = sc.nextLong ();
     for (int i 0 =; I <10; I ++) {
      IF (STU [I] == .studentid SI1) {
       String str2 = "Name:" + stu [i] .studentname + " Sex:" + stu [i] .Sexual + " Age:" + stu [i] .age + " Student ID:" + stu [i] .studentid + " Remarks : "STU + [I] .ps;
       System.out.println (str2);
       .. System.out.println (" \ T "+" name. 1 "+" \ n "+" 2 gender "+" \ n ... "+" Age 3 "+" \ n "+" 4 Student ID "+" \ n "+" 5 Remarks "+" \ the n-");
       System.out.println (" Please enter your want to operate ID: ");
       int sc.nextInt L1 = ();
       the while (end2) {
        Switch (L1) {
        Case. 1:
         System.out.println (" Please enter your name after the change: ");
         STU [I]. = sc.next studentName ();
         BREAK;
        Case 2:
         System.out.println ( "After you enter the sex change: ");
         STU [i] = .Sexual sc.next ();
         BREAK;
        Case 3:
         System.out.println ( "Please enter your age, after the change:");
         STU [i] = .age sc.nextInt ();
         BREAK;
        Case 4:
         System.out.println ( "Please enter the school after you change number: ");
         STU [i] = .studentid sc.nextLong ();
         BREAK;
        Case 5:
         System.out.println (" Please enter a comment after you change :: ");
         STU [i] = sc .ps .next ();
         BREAK;
        default:
         System.out.println ( "enter the numbers wrong!");
        }
        System.out.println ( "whether to continue to modify yes / NO?");
        String AN1 = sc.next ();
        IF (an1.compareTo ( "Yes") == 0) {
         end2 = to true;
        }
        IF (an1.compareTo ( "NO") == 0) {
         end2 = false;
        }
       }
      }
     }
     BREAK;
    Case 'G': // Query a student information
     Boolean = END4 to true;
     the while (END4) {
     System.out.println (+ "\ n-" + "(. 1) by student number inquiry." . "(2) by name query" + "\ n" + "serial number input operation, continue" + "\ n-");
     int sc.nextInt L3 = ();
     IF (L3 ==. 1) {
      the System.out. println ( "Please enter the Student ID:");
      Long stid2 sc.nextLong = ();
      for (int I = 0; I <10; I ++) {
       IF (STU [I] == .studentid stid2) {
       String Str3 = "name:" + stu [i] .studentname + " sex:" + stu [i] .Sexual + " Age:" + stu [i] .age + " Student ID:" + stu [i] .studentid + " NOTE:" STU + [I] .ps;
       System.out.println (Str3);
       }
      }
     }
     if(l3==2) {
      System.out.println ( "Please enter the name:");
      String stna2 sc.next = ();
      for (int I = 0; I <10; I ++) {
       IF (stna2.compareTo (STU [I] .studentname ) == 0) {
        String str4 = "name:" + stu [i] .studentname + " gender:" + stu [i] .Sexual + " Age:" + stu [i] .age + " Student ID:" + stu [ i] .studentid + "Note:" + STU [i] .ps;
        System.out.println (str4);
       }
      }
     }
     System.out.println ( "whether to continue the inquiry yes / NO?");
     String AN4 = sc. Next ();
     IF (an4.compareTo ( "Yes") == 0) {
      END4 = to true;
     }
     IF (an4.compareTo ( "NO") == 0) {
      END4 = to false;
     }
     }
     BREAK;
    Case 'H':
     System.out.println ( "exit the system!");
     to false = End;
     BREAK;
    default:
     System.out.println ( "Error letter input operation!");
     }
   }
  }
  the catch (IOException E) {
   System.out.println (e.toString ());
  }
 }
}

Guess you like

Origin www.cnblogs.com/wbwhy/p/11703607.html
Recommended