时间:2021-07-01 10:21:17 帮助过:10人阅读
2,管理员界面设计
以管理员身份登录成功后,可以进行信息(实验室地址,实验室容纳量)录入,信息修改,生成课表等操作,效果图如下:
代码:
package 实验室安排系统; import java.awt.*; import java.awt.event.*; import java.sql.SQLException; import javax.swing.*; import 实验室安排系统.MainUI; public class GlyUI extends JFrame implements ActionListener { //定义组件 JButton jb1,jb2,jb3=null; JPanel jp3,jp4=null; JLabel jlb1,jlb3,jlb4,jlb6=null; public static void main(String[] args) { GlyUI ui=new GlyUI(); } public GlyUI() { jp3=new JPanel(); jp3.setBounds(0, 220, 378, 23); getContentPane().setLayout(null); jlb1=new JLabel("\u4F60\u597D\uFF0C\u7BA1\u7406\u5458"); jlb1.setBounds(68, 15, 231, 49); getContentPane().add(jlb1); jb2=new JButton("\u4FE1\u606F\u5F55\u5165"); jb2.setBounds(15, 79, 105, 61); getContentPane().add(jb2); jb2.setForeground(Color.BLUE); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); new GlyInputUI(); } }); //创建组件 jb3=new JButton("信息修改"); jb3.setForeground(Color.BLUE); jb3.setBounds(135, 79, 105, 61); getContentPane().add(jb3); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new glyxg(); } }); jb1=new JButton("\u751F\u6210\u8BFE\u8868"); jb1.setBounds(255, 79, 105, 61); getContentPane().add(jb1); jb1.setForeground(Color.BLUE); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { GlykbUI gl=new GlykbUI(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //生成课表 JOptionPane.showMessageDialog(null,"生成成功","提示消息",JOptionPane.WARNING_MESSAGE); dispose(); new GlyUI(); } }); getContentPane().add(jp3); jlb3=new JLabel("最新公告:"); jlb3.setBounds(68, 180, 90, 21); getContentPane().add(jlb3); jlb3.setForeground(Color.red); jlb4=new JLabel("\u8BF7\u5C3D\u5FEB\u5B8C\u5584\u6559\u5B66\u4FE1\u606F"); jlb4.setBounds(173, 180, 162, 21); getContentPane().add(jlb4); this.setTitle("实验室安排系统"); this.setSize(400,300); this.setLocation(500, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { } }View Code
2.1 管理员进入信息录入界面,输入实验室ID,实验室人数后点提交,将数据长传至数据库,成功后点击返回,返回到管理员界面:
其中,实验室的数据表表结构如下:
代码:
package 实验室安排系统; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.TableColumn; import javax.swing.JPanel; import javax.swing.JPasswordField; import java.awt.BorderLayout; import java.awt.GridLayout; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.lang.model.type.NullType; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class GlyInputUI extends JFrame implements ActionListener { //定义组件 JPanel jp1,jp2,jp4=null; JButton jb1=null,jb2=null; JLabel jlb1,jlb2,jlb3=null; JTextField jtf1,jtf2=null; public static void main(String[] args) { new GlyInputUI(); } public static void addroom(String romid,int romnum){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from administrator"; ResultSet rs = statement.executeQuery(sql); int rowcount=0; while(rs.next()){ rowcount=rs.getInt(1); } String sql1 = "insert into administrator values(?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) con.prepareStatement(sql1); pstmt.setInt(1, rowcount+1); pstmt.setString(2, romid); pstmt.setInt(3, romnum); pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } rs.close(); con.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public GlyInputUI() { //创建组件 jb1=new JButton("提交"); jb1.setBounds(40, 0, 145, 40); jb1.addActionListener(this); jb2=new JButton("返回"); jb2.setBounds(209, 0, 139, 40); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new GlyUI(); } }); jp1=new JPanel(); jp2=new JPanel(); jp4=new JPanel(); jlb1=new JLabel("\u5B9E\u9A8C\u5BA4ID"); jlb1.setBounds(43, 8, 90, 21); jlb2=new JLabel(" \u5B9E\u9A8C\u5BA4\u4EBA\u6570"); jlb2.setBounds(37, 8, 114, 21); jtf1=new JTextField(10); jtf1.setBounds(152, 5, 197, 27); jtf2=new JTextField(10); jtf2.setBounds(155, 5, 195, 27); jp1.setLayout(null); //加入到JPanel中 jp1.add(jlb1); jp1.add(jtf1); jp2.setLayout(null); jp2.add(jlb2); jp2.add(jtf2); jp4.setLayout(null); jp4.add(jb1); //添加按钮 jp4.add(jb2); //加入JFrame中 getContentPane().add(jp1); getContentPane().add(jp2); getContentPane().add(jp4); getContentPane().setLayout(new GridLayout(3,1)); //选择GridLayout布局管理器 this.setTitle("实验室安排系统"); this.setSize(400,200); this.setLocation(400, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出 this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { //事件判断 if(e.getActionCommand()=="提交") { String romid=jtf1.getText(); String snum=jtf2.getText(); if(jtf1.getText().isEmpty()||jtf2.getText().isEmpty()){ JOptionPane.showMessageDialog(null,"无效的输入","提示消息",JOptionPane.WARNING_MESSAGE); } else{ int num=Integer.parseInt(snum); addroom(romid, num); JOptionPane.showMessageDialog(null,"添加成功!","提示消息",JOptionPane.WARNING_MESSAGE); } } } }View Code
2.2管理员修改信息设计
管理员可以修改实验室的容量,输入后点击修改,将更新数据库中的记录信息
代码:
package 实验室安排系统; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; public class glyxg extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { new glyxg(); } /** * Create the frame. */ public glyxg() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u4FEE\u6539\u5B9E\u9A8C\u5BA4\u5BB9\u91CF"); lblNewLabel.setBounds(161, 32, 252, 21); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u8BF7\u8F93\u5165\u8981\u4FEE\u6539\u7684\u5B9E\u9A8C\u5BA4ID:"); lblNewLabel_1.setBounds(40, 69, 246, 21); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(301, 66, 96, 27); contentPane.add(textField); textField.setColumns(10); JLabel lblNewLabel_2 = new JLabel("\u8BF7\u8F93\u5165\u4FEE\u6539\u540E\u7684\u5B9E\u9A8C\u5BA4\u5BB9\u91CF\uFF1A"); lblNewLabel_2.setBounds(40, 133, 234, 21); contentPane.add(lblNewLabel_2); textField_1 = new JTextField(); textField_1.setBounds(301, 130, 96, 27); contentPane.add(textField_1); textField_1.setColumns(10); JButton btnNewButton = new JButton("\u786E\u8BA4\u4FEE\u6539"); btnNewButton.setBounds(40, 186, 163, 29); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String rid=textField.getText(); String ru=textField_1.getText(); int romnum=Integer.parseInt(ru); try { xg(rid, romnum); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } JOptionPane.showMessageDialog(null,"修改成功!","提示消息",JOptionPane.WARNING_MESSAGE); } });; contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u8FD4\u56DE\u4E0A\u4E00\u7EA7"); btnNewButton_1.setBounds(252, 186, 145, 29); btnNewButton_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dispose(); new GlyUI(); } }); contentPane.add(btnNewButton_1); this.setVisible(true); this.setLocation(500, 300); } public static void xg(String s1,int n) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rs=0; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); PreparedStatement stmt=null; String sql="update administrator set roomnum = "+String.valueOf(n)+" where roomid = "+s1; stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }View Code
2.3管理员生成课表算法
排课表算法的大致思路:
1) 同一班级的学生在同一时间不能安排在两个实验室
2) 同一教师在同一时间不能教授两个班级
3) 同一实验室在同一时间不能安排两个班级
4) 某一实验室上课的人数不应大于实验室的容纳量
5) 课程在一周上多次时, 要有一定的间隔性
6) 对同一教师, 同一上课对象应尽量选择相对固定的几个实验室
7) 优先满足一些特殊要求(比如有些老师喜欢上上午的课,可以优先满足,有些老师不喜欢上某个时间片的课,我们尽量避免给他们在这个时间段排课)
因此,我将两周的时间分为20个时间片,以老师的喜好时间为基础来给他们安排合适的时间片来开展教学活动。时间片的排布如下:
我们这样定义教师的数据表:
(注:id为记录在数据表中的位置,与优先级有关,name为教师姓名,classid为教师所授班名称,classnum为班级人数,badtime1为不想上课的时间片1,
badtime2位不想上课的时间片2,count为授课次数是6次)
定义课程表的表结构:
(注:id为记录在数据表中的位置,与优先级有关,name为教师姓名,room为上课地点,bd1为不想上课的时间片1,bd2位不想上课的时间2,t1~t6为教师
上课的时间片)
生成课表算法:要生成A老师的课表,先通过访问数据库获得A老师不想被安排上课的时间片,和A老师要上课的实验室,在A老师的可用时间片里,为A老师
生成6个上课的时间片,并写入到数据库课程表表单中,供老师查询,算法的流程图如下;
代码:
package 实验室安排系统; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.Random; import java.util.Set; import java.util.TreeSet; import javax.swing.JOptionPane; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class GlykbUI { public static void main(String[] args) throws SQLException { new GlykbUI(); } public GlykbUI() throws SQLException{ int ternum=Getternum(); int timetabenum=Gettimetablenum(); for(int i=timetabenum+1;i<=ternum;i++){ String tname=null,roomid=null; int tb1=0,tb2=0; ResultSet rs1=find(i, "teacher"); while(rs1.next()){ tname=rs1.getString(2); tb1=rs1.getInt(5); tb2=rs1.getInt(6); } if(i<=10){ ResultSet rs2=find(i, "administrator"); while(rs2.next()){ roomid=rs2.getString(2); } }else{ ResultSet rs2=find(i-10, "administrator"); while(rs2.next()){ roomid=rs2.getString(2); } } //System.out.println(tname+roomid); Set<Integer> set=getrandom(i,tb1, tb2); int time[]=new int[6]; int j=0; Iterator<Integer> it=set.iterator(); while(it.hasNext()){ time[j++]=it.next(); } //System.out.println(Arrays.toString(time)); addtimetable(i, tname, roomid, tb1, tb2, time); } } public static int Getternum(){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rowcount=0; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from teacher"; ResultSet rs = statement.executeQuery(sql); while(rs.next()){ rowcount=rs.getInt(1); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rowcount; } public static int Gettimetablenum(){ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; int rowcount=0; try { Class.forName(driver); con=(Connection) DriverManager.getConnection(url,user,password); Statement statement = (Statement) con.createStatement(); String sql = "select count(*) from timetable"; ResultSet rs = statement.executeQuery(sql); while(rs.next()){ rowcount=rs.getInt(1); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rowcount; } public static ResultSet find(int i,String s) throws SQLException{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; try { Class.forName(driver); con = (Connection)DriverManager.getConnection(url,user,password); //if(!con.isClosed()) //System.out.println("Succeeded connecting to the Database!"); PreparedStatement stmt=null; String sql="select * from "+s+" where id = "+String.valueOf(i); stmt=(PreparedStatement) con.prepareStatement(sql); rs = stmt.executeQuery(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public static Set<Integer> getrandom(int i,int a,int b) throws SQLException{ Random ran=new Random(i); Set<Integer> set =new TreeSet<>(); if(i<=10){ while(true){ int r1=ran.nextInt(3)+1; if(r1!=a&&r1!=b) set.add(r1); if(set.size()==1) break; } while(true){ int r2=ran.nextInt(3)+4; if(r2!=a&&r2!=b) set.add(r2); if(set.size()==2) break; } while(true){ int r3=ran.nextInt(4)+7; if(r3!=a&&r3!=b) set.add(r3); if(set.size()==3) break; } System.out.println(set); while(true){ int r4=ran.nextInt(3)+11; if(r4!=(a+10)&&r4!=(b+10)) set.add(r4); if(set.size()==4) break; } while(true){ int r5=ran.nextInt(3)+14; if(r5!=(a+10)&&r5!=(b+10)) set.add(r5); if(set.size()==5) break; } while(true){ int r6=ran.nextInt(4)+17; if(r6!=(a+10)&&r6!=(b+10)) set.add(r6); if(set.size()==6) break; } System.out.println(set); }else{ Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/Demo"; String user = "root"; String password = "yfz"; ResultSet rs=null; int t1=0; int t2=0; int t3=0; int t4=0; int t5=0<