I am developing a simple quiz app on Android. The questions in the quiz should become more difficult as advancing in the quiz. Now, I am trying to implement a static method that takes a list of Question objects and produces (pick) a sub-list of the appropriate questions depending on the difficulty and it should be ordered (First question in the list is the simplest one). The app has 3 levels (modes) of difficulties.
Here is the method snippet:
public static List<Question> getQuestions(List<Question> availableQuestions,
int quizDifficulty, int numberOfQuestion)
{
if(availableQuestions.size() < numberOfQuestion)
throw NotEnoughQuestionsException();
List<Question> questions = new ArrayList<Question>(numberOfQuestion);
if(quizDifficulty == 0) // Easy
{
// ...
return questions;
}
else if(quizDifficulty == 2) // Hard
{
// ...
return questions;
}
else /*if(quizDifficulty == 1)*/ // Normal
{
// ...
return questions;
}
}
Each Question object has a field difficulty which is in range 1 (most simple) to 10 (most difficult), and this field can be access by getDifficulty() method.
As I though of a way to implement the method, I decided to make the questions’ difficulties should not exceed level 8 for Easy mode, and they should be more than level 3 in the Hard mode, and in between level 9 and level 2 in the Normal mode.
The problem is that the provided list of questions availableQuestions is not guaranteed to contain all the required levels of difficulties, for example, all the questions are in level 1.
So, my question is, what is the best idea to implement this method?
EDIT:
Here is my progress so far:
public static List<Question> getQuestions(List<Question> availableQuestions,
int quizDifficulty, int numberOfQuestion)
{
if(availableQuestions.size() < numberOfQuestion)
throw NotEnoughQuestionsException();
List<Question> questions = new ArrayList<Question>(numberOfQuestion);
Map<Integer, List<Question>> map = new HashMap<Integer, List<Question>>();
for(int i = 1; i <= 10; i++) map.put(i, new ArrayList<Question>());
for(Question question : availableQuestions)
map.get(question.getDifficulty()).add(question);
int L1 = map.get(1).size(); // number of questions with level 1
int L2 = map.get(2).size();
int L3 = map.get(3).size();
int L4 = map.get(4).size();
int L5 = map.get(5).size();
int L6 = map.get(6).size();
int L7 = map.get(7).size();
int L8 = map.get(8).size();
int L9 = map.get(9).size();
int L10 = map.get(10).size();
final int L1_TO_L8 = 0;
final int L1_TO_L9 = 1;
final int L1_TO_L10 = 2;
final int L2_TO_L9 = 3;
final int L2_TO_L10 = 4;
final int L3_TO_L10 = 5;
int status;
if(difficulty == 0) // Easy (level 1 to level 8)
{
int missing = questionsCount - (L1+L2+L3+L4+L5+L6+L7+L8);
if(missing > 0) // not enough questions in L1 through L8
{
if(missing - L9 > 0) // we must include all the level
{
status = L1_TO_L10;
}
else // enough questions in L1 through L9
{
status = L1_TO_L9;
}
}
else // enough questions in L1 through L8
{
status = L1_TO_L8;
}
}
else if(difficulty == 2) // Hard (level 3 to level 10)
{
int missing = questionsCount - (L3+L4+L5+L6+L7+L8+L9+L10);
if(missing > 0) // not enough questions in L3 through L10
{
if(missing - L2 > 0) // we must include all the level
{
status = L1_TO_L10;
}
else // enough questions in L2 through L10
{
status = L2_TO_L10;
}
}
else // enough questions in L3 through L10
{
status = L3_TO_L10;
}
}
else /*if(difficulty == 1)*/ // Normal (level 2 to level 9)
{
int missing = questionsCount - (L2+L3+L4+L5+L6+L7+L8+L9);
if(missing > 0) // not enough questions in L2 through L9
{
if(missing - L1 > 0) // we must include all the level
{
status = L1_TO_L10;
}
else // enough questions in L1 through L9
{
status = L1_TO_L9;
}
}
else // enough questions in L2 through L9
{
status = L2_TO_L9;
}
}
// ...
}
I have implemented my own algorithm and it works just perfect, though it has a lot of duplicated codes: