在做以前的課程設計時,曾經用c語言設計過一個在doc窗口上的學生信息管理系統。與今天要介紹的Java版本學生成績管理系統很相似,但是用c語言編寫程序有它自己的弊端。c語言是面向過程的設計語言,而Java是面向對象的設計語言,代碼量要比c簡單些。但這并不是主要的區別,用面向過程與面向對象所編寫的程序主要區別還是設計思路,這個在字面上也就體現出來了。下面我給大家介紹一下Java版的學生成績管理系統的設計過程。

      程序的最終執行是要在控制臺上顯示的,這就需要有IO方法。在菜單欄通過選擇不同的操作,來完成學生成績的添加、查詢、修改及退出。我要介紹的這個程序主要不是為了完成成績管理功能,只是要把主要思路介紹給大家,所以功能不是很完善,只能處理一個學生的信息。好了閑話少說,進入代碼實現階段:

主程序的設計,Main

package menu.Menu;   

import com.dr.demo.menu.Menu;

public class Main {   
        public static void main(String[] args) {   
                new Menu();   
        }   
}

是不是很簡單呀,說對了,程序的入口main里面只有一個實例化一個匿名Menu對象的操作。接下來就是Menu的編寫了。

Menu類的設計。

package menu;

import util.InputDate;
import PersonOperate.PersonOperate;

public class Menu {
    InputDate input = null;
    public Menu(){
        input = new InputDate();//循環出現菜單
        while(true){
            this.show();
             }
        }

     //定義的菜單內容  

    public void show(){
         System.out.println("\t\t\t1、增加學生信息");   
         System.out.println("\t\t\t2、瀏覽學生信息");   
         System.out.println("\t\t\t3、修改學生信息");   
         System.out.println("\t\t\t4、退出系統");   
         System.out.print("\n\n請選擇要使用的操作:"); 
         int temp = input.getInt();
         switch(temp){
         case 1:{
             new PersonOperate().add();//增加學生信息
             break;
         }
         case 2:{
             new PersonOperate().show();//瀏覽學生信息
             break;
         }
         case 3:{
             new PersonOperate().update();//修改學生信息
             break;
         }
         case 4:{
             System.out.println("是否推出系統Y/N");//退出系統
             String t = input.getString();
             if("Y".equals(t)||"y".equals(t))
             {
             System.out.println("系統退出!");   
             System.exit(1);    }
             else break;
         }
          default:{   
             System.out.println("輸入的內容不正確");   
             break;   
     }   
         }
    }
}

當程序運行時,控制臺主界面的設計就是這樣的。需要說明的是,case語句中要實例化的類,及調用類的方法這些都是放在其他類中的,這體現了面向對象編程的一個特點,封裝性。在主界面設計時,我們只需考慮要實現那些功能,具體功能放在其他類中實現。

PersonOperate類的設計。

package PersonOperate;

import person.Person;
import util.FileOperate;
import util.InputDate;

public class PersonOperate {
      private  InputDate input = null;
      public PersonOperate(){
          this.input = new InputDate();
      }

//完成具體的Person對象操作

      public void add(){
          System.out.println("請輸入姓名:");
          String name = this.input.getString();
          System.out.println("請輸入年齡");
          int  age = this.input.getInt();
          System.out.println("請輸入成績");
          float score = this.input.getFloat();

        //生成Person對象,把對象保存在文件中

          Person per = new Person(name,age,score);
          try{
              new FileOperate().save(per);

         //io操作層

           System.out.println("數據保存成功!");
          }catch(Exception e){
              System.out.println("數據保存失敗!");
          }   
      }
      public void show(){

      //從文件中把內容讀進來

          Person per = null;
          try{
              per = (Person)new FileOperate().read();
          }catch(Exception e){
              System.out.println("數據讀取失敗,請確定數據已經存入!");
          }
          if(per!=null){
              System.out.println(per);
          }
      }
      public void update(){
          Person per = null;
          try{
              per =(Person) new FileOperate().read();
          }catch(Exception e){
              System.out.println("數據讀取失敗,請確定數據已經存入!");
          }
          if(per!=null){
              System.out.println("請輸入新姓名:(原姓名"+per.getName()+")");
              String name = this.input.getString();
              System.out.println("請輸入新姓名:(原姓名"+per.getName()+")");
              int age = this.input.getInt();
              System.out.println("請輸入新姓名:(原姓名"+per.getName()+")");
              float score = this.input.getFloat();

              //信息重新設置
              per.setAge(age);
              per.setName(name);
              per.setScore(score);
              try{
                  new FileOperate().save(per);
                  System.out.println("數據更新成功!");
              }catch(Exception e){
                  System.out.println("數據更新失敗!");}
          }
      }
}

在PersonOperate類中設計了學生成績的添加、查詢、修改方法。這里我們看到,在這些方法里面又實例化了其他類的對象并調用了這些類中的方法。PersonOperate類在這里起承上啟下的作用。它實例化并調用一些有關IO底層的類及方法。有關底層的東西是不變的,如果要添加其它功能,只需在PersonOperate類里在新增方法并在Menu里調用就可以。這樣可以減小程序的維護。還能提高代碼的可讀性,易于查找程序中的錯誤。體現了面向對象設計的另一特點三層架構體系。下面就來實現底層的類。

FileOperate類與InputDate類:

package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileOperate {
       public static final String Filename = "e:\\person.txt";//保存數據路徑

//把對象保存在文件之中

   public void save(Object obj) throws Exception{
           ObjectOutputStream out = null;
           try{
               out = new ObjectOutputStream(new FileOutputStream(new File(Filename)));
               out.writeObject(obj);
           }catch(Exception e){
                     throw e;
           }finally{
               try{
                   out.close();
                   }catch(Exception e){}
               }
           }

//把對象從文件之中讀出來
       public Object read() throws Exception{
           Object obj = null;
           ObjectInputStream input = null;
           try{
               input = new ObjectInputStream(new FileInputStream(new File(Filename)));
               obj = input.readObject();
           }catch(Exception e){
               throw e;
           }finally{
               try{
                   input.close();
                   }catch(Exception e){}
           }
             return obj;         
       }
    }

package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputDate {
    private BufferedReader buf = null;
    public InputDate(){
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    public String getString(){
        String str = null;
        try{
            str = buf.readLine();
        }catch(IOException e){}
        return  str;
    }
    public int getInt(){
        int temp = 0;
        String str = null;
        boolean flag = true;
        while(flag){
            str = this.getString();
            if(!(str.matches("\\d+"))){
                System.out.println("輸入的內容必須是整數,請重新輸入:");
            }
            else{
                temp = Integer.parseInt(str);
                flag = false;
            }
        }
        return temp;
    }
    public float getFloat(){
        float temp = 0;
        String str = null;
        boolean flag = true;
        while(flag){
            str = this.getString();
            if(!(str.matches("\\d+?.\\d{1,2}"))){
                System.out.println("輸入的內容必須是小數(小數點后兩位),請重新輸入:");
            }
            else{
                temp = Float.parseFloat(str);
                flag = false;
            }
        }
        return temp;
    }

}

上面定義的兩個類是這個程序中對數據進行的最底層操作。分別完成向文件進行數據的讀取與寫入和從鍵盤讀取數據進行功能的選取。每一個程序都有這部分,而且是很相似的,但再往上看就會有很大的不同。最后是有關學生的基本信息類的定義了,也是很簡單的。

Person類的定義:

package person;

import java.io.Serializable;

public class Person implements Serializable{
    private String name;
    private int age;
    private float score;
    public Person(){}
    public Person(String name,int age,float score){
        this.name=name;
        this.age=age;
        this.score=score;
    }
    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 float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }

//要重寫toString方法
  public String toString(){
        return "姓名:"+this.name+",年齡:"+this.age+",成績:"+this.score;
    }

}

好了,這個程序到此告一段落,雖然功能簡單但是它體現的面向對象編程的最基本的思路,封裝性,三層架構等等多么大的程序也是由這些思路構成的。只是本文沒有涉及到數據庫、多線程、web等技術,但在以后我會加入的。本文寫得不清楚的地方,希望大家多多指點。