Skip to content

Instantly share code, notes, and snippets.

@HOLARO
Created February 4, 2018 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HOLARO/58b06a411b68799a597b7a558eb72500 to your computer and use it in GitHub Desktop.
Save HOLARO/58b06a411b68799a597b7a558eb72500 to your computer and use it in GitHub Desktop.
This is the .java file for my CoffeeQuiz app.
package com.example.android.coffeequizv2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
/**
* This app displays a coffee quiz game and calculates the user's score.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSubmit(View view) {
/** define the score at the beginning of the quiz game*/
int points = 0;
// User's name in the name field
String userName = ((EditText) findViewById(R.id.user_name)).getText().toString();
// Question 1 - counts 1 point for correct answer
CheckBox q1a1 = (CheckBox) findViewById(R.id.q1a1);
if (q1a1.isChecked()) {
points++;
}
//Question 2 - counts 1 point for correct answer
EditText text = (EditText) (findViewById(R.id.q2a1));
String textFrom = text.getText().toString();
String textFromField = text.getText().toString();
if ("AMERICANO".equals(textFromField)) {
points++;
}
//Question 3 - counts 1 point for correct answer
boolean currencyChecked = ((RadioButton) findViewById(R.id.q3a1)).isChecked();
if (currencyChecked) {
points++;
}
// Question 4 - counts 1 point for correct answer
CheckBox q4a2 = (CheckBox) findViewById(R.id.q4a2);
if (q4a2.isChecked()) {
points++;
}
//Prints out the score
if (points < 4) {
Toast.makeText(this, "Congratulations " + userName + "!" + "You scored: " + points + " out of 4 points.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Great job " + userName + "!" + "You've got all right!", Toast.LENGTH_LONG).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment