Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xtremeware.straighthoodtrivia.db;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import com.xtremeware.straighthoodtrivia.quiz.Question;
- public class DBHelper extends SQLiteOpenHelper
- {
- // The androis's default system path of the application database.
- private static String DB_PATH = "/data/data/com.xtremeware.straighthoodtrivia/databases/";
- private static String DB_NAME = "QuestionsDb";
- private static String DB_TABLE = "QUESTIONS";
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- private static final int DATABASE_VERSION = 2;
- private static final String TAG = null;
- // Constructor
- // Takes and keeps a reference of the passed context in order to access to the
- // applications assets and resources.
- // @ context
- public DBHelper(Context context)
- {
- super(context, DB_NAME, null, DATABASE_VERSION);
- this.myContext = context;
- }
- // Creates a empty database on the system and rewrites it with my own database
- public void createDataBase() throws IOException
- {
- boolean dbExist = checkDataBase();
- if(dbExist)
- {
- }
- else
- { // By calling this method an empty database will be created into the default system path
- // of my application so I am gonna be able to overwrite the database with my database.
- this.getReadableDatabase();
- try
- {
- copyDataBase();
- }
- catch (IOException e)
- {
- throw new Error("Error copying database");
- }
- }
- }
- // Check if the database already exists to avoid re-copying the file each time you open the application.
- // @return true if it exists, false if it doesn't
- private boolean checkDataBase()
- {
- SQLiteDatabase checkDB = null;
- try
- {
- String myPath = DB_PATH + DB_NAME;
- checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- }
- catch (SQLiteException e)
- {
- // Database doesn't exist yet.
- }
- if(checkDB != null)
- {
- checkDB.close();
- }
- return checkDB != null ? true : false;
- }
- // Copies the database from your local assets-folder to the just created empty database in
- // the system folder, from where it can be accessed and handled.
- // This is done by transferring bytestreams.
- private void copyDataBase() throws IOException
- {
- // Open your local db as the input stream
- InputStream myInput = myContext.getAssets().open(DB_NAME);
- // Path to the empty db just created
- String outFileName = DB_PATH + DB_NAME;
- // Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
- // Transfer bytes from the inputfile to the output file
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer))>0)
- {
- myOutput.write(buffer, 0, length);
- }
- // Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
- }
- public void openDataBase() throws SQLException
- {
- // Open the database
- String myPath = DB_PATH + DB_NAME;
- myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- }
- @Override
- public synchronized void close()
- {
- if(myDataBase != null)
- myDataBase.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
- if (oldVersion == 1 && newVersion >= 2){
- db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
- db.execSQL("Create table new_table (...)");
- onCreate(db);
- }
- }
- // Add the public methods to access and get content from the database.
- // You could return cursors by doing "return myDataBase.query(....)" so
- // it would be easy for you to create adapters for the views.
- public List<Question> getQuestionSet(int difficulty, int numQ)
- {
- List<Question> questionSet = new ArrayList<Question>();
- Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty +
- " ORDER BY RANDOM() LIMIT " + numQ, null);
- while (c.moveToNext())
- {
- // LOG.d("QUESTION", "Question Found in DB: " + c.getString(1));
- Question q = new Question();
- q.setQuestion(c.getString(1));
- q.setAnswer(c.getString(2));
- q.setOption1(c.getString(3));
- q.setOption2(c.getString(4));
- q.setOption3(c.getString(5));
- q.setRating(difficulty);
- questionSet.add(q);
- }
- return questionSet;
- }
- }
- package com.xtremeware.straighthoodtrivia.quiz;
- import java.util.ArrayList;
- import java.util.List;
- // This class represents the current game being played, tracks
- // the score and player details
- public class GamePlay
- {
- private int numRounds;
- private int difficulty;
- private String playerName;
- private int right;
- private int wrong;
- private int round;
- private List<Question> questions = new ArrayList<Question>();
- // Return the playerName
- public String getPlayerName()
- {
- return playerName;
- }
- // @param playerName the playerName to set
- public void setPlayerName(String playerName)
- {
- this.playerName = playerName;
- }
- // Return the right
- public int getRight()
- {
- return right;
- }
- // @param right the right to set
- public void setRight(int right)
- {
- this.right = right;
- }
- // Return the wrong
- public int getWrong()
- {
- return wrong;
- }
- // @param wrong the wrong to set
- public void setWrong(int wrong)
- {
- this.wrong = wrong;
- }
- // Return the round
- public int getRound()
- {
- return round;
- }
- // @param round the round to set
- public void setRound(int round)
- {
- this.round = round;
- }
- // @param difficulty the difficulty to set
- public void setDifficulty(int difficulty)
- {
- this.difficulty = difficulty;
- }
- // @param the difficulty
- public int getDifficulty()
- {
- return difficulty;
- }
- // @param questions the questions to set
- public void setQuestions(List<Question> questions)
- {
- this.questions = questions;
- }
- // @param q the question to add
- public void addQuestions(Question q)
- {
- this.questions.add(q);
- }
- // @return the questions
- public List<Question> getQuestion()
- {
- return questions;
- }
- public Question getNextQuestion()
- {
- // Get the question
- Question next = questions.get(this.getRound());
- // Update the round number to the next round
- this.setRound(this.getRound() + 1);
- return next;
- }
- // Method to increment the number of correct answers this game
- public void incrementRightAnswers()
- {
- right++;
- }
- // Method to increment the number of incorrect answers this game
- public void incrementWrongAnswers()
- {
- wrong++;
- }
- // @param numRounds the numRounds to set
- public void setNumRounds(int numRounds)
- {
- this.numRounds = numRounds;
- }
- // @return the numRounds
- public int getNumRounds()
- {
- return numRounds;
- }
- // Method that checks if the game is over
- // @return boolean
- public boolean isGameOver()
- {
- return (getRound() >= getNumRounds());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement