commit
708c485cd0
|
|
@ -15,6 +15,7 @@
|
|||
android:exported="false"
|
||||
android:label="@string/title_activity_notes"/>
|
||||
<activity android:name=".main.MainActivity"/>
|
||||
<activity android:name=".quiz.QuizActivity"/>
|
||||
<activity
|
||||
android:name=".login.LoginActivity"
|
||||
android:exported="true">
|
||||
|
|
|
|||
|
|
@ -13,11 +13,16 @@ public class MainActivity extends AppCompatActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
setTitle("Home- " + getIntent().getStringExtra("username"));
|
||||
findViewById(R.id.notes_button).setOnClickListener(new BtnOnClickListener());
|
||||
initButtons();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
finish();
|
||||
}
|
||||
|
||||
private void initButtons() {
|
||||
findViewById(R.id.notes_button).setOnClickListener(new BtnOnClickListener());
|
||||
findViewById(R.id.quiz_button).setOnClickListener(new BtnOnClickListener());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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("#");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,77 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -5,12 +5,16 @@
|
|||
<string name="passwordFieldHint">Password</string>
|
||||
<string name="loginButtonText">Login</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>
|
||||
<!-- Notes View -->
|
||||
<string name="addButtonText">Add</string>
|
||||
<string name="editButtonText">Edit</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="quiz_button_text">Quiz</string>
|
||||
<string name="submit_button_text">Submit</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue