学生成绩管理系统

学校小学期的一个作业,使用文件实现对数据的存取,若用数据库可能跟简单一些,不过系统很小,所以区别也不是很大。发上来大家批评批评。。。

说明:

此程序还需在程序当前文件夹中建一个StudentInfo文件夹,其中再放一个StudentInfo.txt文件格式如下:

Name ID Sex S1 S2 S3 S4 S5
xxx 02281301 男  88  87  90  82  89
yyy 02281302 男 85 94 86 87 89
zzz   02281303 男 84 86 81 82 81

另还要建一Image文件夹,里面放一些学生头像,但如果不想实现这部分功能也可不放。

源码:

ManageGUI.java


package ScoreManage;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.*;

/**
 
 This class provides a interactive interface to users
 
*/
public class ManageGUI {
 ScoreControl ScrCtrl;
 
 JFrame mainF;
 JPanel mainP;
 
 JMenuBar mainMB;
 JMenu fileM, manageM, helpM;
 JMenuItem stuinfoMI, saveMI, quitMI, queryMI, insertMI, deleteMI, searchMI, infoMI; 
 
 //JTable
 DefaultTableModel TModel;
 JTable InfoTable;
    Object[][] InfoCell;
 String[] ColumnName;
  
 //JList
 final String PATH = "Image/";
 DefaultListModel LModel;
 JList sList;
 JLabel sImage;
 JTextArea stuinfoTA; 
 ImageIcon img;
 JSplitPane innerPane;
 JSplitPane outerPane;
 
 
 //query
 JPanel queryP;
 JTextField queryTF;
 ButtonGroup queryBG;
 JRadioButton first, second, third;
 JTextArea queryTA;
 ArrayList QStuInfo = new ArrayList();
 
