Revision: 1779
Author: [email protected]
Date: Thu Dec 10 14:36:43 2009
Log: Provide CLI interface to multiple choice questions.
http://code.google.com/p/simal/source/detail?r=1779

Modified:
   
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
   
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
   
/trunk/uk.ac.osswatch.simal.ssmm/src/test/uk/ac/osswatch/simal/ssmm/model/TestMultipleChoiceQuestion.java

=======================================
---  
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
    
Thu Dec 10 05:39:37 2009
+++  
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/SustainabilityRating.java
    
Thu Dec 10 14:36:43 2009
@@ -4,6 +4,7 @@
  import java.util.LinkedHashMap;
  import java.util.Scanner;

+import uk.ac.osswatch.simal.ssmm.model.MultipleChoiceQuestion;
  import uk.ac.osswatch.simal.ssmm.model.Question;

  public class SustainabilityRating {
@@ -21,7 +22,8 @@

                  askInfoQuestions();
                  askLegalQuestions();
-
+
+                 System.out.println("\nResponse summary\n");
                  reportAll(infoQuestions);
                  reportAll(legalQuestions);
          }
@@ -42,7 +44,14 @@
            System.out.println("General Information");
            System.out.println("===================");

-           Question question = new Question("License type", "Is the licence  
recognised as a common Free and Open Source licences?", "If the licence has  
been recognised by either of these bodies, it is more likely to have been  
assessed and found to be relatively open than a new licence  or one which  
has not been OSI or FSF approved.");
+               LinkedHashMap<String, String> options = new 
LinkedHashMap<String,  
String>();
+               options.put("Don't know", "The licence model is not currently  
understood.");
+               options.put("Proprietary", "The licence used is a proprietary 
licence  
that has not been recognised by either the Free Software Foundation or the  
Open source Initiative");
+               options.put("OSI", "The licence used has been approved by the 
Open  
Source Initiative.");
+               options.put("FSF", "The licence has been recognised by the Free 
Software  
Foundation as a Free Software License");
+               options.put("FSF and OSI", "The license is recognised by the 
FSF and by  
the OSI.");
+
+           MultipleChoiceQuestion question = new 
MultipleChoiceQuestion("License  
type", "Is the licence recognised as a common Free and Open Source  
licences?", "If the licence has been recognised by either of these bodies,  
it is more likely to have been assessed and found to be relatively open  
than a new licence or one which has not been OSI or FSF approved.",  
options);
                legalQuestions.put(question.getLabel(), question);

            askAll(legalQuestions);
@@ -66,7 +75,6 @@
         * @param questions
         */
        private static void reportAll(LinkedHashMap<String, Question> 
questions) {
-               System.out.println("\nResponse summary\n");
                Iterator<Question> itr = questions.values().iterator();
                while (itr.hasNext()) {
                        Question question = itr.next();
@@ -87,14 +95,61 @@
                System.out.println("\n");
                printHeading(question);
                System.out.println(question.getText());
+               String answer;
+
+               if (question instanceof MultipleChoiceQuestion) {
+                       answer = 
getMultiChoiceResponse((MultipleChoiceQuestion) question);
+               } else {
+                   answer = getFreeFormresponse(question);
+               }
+               return question.getAnswer();
+       }
+
+       /**
+        * Get the answer to a multichoice question.
+        *
+        * @param question
+        * @return
+        */
+       private static String getMultiChoiceResponse(MultipleChoiceQuestion  
question) {
+               Iterator<String> keys = 
question.getOptions().keySet().iterator();
+               int idx = 0;
+               while (keys.hasNext()) {
+                       String key = keys.next();
+                       System.out.print(idx);
+                       System.out.print(": ");
+                       System.out.print(key);
+                       System.out.print(" (");
+                       System.out.print(question.getOptions().get(key));
+                       System.out.println(")");
+                       idx = idx + 1;
+               }
+
                Scanner in = new Scanner(System.in);
-               String answer = in.nextLine();
+               String response = in.nextLine();
+               if (response.equals(HELP_COMMAND)) {
+                       System.out.println(question.getDetails());
+                       return ask(question);
+               }
+               int intAnswer = new Integer(response);
+               if (intAnswer < 1 || intAnswer >= idx) {
+                       System.out.println("Please enter a value between 0 and 
" + (idx - 1));
+                       return ask(question);
+               }
+               question.setAnswer(intAnswer);
+               return question.getAnswer();
+       }
+
+       private static String getFreeFormresponse(Question question) {
+               String answer;
+               Scanner in = new Scanner(System.in);
+               answer = in.nextLine();
                if (answer.equals(HELP_COMMAND)) {
                        System.out.println(question.getDetails());
                        return ask(question);
                }
                question.setAnswer(answer);
-               return question.getAnswer();
+               return answer;
        }

        private static void printHeading(Question question) {
=======================================
---  
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
    
Thu Dec 10 07:04:10 2009
+++  
/trunk/uk.ac.osswatch.simal.ssmm/src/main/uk/ac/osswatch/simal/ssmm/model/MultipleChoiceQuestion.java
    
Thu Dec 10 14:36:43 2009
@@ -1,10 +1,10 @@
  package uk.ac.osswatch.simal.ssmm.model;

-import java.util.HashMap;
+import java.util.LinkedHashMap;


  public class MultipleChoiceQuestion extends Question {
-       HashMap<String, String> options = new HashMap<String, String>();
+       LinkedHashMap<String, String> options = new LinkedHashMap<String,  
String>();

        /**
         * Create a multiple choice question.
@@ -14,7 +14,7 @@
         * @param details descriptive details about the question
         * @param options the options available, where the key is used as a 
short  
hand for the question and the value is a description of the question.
         */
-       public MultipleChoiceQuestion(String label, String text, String 
details,  
HashMap<String, String> options) {
+       public MultipleChoiceQuestion(String label, String text, String 
details,  
LinkedHashMap<String, String> options) {
                super(label, text, details);
                this.options = options;
        }
@@ -24,21 +24,22 @@
         *
         * @return a hasmap keyed by the short version of the response. The 
value  
is a full description of the response.
         */
-       public HashMap<String, String> getOptions() {
+       public LinkedHashMap<String, String> getOptions() {
                return options;
        }

        /**
         * Set a map of options available to answer this question.
         *
-        * @param options a hasmap keyed by the short version of the response.  
The value is a full description of the response.
+        * @param options a linked hashmap keyed by the short version of the  
response. The value is a full description of the response.
         */
-       public void setOptions(HashMap<String, String> options) {
+       public void setOptions(LinkedHashMap<String, String> options) {
                this.options = options;
        }

        /**
         * Add an option to the possible answers.
+        *
         * @param option short version of the option
         * @param description a full description of the option
         */
@@ -48,10 +49,20 @@

        /**
         * Remove an option to the possible answers.
+        *
         * @param option short version of the option
         */
        public void removeOption(String option) {
                options.remove(option);
        }
+
+       /**
+        * Set the recorded answer according to an index pointer into the  
LinkedhashMap of options.
+        *
+        * @param index 0 base index into the options
+        */
+       public void setAnswer(int index) {
+               setAnswer((String)getOptions().keySet().toArray()[index]);
+       }

  }
=======================================
---  
/trunk/uk.ac.osswatch.simal.ssmm/src/test/uk/ac/osswatch/simal/ssmm/model/TestMultipleChoiceQuestion.java
        
Thu Dec 10 07:04:10 2009
+++  
/trunk/uk.ac.osswatch.simal.ssmm/src/test/uk/ac/osswatch/simal/ssmm/model/TestMultipleChoiceQuestion.java
        
Thu Dec 10 14:36:43 2009
@@ -5,15 +5,16 @@
  import org.junit.BeforeClass;
  import org.junit.Test;

-import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Iterator;

  public class TestMultipleChoiceQuestion {
-       static HashMap<String, String> options;
+       static LinkedHashMap<String, String> options;
        static MultipleChoiceQuestion question;

        @BeforeClass
        public static void createQuestion() {
-               options = new HashMap<String, String>();
+               options = new LinkedHashMap<String, String>();
                options.put("One", "Option one");
                options.put("Two", "Option two");
                options.put("Three", "Option three");
@@ -26,6 +27,19 @@
        public void testOptionsSize() {
                assertEquals("There is an incorrect number of options", 
options.size(),  
question.getOptions().size());
        }
+
+       @Test
+       public void testOptionsOrder() {
+               Iterator<String> itr = 
question.getOptions().keySet().iterator();
+               String key = itr.next();
+               assertEquals("First key is incorrect", "One", key);
+               key = itr.next();
+               assertEquals("Two key is incorrect", "Two", key);
+               key = itr.next();
+               assertEquals("Three key is incorrect", "Three", key);
+               key = itr.next();
+               assertEquals("Four key is incorrect", "Four", key);
+       }

        @Test
        public void testAddRemoveOption() {
@@ -36,4 +50,10 @@
                question.removeOption("Five");
                assertEquals("There is an incorrect number of options after 
removing an  
option", originalSize, question.getOptions().size());
        }
-}
+
+       @Test
+       public void testAnswer() {
+               question.setAnswer(1);
+               assertEquals("The answer is not correctly set when using an 
integer  
index", "Two", question.getAnswer());
+       }
+}

--

You received this message because you are subscribed to the Google Groups 
"Simal Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/simal-commits?hl=en.


Reply via email to