MultipleChoiceQuestion.h

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  *
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00040 class Answer
00041 {
00042 public:
00049   Answer(string answer, string value, string explanation)
00050   {
00051     mAnswer = answer;
00052     mValue = value;
00053     mExplanation = explanation;
00054   }
00055 
00059   bool operator == (string x)
00060   {
00061     return (mAnswer == x);
00062   }
00066   string getAnswer() 
00067   { 
00068     return mAnswer; 
00069   }
00070 
00074   string getValue() 
00075   { 
00076     return mValue; 
00077   }
00078 
00082   string getExplanation() 
00083   { 
00084     return mExplanation; 
00085   }
00086 protected:
00088   string mAnswer;
00089 
00091   string mValue;
00092 
00094   string mExplanation;
00095 };
00096 
00101 typedef vector<Answer> AnswerSet;
00102 
00106 class MultipleChoiceQuestion: public FreeTextQuestion
00107 {
00108 public:
00112   void addAnswer(string answer, string value, string explanation)
00113   {
00114     mAnswerSet.push_back(Answer(answer,value,explanation));
00115   }
00116 
00120   virtual void explain()
00121   {
00122     AnswerSet::iterator itAnswers;
00123 
00124     /* Explain the quistion as usual.
00125      */
00126     FreeTextQuestion::explain();
00127     
00128     /* Explain the allowed answers.
00129      */
00130     cout << "POSSIBLE VALUES:\n";
00131 
00132     /* Iterate through the answer set.
00133      */
00134     for(itAnswers = mAnswerSet.begin(); itAnswers != mAnswerSet.end(); itAnswers++)
00135     {
00136       /* Print the answer and its explanation.
00137        */
00138       cout << itAnswers->getAnswer() << "\t\t" << itAnswers->getExplanation() << "\n";
00139     }
00140     cout << "\n";
00141   }
00142 
00146   size_t countAnswers() 
00147   { 
00148     return mAnswerSet.size(); 
00149   }
00150 
00154   virtual string ask()
00155   {
00156     AnswerSet::iterator itAnswers;
00157     string options;
00158 
00159     /* Iterate through the answer set to create the 
00160      * options list.
00161      */
00162      
00163     for(itAnswers = mAnswerSet.begin(); itAnswers != mAnswerSet.end(); itAnswers++)
00164     {
00165       if (options != "")
00166         options += ",";
00167       options += itAnswers->getAnswer();
00168     }
00169 
00170     /* Set the options list.
00171      */
00172     setOptions(options);
00173 
00174     /* Keep repeating the question until a valid
00175      * answer is received.
00176      */
00177     for (;;)
00178     {
00179       /* Ask the question as a FreeTextQuestion. This
00180        * takes care of the explanation and default
00181        * value handling.
00182        */
00183       FreeTextQuestion::ask();
00184 
00185       /* Try to find the answer received in the answer
00186        * set.
00187        */
00188       itAnswers = find(mAnswerSet.begin(), mAnswerSet.end(), mAnswer);
00189 
00190       /* Has a matching answer be found?
00191        */
00192       if (itAnswers != mAnswerSet.end())
00193       {
00194         /* Get the value for the received answer.
00195          */
00196         string value = itAnswers->getValue();
00197         haveChosen(mAnswer);
00198         mAnswer = value;
00199 
00200         /* Return the value.
00201          */
00202         return mAnswer;
00203       }
00204 
00205       /* No valid, matching answer received.
00206        */
00207       cout << "\nPlease enter a valid value, or '?' for help.\n\n";
00208     }
00209   };
00210 
00214   void dropChoice(string choice)
00215   {
00216     AnswerSet::iterator itAnswers;
00217 
00218     /* Try to find the answer in the answer set.
00219      */
00220     itAnswers = find(mAnswerSet.begin(), mAnswerSet.end(), mAnswer);
00221 
00222     /* Has a matching answer be found?
00223      */
00224     if (itAnswers != mAnswerSet.end())
00225     {
00226       /* Delete the answer from the list.
00227        */
00228       mAnswerSet.erase(itAnswers);
00229     }
00230   }
00231 
00236   virtual void haveChosen(string choice) 
00237   {
00238   }
00239 
00243   string getFirstChoice()
00244   {
00245     return mAnswerSet.begin()->getAnswer();
00246   }
00247 protected:
00249   AnswerSet mAnswerSet;
00250 };

SourceForge.net Logo
Project space on SourceForge.net