 public ManageGUI() {
  //creates a aggregative object to operate the SInfo
  ScrCtrl = new ScoreControl();
  
  mainF = new JFrame();
  mainF.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    quitDlg();   
   }
  });
  mainP = new JPanel();
  mainP.add(new JLabel("Students' Information Report"));
  
  addMenu();
  
  addComponent();
  
  showFrame();
  
  System.gc(); 
 }
 
 public void addMenu() {
  mainMB = new JMenuBar();
  fileM = new JMenu("File");
  manageM = new JMenu("Manage");
  helpM = new JMenu("Help");
  
  mainF.setJMenuBar(mainMB); 
  mainMB.add(fileM);
  mainMB.add(manageM);
  mainMB.add(helpM);
  
  stuinfoMI = new JMenuItem("StuInfo");
  stuinfoMI.addActionListener(new MListener());
  fileM.add(stuinfoMI);
  saveMI = new JMenuItem("Save");
  saveMI.addActionListener(new MListener());
  fileM.add(saveMI);
  quitMI = new JMenuItem("Quit"); 
  quitMI.addActionListener(new MListener());    
  fileM.add(quitMI);
  
  queryMI = new JMenuItem("Query");
  queryMI.addActionListener(new MListener());
  manageM.add(queryMI);
  insertMI = new JMenuItem("Insert");
  insertMI.addActionListener(new MListener());
  manageM.add(insertMI);
  deleteMI = new JMenuItem("Delete");
  deleteMI.addActionListener(new MListener());
  manageM.add(deleteMI);
  searchMI = new JMenuItem("Search");
  searchMI.addActionListener(new MListener());
  manageM.add(searchMI);
  
  infoMI = new JMenuItem("Info");
  infoMI.addActionListener(new MListener());
  helpM.add(infoMI);
 }
 
 public void addComponent() {
  createStuInfoTable();
  createStuInfoList();
 }
 
 public void showFrame() {
  mainF.setTitle("ScoreManage");
  mainF.setSize(650,550);
  mainF.setLocation(200,100);
  mainF.setVisible(true); 
 }

 public void createStuInfoTable() {
  InfoCell = new Object[ScrCtrl.StuList.size()][ScrCtrl.SNameList.size()];
  int j = 0;
  for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
   while(j < ScrCtrl.SNameList.size()) {
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getName();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getNum();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getSex();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getScore1();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getScore2();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getScore3();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getScore4();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getScore5();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getTotalScore();
    InfoCell[i][j++] = ((StudentInfo)ScrCtrl.StuList.get(i)).getAverageScore(); 
   }
   j = 0; 
  }
  ColumnName = new String[ScrCtrl.SNameList.size()];
  for(int i = 0; i < ScrCtrl.SNameList.size(); i++) {
   ColumnName[i] = (String)ScrCtrl.SNameList.get(i);  
  } 
  
  Dimension VPSize=new Dimension(600, 400);
  TModel =new DefaultTableModel(InfoCell,ColumnName);
  InfoTable = new JTable(TModel); 
  InfoTable.setEnabled(false);
  InfoTable.setPreferredScrollableViewportSize(VPSize);
  mainP.add(new JScrollPane(InfoTable));
  mainF.getContentPane().add(mainP, BorderLayout.CENTER); 
 }
 
 public void createStuInfoList() {
  LModel = new DefaultListModel();
  for(int i = 0; i < ScrCtrl.StuList.size(); i++)
   LModel.addElement(ScrCtrl.StuList.get(i)); 
  sList = new JList(LModel);
  sImage = new JLabel();
  stuinfoTA = new JTextArea(4,10); 
                     
     innerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
              new JScrollPane(sList), sImage);
  innerPane.setBackground(Color.WHITE);
  innerPane.setContinuousLayout(true);
     innerPane.setOneTouchExpandable(true);
     outerPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                   innerPane, new JScrollPane(stuinfoTA));
           
        sList.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent event) {
             StudentInfo value = (StudentInfo)sList.getSelectedValue();
               if(value != null) {
                int index = (int)(Math.random() * 10);
                img = new ImageIcon(PATH + index + ".gif");
                sImage.setIcon(img);
                stuinfoTA.setText("Student Record :" + "/n" + value.getName() + "/n" +
                  value.getNum() + "/n" + value.getSex());
               }
          }
     }); 
 }
 
 class MListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   Object command = e.getSource();

   if(command == stuinfoMI) {
    stuInfoDlg();
   }
   else if(command == saveMI) {
    writeFileDlg();   
   }
   else if(command == quitMI) {
    quitDlg();
   }
   else if(command == queryMI) {
    queryDlg();
   }
   else if(command == insertMI) {
    insertDlg();
   }
   else if(command == deleteMI) {
    deleteDlg();
   }
   else if(command == searchMI) {
    searchDlg(); 
   }
   else if(command == infoMI) {
    infoDlg();
   }
  } 
 }
 
 
 public void stuInfoDlg() {   
     JOptionPane.showConfirmDialog(null, outerPane, "Student Info" ,
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
 }
 
 public void writeFileDlg() {
  ScrCtrl.WriteFile(); 
  JPanel msgP = new JPanel();
  msgP.add(new JLabel("YOUR FILE IS SAVED IN THE CURRENT DIR")); 
  JOptionPane.showMessageDialog(null, msgP, "Save", JOptionPane.YES_OPTION);
 }
 
 public void quitDlg() {
  JPanel msgP = new JPanel();
  msgP.add(new JLabel("Save File?"));
  if(JOptionPane.showConfirmDialog(null, msgP, "Save",
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)== JOptionPane.OK_OPTION) {
      ScrCtrl.WriteFile();   
     }
     System.exit(0);
 }

 //query begin 
 public void queryDlg() {
  queryP = new JPanel();
    
  queryTF = new JTextField(8);
  JPanel infoP = new JPanel();
  infoP.add(new JLabel("Subject"));
  infoP.add(queryTF);
  
  queryBG = new ButtonGroup(); 
  first = new JRadioButton("0<=Score<60");
  first.addActionListener(new RBListener());
  queryBG.add(first);
  second = new JRadioButton("60<=Score<90");
  second.addActionListener(new RBListener());
  queryBG.add(second);
  third = new JRadioButton("90<=Score<100");
  third.addActionListener(new RBListener());
  queryBG.add(third);
  
  JPanel btnP = new JPanel();
  btnP.add(first);
  btnP.add(second);
  btnP.add(third);
  
  queryP.add(infoP);
  queryP.add(btnP);
    
  queryTA = new JTextArea(10,30);
  queryTA.setEditable(false);  
  
  JSplitPane querySP = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                   queryP, new JScrollPane(queryTA));
  
;  
  JOptionPane.showConfirmDialog(null, querySP, "Query" ,
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
 }
 
 public void selectQuery(int flag) {
  String subject = queryTF.getText().trim();
  
  int below = 0, high = 0;
  //clear the contents of the queryTA
  queryTA.setText("");
  switch(flag) {
   case 1:  below = 0;
      high = 60;
      break;
   
   case 2:  below = 60;
      high = 90;
      break;
     
   case 3:  below = 90;
      high = 100;
      break;
   
   default: break;
  }
  
  //select the suject
  if(subject.equals(ScrCtrl.SNameList.get(3))) {
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore1()) >= below &&
     Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore1()) < high)
     QStuInfo.add((StudentInfo)ScrCtrl.StuList.get(i));
    
   }
   displayQuery();
  }
  else if(subject.equals(ScrCtrl.SNameList.get(4))) {
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore2()) >= below &&
     Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore2()) < high)
     QStuInfo.add((StudentInfo)ScrCtrl.StuList.get(i));
   }
   displayQuery();
  }
  else if(subject.equals(ScrCtrl.SNameList.get(5))) {
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore3()) >= below &&
     Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore3()) < high)
     QStuInfo.add((StudentInfo)ScrCtrl.StuList.get(i));
   }
   displayQuery();
  }
  else if(subject.equals(ScrCtrl.SNameList.get(6))) {
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore4()) >= below &&
     Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore4()) < high)
     QStuInfo.add((StudentInfo)ScrCtrl.StuList.get(i));
   }
   displayQuery();
  }
  else if(subject.equals(ScrCtrl.SNameList.get(7))) {
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore5()) >= below &&
     Integer.parseInt(((StudentInfo)ScrCtrl.StuList.get(i)).getScore5()) < high)
     QStuInfo.add((StudentInfo)ScrCtrl.StuList.get(i));
   }
   displayQuery();
  }
  else {
   queryTA.setText("The subject do not exist"); 
  } 
 }
 
 public void displayQuery() {  
  if(QStuInfo.size() == 0) {
   queryTA.setText("No matched Item is found");  
  }
  else {
   for(int i = 0; i < QStuInfo.size(); i++) {
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getName() + "/t");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getNum() + "/t");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getSex() + "/t");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getScore1() + "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getScore2() + "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getScore3() + "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getScore4() + "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getScore5() + "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getTotalScore()+ "  ");
     queryTA.append(((StudentInfo)QStuInfo.get(i)).getAverageScore() + "/r/n");
   }  
  }
  
  //destroy the objects in the QStuInfo array
  QStuInfo.clear(); 
 }
 
 class RBListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   Object command = e.getSource();
   if(command == first) {
    selectQuery(1);
   }
   else if(command == second) {
    selectQuery(2);
   }
   else if(command == third) {
    selectQuery(3);
   }
  } 
 }
 //query end
 
 
 //insert
 public void insertDlg() {
  JPanel insertP = new JPanel();
  
  JTextField nameTF = new JTextField(6);
  JTextField numTF = new JTextField(6);
  JTextField sexTF = new JTextField(6);
  JTextField score1TF = new JTextField(6);
  JTextField score2TF = new JTextField(6);
  JTextField score3TF = new JTextField(6);
  JTextField score4TF = new JTextField(6);
  JTextField score5TF = new JTextField(6);
  
  insertP.setLayout(new GridLayout(4,4));
  insertP.add(new JLabel("Name"));
  insertP.add(nameTF);
  insertP.add(new JLabel("ID"));
  insertP.add(numTF);
  insertP.add(new JLabel("Sex"));
  insertP.add(sexTF);
  insertP.add(new JLabel("Score1"));
  insertP.add(score1TF);
  insertP.add(new JLabel("Score2"));
  insertP.add(score2TF);
  insertP.add(new JLabel("Score3"));
  insertP.add(score3TF);
  insertP.add(new JLabel("Score4"));
  insertP.add(score4TF);
  insertP.add(new JLabel("Score5"));
  insertP.add(score5TF);
  
  
  if(JOptionPane.showConfirmDialog(null, insertP, "Insert",
   JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE)== JOptionPane.OK_OPTION) {
    String name = nameTF.getText().trim();
    String num = numTF.getText().trim();
    String sex = sexTF.getText().trim();
    String score1 = score1TF.getText().trim();
    String score2 = score2TF.getText().trim();
    String score3 = score3TF.getText().trim();
    String score4 = score4TF.getText().trim();
    String score5 = score5TF.getText().trim();
    if((!name.equals("")) && (!num.equals("")) && (!sex.equals("")) &&
     (!score1.equals("")) && (!score2.equals("")) && (!score3.equals("")) &&
       (!score4.equals("")) && (!score5.equals(""))) {
     for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
      if(num.equals(((StudentInfo)ScrCtrl.StuList.get(i)).getNum())) {
       JPanel msgP = new JPanel();
       msgP.add(new JLabel("THE RECORD EXIST"));
       JOptionPane.showMessageDialog(null, msgP, "Message" ,JOptionPane.YES_OPTION);
       return; 
      } 
     }   
     String totalscore = String.valueOf(Integer.parseInt(score1) +
          Integer.parseInt(score2) + Integer.parseInt(score3) +
           Integer.parseInt(score4) + Integer.parseInt(score5));
     String averagescore = String.valueOf(Integer.parseInt(totalscore) / 5);
      
     StudentInfo SInfo = new StudentInfo(name,num,sex,score1,score2,
          score3,score4,score5,totalscore,averagescore);
          
     ScrCtrl.StuList.add(SInfo);     
     ScrCtrl.SortStuInfo();
     //ScrCtrl.WriteFile();
      
     String[] rowCell = {name, num, sex, score1, score2, score3, score4, score5,
          totalscore, averagescore};
     //change JTable
     TModel.addRow(rowCell); 
     //change JList
     LModel.addElement(SInfo);
     
     System.out.println(ScrCtrl.StuList.size());
    }
    else {
     JPanel msgP = new JPanel();
     msgP.add(new JLabel("YOU SHOULD FILL ALL BLANKS"));
     JOptionPane.showMessageDialog(null, msgP, "Message" ,JOptionPane.YES_OPTION); 
    }
  }
 }
 
 //delete
 public void deleteDlg() {
  JPanel delP = new JPanel();
  JTextField delTF = new JTextField(8);
  delP.add(new JLabel("ID"));
  delP.add(delTF);
  if(JOptionPane.showConfirmDialog(null, delP, "Delete",
   JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE)== JOptionPane.OK_OPTION) {
   String num = delTF.getText().trim();
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(num.equals(((StudentInfo)ScrCtrl.StuList.get(i)).getNum())) { 
     Vector SInfo = new Vector();
     StudentInfo temp = (StudentInfo)ScrCtrl.StuList.get(i);
     SInfo.add(temp.getName());
     SInfo.add(temp.getNum());
     SInfo.add(temp.getSex());
     SInfo.add(temp.getScore1());
     SInfo.add(temp.getScore2());
     SInfo.add(temp.getScore3());
     SInfo.add(temp.getScore4());
     SInfo.add(temp.getScore5());
     SInfo.add(temp.getTotalScore());
     SInfo.add(temp.getAverageScore());          
     
     System.out.println(SInfo);
     
     
     for(int j = 0; j < TModel.getRowCount(); j++) {
      if(TModel.getDataVector().get(j).equals(SInfo)) {
       //change JTable
       TModel.removeRow(j);
       //change JList
       LModel.remove(j);
      }
     }           
         
     ScrCtrl.StuList.remove(i);
     //ScrCtrl.WriteFile();
     System.out.println(ScrCtrl.StuList.size());
     return;
    } 
   }
   JPanel msgP = new JPanel();
   msgP.add(new JLabel("THE RECORD DOES NOT EXIST"));
   JOptionPane.showMessageDialog(null, msgP, "Message" ,JOptionPane.YES_OPTION);
  }
 }
 
 //search
 public void searchDlg() {
  JPanel schP = new JPanel();
  JTextField schTF = new JTextField(8);
  schP.add(new JLabel("ID"));
  schP.add(schTF);
  if(JOptionPane.showConfirmDialog(null, schP, "Search",
   JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE)== JOptionPane.OK_OPTION) {
   String num = schTF.getText().trim();
   for(int i = 0; i < ScrCtrl.StuList.size(); i++) {
    if(num.equals(((StudentInfo)ScrCtrl.StuList.get(i)).getNum())) { 
     StudentInfo temp = (StudentInfo)ScrCtrl.StuList.get(i);
     JPanel msgP = new JPanel();
     msgP.add(new JLabel(temp.getName() + "  " + temp.getNum() + "   " +
        temp.getSex() + "   " + temp.getScore1() + "   " +
        temp.getScore2() + "   " + temp.getScore3() + "   " +
        temp.getScore4() + "   " + temp.getScore5() + "   " +
        temp.getTotalScore() + "   " + temp.getAverageScore()
       ));
     JOptionPane.showMessageDialog(null, msgP, "Message" ,JOptionPane.YES_OPTION);
     return;
    }
   }
   JPanel msgP = new JPanel();
   msgP.add(new JLabel("THE RECORD DOES NOT EXIST"));
   JOptionPane.showMessageDialog(null, msgP, "Message" ,JOptionPane.YES_OPTION);
  }
 }
 
 //demo info
 public void infoDlg() {
  JPanel msgP = new JPanel();
  msgP.setLayout(new GridLayout(4,1));
  msgP.add(new JLabel("This is a ScoreManage Demo"));
  msgP.add(new JLabel("Author : pluto"));
  msgP.add(new JLabel("Date : 2005.1"));
  msgP.add(new JLabel("Site : BJTU 15#")); 
  JOptionPane.showMessageDialog(null, msgP, "Info", JOptionPane.YES_OPTION);
 }
 
 public static void main(String[] args) { 
  new ManageGUI(); 
 }
}

StudentInfo.java


package ScoreManage;

/**

 This class encapsulates the informations of students
 
*/
class StudentInfo {
 //static info
 String name, num, sex;
 //score info
 String score1, score2, score3, score4, score5, totalscore, averagescore;
 
 public StudentInfo(String name ,String num ,String sex,
    String score1, String score2, String score3, String score4,
     String score5, String totalscore, String averagescore) {
  this.name = name;
  this.num = num;
  this.sex = sex; 
  
  this.score1 = score1;
  this.score2 = score2;
  this.score3 = score3;
  this.score4 = score4;
  this.score5 = score5;
  this.totalscore = totalscore;
  this.averagescore = averagescore;
 }
 
 public String getName() {
  return name; 
 }
 
 public String getNum() {
  return num;
 }
 
 public String getSex() {
  return sex; 
 }
 
 public String getScore1() {
  return score1; 
 }
 
 public String getScore2() {
  return score2; 
 }
 
 public String getScore3() {
  return score3; 
 }
 
 public String getScore4() {
  return score4; 
 }
 
 public String getScore5() {
  return score5; 
 }
 
 public String getTotalScore() {
  return totalscore; 
 }
 
 public String getAverageScore() {
  return averagescore; 
 }
 
 //The default stuinfoTA of the object
 public String toString() {
  return name; 
 }
}

ScoreControl.java


package ScoreManage;

import java.io.*;
import java.util.*;

/**

 This class is the main class that interacts with other classes

*/
public class ScoreControl { 
 public ArrayList SNameList = new ArrayList(); 
 public ArrayList StuList = new ArrayList();
 
 public ScoreControl() {
  ReadFile(); 
  SortStuInfo();
  //WriteFile();
 }
 
 public void ReadFile() {    
  File stuf = null;
  FileInputStream fis = null;
  BufferedReader br = null;
  StudentInfo SInfo;
  String stu, name, num, sex;
  String score1, score2, score3, score4, score5, totalscore, averagescore;
  StringTokenizer temp;
  
  stuf = new File("Student Info/StudentInfo.txt");
  try {
   fis = new FileInputStream(stuf);
  }catch(Exception efis) {
   System.out.println("FileInputStream Error");
   efis.printStackTrace(); 
  }
  br = new BufferedReader(new InputStreamReader(fis));
  
  try {
   //read the subjects name from the file
   stu = br.readLine();
   temp = new StringTokenizer(stu);
   while(temp.hasMoreTokens()) {
     SNameList.add(temp.nextToken()); 
   }
   SNameList.add("Total");
   SNameList.add("Average");  
   //read the students' infos from the file
   while(true) {  
    if((stu = br.readLine()) == null) {
     break;
    }
    else {
     temp = new StringTokenizer(stu);
     
     //static infos
     name = temp.nextToken();    
     num = temp.nextToken(); 
     sex = temp.nextToken();
     
     //score infos
     score1 = temp.nextToken();
     score2 = temp.nextToken();
     score3 = temp.nextToken();
     score4 = temp.nextToken();
     score5 = temp.nextToken();
     totalscore = String.valueOf(Integer.parseInt(score1) +
         Integer.parseInt(score2) + Integer.parseInt(score3) +
          Integer.parseInt(score4) + Integer.parseInt(score5));
     averagescore = String.valueOf(Integer.parseInt(totalscore) / 5);
     
     
     //create StudentInfo objects and put them in the SInfo array
     SInfo = new StudentInfo(name,num,sex,score1,score2,
         score3,score4,score5,totalscore,averagescore);
         
     StuList.add(SInfo);
    }
   }
   //close the current
   fis.close();
   br.close();
  }catch(Exception ebr) {
   System.out.println("BufferedReader Error");
   ebr.printStackTrace(); 
  }
 }
 
 //sort the students by the total score
 public void SortStuInfo() {  
  Object tempSInfo;
  for(int i = 0; i < StuList.size() - 1; i++) {
   for(int j = i + 1; j < StuList.size(); j++) {
    if(Integer.parseInt(((StudentInfo)StuList.get(j)).getTotalScore()) >
      Integer.parseInt(((StudentInfo)StuList.get(i)).getTotalScore())) {
     tempSInfo = StuList.get(j);
     StuList.set(j, StuList.get(i));
     StuList.set(i, tempSInfo);
    }
   } 
  }  
 }
 
 
 //write the sorted students' infos into a new file
 public void WriteFile() {
  File stuf = null;
  FileOutputStream fos = null;
  PrintWriter pw = null;
  
  stuf = new File("Student Info/SInfo.txt");
  try {
   fos = new FileOutputStream(stuf);
  }catch(IOException efos) {
   System.out.println("FileOutputStream Error");
   efos.printStackTrace(); 
  }
  pw = new PrintWriter(fos);
  
  try {
   //write the first line into the new file
   for(int i = 0; i < SNameList.size(); i++) {
    pw.write((String)SNameList.get(i) + "     ");
    pw.flush();
   }
   pw.write("/r/n");
   pw.flush();
  
   //write the students' infos into the new file
   for(int j = 0; j < StuList.size(); j++) {  
    pw.write(((StudentInfo)StuList.get(j)).getName() + "/t");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getNum() + "/t");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getSex() + "/t");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getScore1() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getScore2() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getScore3() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getScore4() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getScore5() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getTotalScore() + "    ");
    pw.flush();
    pw.write(((StudentInfo)StuList.get(j)).getAverageScore() + "/r/n");
    pw.flush();
   }
   
   fos.close();
   pw.close();    
  }catch(Exception epw) {
   System.out.println("PrintWriter Error");
   epw.printStackTrace(); 
  } 
 }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值