Skip to content

Instantly share code, notes, and snippets.

@edenizk
Created June 9, 2018 01:43
Show Gist options
  • Save edenizk/40a88e3a97945e8ff9035685b30fc2f2 to your computer and use it in GitHub Desktop.
Save edenizk/40a88e3a97945e8ff9035685b30fc2f2 to your computer and use it in GitHub Desktop.
Polish Time Quiz app
package com.example.deniz.quizer;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.security.SecureRandom;
public class MainActivity extends AppCompatActivity {
String answerHour,answerHour2,answerMin;
String answers[] = {"","",""};
TextView txtQuestion;
int[] Score = {0};
int[] Time = {0};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final boolean[] clicked = {false};
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
final TextView txtScore = (TextView) findViewById(R.id.txtScore);
final EditText edtAnswer = (EditText)findViewById(R.id.edtAnswer);
ImageButton btnSend = (ImageButton) findViewById(R.id.btnSend);
randomQuestionGenerator();
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(clicked[0] == false){
boolean equals = false;
Log.i("LOG:" , "edt " + edtAnswer.getText());
String edtText = String.valueOf(edtAnswer.getText());
if(edtText.isEmpty()){
edtAnswer.setTextColor(Color.RED);
Time[0] = Time[0] + 1;
edtAnswer.setText("CLICK TO CONTINUE");
equals=true;
}
for(int i = 0 ; i < answers.length; i++){
if(edtText.equals(answers[i])){
edtAnswer.setTextColor(Color.GREEN);
Time[0] = Time[0] + 1;
Score[0]= Score[0] + 1;
equals=true;
}
}
edtText = edtText.replaceAll("\\s+","");
for(int i = 0 ; i < answers.length; i++){
answers[i] = answers[i].replaceAll("\\s+","");
if(edtText.equals(answers[i])){
edtAnswer.setTextColor(Color.GREEN);
Time[0] = Time[0] + 1;
Score[0]= Score[0] + 1;
equals=true;
}
}
if(equals == false){
edtAnswer.setTextColor(Color.RED);
Time[0] = Time[0] + 1;
}
txtScore.setText(Score[0] + "/" + Time[0]);
clicked[0] = true;
}
else{
randomQuestionGenerator();
edtAnswer.setText("");
edtAnswer.setTextColor(Color.parseColor("#000000"));
clicked[0] = false;
}
}
});
}
private void randomQuestionGenerator (){
Log.i("LOG:", "come");
SecureRandom scrNum = new SecureRandom();
int hour = 1 + scrNum.nextInt(24);
int min = 0 + scrNum.nextInt(59);
String time = "";
if(hour < 10) {
time = "0" + hour + ":";
}
else {
time = hour + ":";
}
if(min < 10){
time = time + "0" + min;
}
else {
time = time + min + "";
}
answerHour = answerHourGenerator(hour);
answerHour2 = answerHourGenerator2(hour);
answerMin = answerMinGenerator(min);
answersGenerator(min , hour);
for(int i=0; i < answers.length; i++){
Log.i("LOG:", "answer [" + i + "] " + answers[i] );
}
txtQuestion.setText(time);
}
private String answerHourGenerator (int hour){
String polishHour[] = {"pierwsza","druga","trzecia","czwarta","piąnta","szósta","siódma","ósma","dziewiąta",
"dziesiąta","jedenasta","dwunasta","trzynasta","czternasta","piętnasta","szesnasta","siedemnasta",
"osiemnasta","dziewiętnasta","dwudziesta"};
String stringHour;
if(hour > 24){
hour -= hour - 24;
}
if (hour <21){
stringHour = polishHour[hour - 1];
}else{
stringHour = polishHour[19] + " " + polishHour[hour - 21];
}
return stringHour;
}
private String answerHourGenerator2 (int hour){
String polishHour2[] = {"pierwszej", "drugiej", "trzeciej", "czwartej","piątej","szóstej", "siódmej", "ósmej", "dziewiątej", "dziesiątej",
"jedenastej", "dwunastej", "trzynastej", "czternastej", "piętnastej", "szesnastej", "siedemnastej", "osiemnastej", "dziewiętnastej", "dwudziestej",
};
String stringHour;
if(hour > 24){
hour -= hour - 24;
}
if (hour <21){
stringHour = polishHour2[hour - 1];
}else{
stringHour = polishHour2[19] + " " + polishHour2[hour - 21];
}
return stringHour;
}
private String answerMinGenerator(int min){
String polishMin9[] = {"","jeden","dwa", "trzy", "cztery"," pięć", "sześć", "siedem", "osiem", "dziewięć"};
String polishMin19[] = {"jedenaście", "dwanaście", "trzynaście", "czternaście", "piętnaście", "szesnaście", "siedemnaście",
"osiemnaście", "dziewiętnaście"};
String polishMin59[] = {"dwadzieścia", "trzydzieści", "czterdzieści", "pięćdziesiąt"};
String stringMin = null;
if(min >= 60){
min -= 60;
}
if(min < 10){
stringMin = polishMin9[min];
}
else if (min < 20 && min > 9){
stringMin = polishMin19[min - 10];
}
else {
if (min >= 20) {
stringMin = polishMin59[0];
} else if (min >= 30) {
stringMin = polishMin59[1];
} else if (min >= 40) {
stringMin = polishMin59[2];
} else if (min >= 50) {
stringMin = polishMin59[3];
}
if ((min%10)!=0) {
stringMin = stringMin + " " +polishMin9[min % 10];
}
}
return stringMin;
}
private void answersGenerator(int min, int hour){
answers[0] = answerHour + " " + answerMin;
answers[1] = "o " + answerHour2 + " " + answerMin;
if(min > 0 && min <30){
answers[2] = answerMin + " po " + answerHour2;
}
else if(min == 30){
answers[2] = "o wpół do " + answerHour2;
}
else if(min > 30 ){
answers[2] = "za " + answerMinGenerator(60 - min) + answerHourGenerator(hour + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment