WEB开发网
开发学院软件开发Java Java SE 自动排课系统基础实现 阅读

Java SE 自动排课系统基础实现

 2009-09-16 00:00:00 来源:WEB开发网   
核心提示:Java代码//Class.javapackagecom.youngmaster;publicclassClass{privateStringname;//班级名称privateintid;//班级号privateintnumber;//班级人数privateSequencecs;publicClass(){}publ

Java代码

//Class.java 
 
package com.youngmaster; 
public class Class { 
 private String name;//班级名称 
 private int id;//班级号 
 private int number;//班级人数 
 private Sequence cs; 
 public Class(){ 
  
 } 
 public Class(int id,String name,int number){ 
  this.name = name; 
  this.number = number; 
  this.id = id; 
  setCs(new Sequence()); 
 } 
 
public String getName() { 
 return name; 
} 
 
public void setName(String name) { 
 this.name = name; 
} 
 
public int getNumber() { 
 return number; 
} 
public void setNumber(int number) { 
 this.number = number; 
} 
public int getId() { 
 return id; 
} 
public void setId(int id) { 
 this.id = id; 
} 
public Sequence getCs() { 
 return cs; 
} 
public void setCs(Sequence cs) { 
 this.cs = cs; 
} 
 
 
} 
 
 
//ClassDemo.java 
 
package com.youngmaster; 
import java.io.*; 
import java.util.Date; 
import java.util.Calendar; 
import jxl.*; 
import jxl.write.*; 
public class ClassDemo { 
public void print()throws Exception{ 
 WritableWorkbook book = Workbook.createWorkbook(new File("Teaching.xls")); 
  WritableSheet sheet = book.createSheet("第一页",0); 
  Label[] label = new Label[5]; 
  label[0] = new Label(0,0,"星期"); 
  label[1] = new Label(1,0,"日期"); 
  label[2] = new Label(2,0,"剑锋"); 
  label[3] = new Label(3,0,"秋静"); 
  label[4] = new Label(4,0,"秦少游"); 
  for(Label l:label){ 
   sheet.addCell(l); 
  } 
 //输出日期 
  for(int i=1;i<365;i++){ 
   Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.DATE,i-1); 
    double m = cal.get(Calendar.DAY_OF_WEEK); 
    jxl.write.Number number = new jxl.write.Number(0,i,m);  
     
    sheet.addCell(number); 
    Date date = cal.getTime(); 
   jxl.write.DateTime dt = new jxl.write.DateTime(1,i,date); 
    sheet.addCell(dt); 
     
  } 
   
  book.write(); 
  book.close(); 
} 
 
 public static void main(String[] args) { 
 ClassDemo cd = new ClassDemo(); 
 try {cd.print(); 
 } 
 catch(Exception e){ 
 System.out.println(e); 
 } 
 } 
 
 } 
 
 
 
//ClassRoom.java 
 
package com.youngmaster; 
 
public class ClassRoom { 
 private String name;//教室名称 
 private int id;//教室号 
 private int number;//教室容纳人数 
 
  public ClassRoom(){ 
   
  } 
  public ClassRoom(int id,String name,int number){ 
   this.name = name; 
   this.id = id; 
   this.number = number; 
  } 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
 public int getId() { 
 return id; 
 } 
 public void setId(int id) { 
 this.id = id; 
 } 
 public int getNumber() { 
 return number; 
 } 
 public void setNumber(int number) { 
 this.number = number; 
 } 
} 
 
 
//Course.java 
 
package com.youngmaster; 
 
public class Course { 
 private String name;//课程名称 
 private int times;//课时数 
 private int timesWeek; 
 private int id;//课程号 
  public int getId() { 
 return id; 
 } 
 public void setId(int id) { 
 this.id = id; 
 } 
 public int getTimes() { 
 return times; 
 } 
 public void setTimes(int times) { 
 this.times = times; 
 } 
 public Course(){ 
   
  } 
  public Course(int id,String name,int times,int timesWeek){ 
   this.name = name; 
   this.id = id; 
   this.times = times; 
   this.timesWeek = timesWeek; 
  } 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
 public int getTimesWeek() { 
 return timesWeek; 
 } 
 public void setTimesWeek(int timesWeek) { 
 this.timesWeek = timesWeek; 
 } 
 
} 
 
 
//OrderCourse.java 
 
package com.youngmaster; 
 
import java.util.LinkedList; 
import java.util.Random; 
import java.util.TreeSet; 
 
public class OrderCourse { 
 private Teacher t1, t2, t3; 
 
 private Class c1, c2, c3; 
 
 private Course cs1, cs2, cs3; 
 
 private ClassRoom cr1, cr2, cr3; 
 
 private LinkedList<Course> csList; 
 
 private LinkedList<ClassRoom> crList; 
 
 private LinkedList<Teacher> tList; 
 
 private LinkedList<Class> cList; 
 // 已经排成的时间段 
 LinkedList<String> sub1; 
 LinkedList<String> sub2; 
 LinkedList<String> sub3; 
 // 
 LinkedList<String> temList1; 
 LinkedList<String> temList2; 
 
 public OrderCourse() { 
 // 设置老师 
 t1 = new Teacher(1001, "剑锋"); 
 t2 = new Teacher(1002, "秋静"); 
 t3 = new Teacher(1003, "秦少游"); 
 tList = new LinkedList<Teacher>(); 
 tList.add(t1); 
 tList.add(t2); 
 tList.add(t3); 
 // 设置上课班级 
 c1 = new Class(0601, "JAVA0601班", 20); 
 c2 = new Class(0602, "JAVA0602班", 25); 
 c3 = new Class(0603, "JAVA0603班", 19); 
 cList = new LinkedList<Class>(); 
 cList.add(c1); 
 cList.add(c2); 
 cList.add(c3); 
 // 设置课程 
 cs1 = new Course(1001, "javabase", 29, 6); 
 cs2 = new Course(1002, "jsp", 35, 6); 
 cs3 = new Course(1003, "oracle", 51, 6); 
 csList = new LinkedList<Course>(); 
 csList.add(cs1); 
 csList.add(cs2); 
 csList.add(cs3); 
 // 设置上课教室 
 cr1 = new ClassRoom(1001, "实验楼303", 50); 
 cr2 = new ClassRoom(1002, "南方商务大厦", 40); 
 cr3 = new ClassRoom(1003, "先锋公司", 30); 
 crList = new LinkedList<ClassRoom>(); 
 crList.add(cr1); 
 crList.add(cr2); 
 crList.add(cr3); 
 
 } 
 
 public void initShow() { 
 System.out.println("目前排课的资源情况如下:"); 
 System.out.println("教师情况"); 
 for (Teacher t : tList) { 
  System.out.println("教师ID:" + t.getId() + " 教师姓名: " + t.getName()); 
 } 
 System.out.println("教室情况"); 
 for (ClassRoom cr : crList) { 
  System.out.println("教室ID:" + cr.getId() + " 教室名称: " + cr.getName() 
   + "" + cr.getNumber()); 
 } 
 System.out.println("课程情况"); 
 for (Course cs : csList) { 
  System.out.println("课程ID:" + cs.getId() + " 课程名称: " + cs.getName() 
   + " 课时数:" + cs.getTimes() + " 每周课时 " + cs.getTimesWeek()); 
 } 
 System.out.println("班级情况"); 
 for (Class c : cList) { 
  System.out.println("班级ID:" + c.getId() + " 班级名称: " + c.getName() 
   + " 班级人数:" + c.getNumber()); 
 } 
 } 
 
 // 增加排课资源数 
 public void add() { 
 
 } 
 
 // 从LinkedList<String>中随机取出timesWeek个元素组成的LinkedList<String> 
 public LinkedList<String> randList(LinkedList<String> list, int timesWeek) { 
 LinkedList<String> subList = new LinkedList<String>(); 
 Random rand = new Random(); 
 
 int j = 0; 
 while (true) { 
  String m = list.get(rand.nextInt((list.size() - 1))); 
  if (!subList.contains(m)) { 
  subList.add(m); 
  j++; 
  } 
  if (j == timesWeek) { 
  break; 
  } 
 } 
 return subList; 
 } 
 
 // 对随机时间段排序 
 public TreeSet<String> listToTree(LinkedList<String> sub){ 
 TreeSet<String> set =new TreeSet<String>(sub); 
  
 return set; 
 } 
 public TreeSet<String> AllTree(LinkedList<String> sub1,LinkedList<String> sub2, 
  LinkedList<String> sub3){ 
 TreeSet<String> set =new TreeSet<String>(sub1); 
 set.addAll(sub2); 
 set.addAll(sub3); 
 return set; 
 } 
 
 // 排课流程 
 
