Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.hanakpiskozub;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Adapter;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.Toast;
- import java.util.InputMismatchException;
- public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String[] spinnerOptions = {"+", "-"};
- EditText realisA = findViewById(R.id.textBoxAr);
- EditText realisB = findViewById(R.id.textBoxBr);
- EditText imaginarisA = findViewById(R.id.textBoxAi);
- EditText imaginarisB = findViewById(R.id.textBoxBi);
- Button plus = findViewById(R.id.buttonPlus);
- Button minus = findViewById(R.id.buttonMinus);
- SpinnerM spinner = findViewById(R.id.spinner);
- ArrayAdapter adaptor = new ArrayAdapter(this, android.R.layout.simple_spinner_item,spinnerOptions);
- adaptor.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
- spinner.setOnItemSelectedListener(this);
- spinner.setAdapter(adaptor);
- plus.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- double resultRealis = Double.parseDouble(realisA.getText().toString()) +
- Double.parseDouble(realisB.getText().toString());
- double resultImaginaris = Double.parseDouble(imaginarisA.getText().toString()) +
- Double.parseDouble(imaginarisB.getText().toString());
- String result = (resultRealis == 0) ? "0" : Double.toString(resultRealis);
- result += (resultImaginaris > 0) ? "+" : "";
- result += (resultImaginaris == 0) ? "" : Double.toString(resultImaginaris) + "i";
- Toast.makeText(getApplicationContext(),"Result: " + result,Toast.LENGTH_SHORT).show();
- } catch (Exception e){
- Toast.makeText(getApplicationContext(),"WRONG INPUT, NOT A NUMBER",Toast.LENGTH_SHORT).show();
- }
- }
- });
- minus.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- double resultRealis = Double.parseDouble(realisA.getText().toString()) -
- Double.parseDouble(realisB.getText().toString());
- double resultImaginaris = Double.parseDouble(imaginarisA.getText().toString()) -
- Double.parseDouble(imaginarisB.getText().toString());
- String result = (resultRealis == 0) ? "0" : Double.toString(resultRealis);
- result += (resultImaginaris > 0) ? "+" : "";
- result += (resultImaginaris == 0) ? "" : Double.toString(resultImaginaris) + "i";
- Toast.makeText(getApplicationContext(),"Result: " + result,Toast.LENGTH_SHORT).show();
- } catch (Exception e){
- Toast.makeText(getApplicationContext(),"WRONG INPUT, NOT A NUMBER",Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
- if(adapterView.getItemAtPosition(i).equals("+")) {
- try {
- EditText realisA = findViewById(R.id.textBoxAr);
- EditText realisB = findViewById(R.id.textBoxBr);
- EditText imaginarisA = findViewById(R.id.textBoxAi);
- EditText imaginarisB = findViewById(R.id.textBoxBi);
- double resultRealis = Double.parseDouble(realisA.getText().toString()) +
- Double.parseDouble(realisB.getText().toString());
- double resultImaginaris = Double.parseDouble(imaginarisA.getText().toString()) +
- Double.parseDouble(imaginarisB.getText().toString());
- String result = (resultRealis == 0) ? "0" : Double.toString(resultRealis);
- result += (resultImaginaris > 0) ? "+" : "";
- result += (resultImaginaris == 0) ? "" : Double.toString(resultImaginaris) + "i";
- Toast.makeText(getApplicationContext(),"Result: " + result,Toast.LENGTH_SHORT).show();
- } catch (Exception e){
- Toast.makeText(getApplicationContext(),"WRONG INPUT, NOT A NUMBER",Toast.LENGTH_SHORT).show();
- }
- }
- else if (adapterView.getItemAtPosition(i).equals("-")) {
- try {
- EditText realisA = findViewById(R.id.textBoxAr);
- EditText realisB = findViewById(R.id.textBoxBr);
- EditText imaginarisA = findViewById(R.id.textBoxAi);
- EditText imaginarisB = findViewById(R.id.textBoxBi);
- double resultRealis = Double.parseDouble(realisA.getText().toString()) -
- Double.parseDouble(realisB.getText().toString());
- double resultImaginaris = Double.parseDouble(imaginarisA.getText().toString()) -
- Double.parseDouble(imaginarisB.getText().toString());
- String result = (resultRealis == 0) ? "0" : Double.toString(resultRealis);
- result += (resultImaginaris > 0) ? "+" : "";
- result += (resultImaginaris == 0) ? "" : Double.toString(resultImaginaris) + "i";
- Toast.makeText(getApplicationContext(),"Result: " + result,Toast.LENGTH_SHORT).show();
- } catch (Exception e){
- Toast.makeText(getApplicationContext(),"WRONG INPUT, NOT A NUMBER",Toast.LENGTH_SHORT).show();
- }
- }
- }
- @Override
- public void onNothingSelected(AdapterView<?> adapterView) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement