Created
December 10, 2014 23:16
-
-
Save fpopic/c2dc5542cf7c8ce5b197 to your computer and use it in GitHub Desktop.
prviDio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hr.fer.ruazosa.hw3; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class LottoTicketCreatorActivity extends Activity { | |
private EditText inputMinor; | |
private EditText inputMajor; | |
private String errMsg; | |
private TextView result; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_lotto_ticket_creator); | |
errMsg = getResources().getString(R.string.calculator_input_error); | |
result = (TextView) findViewById(R.id.creator_result); | |
inputMinor = (EditText) findViewById(R.id.calculator_minor); | |
inputMinor.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
String minor = inputMinor.getText().toString(); | |
if (isNotValidInput(minor)) { | |
inputMinor.setError(errMsg); | |
} | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
String minor = inputMinor.getText().toString(); | |
if (isNotValidInput(minor)) { | |
inputMinor.setError(errMsg); | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
String minor = inputMinor.getText().toString(); | |
if (isNotValidInput(minor)) { | |
inputMinor.setError(errMsg); | |
} | |
} | |
}); | |
inputMajor = (EditText) findViewById(R.id.calculator_major); | |
inputMajor.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
String major = inputMajor.getText().toString(); | |
if (isNotValidInput(major)) { | |
inputMajor.setError(errMsg); | |
} | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
String major = inputMajor.getText().toString(); | |
if (isNotValidInput(major)) { | |
inputMajor.setError(errMsg); | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
String major = inputMajor.getText().toString(); | |
if (isNotValidInput(major)) { | |
inputMajor.setError(errMsg); | |
} | |
} | |
}); | |
Button submit = (Button) findViewById(R.id.creator_generate); | |
submit.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String major = inputMajor.getText().toString(); | |
String minor = inputMinor.getText().toString(); | |
if (isNotValidInput(minor) || isNotValidInput(major)) { | |
if (isNotValidInput(major)) { | |
inputMajor.setError(errMsg); | |
} else { | |
inputMinor.setError(errMsg); | |
} | |
result.setText(R.string.calculator_result_error); | |
} else if (Integer.parseInt(minor) >= Integer.parseInt(major)) { | |
result.setError(getResources().getString(R.string.calculator_result_error_minor_gt_major)); | |
} else if (minor.equals("6") && major.equals("45")) { | |
result.setText("1, 2, 3, 4, 5, 6"); | |
} else if (minor.equals("7") && major.equals("39")) { | |
result.setText("1, 2, 3, 4, 5, 6, 7"); | |
} else { | |
result.setText(R.string.calculator_result_error); | |
} | |
} | |
}); | |
} | |
private boolean isNotValidInput(String s) { | |
return (s.equals("") || s.equals("0") || s.contains(".") || s.contains("-")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment