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

📄 oltsitemfactory.java

📁 一个带界面的在线测试系统
💻 JAVA
字号:
/**
 * MyItemFactory.java
 * create by ZZ, 2007.12.16
 */
package olts.application;

import java.util.*;
import olts.exception.*;
/**
 * @author ZZ
 * @version 1.0
 *
 */
public class OltsItemFactory implements ItemFactory {
	
	private Map<String, Class> itemTemplate ;
	
	public OltsItemFactory(){
		
		this.itemTemplate = new HashMap<String, Class>();
		
		this.itemTemplate.put("BlankFillItem", BlankFillItem.class);
		this.itemTemplate.put("SingleChoiceItem", SingleChoiceItem.class);
		this.itemTemplate.put("TrueFalseItem", TrueFalseItem.class);
	}
	
	/**
	 * 该方法用于按照产生一个具体的试题,
	 * 该方法抽象了从一个抽象的试题创建具体的试题的过程。
	 * @param i 抽象的试题
	 * @return 具体的试题
	 */
	public Item makeConcreteItem(Item i)throws ItemCreateException{
		try {
			
			Class itemClass = this.itemTemplate.get(i.getType());
			if (itemClass == null ) throw new ItemCreateException();
			Item item = (Item)itemClass.newInstance();
			item.setItem(i);
			return item;

		}catch (Exception e){
			throw new ItemCreateException();
		}
		
	}
	
	public Item makeConcreteItem(int id, int diff, int time, 
			int score, String con, String ans, String type)throws ItemCreateException
	{
		try {
			
			Class itemClass = this.itemTemplate.get(type);
			if (itemClass == null ) throw new ItemCreateException();
			Item item = (Item)itemClass.newInstance();
			
			item.setId(id);
			item.setDifficulty(diff);
			item.setTimeLimit(time);
			item.setScore(score);
			item.setContent(con);
			item.setAnswer(ans);
			
			return item;

		}catch (Exception e){
			throw new ItemCreateException();
		}
	}
	

	/**
	 * 给方法用于得到目前系统中所有题目的类型
	 * @return 表示题目类型的String[]
	 */
	public Object[] getItemTypes(){
		Object[] buf = this.itemTemplate.keySet().toArray();
		return buf;
	}
	
	public static void main(String[] args){
		//测试
		try{
			ItemFactory itemf = new OltsItemFactory();
			Object[] types = itemf.getItemTypes(); 
			for (int i = 0; i < types.length; i++){
				System.out.println(types[i]);
			}
			
		    int id = 2;
		    int diff = 1;
		    String content = "你知道我在等你吗?";
		    String answer = "true";
		    int score = 100;
		    int timelimit = 10;
		    
		    Item item = new TrueFalseItem(id, diff, timelimit,
		    		content, answer, score);
		    System.out.println(itemf.makeConcreteItem(item));
		    System.out.println(itemf.makeConcreteItem
		    		(id, diff, timelimit, score, content, answer, "BlankFillItem"));
		    
		}catch(Exception e){
			System.out.println(e);
		}
	}

}

⌨️ 快捷键说明

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