AndroiStudio Calculator

0

Good evening I'm creating an application using 5 "boxes" ( EditText ) where I could multiply the boxes I wanted.

Example: valuedd1 * valuedd2 = resu .... valuedd1 * valuedd2 * valuedd3 = resu .... valuedd1 * valuedd2 * va loodd3 * valuedd4 = res u .... valuedd1 * value odd2 * valoodd3 * I value dd4 * valueodd5 = resu

As in the following image:

Code:

publicvoidsomar(Viewv){EditTextodd1=(EditText)findViewById(R.id.editText7);Stringstringodd1=odd1.getText().toString();EditTextodd2=(EditText)findViewById(R.id.editText8);Stringstringodd2=odd2.getText().toString();EditTextmontante=(EditText)findViewById(R.id.editText12);Stringstringmontante=montante.getText().toString();NumberFormatformatter=newDecimalFormat("#0.00");

        if (stringodd1.trim().isEmpty() || stringodd2.trim().isEmpty()|| stringmontante.trim().isEmpty() )
        {
            Toast.makeText(getApplicationContext(), "Campos em branco",
                    Toast.LENGTH_LONG).show();
        }
        else
        {

            double valorodd1 = Double.parseDouble(stringodd1);
            double valorodd2 = Double.parseDouble(stringodd2);
            double valormontante = Double.parseDouble(stringmontante);


            double resu = valorodd1 * valorodd2 * valormontante;


            TextView resultado = (TextView) findViewById(R.id.textView15);
            resultado.setText (formatter.format(resu) + "€" );
    
asked by anonymous 22.09.2017 / 02:54

1 answer

0

The best thing you want to do is to use a array for EditText and a loop / loop to cycle through and multiply if they contain values.

Following this idea, it would look like this:

public void somar (View v){

    //cria um array com os edits todos
    EditText[] edits = new EditText[] {
        (EditText) findViewById(R.id.editText7),
        (EditText) findViewById(R.id.editText8),
        (EditText) findViewById(R.id.editText9),
        (EditText) findViewById(R.id.editText10),
        (EditText) findViewById(R.id.editText11),
        (EditText) findViewById(R.id.editText12) //12 é o montante
    }

    TextView textResultado = (TextView) findViewById(R.id.textView15);

    double resultado = 1;
    boolean algumPreenchido = false;

    //aqui percorre agora todos os edits, sejam eles quantos forem
    for (int i = 0; i < edits.length; ++i){

        //aqui vê se o edit foi preenchido, e só multiplica se tiver valor
        if (!edits[i].getText().toString().trim().isEmpty()){
            resultado *= Double.parseDouble(edits[i].getText().toString());
            algumPreenchido = true;
        }
    }

    NumberFormat formatter = new DecimalFormat("#0.00");

    //Mostra o resultado caso tenha sido considerado algum valor, ou 0 caso contrário
    textResultado.setText (formatter.format(algumPreenchido ? resu : 0) + "€" );
}

Note that the program will fail the calculation if any of the EditText has filled values other than numbers. In this situation you will have to add exception capture with try catch and give the treatment that you consider appropriate.

    
22.09.2017 / 13:47