 public void order(Teacher t){ 
 Course course = null; 
 if(t==t1){ 
  course = cs1; 
 } 
 else if(t==t2){ 
  course = cs2; 
 } 
 else if(t==t3){ 
  course = cs3; 
 } 
 temList1 = (LinkedList<String>) (t.getTs().getList().clone());// temList1存放原教师空闲时间 
 t.getTs().getList().retainAll(c1.getCs().getList());// 求教师与学生时间交集 
 temList2 = t.getTs().getList();// temList2存放教师与学生时间交集 
 sub1 = randList(temList2, course.getTimesWeek());// 取出教师和学生的一定次数的随机组合 
 c1.getCs().getList().removeAll(sub1);// 移去被分去的时间 
 t.getTs().setList(temList1);// 恢复t1中时间 
 t.getTs().getList().removeAll(sub1);// 移去被分去的时间 
 
 temList1 = (LinkedList<String>) (t.getTs().getList().clone());// temList1存放原教师空闲时间 
 t.getTs().getList().retainAll(c2.getCs().getList());// 求交集 
 temList2 = t.getTs().getList();// temList2存放教师与学生时间交集 
 sub2 = randList(temList2, course.getTimesWeek());// 取出教师和学生的一定次数的随机组合 
 c2.getCs().getList().removeAll(sub2);// 移去被分去的时间 
 t.getTs().setList(temList1);// 恢复t1中时间 
 t.getTs().getList().removeAll(sub2);// 移去被分去的时间 
 
 temList1 = (LinkedList<String>) (t.getTs().getList().clone());// temList1存放原教师空闲时间 
 t.getTs().getList().retainAll(c3.getCs().getList());// 求交集 
 temList2 = t.getTs().getList();// temList2存放教师与学生时间交集 
 sub3 = randList(temList2, course.getTimesWeek());// 取出教师和学生的一定次数的随机组合 
 c3.getCs().getList().removeAll(sub3); 
 t.getTs().setList(temList1);// 恢复t1中时间 
 t.getTs().getList().removeAll(sub3);// 移去被分去的时间 
 } 
 // 输入课表方法 
 public void showCourseTable(Teacher t){ 
 Course course = null; 
 if(t==t1){ 
  course = cs1; 
 } 
 else if(t==t2){ 
  course = cs2; 
 } 
 else if(t==t3){ 
  course = cs3; 
 } 
 System.out.println(); 
 System.out.println(t.getName() + "的课表详情排列如下: " + "课程名称: " 
  + course.getName()); 
 System.out.println("班级名称: " + c1.getName() + " 教室名称: " + cr1.getName()); 
 for (String s : listToTree(sub1)) { 
  System.out.println(s); 
 } 
 
 System.out.println("班级名称: " + c2.getName() + " 教室名称: " + cr2.getName()); 
 for (String s : listToTree(sub2)) { 
  System.out.println(s); 
 } 
 
 System.out.println("班级名称: " + c3.getName() + " 教室名称: " + cr3.getName()); 
 for (String s : listToTree(sub3)) { 
  System.out.println(s); 
 } 
  
  
// System.out.println("********************"); 
// for (String s : AllTree(sub1,sub2,sub3)) { 
//  System.out.println(s); 
//  if(sub1.contains(s)){ 
//   
//   } 
//  else if(sub2.contains(s)){ 
//   
//  } 
//  else if(sub3.contains(s)){ 
//   
//  }  
// } 
 } 
 public void control() { 
 // t1排课 
 order(t1); 
 showCourseTable(t1); 
  
 // t2 排课 
 order(t2); 
 showCourseTable(t2); 
 // t3 排课 
 order(t3); 
 showCourseTable(t3); 
  
 } 
} 
 
 
//Sequence.java 
 
package com.youngmaster; 
 
import java.util.LinkedList; 
 
public class Sequence { 
private LinkedList<String> list = new LinkedList<String>(); 
public Sequence(){ 
 list.add("a-星期一 1-2 节"); 
 list.add("b-星期一 3-4 节"); 
 list.add("c-星期一 5-6 节"); 
 list.add("d-星期一 7-8 节"); 
 list.add("e-星期二 1-2 节"); 
 list.add("f-星期二 3-4 节"); 
 list.add("g-星期二 5-6 节"); 
 list.add("h-星期二 7-8 节"); 
 list.add("i-星期三 1-2 节"); 
 list.add("j-星期三 3-4 节"); 
 list.add("k-星期三 5-6 节"); 
 list.add("l-星期三 7-8 节"); 
 list.add("m-星期四 1-2 节"); 
 list.add("n-星期四 3-4 节"); 
 list.add("o-星期四 5-6 节"); 
 list.add("p-星期四 7-8 节"); 
 list.add("q-星期五 1-2 节"); 
 list.add("r-星期五 3-4 节"); 
 list.add("s-星期五 5-6 节"); 
 list.add("t-星期五 7-8 节"); 
} 
public LinkedList<String> getList() { 
 return list; 
} 
public void setList(LinkedList<String> list) { 
 this.list = list; 
} 
 
} 
 
 
//Teacher.java 
 
package com.youngmaster; 
 
public class Teacher { 
 private int id; 
 private String name; 
  private Sequence ts; 
 
 public Sequence getTs() { 
 return ts; 
 } 
 
 public void setTs(Sequence ts) { 
 this.ts = ts; 
 } 
 
 public Teacher() { 
 
 } 
 
 public Teacher(int id,String name) { 
 this.name = name; 
 this.id = id; 
 setTs(new Sequence()); 
 } 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
 
 public int getId() { 
 return id; 
 } 
 
 public void setId(int id) { 
 this.id = id; 
 } 
} 
 
 
//TestDemo.java 
 
package com.youngmaster; 
 
import java.util.Scanner; 
 
public class TestDemo { 
 public static void main(String[] args) { 
 OrderCourse ordercourse = new OrderCourse(); 
 ordercourse.initShow(); 
 System.out.print("增加资源(以上陈列的),请输入y,否则进行排课操作:"); 
 Scanner scanner = new Scanner(System.in); 
 String input = scanner.next(); 
 if (input.equals("y")) { 
  ordercourse.add(); 
 } 
 System.out.println("系统进行排课中..."); 
 ordercourse.control(); 
 
 } 
}

Tags:Java SE 自动

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接