Skip to content

Commit

Permalink
Just a backup commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Volpym committed Oct 13, 2019
1 parent 150d0c6 commit 647d3fd
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 95 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ dependencies {
implementation "com.google.code.gson:gson:2.8.2"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
annotationProcessor "android.arch.persistence.room:compiler:$lifecycle_version"
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

}

48 changes: 48 additions & 0 deletions app/src/main/java/com/example/multiplechoice/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.multiplechoice;

public class Answer {
private static String answer1;
private static String answer2;
private static String answer3;
private static String answer4;
private static String correctAnswer;


public Answer(String answer1, String answer2, String answer3, String answer4, String correctAnswer) {
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.answer4 = answer4;
this.correctAnswer = correctAnswer;
}

public static String getAnswer1() {
return answer1;
}

public static String getAnswer2() {
return answer2;
}

public static String getAnswer3() {
return answer3;
}

public static String getAnswer4() {
return answer4;
}

public static String getCorrectAnswer() {
return correctAnswer;
}


public boolean isNull(String answer4){
boolean check = false;
if(answer4 == null){
check = true;
}
return check;
}

}
117 changes: 98 additions & 19 deletions app/src/main/java/com/example/multiplechoice/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,125 @@
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.w3c.dom.Text;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

FetchQ fetchq = new FetchQ();
List<Question> questions = null;
String correctAnswer = null;
String correctAnswer;
private Bundle savedInstanceState;
int iterator = 0;
Question question;


@Override
protected void onCreate(Bundle savedInstanceState) {
this.savedInstanceState = savedInstanceState;
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
TextView tQuestion = findViewById(R.id.textViewQustion);
Button option1 = findViewById(R.id.bOption1);
Button option2 = findViewById(R.id.bOption2);
Button option3 = findViewById(R.id.bOption3);
Button option4 = findViewById(R.id.bOption4);

TextView tQuestion = findViewById(R.id.textViewQuestion);
Button answer1 = findViewById(R.id.bAnswer1);
Button answer2 = findViewById(R.id.bAnswer2);
Button answer3 = findViewById(R.id.bAnswer3);
Button answer4 = findViewById(R.id.bAnswer4);

answer1.setOnClickListener(this);
answer2.setOnClickListener(this);
answer3.setOnClickListener(this);
answer4.setOnClickListener(this);



//Retrieves all the questions from the database and shuffles it to create a random order of presentation for the questions
try {
questions = fetchq.execute().get();
System.out.println(questions);
Collections.shuffle(questions);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}

this.question = loadQuestionElements(questions, iterator, tQuestion, answer1, answer2, answer3, answer4);
this.correctAnswer = question.getCorrectAnswer();

}


public Question loadQuestionElements(List<Question> questions, int iterator, TextView question, Button option1, Button option2, Button option3, Button option4){
Question q = questions.get(iterator);
question.setText(q.getQuestion());
option1.setText(q.getOption1());
option2.setText(q.getOption2());
option3.setText(q.getOption3());
option4.setText(q.getOption4());
this.correctAnswer = q.getCorrectAnswer();
this.iterator++;
return q;
}

@Override
public void onClick(View v) {
Button button = (Button) v;

switch(v.getId()){
case R.id.bAnswer1:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}




case R.id.bAnswer2:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}




case R.id.bAnswer3:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}



case R.id.bAnswer4:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}







}

}


private class FetchQ extends AsyncTask<Void, Void, List<Question>> {

List<Question> questions;
Expand All @@ -52,23 +130,24 @@ private class FetchQ extends AsyncTask<Void, Void, List<Question>> {
@Override
protected List<Question> doInBackground(Void... voids) {
try {
this.questions = Question.fetchQuestions();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
this.questions = Question.fetchQuestions();
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return questions;
return questions;
}
}

public void setList(List<Question> questions){
this.questions=questions;
}







}





17 changes: 16 additions & 1 deletion app/src/main/java/com/example/multiplechoice/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Question (int i, String q, String o1, String o2, String o3, String o4, St
public String getQuestion() {
return question;
}

public int getId() {
return id;
}
Expand All @@ -64,9 +65,21 @@ public String getCorrectAnswer() {
return correctAnswer;
}

public boolean isCorrect(String answer,String correctAnswer){
boolean value;
if (answer.equals(correctAnswer)){
value = true;
}else{
value = false;
}
return value;
}



public static List<Question> fetchQuestions () throws JSONException, IOException {

HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost/icd_tei_ser/script/question/get_questions.php").openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL("http://192.168.1.6/icd_tei_ser/script/question/get_questions.php").openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-length", "0");
connection.setUseCaches(false);
Expand Down Expand Up @@ -107,6 +120,8 @@ public static List<Question> fetchQuestions () throws JSONException, IOException

}



}


19 changes: 19 additions & 0 deletions app/src/main/java/com/example/multiplechoice/Timer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.multiplechoice;

import android.os.CountDownTimer;

public class Timer extends CountDownTimer {
public Timer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {

}
}
Loading

0 comments on commit 647d3fd

Please sign in to comment.