Android Open Source - Trivial Question






From Project

Back to project page Trivial.

License

The source code is released under:

Apache License

If you think the Android project Trivial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *    Copyright 2010 Chris Sinjakli//  ww  w .j  ava  2 s .  co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Chris Sinjakli
 */

package uk.co.sinjakli.trivial;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

final class Question {
  
  private final String questionType;
  private final String question;
  private final ArrayList<String> answers;
  
  private Question(final String questionType, final String question, final ArrayList<String> answers) {
    this.questionType = questionType;
    this.question = question;
    this.answers = answers;
  }
  
  /**
   * Takes a comma separated String of the question type, the question, and the answers, and returns a {@link Question} representation of them.
   * 
   * @param input The input String made up of the type of question, the question itself, and the answers (comma separated).
   * @return a {@link Question} representation of the input string.
   * @throws IllegalArgumentException
   */
  public static Question parse(final String input) throws IllegalArgumentException {
    
    final String[] questionComponents;
    final String questionType;
    final String question;
    final ArrayList<String> answers = new ArrayList<String>();
    
    // Split the input on commas
    if (null != input) {
      questionComponents = input.split(",");
    } else {
      throw new IllegalArgumentException("Question to be parsed was null."); // Dev String
    }
    
    // Extract the component parts of the question
    if (4 < questionComponents.length) {
      questionType = questionComponents[0].trim();
      question = questionComponents[1].trim();
      
      for (int i = 2; i < questionComponents.length; i++) {
        answers.add(questionComponents[i].trim());
      }
      
    } else {
      throw new IllegalArgumentException("Question had insufficient parts. Note that at least two answers are required."); // Dev String
    }
    
    // Construct and return a new Question
    return new Question(questionType, question, answers);
    
  }

  /**
   * Returns the type of the {@link Question}
   * 
   * @return the type of the {@link Question}
   */
  public final String getQuestionType() {
    return questionType;
  }

  /**
   * Returns the question element of the {@link Question}
   * 
   * @return the question element of the {@link Question}
   */
  public final String getQuestion() {
    return question;
  }

  /**
   * Returns the answers, in a random order, as an ArrayList of String
   * 
   * @return the answers, in a random order, as an ArrayList of String
   */
  public final List<String> getAnswers() {
    // ArryList is not immutable, create a defensive copy
    final ArrayList<String> answers = new ArrayList<String>(this.answers);
    Collections.shuffle(answers);
    return answers;
  }

  /**
   * Returns the correct answer to the {@link Question}
   * 
   * @return the correct answer to the {@link Question}
   */
  public final String getAnswer() {
    return answers.get(0);
  }

}




Java Source Code List

uk.co.sinjakli.trivial.HomeActivity.java
uk.co.sinjakli.trivial.Question.java
uk.co.sinjakli.trivial.QuizActivity.java
uk.co.sinjakli.trivial.QuizEndActivity.java