org.ESLM.Parser.ExerciseFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.ESLM.Parser.ExerciseFactory.java

Source

package org.ESLM.Parser;

/*
Copyright (C) 2015 Rmi Even, Eric Perlinski
<remi.even@telecomnancy.net>
<eric.perlinski@telecomnancy.net>
    
This file is part of ESLM.
    
ESLM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
ESLM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.HashMap;

import org.ESLM.Model.Exercise;
import org.ESLM.Model.Instruction;
import org.ESLM.Model.IrregularVerbExercise;
import org.ESLM.Model.IrregularVerbInstruction;
import org.ESLM.Model.OneAnswerInstruction;
import org.ESLM.Model.OneAnswerQuestion;
import org.ESLM.Model.SimpleQuestion;
import org.ESLM.Model.SimpleQuestionInstruction;
import org.ESLM.Model.VocabularyExercise;
import org.ESLM.Model.VocabularyInstruction;
import org.ESLM.Model.YesNoInstruction;
import org.ESLM.Model.YesNoQuestion;
import org.json.JSONArray;
import org.json.JSONObject;

public class ExerciseFactory {

    public static Instruction createInstruction(JSONObject JSONExercise) throws BadFormedJSONExerciseException {
        switch (JSONExercise.optString("type")) {
        case ("OneAnswerQuestion"): {
            String question = JSONExercise.getString("question");
            String[] answers = createAnswers(JSONExercise.getJSONArray("answers"));
            int correctAnswerIndex = JSONExercise.getInt("correctAnswer");
            return new OneAnswerInstruction(question, answers, correctAnswerIndex);
        }
        case ("YesNoQuestion"): {
            String question = JSONExercise.getString("question");
            boolean correctAnswer = JSONExercise.getBoolean("correctAnswer");
            return new YesNoInstruction(question, correctAnswer);
        }
        case ("SimpleQuestion"): {
            String question = JSONExercise.getString("question");
            String answer = JSONExercise.getString("answer");
            return new SimpleQuestionInstruction(question, answer);
        }
        case ("IrregularVerbExercise"): {
            String infinitive = JSONExercise.getString("infinitive");
            String pastSimple = JSONExercise.getString("pastSimple");
            String pastParticiple = JSONExercise.getString("pastParticiple");
            String[] correctAnswer = { infinitive, pastSimple, pastParticiple };
            String translation = JSONExercise.getString("translation");
            boolean alsoRegular = JSONExercise.getBoolean("alsoRegular");
            return new IrregularVerbInstruction(correctAnswer, translation, alsoRegular);
        }
        case ("VocabularyExercise"): {
            String sourceLanguage = JSONExercise.getString("sourceLanguage");
            String goalLanguage = JSONExercise.getString("goalLanguage");
            JSONArray JSONVocabularyList = JSONExercise.getJSONArray("list");
            HashMap<String, String> vocabularyList = parseVocabularyList(JSONVocabularyList);
            return new VocabularyInstruction(vocabularyList, sourceLanguage, goalLanguage);
        }
        default:
            throw new BadFormedJSONExerciseException("No type or unknown type of exercise.", JSONExercise);
        }
    }

    public static Exercise createExercise(JSONObject JSONExercise) throws BadFormedJSONExerciseException {
        return createFromInstruction(createInstruction(JSONExercise));
    }

    public static Exercise createFromInstruction(Instruction instruction) {
        if (instruction instanceof OneAnswerInstruction)
            return new OneAnswerQuestion((OneAnswerInstruction) instruction);
        else if (instruction instanceof YesNoInstruction)
            return new YesNoQuestion((YesNoInstruction) instruction);
        else if (instruction instanceof SimpleQuestionInstruction)
            return new SimpleQuestion((SimpleQuestionInstruction) instruction);
        else if (instruction instanceof IrregularVerbInstruction)
            return new IrregularVerbExercise((IrregularVerbInstruction) instruction);
        else if (instruction instanceof VocabularyInstruction)
            return new VocabularyExercise((VocabularyInstruction) instruction);
        else
            return null;
    }

    private static HashMap<String, String> parseVocabularyList(JSONArray vocabularyList) {
        int numberOfElements = vocabularyList.length();
        HashMap<String, String> parsedList = new HashMap<String, String>(numberOfElements);
        for (int i = 0; i < numberOfElements; i++) {
            String word = vocabularyList.getJSONObject(i).getString("word");
            String translation = vocabularyList.getJSONObject(i).getString("translation");
            parsedList.put(word, translation);
        }
        return parsedList;
    }

    private static String[] createAnswers(JSONArray JSONAnswers) {
        int numberOfAnswers = JSONAnswers.length();
        String[] answers = new String[numberOfAnswers];
        for (int i = 0; i < numberOfAnswers; i++) {
            answers[i] = JSONAnswers.getString(i);
        }
        return answers;
    }
}