From 46935bdaba73676615ddf2b83c76b848ece6a04f Mon Sep 17 00:00:00 2001 From: cccccyb <995134776@qq.com> Date: Sun, 12 Jun 2022 03:25:34 +0800 Subject: [PATCH] TeacherUI:Almost completed and the test of DateBase didn't happen problem.But the test with ggb will not start. --- .../com/cfive/classroom/teacher/Attend.form | 10 +- .../com/cfive/classroom/teacher/Attend.java | 149 +++++++++++++----- .../com/cfive/classroom/teacher/CheckIn.java | 2 +- .../com/cfive/classroom/teacher/Main.java | 29 ++-- .../cfive/classroom/teacher/SendMessage.java | 4 +- .../com/cfive/classroom/teacher/SignIn.java | 26 +-- .../cfive/classroom/teacher/TeacherTest.java | 32 ++++ 7 files changed, 188 insertions(+), 64 deletions(-) create mode 100644 Teacher/src/test/java/com/cfive/classroom/teacher/TeacherTest.java diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.form b/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.form index 524a958..dd7e6b7 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.form @@ -1,6 +1,6 @@
- + @@ -58,6 +58,14 @@ + + + + + + + + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.java b/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.java index f91d31a..763a3b6 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Attend.java @@ -6,13 +6,18 @@ import com.cfive.classroom.library.database.bean.Attendance; import com.cfive.classroom.library.database.util.DependenciesNotFoundException; import com.cfive.classroom.library.database.util.NoConfigException; import com.cfive.classroom.library.net.TeacherNet; +import com.cfive.classroom.library.net.util.MessageObject; +import com.cfive.classroom.library.net.util.MessageType; +import com.cfive.classroom.library.net.util.ReceiveListener; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; import javax.swing.table.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.SQLException; @@ -20,7 +25,6 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -import java.util.Vector; public class Attend { private static final Attend attend = new Attend(); @@ -31,37 +35,39 @@ public class Attend { private final Object[] t2_columnTitle = {"考勤号", "学号", "姓名", "签到状态"}; private JTable table_already; private JTable table_undo; + private JButton bt_refresh; private TeacherNet teacherNet; private String courseID; private final List attendancesList = new ArrayList<>(); private final List alreadyList = new ArrayList<>(); private final List undoList = new ArrayList<>(); + private DefaultTableModel undoTableModel,alreadyTableModel; private static final Logger LOGGER = LogManager.getLogger(); public Attend() { //学生签到信息监听 -/* - teacherNet.setOnReceiveListener(new ReceiveListener() { - @Override - public void onReceive(MessageObject messageObject) { - //判断该信息类型是否为签到并且签到状态是否为已签 - if (messageObject.getMessageType() == MessageType.CheckIn && messageObject.getAttStatus() == AttStatus.signed) { - try { - //将学生签到状态修改进数据表中 - DatabaseHelper.updateAttendance(messageObject.getStuNo(), messageObject.getAttStatus()); - } catch (NoConfigException e) { - JOptionPane.showMessageDialog(null, "没有数据库配置文件", "警告", JOptionPane.ERROR_MESSAGE); - LOGGER.error("No configuration", e); - } catch (SQLException e) { - JOptionPane.showMessageDialog(null, "数据库出错", "警告", JOptionPane.ERROR_MESSAGE); - LOGGER.error("SQLException", e); - } catch (DependenciesNotFoundException e) { - LOGGER.error("DependenciesNotFoundException", e); + if(teacherNet!=null){ + teacherNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + //判断该信息类型是否为签到并且签到状态是否为已签 + if (messageObject.getMessageType() == MessageType.CheckIn && messageObject.getAttStatus() == AttStatus.signed) { + try { + //将学生签到状态修改进数据表中 + DatabaseHelper.updateAttendance(messageObject.getStuNo(), messageObject.getAttStatus(),messageObject.getLocalDateTime()); + } catch (NoConfigException e) { + JOptionPane.showMessageDialog(null, "没有数据库配置文件", "警告", JOptionPane.ERROR_MESSAGE); + LOGGER.error("No configuration", e); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null, "数据库出错", "警告", JOptionPane.ERROR_MESSAGE); + LOGGER.error("SQLException", e); + } catch (DependenciesNotFoundException e) { + LOGGER.error("DependenciesNotFoundException", e); + } } } - } - }); -*/ + }); + } //未签表格的鼠标点击事件 table_undo.addMouseListener(new MouseAdapter() { @@ -75,13 +81,9 @@ public class Attend { Object[] options = {AttStatus.absence, AttStatus.signed, AttStatus.leave_early, AttStatus.late, AttStatus.personal_leave, AttStatus.public_holiday, AttStatus.sick_leave, AttStatus.not_signed}; AttStatus attStatus = (AttStatus) JOptionPane.showInputDialog(null, "选择您所要修改的签到状态", "提示", JOptionPane.PLAIN_MESSAGE, null, options, options[0]); - LOGGER.debug("attStatus: " + attStatus); - if (attStatus == null) { - LOGGER.debug("test: " + table_undo.getModel().getValueAt(row, col)); - table_undo.getModel().setValueAt(AttStatus.not_signed, row, col); - } - String modifyStat = String.valueOf(attStatus); - if (modifyStat != null) { + + if (attStatus != null) { + String modifyStat = String.valueOf(attStatus); //将修改后的状态显示出来 table_undo.getModel().setValueAt(modifyStat, row, col); //将修改在数据库中更改 @@ -98,14 +100,83 @@ public class Attend { JOptionPane.showMessageDialog(null, "未查询到该数据", "错误", JOptionPane.ERROR_MESSAGE); LOGGER.error("DependenciesNotFoundException", e); } - } else { - JOptionPane.showMessageDialog(null, "您未进行选择", "提示", JOptionPane.INFORMATION_MESSAGE); + bt_refresh.doClick(); } } } }); + //刷新按钮的监听 + bt_refresh.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //清空列表 + ((DefaultTableModel)table_already.getModel()).setRowCount(0); + ((DefaultTableModel)table_undo.getModel()).setRowCount(0); + attendancesList.clear(); + alreadyList.clear(); + undoList.clear(); + //重新加载 + try { + attendancesList.addAll(DatabaseHelper.selectAttendanceByCourse(Long.parseLong(courseID))); //导入该课程考勤名单 + for (Attendance attendance : attendancesList) { + if (attendance.getAttStatus() == AttStatus.signed) { //筛选出已签到的考勤列表 + alreadyList.add(attendance); + } else { + undoList.add(attendance); + } + } + } catch (DependenciesNotFoundException ex) { + throw new RuntimeException(ex); + } catch (NoConfigException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + //已签考勤表格 + alreadyTableModel = new DefaultTableModel(t1_columnTitle, 0) { + @Override + public boolean isCellEditable(int row, int column) { + return false; + } + }; + if (alreadyList != null) { + for (Attendance attendance : alreadyList) { + String stuID = String.valueOf(attendance.getStudent().getStuID()); + String stuName = String.valueOf(attendance.getStudent().getStuName()); + LocalDateTime attTime = attendance.getAttTime(); + String attTime1 = attTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + Object row[] = {stuID, stuName, attTime1}; + alreadyTableModel.addRow(row); + } + } + attend.table_already.setModel(alreadyTableModel); + + //未签考勤表格 + undoTableModel = new DefaultTableModel(t2_columnTitle, 0); + for (Attendance attendance : undoList) { + String attID = attendance.getAttID(); + String stuID = String.valueOf(attendance.getStudent().getStuID()); + String stuName = String.valueOf(attendance.getStudent().getStuName()); + AttStatus attStatus = attendance.getAttStatus(); + Object row[] = {attID, stuID, stuName, String.valueOf(attStatus)}; + undoTableModel.addRow(row); + } + attend.table_undo.setModel(undoTableModel); + //设置第一列隐藏 + TableColumn tableColumn = attend.table_undo.getColumnModel().getColumn(0); + tableColumn.setWidth(0); + tableColumn.setMinWidth(0); + tableColumn.setPreferredWidth(0); + tableColumn.setMaxWidth(0); + attend.table_undo.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0); + attend.table_undo.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0); + attend.table_undo.setCellSelectionEnabled(true); + + } + }); } + public static void main(String[] args) { frame.setContentPane(attend.rootPanel); frame.setSize(600, 400); @@ -143,7 +214,7 @@ public class Attend { throw new RuntimeException(e); } //已签考勤表格 - DefaultTableModel alreadyTableModel = new DefaultTableModel(t1_columnTitle, 0) { + alreadyTableModel = new DefaultTableModel(t1_columnTitle, 0) { @Override public boolean isCellEditable(int row, int column) { return false; @@ -162,9 +233,13 @@ public class Attend { attend.table_already.setModel(alreadyTableModel); //未签考勤表格 - DefaultTableModel undoTableModel = new DefaultTableModel(t2_columnTitle, 0); + undoTableModel = new DefaultTableModel(t2_columnTitle, 0){ + @Override + public boolean isCellEditable(int row, int column) { + return false; + } + }; for (Attendance attendance : undoList) { - LOGGER.debug(attendance.getAttID()); String attID = attendance.getAttID(); String stuID = String.valueOf(attendance.getStudent().getStuID()); String stuName = String.valueOf(attendance.getStudent().getStuName()); @@ -181,10 +256,8 @@ public class Attend { tableColumn.setMaxWidth(0); attend.table_undo.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0); attend.table_undo.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0); - - LOGGER.debug(table_undo.getColumnModel().getColumnCount()); - LOGGER.debug(undoTableModel.getColumnCount()); attend.table_undo.setCellSelectionEnabled(true); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java b/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java index 92221d5..4f2e564 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java @@ -78,7 +78,7 @@ public class CheckIn { number = n1 + n2 + n3 + n4; if (number != null) { LOGGER.info(number); - teacherNet.sendAllMessage(new MessageObject(null, null, number, null, null, null, MessageType.CheckIn)); + teacherNet.sendAllMessage(new MessageObject(null, null, number, null, null, null,null,MessageType.CheckIn)); JOptionPane.showMessageDialog(null, "签到码发布成功", "消息", JOptionPane.INFORMATION_MESSAGE); frame.dispose(); } else { diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java index a6e9400..0f55b00 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java @@ -124,7 +124,7 @@ public class Main { } JOptionPane.showMessageDialog(null, "恭喜以下同学被选中:\n\t\n" + count); //将选人结果群发出去 - teacherNet.sendAllMessage(new MessageObject(null, null, null, null, count, null, MessageType.Select)); + teacherNet.sendAllMessage(new MessageObject(null, null, null, null, count, null, null,MessageType.Select)); } else { JOptionPane.showMessageDialog(null, "学生名单未导入", "错误", JOptionPane.ERROR_MESSAGE); } @@ -152,19 +152,22 @@ public class Main { }); //主界面线程监听 - teacherNet.setOnReceiveListener(new ReceiveListener() { - @Override - public void onReceive(MessageObject messageObject) { - //学生端举手监听 - if (messageObject.getMessageType() == MessageType.RaiseHand) { - JOptionPane.showMessageDialog(null, messageObject.getStuName() + " 举手了", "温馨提示!", JOptionPane.INFORMATION_MESSAGE); + if(teacherNet!=null){ + teacherNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + //学生端举手监听 + if (messageObject.getMessageType() == MessageType.RaiseHand) { + JOptionPane.showMessageDialog(null, messageObject.getStuName() + " 举手了", "温馨提示!", JOptionPane.INFORMATION_MESSAGE); + } + //学生留言监听 + if (messageObject.getMessageType() == MessageType.Chat) { + JOptionPane.showMessageDialog(null, messageObject.getMessage(), "学生 " + messageObject.getStuName() + " 向您留言", JOptionPane.INFORMATION_MESSAGE); + } } - //学生留言监听 - if (messageObject.getMessageType() == MessageType.Chat) { - JOptionPane.showMessageDialog(null, messageObject.getMessage(), "学生 " + messageObject.getStuName() + " 向您留言", JOptionPane.INFORMATION_MESSAGE); - } - } - }); + }); + } + } public static void main(String[] args) { diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java index 87102fe..45b46c8 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java @@ -16,7 +16,7 @@ import java.time.format.DateTimeFormatter; public class SendMessage { private static final SendMessage sendMessage=new SendMessage(); - private static JFrame frame = new JFrame("SendMessage"); + private static JFrame frame = new JFrame("发送消息"); private JPanel rootPanel; private JTextArea messageInput; private JButton bt_sendMessage; @@ -48,7 +48,7 @@ public class SendMessage { if(messageToAll!=null){ LocalDateTime sendTime = LocalDateTime.now(); messageShow.append("@所有人: "+sendTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"))+'\n'+messageToAll+'\n'); - MessageObject messageObject = new MessageObject(null,null,null,"@所有人:"+messageToAll,null,null,MessageType.ChatToAll); + MessageObject messageObject = new MessageObject(null,null,null,"@所有人:"+messageToAll,null,null,null,MessageType.ChatToAll); teacherNet.sendAllMessage(messageObject); messageInput.setText(""); }else { diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java index 2358ab2..bcb6db1 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.Logger; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.net.NoRouteToHostException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.sql.SQLException; @@ -25,6 +26,7 @@ public class SignIn { private JPasswordField password_input; private String workerNo,password; private static final Logger LOGGER = LogManager.getLogger(); + private static boolean isConnectedToDatabase = false; public static void main(String[] args) { FlatLightLaf.setup(); @@ -42,26 +44,32 @@ public class SignIn { }else{ //根据输入的工号和密码利用加盐位进行判断 try { - if(DatabaseHelper.checkPasswdInTeacher(Long.valueOf(sigIn.workerNo), sigIn.password)){ + if (DatabaseHelper.checkPasswdInTeacher(Long.valueOf(sigIn.workerNo), sigIn.password)) { ClassList classList = new ClassList(); classList.start(sigIn.workerNo); //将工号传参到下一个界面 frame.setVisible(false); - }else { - JOptionPane.showMessageDialog(null,"密码错误,请重新输入","错误!!",JOptionPane.ERROR_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "密码错误,请重新输入", "错误!!", JOptionPane.ERROR_MESSAGE); } + isConnectedToDatabase = true; } catch (NoConfigException ex) { - JOptionPane.showMessageDialog(null,"没有数据库配置文件","警告",JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "没有数据库配置文件", "警告", JOptionPane.ERROR_MESSAGE); LOGGER.error("No configuration", e); } catch (SQLException ex) { - JOptionPane.showMessageDialog(null,"数据库出错","警告",JOptionPane.ERROR_MESSAGE); - LOGGER.error("SQLException",e); + JOptionPane.showMessageDialog(null, "数据库出错", "警告", JOptionPane.ERROR_MESSAGE); + LOGGER.error("SQLException", e); } catch (DependenciesNotFoundException ex) { - JOptionPane.showMessageDialog(null,"未查询到该数据","错误",JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "未查询到该数据", "错误", JOptionPane.ERROR_MESSAGE); LOGGER.error("DependenciesNotFoundException", e); } catch (NoSuchAlgorithmException ex) { - LOGGER.error("NoSuchAlgorithmException",e); + LOGGER.error("NoSuchAlgorithmException", e); } catch (InvalidKeySpecException ex) { - LOGGER.error("InvalidKeySpecException",e); + LOGGER.error("InvalidKeySpecException", e); + } finally { + if (!isConnectedToDatabase) { + JOptionPane.showMessageDialog(null, "无法连接到数据库", "错误", JOptionPane.ERROR_MESSAGE); + } + isConnectedToDatabase = false; } } diff --git a/Teacher/src/test/java/com/cfive/classroom/teacher/TeacherTest.java b/Teacher/src/test/java/com/cfive/classroom/teacher/TeacherTest.java new file mode 100644 index 0000000..58c7824 --- /dev/null +++ b/Teacher/src/test/java/com/cfive/classroom/teacher/TeacherTest.java @@ -0,0 +1,32 @@ +package com.cfive.classroom.teacher; + +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.util.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.NoConfigException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; + +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.sql.SQLException; + +public class TeacherTest { + private static final Logger LOGGER = LogManager.getLogger(); + @Test + void modifyPw(){ + try { + DatabaseHelper.changePasswdInTeacher(Long.valueOf("1002"),"10021002"); + } catch (NoConfigException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (DependenciesNotFoundException e) { + throw new RuntimeException(e); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } catch (InvalidKeySpecException e) { + throw new RuntimeException(e); + } + } +}