在做以前的課程設(shè)計時,曾經(jīng)用c語言設(shè)計過一個在doc窗口上的學(xué)生信息管理系統(tǒng)。與今天要介紹的Java版本學(xué)生成績管理系統(tǒng)很相似,但是用c語言編寫程序有它自己的弊端。c語言是面向過程的設(shè)計語言,而Java是面向?qū)ο蟮脑O(shè)計語言,代碼量要比c簡單些。但這并不是主要的區(qū)別,用面向過程與面向?qū)ο笏帉懙某绦蛑饕獏^(qū)別還是設(shè)計思路,這個在字面上也就體現(xiàn)出來了。下面我給大家介紹一下Java版的學(xué)生成績管理系統(tǒng)的設(shè)計過程。

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

主程序的設(shè)計,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類的設(shè)計。

package menu;

import util.InputDate;
import PersonOperate.PersonOperate;

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

     //定義的菜單內(nèi)容  

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

當(dāng)程序運(yùn)行時,控制臺主界面的設(shè)計就是這樣的。需要說明的是,case語句中要實例化的類,及調(diào)用類的方法這些都是放在其他類中的,這體現(xiàn)了面向?qū)ο缶幊痰囊粋€特點,封裝性。在主界面設(shè)計時,我們只需考慮要實現(xiàn)那些功能,具體功能放在其他類中實現(xiàn)。

PersonOperate類的設(shè)計。

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("數(shù)據(jù)保存成功!");
          }catch(Exception e){
              System.out.println("數(shù)據(jù)保存失敗!");
          }   
      }
      public void show(){

      //從文件中把內(nèi)容讀進(jìn)來

          Person per = null;
          try{
              per = (Person)new FileOperate().read();
          }catch(Exception e){
              System.out.println("數(shù)據(jù)讀取失敗,請確定數(shù)據(jù)已經(jīng)存入!");
          }
          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("數(shù)據(jù)讀取失敗,請確定數(shù)據(jù)已經(jīng)存入!");
          }
          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();

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

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

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";//保存數(shù)據(jù)路徑

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

   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("輸入的內(nèi)容必須是整數(shù),請重新輸入:");
            }
            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("輸入的內(nèi)容必須是小數(shù)(小數(shù)點后兩位),請重新輸入:");
            }
            else{
                temp = Float.parseFloat(str);
                flag = false;
            }
        }
        return temp;
    }

}

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

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;
    }

}

好了,這個程序到此告一段落,雖然功能簡單但是它體現(xiàn)的面向?qū)ο缶幊痰淖罨镜乃悸罚庋b性,三層架構(gòu)等等多么大的程序也是由這些思路構(gòu)成的。只是本文沒有涉及到數(shù)據(jù)庫、多線程、web等技術(shù),但在以后我會加入的。本文寫得不清楚的地方,希望大家多多指點。