📄 itemlibrary.java
字号:
/**
* IteMKubrart.java
* create by ZZ, 2007.12.16
*/
package olts.application;
import java.util.*;
import java.sql.*;
import olts.persistent.*;
import olts.database.DatabaseUtil;
import olts.exception.*;
/**
* 该类就是试题库,用于管理试题
* @author ZZ
* @version 1.0
*
*/
public class ItemLibrary {
private ItemMapper mapper;
private List cache;
private ItemFactory factory;
private Random rd = new Random(47);
public ItemLibrary(ItemMapper im,ItemFactory f)
throws SQLException,ItemCreateException{
this.mapper = im;
this.factory = f;
this.init();
}
private void init()throws SQLException{
this.cache = new LinkedList();
List buf = this.mapper.getAllItems();
for (int i = 0; i < buf.size(); i++){
Item item = (Item)buf.get(i);
this.cache.add(item);
}//得到所有的试题~
}
/**
* 得到题库里的所有试题,
* 得到的是试题库里所有试题的一份复制
*/
public List getAllItems()throws ItemCreateException, SQLException{
this.init();//继续偷懒
List temp = new LinkedList();
for (int i = 0; i < this.cache.size(); i++){
Item item = this.factory.makeConcreteItem((Item)this.cache.get(i));
temp.add(item);
}
return temp;
}
/**
* 得到某一难度上的所有试题的一份复制
* @param difficulty 试题的难度
*/
public List getItemByDifficulty(int difficulty)throws ItemCreateException{
List temp = new LinkedList();
for (int i = 0; i < this.cache.size(); i++){
Item item = (Item)this.cache.get(i);
if (item.getDifficulty() == difficulty){
item = this.factory.makeConcreteItem(item);
temp.add(item);
}
}
return temp;
}
/**
* 按照试题的标识取得一个试题
*/
public Item getItemById(int id)throws ItemCreateException{
for (int i = 0; i < this.cache.size(); i++){
Item item = (Item)this.cache.get(i);
if (item.getId() == id){
return this.factory.makeConcreteItem(item);
}
}
return null;
}
/**
* 随机的抽取一条试题
*/
public Item getItemRandom()throws ItemCreateException{
int i = this.cache.size();
if (i == 0) return null;
while (i >= this.cache.size()){
i = rd.nextInt(this.cache.size());
}
Item item = (Item) this.cache.get(i);
return this.factory.makeConcreteItem(item);
}
/**
* 往试题库里添加一条试题
*/
public void addItem(int id, int diff, int time, int score, String con, String ans,
String type)throws ItemCreateException,SQLException,PersistItemException{
Item item = this.factory.makeConcreteItem(id, diff, time, score, con, ans, type);
this.mapper.persistItem(item);
this.cache.add(item);//加入到缓存中去
}
public void addItem(Item i)throws ItemCreateException,SQLException,PersistItemException{
Item item = this.factory.makeConcreteItem(i);
this.mapper.persistItem(item);
this.cache.add(item);
}
/**
* 往试题库里更新一条试题的信息
*/
public void updateItem(int oldId, int id, int diff, int time, int score, String con,
String ans, String type)throws ItemCreateException,SQLException,PersistItemException{
Item item = this.factory.makeConcreteItem(id, diff, time, score, con, ans, type);
this.mapper.updateItem(oldId, item);
this.init();//偷个小懒
}
public void updateItem(Item oldItem, Item newItem)throws ItemCreateException,SQLException,PersistItemException{
Item item = this.factory.makeConcreteItem(newItem);
this.mapper.updateItem(oldItem.getId(), item);
this.init();//偷个小懒
}
/**
* 删除一条试题信息
* @param id 要删除的试题
*/
public void removeItem(int id)throws SQLException{
this.mapper.removeItem(id);
this.init();//偷个小懒
}
/**
*得到试题库里试题的所有类型信息
*/
public Object[] getAllItemTypes(){
return this.factory.getItemTypes();
}
public static void main(String[] args) {
// 测试之~
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};";
url += "DBQ=./db.mdb";
DatabaseUtil du = null;
ItemMapper im = null;
ItemLibrary ilib = null;
ItemFactory ifc = null;
try{
//测试连接
du = new DatabaseUtil(driver,url,"","");
im = new ItemMapper(du);
ifc = new OltsItemFactory();
ilib = new ItemLibrary(im, ifc);
List l = ilib.getAllItems();
for(int i =0; i < l.size(); i++){
Item temp =(Item)l.get(i);
System.out.println(temp);
}
System.out.println(ilib.getItemRandom());
int id = 18848;
int diff = 1;
String con = "1";
String ans = "1";
int score = 1;
int time = 1;
String type = "TrueFalseItem";
int oid = 3;
//测试插入命令
ilib.addItem(id, diff, time, score, con, ans, type);
//测试修改命令
//ilib.updateItem(3, id, diff, time, score, con, ans, type);
//测试删除命令
//ilib.removeItem(2);
//im.persistItem(item);
//im.removeItem(item);
}catch(Exception e){
System.out.println(e);
}finally {
if (du != null ){
try{
du.close();
}catch(Exception e){
System.out.println(e);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -