Merge pull request #2 from r0r-5chach/quiz

quiz completed
This commit is contained in:
Joshua Perry 2023-03-26 19:33:25 +01:00 committed by GitHub
commit 708c485cd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 240 additions and 3 deletions

View File

@ -15,6 +15,7 @@
android:exported="false" android:exported="false"
android:label="@string/title_activity_notes"/> android:label="@string/title_activity_notes"/>
<activity android:name=".main.MainActivity"/> <activity android:name=".main.MainActivity"/>
<activity android:name=".quiz.QuizActivity"/>
<activity <activity
android:name=".login.LoginActivity" android:name=".login.LoginActivity"
android:exported="true"> android:exported="true">

View File

@ -13,11 +13,16 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
setTitle("Home- " + getIntent().getStringExtra("username")); setTitle("Home- " + getIntent().getStringExtra("username"));
findViewById(R.id.notes_button).setOnClickListener(new BtnOnClickListener()); initButtons();
} }
@Override @Override
public void onBackPressed() { public void onBackPressed() {
finish(); finish();
} }
private void initButtons() {
findViewById(R.id.notes_button).setOnClickListener(new BtnOnClickListener());
findViewById(R.id.quiz_button).setOnClickListener(new BtnOnClickListener());
}
} }

View File

@ -0,0 +1,43 @@
package xyz.r0r5chach.cpsAssist.quiz;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import xyz.r0r5chach.cpsAssist.R;
public class BtnOnClickListener implements View.OnClickListener{
private final String[] correctAnswers;
public BtnOnClickListener(String[] correctAnswers) {
this.correctAnswers = correctAnswers;
}
@Override
public void onClick(View v) {
LinearLayout l = (LinearLayout) v.getParent();
RadioGroup[] questions = new RadioGroup[]{l.findViewById(R.id.question1), l.findViewById(R.id.question2), l.findViewById(R.id.question3), l.findViewById(R.id.question4), l.findViewById(R.id.question5)};
int score = 0;
for (int i = 0; i < questions.length; i++) {
if (questions[i].getCheckedRadioButtonId() == -1) {
continue;
}
RadioButton answer = questions[i].findViewById(questions[i].getCheckedRadioButtonId());
if (answer.getText().toString().equals(correctAnswers[i])) {
score++;
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Results");
builder.setMessage(score + "/5");
builder.setPositiveButton("Save", new DialogOnClickListener(((Activity)v.getContext()).getIntent().getStringExtra("username"), score));
builder.setNeutralButton("Ok", new DialogOnClickListener());
builder.create().show();
}
}

View File

@ -0,0 +1,36 @@
package xyz.r0r5chach.cpsAssist.quiz;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.io.File;
import xyz.r0r5chach.cpsAssist.notes.Notes;
public class DialogOnClickListener implements DialogInterface.OnClickListener {
private String username;
private int score;
public DialogOnClickListener() {
}
public DialogOnClickListener(String username, int score) {
this.score = score;
this.username = username;
}
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog d = (AlertDialog) dialog;
if (which == DialogInterface.BUTTON_POSITIVE) {
Notes notes = new Notes(d.getContext().getExternalFilesDir(null), username);
notes.createNote();
File note = notes.getNote(notes.getAmount()-1);
notes.updateNote(note, "Quiz Results: " + score + "/5");
}
d.dismiss();
}
}

View File

@ -0,0 +1,53 @@
package xyz.r0r5chach.cpsAssist.quiz;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Quiz {
private final String[] questions;
private String[] correctAnswers;
private final List<List<String>> answers;
private final String storedCorrectAnswers;
private final String storedIncorrectAnswers;
public Quiz(String storedQuestions, String storedCorrectAnswers, String storedIncorrectAnswers) {
questions = parseList(storedQuestions);
answers = new ArrayList<>();
this.storedCorrectAnswers = storedCorrectAnswers;
this.storedIncorrectAnswers = storedIncorrectAnswers;
parseAnswers();
}
public String[] getCorrectAnswers() {
return correctAnswers;
}
public String[] getQuestions() {
return questions;
}
public List<List<String>> getAnswers() {
return answers;
}
private void parseAnswers() {
correctAnswers = parseList(storedCorrectAnswers);
String[] incorrectAnswers = parseList(storedIncorrectAnswers);
for (int i = 0; i < incorrectAnswers.length; i++) {
List<String> answers = new ArrayList<>();
answers.add(correctAnswers[i]);
answers.addAll(Arrays.asList(incorrectAnswers[i].split("%")));
this.answers.add(answers);
}
}
private String[] parseList(String list) {
return list.split("#");
}
}

View File

@ -1,4 +1,77 @@
package xyz.r0r5chach.cpsAssist.quiz; package xyz.r0r5chach.cpsAssist.quiz;
public class QuizActivity { import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.stream.IntStream;
import xyz.r0r5chach.cpsAssist.R;
public class QuizActivity extends AppCompatActivity {
private Quiz quiz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
LinearLayout layout = findViewById(R.id.quiz_layout);
quiz = new Quiz(getString(R.string.questions), getString(R.string.correct_answers), getString(R.string.incorrect_answers));
initQuestions(layout);
initSubmit(layout);
}
private void initSubmit(LinearLayout l) {
Button button = new Button(this);
button.setText(R.string.submit_button_text);
button.setOnClickListener(new BtnOnClickListener(quiz.getCorrectAnswers()));
l.addView(button);
}
private void initQuestions(LinearLayout l) {
IntStream.range(0, quiz.getQuestions().length).forEach(i -> {
RadioGroup questionGroup = new RadioGroup(this);
int id = getId(i);
questionGroup.setId(id);
TextView question = new TextView(this);
question.setText(quiz.getQuestions()[i]);
questionGroup.addView(question);
for (String answer : quiz.getAnswers().get(i)) {
RadioButton answerButton = new RadioButton(this);
answerButton.setText(answer);
questionGroup.addView(answerButton);
}
l.addView(questionGroup);
});
}
private int getId(int index) {
int id;
switch(index) {
case 0:
id = R.id.question1;
break;
case 1:
id = R.id.question2;
break;
case 2:
id = R.id.question3;
break;
case 3:
id = R.id.question4;
break;
case 4:
id = R.id.question5;
break;
default:
id = 0;
}
return id;
}
} }

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/quiz_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/quiz_button_text" />
</LinearLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="question" type="id" />
<item name="question1" type="id" />
<item name="question2" type="id" />
<item name="question3" type="id" />
<item name="question4" type="id" />
<item name="question5" type="id"/>
</resources>

