coolbud012

Calculation

Mar 2nd, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. package com.droidacid.apticalc.tys;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. import android.app.AlertDialog;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.os.CountDownTimer;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17.  
  18. import com.droidacid.apticalc.MyActionBar;
  19. import com.droidacid.apticalc.R;
  20. import com.droidacid.apticalc.tys.model.Question;
  21.  
  22. public class FourCalculation extends MyActionBar implements OnClickListener {
  23. private static final String TAG = "CalculationActivity";
  24.  
  25. Button bOne, bTwo, bThree, bFour, bFive, bSix, bSeven, bEight, bNine,
  26. bZero, bClear;
  27. TextView tvQuestions, tvAnswers, tvTimer, tvNumberOfQuestions;
  28.  
  29. private CountDownTimer timer;
  30.  
  31. private final static int EASY = 0;
  32. private final static int MEDIUM = 1;
  33. private final static int HARD = 2;
  34. private static final int ADDITION = 0;
  35. private static final int SUBTRACT = 1;
  36. private static final int MULTIPLY = 2;
  37. private static final int DIVIDE = 3;
  38. private static final int ALL = 4;
  39.  
  40. int randomValue = (int) Math.random();
  41. int availableTime = 30;
  42. int num1, num2, result, rightAnswers, wrongAnswers, timeTaken;
  43.  
  44. long starttime = 0;
  45. boolean timerGoingUp = true;
  46. boolean timerRunning;
  47. Timer upTimer = new Timer();
  48.  
  49. private int mDifficulty, mQuestions;
  50.  
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54.  
  55. setContentView(R.layout.tys_four_calculate);
  56. initialize();
  57. questions();
  58. startTimer();
  59. }
  60.  
  61. private void initialize() {
  62. Bundle getBasket = getIntent().getExtras();
  63. Bundle basket = getIntent().getExtras();
  64. mDifficulty = getBasket.getInt("difficulty", 0);
  65. mQuestions = basket.getInt("questionType", 0);
  66.  
  67. String howDifficult;
  68. String QuestionType;
  69.  
  70. if (mDifficulty == EASY)
  71. howDifficult = "Easy";
  72. if (mDifficulty == MEDIUM)
  73. howDifficult = "Medium";
  74. if (mDifficulty == HARD)
  75. howDifficult = "Hard";
  76.  
  77. if (mQuestions == ADDITION)
  78. QuestionType = "Addition";
  79. if (mQuestions == SUBTRACT)
  80. QuestionType = "Subtraction";
  81. if (mQuestions == MULTIPLY)
  82. QuestionType = "Multiplication";
  83. if (mQuestions == DIVIDE)
  84. QuestionType = "Division";
  85. if (mQuestions == ALL)
  86. QuestionType = "All";
  87.  
  88. tvQuestions = (TextView) findViewById(R.id.tv_tys_Questions);
  89. tvTimer = (TextView) findViewById(R.id.tv_tys_Timer);
  90. tvAnswers = (TextView) findViewById(R.id.tv_tys_Answers);
  91. tvNumberOfQuestions = (TextView) findViewById(R.id.tv_tys_NumberOfQuestions);
  92.  
  93. bOne = (Button) findViewById(R.id.bOne);
  94. bTwo = (Button) findViewById(R.id.bTwo);
  95. bThree = (Button) findViewById(R.id.bThree);
  96. bFour = (Button) findViewById(R.id.bFour);
  97. bFive = (Button) findViewById(R.id.bFive);
  98. bSix = (Button) findViewById(R.id.bSix);
  99. bSeven = (Button) findViewById(R.id.bSeven);
  100. bEight = (Button) findViewById(R.id.bEight);
  101. bNine = (Button) findViewById(R.id.bNine);
  102. bZero = (Button) findViewById(R.id.bZero);
  103. bClear = (Button) findViewById(R.id.bClear);
  104.  
  105. bOne.setOnClickListener(this);
  106. bTwo.setOnClickListener(this);
  107. bThree.setOnClickListener(this);
  108. bFour.setOnClickListener(this);
  109. bFive.setOnClickListener(this);
  110. bSix.setOnClickListener(this);
  111. bSeven.setOnClickListener(this);
  112. bEight.setOnClickListener(this);
  113. bNine.setOnClickListener(this);
  114. bZero.setOnClickListener(this);
  115. bClear.setOnClickListener(this);
  116. }
  117.  
  118. @Override
  119. public void onClick(View v) {
  120. if (!timerRunning)
  121. return;
  122.  
  123. switch (v.getId()) {
  124. case R.id.bOne:
  125. tvAnswers.setText(tvAnswers.getText().toString() + "1");
  126. break;
  127. case R.id.bTwo:
  128. tvAnswers.setText(tvAnswers.getText().toString() + "2");
  129. break;
  130. case R.id.bThree:
  131. tvAnswers.setText(tvAnswers.getText().toString() + "3");
  132. break;
  133. case R.id.bFour:
  134. tvAnswers.setText(tvAnswers.getText().toString() + "4");
  135. break;
  136. case R.id.bFive:
  137. tvAnswers.setText(tvAnswers.getText().toString() + "5");
  138. break;
  139. case R.id.bSix:
  140. tvAnswers.setText(tvAnswers.getText().toString() + "6");
  141. break;
  142. case R.id.bSeven:
  143. tvAnswers.setText(tvAnswers.getText().toString() + "7");
  144. break;
  145. case R.id.bEight:
  146. tvAnswers.setText(tvAnswers.getText().toString() + "8");
  147. break;
  148. case R.id.bNine:
  149. tvAnswers.setText(tvAnswers.getText().toString() + "9");
  150. break;
  151. case R.id.bZero:
  152. tvAnswers.setText(tvAnswers.getText().toString() + "0");
  153. break;
  154. case R.id.bClear:
  155. if (tvAnswers.getText().length() > 0) {
  156. String txt = tvAnswers.getText().toString();
  157. tvAnswers.setText(txt.substring(0, txt.length() - 1));
  158. }
  159. break;
  160. }
  161.  
  162. if (tvAnswers.getText().length() == String.valueOf(result).length()) {
  163. verifyAnswer();
  164. }
  165.  
  166. }
  167.  
  168. private void questions() {
  169. int numberOfQuestions = 0;
  170. switch (mDifficulty) {
  171. case EASY:
  172. numberOfQuestions = 10;
  173. break;
  174. case MEDIUM:
  175. numberOfQuestions = 15;
  176. break;
  177. case HARD:
  178. numberOfQuestions = 20;
  179. break;
  180. }
  181. Log.w(TAG, "new set questions");
  182. if ((rightAnswers + wrongAnswers) >= numberOfQuestions) {
  183. calculateScore();
  184.  
  185. return;
  186. }
  187.  
  188. Question mQuestion = new Question(mQuestions, mDifficulty);
  189. tvQuestions.setText(mQuestion.getCalculation());
  190. result = mQuestion.getResult();
  191. }
  192.  
  193. @SuppressWarnings("deprecation")
  194. public void verifyAnswer() {
  195.  
  196. if (result == Integer.valueOf((String) tvAnswers.getText())) {
  197. rightAnswers++;
  198. tvNumberOfQuestions.setText(Integer.toString(rightAnswers) + "/"
  199. + Integer.toString(rightAnswers + wrongAnswers));
  200. tvNumberOfQuestions.setTextColor(Color.GREEN);
  201. } else {
  202. wrongAnswers++;
  203. tvNumberOfQuestions.setText(Integer.toString(rightAnswers) + "/"
  204. + Integer.toString(rightAnswers + wrongAnswers));
  205. tvNumberOfQuestions.setTextColor(Color.RED);
  206.  
  207. AlertDialog wrongDialog = new AlertDialog.Builder(this).create();
  208. wrongDialog.setCancelable(false);
  209. wrongDialog.setTitle("Wrong Answer");
  210. wrongDialog.setMessage("Right answer is : " + result);
  211. wrongDialog.setButton("Ok", new DialogInterface.OnClickListener() {
  212.  
  213. @Override
  214. public void onClick(DialogInterface dialog, int which) {
  215. dialog.cancel();
  216.  
  217. }
  218. });
  219. wrongDialog.show();
  220. }
  221.  
  222. tvAnswers.setText("");
  223. questions();
  224. }
  225.  
  226. private void calculateScore() {
  227. int score = 0;
  228. if (timerGoingUp) {
  229. score = (10 * (rightAnswers) - 5 * (wrongAnswers)) - timeTaken;
  230. } else {
  231. score = (10 * (rightAnswers) - 5 * (wrongAnswers))
  232. + Integer.valueOf(tvTimer.getText().toString());
  233. }
  234.  
  235. stopTimer();
  236. scoreActivity(score);
  237. rightAnswers = 0;
  238. wrongAnswers = 0;
  239. }
  240.  
  241. private void scoreActivity(int score) {
  242. Intent scoresCalc = new Intent(this, TYSScores.class);
  243. Bundle scored = new Bundle();
  244. scored.putInt("rightanswers", rightAnswers);
  245. scored.putInt("wronganswers", wrongAnswers);
  246. scored.putInt("score", score);
  247.  
  248. scoresCalc.putExtras(scored);
  249. startActivity(scoresCalc);
  250. finish();
  251.  
  252. }
  253.  
  254. private void stopTimer() {
  255. upTimer.cancel();
  256. upTimer.purge();
  257. if (timer != null)
  258. timer.cancel();
  259. timer = null;
  260. timerRunning = false;
  261. }
  262.  
  263. private void startTimer() {
  264. if (timerGoingUp) {
  265. starttime = System.currentTimeMillis();
  266. upTimer = new Timer();
  267. upTimer.schedule(new MyUpTimer(), 0, 500);
  268. } else {
  269. showDownTimer();
  270. }
  271. timerRunning = true;
  272. }
  273.  
  274. private void showDownTimer() {
  275. if (timer != null)
  276. timer.cancel();
  277. timer = new CountDownTimer(availableTime * 1000, 10) {
  278. @Override
  279. public void onTick(long millisUntilFinished) {
  280. tvTimer.setText(Long.toString(millisUntilFinished / 1000));
  281. }
  282.  
  283. @Override
  284. public void onFinish() {
  285. calculateScore();
  286. }
  287. }.start();
  288.  
  289. }
  290.  
  291. class MyUpTimer extends TimerTask {
  292. @Override
  293. public void run() {
  294. runOnUiThread(new Runnable() {
  295. @Override
  296. public void run() {
  297. long millis = System.currentTimeMillis() - starttime;
  298. int seconds = (int) (millis / 1000);
  299. timeTaken = seconds;
  300. int minutes = seconds / 60;
  301. seconds = seconds % 60;
  302. tvTimer.setText(String.format("%d:%02d", minutes, seconds));
  303. }
  304. });
  305. }
  306. }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment