课程设计-学生信息管理系统

一、小组成员介绍

小组成员 介绍 主要工作
王柏鸿 组长 gui设计,登录功能,文件读写
张洸洋 组员 重要类的编写,代码规范,团队博客编写

二、Git项目

Git项目地址

三、项目架构、主要流程以及UML类图

项目架构:

主要流程:

UML类图:

四、程序演示

打开.jar程序并登录

导入信息文件.txt

查找学生信息

增加学生信息

删除学生信息

修改学生信息

导出学生信息为.txt文件

五、重要代码展示

学生类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

/**
 *
 * @author 86159
 */


public class Student {                                                         //学生类,用于存储学生信息
private String id;                                                              //学号
private String name;                                                            //姓名
private String birthDay;                                                        //生日
private String gender;                                                          //性别
private String polOutlook;                                                      //政治面貌
private String address;                                                         //家庭住址
private String tel;                                                             //电话
private String dor;                                                             //宿舍号

    public Student(String id, String name, String gender,String birthDay,  String polOutlook, String address, String tel, String dor) {//构造函数
        this.id = id;
        this.name = name;
        this.birthDay = birthDay;
        this.gender = gender;
        this.polOutlook = polOutlook;
        this.address = address;
        this.tel = tel;
        this.dor = dor;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPolOutlook() {
        return polOutlook;
    }

    public void setPolOutlook(String polOutlook) {
        this.polOutlook = polOutlook;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getDor() {
        return dor;
    }

    public void setDor(String dor) {
        this.dor = dor;
    }

    @Override
    public String toString() {
        return id+"  "+name+"  "+gender+"  "+birthDay+"  "+polOutlook+"  "+address+"  "+tel+"  "+dor;
        
    }

    public Boolean IsLegal(){                                                  //判断学生数据是否合法,所有数据均不能为空
        
        if(id==null||name==null||gender==null||birthDay==null||polOutlook==null||address==null||tel==null||dor==null){
            return false;
        }
        
        if(id.length()>0&&name.length()>0&&gender.length()>0&&birthDay.length()>0&&polOutlook.length()>0&&address.length()>0&&tel.length()>0&&dor.length()>0){
            return true;
        }else
        {
            return false;
        }
    }
  
    

    
}

学生列表类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import java.util.ArrayList;

/**
 *
 * @author 86159
 */
public class StudentList {                                        //学生列表类,用于存储学生列表,并对学生列表进行操作

    private ArrayList<Student> students;                            //存储Student的ArrayList

    public StudentList(ArrayList<Student> students) {            //构造函数
        this.students = students;
    }

    public ArrayList<Student> getStudents() {
        return students;
    }

    StudentList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Boolean add(Student student) {                       //在列表里添加一个学生,如果添加学生的学号已经存在,则取消添加并返回false,添加成功则返回true
        if(SearchID(student.getId())!=-1){
            return false;
        }
        students.add(student);
        return true;
    }
    
    public Boolean add(String id, String name, String gender, String birthDay,  String polOutlook, String address, String tel, String dor) {//在列表里添加一个学生,如果添加学生的学号已经存在,则取消添加并返回false,添加成功则返回true
        Student student=new Student(id,name, birthDay, gender, polOutlook, address, tel, dor);
        return students.add(student);
    }
    
    public Boolean remove(Student student){                        //在列表中移除一个学生
        return students.remove(student);
    }
     
    public Boolean delete(int[] index){                           //删除下标数组的所有学生
        for(int i=index.length-1;i>=0;i--){
            students.remove(index[i]);
        }
       
        return true;
    }
    
    
    public int SearchName(String Name){                          //按姓名在列表中搜索学生,返回学生对应的下标,没找到返回-1
        int index = -1;
        
        for(Student student:students){
            if(student.getName().equals(Name)){
                index = students.indexOf(student);
            }
        }
     
        return index;
    }
    
    public int SearchID(String Id){                             //按学号在列表中搜索学生,返回学生对应的下标,没找到返回-1
        int index = -1;
        
        for(Student student:students){
            if(student.getId().equals(Id)){
                index = students.indexOf(student);
            } 
        }
     
        return index;
    }

    @Override
    public String toString() {                                //toString输出学生列表,一行一个学生信息
        String outPut = "";
        for(Student stu:students){
            outPut=outPut+stu.toString()+"\n";
        }
        
        return outPut;
    }

    
    public Student getStudent(int index) {                    //按下标返回一个Student
        return students.get(index);
    }
    
    
     public Student changeStudent(int index,Student stu){       //将学生列表内对应下标的学生改为传进的Student
        return students.set(index, stu);
    }
     
}

GUI主界面类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import Service.Filetrans;
import Service.Student;
import Service.StudentList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;



/**
 *
 * @author 86159
 */
public class stumesJFrame extends javax.swing.JFrame {

    /**
     * Creates new form stumesJFrame
     */
     static ArrayList<Student> students = new ArrayList<>();//定义一个存放学生的数组
     static StudentList studentList = new StudentList(students);//将这个数组放到StudentList类中
    public stumesJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtFileChooser = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        importButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        outputButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setName(""); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "学号", "姓名", "性别", "出生日期", "政治面貌", "家庭住址", "电话", "宿舍号"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setResizable(false);
            jTable1.getColumnModel().getColumn(0).setPreferredWidth(100);
            jTable1.getColumnModel().getColumn(1).setResizable(false);
            jTable1.getColumnModel().getColumn(2).setResizable(false);
            jTable1.getColumnModel().getColumn(2).setPreferredWidth(50);
            jTable1.getColumnModel().getColumn(3).setResizable(false);
            jTable1.getColumnModel().getColumn(4).setResizable(false);
            jTable1.getColumnModel().getColumn(5).setResizable(false);
            jTable1.getColumnModel().getColumn(5).setPreferredWidth(50);
            jTable1.getColumnModel().getColumn(6).setResizable(false);
            jTable1.getColumnModel().getColumn(6).setPreferredWidth(100);
            jTable1.getColumnModel().getColumn(7).setResizable(false);
            jTable1.getColumnModel().getColumn(7).setPreferredWidth(50);
        }

        importButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        importButton.setText("导入学生信息");
        importButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importButtonActionPerformed(evt);
            }
        });

        searchButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        searchButton.setText("查找");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        addButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        addButton.setText("增加");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        deleteButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        deleteButton.setText("删除");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        modifyButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        modifyButton.setText("修改");
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });

        outputButton.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        outputButton.setText("导出学生信息");
        outputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 868, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(importButton, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(outputButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(59, 59, 59)
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(modifyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(importButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(outputButton, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(modifyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

      int[] selectedRows =jTable1.getSelectedRows();//用数组来存放选中的一行或多行
      DefaultTableModel model =(DefaultTableModel)jTable1.getModel();
      for(int i=selectedRows.length;i>0;i--){
          model.removeRow(jTable1.getSelectedRow());//移除表格中选中的行(外部)
      }
        studentList.delete(selectedRows);//在studentList中删除选中的行(内部)
    }                                            

    private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Txt&CSV","txt","csv");
        txtFileChooser.setFileFilter(filter);//文件选择框
        int val =txtFileChooser.showOpenDialog(this);
        if(val==txtFileChooser.APPROVE_OPTION){
            File selectedFile =txtFileChooser.getSelectedFile();//selectedFile为选中的文件
            Filetrans  txtfile =new Filetrans(selectedFile);//将选中的文件存到Filetrans类中
            students=txtfile.read();//调用Filetrans类中的read方法,得到一个学生列表students
            studentList = new StudentList(students);//将得到的students保存在StudentList类中
            DefaultTableModel model =(DefaultTableModel)jTable1.getModel();
            for (Student e : students) {
                //获取每个学生的信息
                String id =e.getId();
                String name=e.getName();
                String birthday=e.getBirthDay();
                String gender=e.getGender();
                String PolOutlook=e.getPolOutlook();
                String address =e.getAddress();
                String tel =e.getTel();
                String dor =e.getDor();
                Object[] o ={id,name,gender,birthday,PolOutlook,address,tel,dor};
                model.addRow(o);//添加一行,该行存放一个学生的信息
            }
        }
        
    }                                            

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        AddDialog  addDialog = new AddDialog(this, true);
        addDialog.setVisible(true);//打开addDialog界面
        DefaultTableModel model =(DefaultTableModel)jTable1.getModel();
        if(studentList.SearchID(addDialog.stu.getId())==-1&&addDialog.stu.IsLegal()){//如果要添加的学号原本并不存在,且要添加的学生信息合法
            stumesJFrame.studentList.add(addDialog.stu);//在studentList中添加该学生(内部)
            Object[] o ={addDialog.stu.getId(),addDialog.stu.getName(),addDialog.stu.getGender(),addDialog.stu.getBirthDay(),addDialog.stu.getPolOutlook(),addDialog.stu.getAddress(),addDialog.stu.getTel(),addDialog.stu.getDor()};
            model.addRow(o);//在表格中添加一行该学生的信息(外部)
        }  
    }                                         

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
       SearchDialog searchDialog = new SearchDialog(this,true);
       searchDialog.setVisible(true);//打开searchDialog界面
       
    }                                            

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
      
        ModifyDialog modifyDialog = new ModifyDialog(this, true);
        modifyDialog.setVisible(true);//打开修改界面
        
        if(ModifyDialog.newstu.IsLegal()){//如果要修改的学生信息填写规范(合法)
            //将ModifyDialong得到的一个修改完的新的学生newstu的信息,在表格中的相应位置进行更改(外部)
            DefaultTableModel model =(DefaultTableModel)jTable1.getModel();
            model.setValueAt(ModifyDialog.newstu.getName(), ModifyDialog.index, 1);
            model.setValueAt(ModifyDialog.newstu.getGender(), ModifyDialog.index, 2);
            model.setValueAt(ModifyDialog.newstu.getBirthDay(), ModifyDialog.index, 3);
            model.setValueAt(ModifyDialog.newstu.getPolOutlook(), ModifyDialog.index,4);
            model.setValueAt(ModifyDialog.newstu.getAddress(), ModifyDialog.index, 5);
            model.setValueAt(ModifyDialog.newstu.getTel(), ModifyDialog.index, 6);
            model.setValueAt(ModifyDialog.newstu.getDor(), ModifyDialog.index, 7);
        }
       
        
    }                                            

    private void outputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        File file = new File("学生信息.txt");//新建一个文件
        
        try{
            FileWriter fw = new FileWriter(file);
            for (Student e : studentList.getStudents()) {
                fw.write(e.toString()+'\n');//逐行写入每个学生的数据
                fw.flush();
            }
             fw.close();
           
        }catch(IOException e){
            e.printStackTrace();
        }
        
       JOptionPane.showMessageDialog(null, "导出成功", "",JOptionPane.INFORMATION_MESSAGE); 
    }                                            

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(stumesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(stumesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(stumesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(stumesJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new stumesJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton importButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton modifyButton;
    private javax.swing.JButton outputButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JFileChooser txtFileChooser;
    // End of variables declaration                   
}

六、总结

在本次课程设计过程中,我们小组完成了设计书上的基本功能,但是,仍有许多可以改进的地方,比如登录系统过于简单,没有注册的功能。以后可以增加一个注册的方法,将用户的账号密码保存到文件里,登录时再调用文件,完成登录。还有在进行修改时,原本想设计成,输入学号,可以先将原本的学生的数据在修改框中预设好,而不是全部重新输入数据。以后深入学习GUI一些模块的功能得以实现该功能。

还可以增加筛选功能,例如在表格性别单元格中,选择男,则表格只显示男生的信息。换成用数据库来存储学生数据。希望能在以后的学习里,不断完善自己的能力,做出更优秀的程序。

posted @ 2021-01-27 19:55  ZGGYY  阅读(122)  评论(0编辑  收藏  举报