View File

@ -5,12 +5,16 @@
<string name="passwordFieldHint">Password</string> <string name="passwordFieldHint">Password</string>
<string name="loginButtonText">Login</string> <string name="loginButtonText">Login</string>
<string name="users">josh#pass%kofi#pass%jaiwin#pass</string> <string name="users">josh#pass%kofi#pass%jaiwin#pass</string>
<string name="questions">What is the capital of the UK?#What is the electron count of helium?#What does RAM stand for?#What is the boiling point of water?#When did WWII end?</string>
<string name="correct_answers">London#2#Random Access Memory#100 degrees celsius#1945</string>
<string name="incorrect_answers">New York City%Paris%Madrid#5%6%9%64#Read Admin Memory%Rapid Access Memory#0 degrees celsius%20 degrees celsius#2001%1666%1920</string>
<string name="title_activity_notes">NotesActivity</string> <string name="title_activity_notes">NotesActivity</string>
<!-- Notes View --> <!-- Notes View -->
<string name="addButtonText">Add</string> <string name="addButtonText">Add</string>
<string name="editButtonText">Edit</string> <string name="editButtonText">Edit</string>
<string name="deleteButtonText">Delete</string> <string name="deleteButtonText">Delete</string>
<string name="note_edit_hint">Note</string> <string name="note_edit_hint">Notes</string>
<string name="saveButtonText">Save</string> <string name="saveButtonText">Save</string>
<string name="quiz_button_text">Quiz</string> <string name="quiz_button_text">Quiz</string>
<string name="submit_button_text">Submit</string>
</resources> </resources>