⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduletest.java

📁 java 北大青鸟 java 北大青鸟
💻 JAVA
字号:
/**  
 * (c) 2005 Aptech Limited.
 * 版权所有
 */

/* 导入所需类. */
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;

/**
 * 这个程序演示 Vector 类的用法.
 * @version 1.0, 2005 年 5 月 20 日
 * @author zl
 */

class Schedule {

/** 声明 Vector 对象以存储不同课程. */
    Vector courses;

/** 声明 Vector 对象以存储不同时隙. */
    Vector times;

/** 声明 Vector 对象以存储不同科目. */
    Vector constraintpair;

/** 声明 Hashtable 对象. */
    Hashtable schedule;

/**
 * 构造方法初始化 Vector 和 Hashtable对象.
 */
    Schedule() {
        courses = new Vector();
        times = new Vector();
        constraintpair = new Vector();
        schedule = new Hashtable();
    }

/**
 * 将不同课程添加到 Vector.
 */
    public void addCourse() {
        courses.addElement("化学 A");
        courses.addElement("物理 A");
        courses.addElement("数学 A");
        courses.addElement("化学 B");
        courses.addElement("物理 B");
        courses.addElement("数学 B");
        courses.addElement("化学 C");
        courses.addElement("物理 C");
        courses.addElement("数学 C");
    }

/**
 * 将不同时间添加到 Vector.
 */
    public void addTime() {
        times.addElement("早上");
        times.addElement("中午");
        times.addElement("晚上");
    }

/**
 * 将对课程的不同约束条件添加到 Vector.
 */
    public void addRestriction() {

        constraintpair.addElement("化学 C / 化学 A");
        constraintpair.addElement("物理 A / 数学 B");
        constraintpair.addElement("化学 A / 物理 C");

        constraintpair.addElement("数学 B / 物理 B");
        constraintpair.addElement("物理 B / 数学 A");
        constraintpair.addElement("数学 A / 物理 A");

        constraintpair.addElement("数学 C / 化学 B");
        constraintpair.addElement("物理 B / 数学 C");
        constraintpair.addElement("化学 C / 物理 C");
    }

/**
 * 检查允许的课程的方法.
 * @param course 包含课程名称
 * @param courseObj 包含课程名称
 * @return boolean 值
 */
    protected boolean allowed(final String course, final String courseObj) {
       return (constraintpair.indexOf(course + "/" + courseObj) < 0);
    }

/**
 * 检查 Vector 的方法.
 * @param course 包含课程名称
 * @param temp 被搜索的值
 * @return boolean 值
 */
    protected boolean allowed(final String course, final Vector temp) {
        for (Enumeration e = temp.elements(); e.hasMoreElements();) {
            String courseObj = (String) (e.nextElement());
            if (!allowed(course , courseObj)) { return false; }
        }
        return true;
    }

/**
 * 安排课程或设置时间表的方法.
 * @param course 包含时隙
 * @param time 包含课程时段
 * @return boolean 值
 */
    boolean scheduleCourse(final int course, final int time) {
        if (time >= times.size()) {
            return false;
        }

        String strTime = (String) (times.elementAt(time));

        if (course >= courses.size()) {
            return true;
        }
        String strCourse = (String) (courses.elementAt(course));

        Vector temp = (Vector) (schedule.get(strTime));
        if (temp == null) {
           temp = new Vector();
           schedule.put(strTime , temp);
        }

       if (allowed(strCourse , temp)) {
           temp.addElement(strCourse);
        } else {
           return false;
        }


       int nextCourse = course + 1;
       int nextTime = (time + 1) % times.size();
       int ctr;
       boolean gotit = false;

       for (ctr = 0; ctr < times.size(); ctr++) {
           gotit = scheduleCourse(nextCourse , nextTime);
           if (gotit) {
              break;
           } else {
              nextTime = (nextTime + 1) % times.size();
           }
       }
       boolean found;
       if (!gotit) {
         temp.removeElement(strCourse);
         found = false;
       } else {
         found = true;
       }
      return found;
}
/**
 * 调用 ScheduleCourse 的方法.
 * @return boolean 值
 */
public boolean myschedule() {
    return scheduleCourse(0 , 0);
}

/**
 * 重写 toSting() 方法.
 * @return String 对象的字符串表示
 */
    public String toString() {
        StringBuffer buf = new StringBuffer();
        for (Enumeration e = times.elements(); e.hasMoreElements();) {
          String t = (String) (e.nextElement());
          Vector temp = (Vector) (schedule.get(t));
          buf.append("时间 ");
          buf.append(t);
          buf.append(":  ");
          if (temp != null && temp.size() > 0) {
             for (Enumeration e2 = temp.elements(); e2.hasMoreElements();) {
                buf.append(e2.nextElement().toString());
                buf.append("   ");
             }
          } else {
             buf.append("----空闲-----");
          }
             buf.append("\n");
       }
      return buf.toString();
    }

}

/**
 * 这个程序演示 Schedule 类的用法.
 * @version 1.0, 2005 年 5 月 20 日
 * @author zl
 */
class ScheduleTest {

/**
  * 构造方法.
  */
    protected ScheduleTest() {
    }

/**
 * 这是 main 方法,所有应用程序的入口点.
 * @param args 传递至 main 方法
 */
    public static void main(String[] args) {
          Schedule scheduleObj = new Schedule();
          scheduleObj.addCourse();
          scheduleObj.addTime();
          scheduleObj.addRestriction();
          boolean okay = scheduleObj.myschedule();
          if (okay) {
               System.out.println("测试有效.........\n" + scheduleObj);
          } else {
               System.out.println("安排失败");
          }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -