Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. package com.xtremeware.straighthoodtrivia.db;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import android.content.Context;
  11. import android.database.Cursor;
  12. import android.database.SQLException;
  13. import android.database.sqlite.SQLiteDatabase;
  14. import android.database.sqlite.SQLiteException;
  15. import android.database.sqlite.SQLiteOpenHelper;
  16. import android.util.Log;
  17.  
  18. import com.xtremeware.straighthoodtrivia.quiz.Question;
  19.  
  20. public class DBHelper extends SQLiteOpenHelper
  21. {
  22. // The androis's default system path of the application database.
  23. private static String DB_PATH = "/data/data/com.xtremeware.straighthoodtrivia/databases/";
  24. private static String DB_NAME = "QuestionsDb";
  25. private static String DB_TABLE = "QUESTIONS";
  26. private SQLiteDatabase myDataBase;
  27. private final Context myContext;
  28. private static final int DATABASE_VERSION = 2;
  29. private static final String TAG = null;
  30.  
  31. // Constructor
  32. // Takes and keeps a reference of the passed context in order to access to the
  33. // applications assets and resources.
  34. // @ context
  35.  
  36. public DBHelper(Context context)
  37. {
  38. super(context, DB_NAME, null, DATABASE_VERSION);
  39. this.myContext = context;
  40. }
  41.  
  42. // Creates a empty database on the system and rewrites it with my own database
  43. public void createDataBase() throws IOException
  44. {
  45. boolean dbExist = checkDataBase();
  46. if(dbExist)
  47. {
  48.  
  49. }
  50. else
  51. { // By calling this method an empty database will be created into the default system path
  52. // of my application so I am gonna be able to overwrite the database with my database.
  53.  
  54. this.getReadableDatabase();
  55. try
  56. {
  57. copyDataBase();
  58. }
  59. catch (IOException e)
  60. {
  61. throw new Error("Error copying database");
  62. }
  63. }
  64. }
  65.  
  66. // Check if the database already exists to avoid re-copying the file each time you open the application.
  67. // @return true if it exists, false if it doesn't
  68.  
  69. private boolean checkDataBase()
  70. {
  71. SQLiteDatabase checkDB = null;
  72. try
  73. {
  74. String myPath = DB_PATH + DB_NAME;
  75. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  76. }
  77. catch (SQLiteException e)
  78. {
  79. // Database doesn't exist yet.
  80. }
  81. if(checkDB != null)
  82. {
  83. checkDB.close();
  84. }
  85.  
  86. return checkDB != null ? true : false;
  87. }
  88.  
  89. // Copies the database from your local assets-folder to the just created empty database in
  90. // the system folder, from where it can be accessed and handled.
  91. // This is done by transferring bytestreams.
  92.  
  93. private void copyDataBase() throws IOException
  94. {
  95. // Open your local db as the input stream
  96. InputStream myInput = myContext.getAssets().open(DB_NAME);
  97.  
  98. // Path to the empty db just created
  99. String outFileName = DB_PATH + DB_NAME;
  100.  
  101. // Open the empty db as the output stream
  102. OutputStream myOutput = new FileOutputStream(outFileName);
  103.  
  104. // Transfer bytes from the inputfile to the output file
  105. byte[] buffer = new byte[1024];
  106. int length;
  107. while ((length = myInput.read(buffer))>0)
  108. {
  109. myOutput.write(buffer, 0, length);
  110. }
  111.  
  112. // Close the streams
  113. myOutput.flush();
  114. myOutput.close();
  115. myInput.close();
  116. }
  117.  
  118. public void openDataBase() throws SQLException
  119. {
  120. // Open the database
  121. String myPath = DB_PATH + DB_NAME;
  122. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  123.  
  124. }
  125.  
  126. @Override
  127. public synchronized void close()
  128. {
  129. if(myDataBase != null)
  130. myDataBase.close();
  131. super.close();
  132. }
  133.  
  134. @Override
  135. public void onCreate(SQLiteDatabase db)
  136. {
  137.  
  138. }
  139.  
  140. @Override
  141. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  142. {
  143. Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
  144.  
  145. if (oldVersion == 1 && newVersion >= 2){
  146. db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
  147. db.execSQL("Create table new_table (...)");
  148. onCreate(db);
  149. }
  150.  
  151. }
  152.  
  153. // Add the public methods to access and get content from the database.
  154. // You could return cursors by doing "return myDataBase.query(....)" so
  155. // it would be easy for you to create adapters for the views.
  156.  
  157. public List<Question> getQuestionSet(int difficulty, int numQ)
  158. {
  159. List<Question> questionSet = new ArrayList<Question>();
  160. Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty +
  161. " ORDER BY RANDOM() LIMIT " + numQ, null);
  162. while (c.moveToNext())
  163. {
  164. // LOG.d("QUESTION", "Question Found in DB: " + c.getString(1));
  165. Question q = new Question();
  166. q.setQuestion(c.getString(1));
  167. q.setAnswer(c.getString(2));
  168. q.setOption1(c.getString(3));
  169. q.setOption2(c.getString(4));
  170. q.setOption3(c.getString(5));
  171. q.setRating(difficulty);
  172. questionSet.add(q);
  173. }
  174. return questionSet;
  175.  
  176. }
  177.  
  178. }
  179.  
  180. package com.xtremeware.straighthoodtrivia.quiz;
  181.  
  182. import java.util.ArrayList;
  183. import java.util.List;
  184.  
  185.  
  186. // This class represents the current game being played, tracks
  187. // the score and player details
  188.  
  189. public class GamePlay
  190. {
  191. private int numRounds;
  192. private int difficulty;
  193. private String playerName;
  194. private int right;
  195. private int wrong;
  196. private int round;
  197.  
  198. private List<Question> questions = new ArrayList<Question>();
  199.  
  200.  
  201. // Return the playerName
  202. public String getPlayerName()
  203. {
  204. return playerName;
  205. }
  206.  
  207. // @param playerName the playerName to set
  208. public void setPlayerName(String playerName)
  209. {
  210. this.playerName = playerName;
  211. }
  212.  
  213. // Return the right
  214. public int getRight()
  215. {
  216. return right;
  217. }
  218.  
  219. // @param right the right to set
  220. public void setRight(int right)
  221. {
  222. this.right = right;
  223. }
  224.  
  225. // Return the wrong
  226. public int getWrong()
  227. {
  228. return wrong;
  229. }
  230.  
  231. // @param wrong the wrong to set
  232. public void setWrong(int wrong)
  233. {
  234. this.wrong = wrong;
  235. }
  236.  
  237. // Return the round
  238. public int getRound()
  239. {
  240. return round;
  241. }
  242.  
  243. // @param round the round to set
  244. public void setRound(int round)
  245. {
  246. this.round = round;
  247. }
  248.  
  249. // @param difficulty the difficulty to set
  250. public void setDifficulty(int difficulty)
  251. {
  252. this.difficulty = difficulty;
  253. }
  254.  
  255. // @param the difficulty
  256. public int getDifficulty()
  257. {
  258. return difficulty;
  259. }
  260.  
  261. // @param questions the questions to set
  262. public void setQuestions(List<Question> questions)
  263. {
  264. this.questions = questions;
  265. }
  266.  
  267. // @param q the question to add
  268. public void addQuestions(Question q)
  269. {
  270. this.questions.add(q);
  271. }
  272.  
  273. // @return the questions
  274. public List<Question> getQuestion()
  275. {
  276. return questions;
  277. }
  278.  
  279. public Question getNextQuestion()
  280. {
  281. // Get the question
  282. Question next = questions.get(this.getRound());
  283.  
  284. // Update the round number to the next round
  285. this.setRound(this.getRound() + 1);
  286. return next;
  287. }
  288.  
  289. // Method to increment the number of correct answers this game
  290. public void incrementRightAnswers()
  291. {
  292. right++;
  293. }
  294.  
  295. // Method to increment the number of incorrect answers this game
  296. public void incrementWrongAnswers()
  297. {
  298. wrong++;
  299. }
  300.  
  301. // @param numRounds the numRounds to set
  302. public void setNumRounds(int numRounds)
  303. {
  304. this.numRounds = numRounds;
  305. }
  306.  
  307. // @return the numRounds
  308. public int getNumRounds()
  309. {
  310. return numRounds;
  311. }
  312.  
  313. // Method that checks if the game is over
  314. // @return boolean
  315. public boolean isGameOver()
  316. {
  317. return (getRound() >= getNumRounds());
  318. }
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement