📄 gradevisitor.java
字号:
/* * GradeVisitor.java * * Created on 2003年11月6日, 下午12:29 */package romulus;import java.sql.*;import java.util.*;/** * The class for grade the score of a test. * Such visitor has two tasks. * The first is adding the correct answer and feedback. * And the other is set the score of the test. * @author Romulus * @version 1.0 */public class GradeVisitor implements Visitor { private Connection con = null; /** Creates new GradeVisitor */ public GradeVisitor(Connection con) { this.con = con; } /** Visit the Content. */ public void VisitContent(Content cont) throws SQLException, RomulusException{ //content will not be graded but should be added to the initial test (feedback will use it) //task 1 //prepare the basic structure int c_type = 0; //prepare the select sql int c_s_ident = RomulusToolSet.getSIdent(con, "Content", cont.getIdent()); int c_i_s_ident = 0; String type_sql = "select con_i_s_ident, con_type from contentlist where con_s_ident =?"; String textcon_sql = "select ident, text from textcontent where s_ident =?"; //content item type PreparedStatement pre = con.prepareStatement(type_sql); pre.setInt(1, c_s_ident); ResultSet res = pre.executeQuery(); while(res.next()){ c_i_s_ident = res.getInt(1); c_type = res.getInt(2); if(c_type == ContentItem.Int_Type_TextContent || c_type == ContentItem.Int_Type_EmTextContent){ //text content only PreparedStatement pre_text = con.prepareStatement(textcon_sql); pre_text.setInt(1, c_i_s_ident); ResultSet res_text = pre_text.executeQuery(); if(res_text.next()){ TextContent textcon = new TextContent(res_text.getString(1), res_text.getString(2), c_type == ContentItem.Int_Type_EmTextContent?true:false); textcon.Accept(this); cont.Add(textcon); } } else{ throw new RomulusException(RomulusException.TypeError); } } res.close(); pre.close(); } /** Visit the Feedback. */ public void VisitFeedback(Feedback fb) throws SQLException, RomulusException{ //feedback will not be graded but the feedback should be added to the initial test //task 1 //prepare the basic structure Content cont = null; //prepare the select sql int f_s_ident = RomulusToolSet.getSIdent(con, "Feedback", fb.getIdent()); int con_s_ident = 0; String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from feedback where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, f_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); fb.setContent(cont); } res.close(); pre.close(); } /** Visit the Objective. */ public void VisitObjective(Objective obj) throws SQLException, RomulusException{ //objectives have already added to the initial test } /** Visit the Question. */ public void VisitQuestion(Question que) throws SQLException, RomulusException{ //prepare the select sql int q_s_ident = RomulusToolSet.getSIdent(con, "Question", que.getIdent()); String fb_sql = "select ident, can_view, title from feedback where s_ident in " +"(select f_s_ident from feedbacklist where q_s_ident =?)"; //get the feedback //task 1 PreparedStatement pre =con.prepareStatement(fb_sql); pre.setInt(1, q_s_ident); ResultSet res = pre.executeQuery(); Feedback fb = null; while(res.next()){ fb = new Feedback(res.getString(1), res.getInt(2), res.getString(3)); fb.Accept(this); que.addFeedback(fb); } res.close(); pre.close(); //get item type //We will not get the items out but only visit them to get the correct answer QuestionItem qitem = que.getItem(); qitem.Accept(this); } /** Visit the QuestionItem. */ public void VisitQuestionItem(QuestionItem qitem) throws SQLException, RomulusException{ //get correct only //task 1 if(qitem instanceof FIB){ int fib_s_ident = RomulusToolSet.getSIdent(con, "FIB", ((FIB)qitem).getIdent()); String fib_sql = "select correctanswer from fib where s_ident = ? "; PreparedStatement pre_qitem = con.prepareStatement(fib_sql); pre_qitem.setInt(1, fib_s_ident); ResultSet res_qitem = pre_qitem.executeQuery(); if(res_qitem.next()){ ((FIB)qitem).setCorrectAnswer(res_qitem.getString(1)); } res_qitem.close(); pre_qitem.close(); } else if(qitem instanceof Choice){ //just let the choiceitem to get the correctness for(Iterator i = ((Choice)qitem).choiceitemIterator(); i.hasNext(); ){ ((ChoiceItem)i.next()).Accept(this); } } else{ throw new RomulusException(RomulusException.TypeError); } } /** Visit Test. */ public void VisitTest(Test test) throws SQLException, RomulusException{ for(Iterator i = test.questionIterator(); i.hasNext(); ){ ((Question)i.next()).Accept(this); } } /** Visit the ContentItem. */ public void VisitChoiceItem(ChoiceItem citem) throws SQLException, RomulusException { //just get the correct flag //task 1 int ci_s_ident = RomulusToolSet.getSIdent(con, "choiceitem", citem.getIdent()); String citem_sql = "select iscorrect from choiceitem where s_ident = ?"; //get content PreparedStatement pre = con.prepareStatement(citem_sql); pre.setInt(1, ci_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ citem.setCorrect(res.getInt(1)==1?true:false); } res.close(); pre.close(); } /**Visit the ContentItem.*/ public void VisitContentItem(ContentItem citem) throws SQLException, RomulusException{ //leaf node, no more content } /**Get the score of the test.*/ public static double VisitTestGetScore(Connection con, Test t) throws SQLException, RomulusException{ t.Accept(new GradeVisitor(con)); double corrnumber = 0; for(Iterator i = t.questionIterator(); i.hasNext(); ){ if(((Question)i.next()).getItem().isCorrect()){ corrnumber++; } } System.out.println(corrnumber/(t.size())); return corrnumber/(t.size()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -