Android Open Source - Quiz Level






From Project

Back to project page Quiz.

License

The source code is released under:

GNU General Public License

If you think the Android project Quiz 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 (c) 2013, Maciej Laskowski. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *//from   www . j  a va  2  s .  c o  m
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  
 * 
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact mlaskowsk@gmail.com if you need additional information
 * or have any questions.
 */

package com.mlaskows.quiz.model.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;
import com.mlaskows.quiz.model.dao.LevelDao;

/**
 * Quiz level. It is XML element and DB entity in one.
 * 
 * @author Maciej Laskowski
 * 
 */
@Root
@DatabaseTable(daoClass = LevelDao.class)
public class Level {

  private static final String SCORING_ID_FIELD_NAME = "scoring_id";

  /** Level id. */
  @Attribute
  @DatabaseField(id = true)
  private int id;

  /** Level name. */
  @Attribute
  @DatabaseField(canBeNull = false)
  private String name;

  /** Scoring element. */
  @Element
  @DatabaseField(foreign = true, canBeNull = false, foreignAutoRefresh = true, columnName = SCORING_ID_FIELD_NAME)
  private Scoring scoring;

  /** Exercises list. */
  @ElementList
  @ForeignCollectionField
  private Collection<Exercise> exercises;

  /** Level score. */
  @DatabaseField
  private int score;

  /** Is level unlocked. */
  @DatabaseField(canBeNull = false)
  private boolean unlocked;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Scoring getScoring() {
    return scoring;
  }

  public void setScoring(Scoring scoring) {
    this.scoring = scoring;
  }

  public Collection<Exercise> getExercises() {
    return exercises;
  }

  public void setExercises(Collection<Exercise> exercises) {
    this.exercises = exercises;
  }

  public int getScore() {
    return score;
  }

  public void setScore(int score) {
    this.score = score;
  }

  public boolean isUnlocked() {
    return unlocked;
  }

  public void setUnlocked(boolean unlocked) {
    this.unlocked = unlocked;
  }

  /**
   * Returns next unsolved exercise in cycle for specified
   * id.
   * 
   * @param id
   *            previous exercise id
   * @param forward
   *            search forward if true, backward if false
   * @return next unsolved exercise
   */
  public Exercise getUnsolvedInCycle(int id, boolean forward) {
    if (getExercises() == null || getExercises().isEmpty()) {
      return null;
    }
    List<Exercise> exercises = null;
    if (forward) {
      exercises = new ArrayList<Exercise>(getExercises());
    } else {
      exercises = Lists.reverse(new ArrayList<Exercise>(getExercises()));
    }
    Iterator<Exercise> ie = Iterators.cycle(exercises);
    int count = 0;
    boolean isAfterSpecified = false;
    // Iterate over cycle.
    while (count <= exercises.size() + id) {
      Exercise e = ie.next();
      /* Search for next unsolved exercise only if
       * specified by method parameter was reached
       * or if parameter equals 0.
       */
      if ((isAfterSpecified || id == 0) && !e.isSolved()) {
        return e;
      }
      /* Exercise specified by method parameter is 
       * reached. Now next exercise can be searched. 
       */
      if (id == e.getId()) {
        isAfterSpecified = true;
      }
      count++;
    }
    return null;
  }

  /**
   * Calculates progress.
   * 
   * @return level progress
   */
  public int getProgress() {
    int ec = exercises.size();
    int pec = 0;
    for (Exercise exercise : exercises) {
      if (exercise.isSolved()) {
        pec++;
      }
    }
    return (int) (((double) pec / (double) ec) * 100);
  }

  @Override
  public String toString() {
    return "Level [id=" + id + ", name=" + name + ", scoring=" + scoring + ", exercises=" + exercises
        + ", unlocked=" + unlocked + "]";
  }

}




Java Source Code List

com.mlaskows.quiz.QuizApplication.java
com.mlaskows.quiz.activity.ExerciseActivity.java
com.mlaskows.quiz.activity.FullScreenActivity.java
com.mlaskows.quiz.activity.LevelsActivity.java
com.mlaskows.quiz.activity.MainActivity.java
com.mlaskows.quiz.activity.ScoreActivity.java
com.mlaskows.quiz.activity.clicklistener.LevelResetListener.java
com.mlaskows.quiz.activity.util.ActivityStarter.java
com.mlaskows.quiz.activity.util.BundleBuilder.java
com.mlaskows.quiz.adapter.LevelsAdapter.java
com.mlaskows.quiz.model.DatabaseHelper.java
com.mlaskows.quiz.model.XmlDataLoader.java
com.mlaskows.quiz.model.dao.AnswerDao.java
com.mlaskows.quiz.model.dao.BaseDaoWrapper.java
com.mlaskows.quiz.model.dao.ExerciseDao.java
com.mlaskows.quiz.model.dao.LevelDao.java
com.mlaskows.quiz.model.dao.QuestionDao.java
com.mlaskows.quiz.model.dao.ScoringDao.java
com.mlaskows.quiz.model.entity.Answer.java
com.mlaskows.quiz.model.entity.Exercise.java
com.mlaskows.quiz.model.entity.Level.java
com.mlaskows.quiz.model.entity.Question.java
com.mlaskows.quiz.model.entity.Quiz.java
com.mlaskows.quiz.model.entity.Scoring.java
com.mlaskows.quiz.model.enums.AnswerType.java
com.mlaskows.quiz.model.enums.QuestionType.java
com.mlaskows.quiz.utility.ImageUtility.java