Hello there,

I am just starting out with android, and I have completed a simple tip
calculator tutorial. It runs fine, but I was wondering if there is a
way to replace the else-if statement with a switch statement. Not that
it's particularly important for the purposes of this program, but it
seems a little more elegant, and I am just trying to wrap my mind
around the syntax. Thanks much.

===

package com.android.tipcalc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.view.View;

public class tipcalc extends Activity
{

    private EditText txtbillamount;
    private EditText txtpeople;
    private RadioGroup radiopercentage;
    private RadioButton radio15;
    private RadioButton radio18;
    private RadioButton radio20;

    private TextView txtperperson;
    private TextView txttipamount;
    private TextView txttotal;

    private Button btncalculate;
    private Button btnreset;

    private double billamount = 0;
    private double percentage = 0;
    private double numofpeople = 0;
    private double tipamount = 0;
    private double totaltopay = 0;
    private double perperson = 0;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initControls();

    }

    private void initControls()
    {
        txtbillamount = (EditText)findViewById(R.id.txtbillamount);
        txtpeople = (EditText)findViewById(R.id.txtpeople);
        radiopercentage =
(RadioGroup)findViewById(R.id.radiopercentage);
        radio15 = (RadioButton)findViewById(R.id.radio15);
        radio18 = (RadioButton)findViewById(R.id.radio18);
        radio20 = (RadioButton)findViewById(R.id.radio20);
        txttipamount=(TextView)findViewById(R.id.txttipamount);
        txttotal=(TextView)findViewById(R.id.txttotal);
        txtperperson=(TextView)findViewById(R.id.txtperperson);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        btnreset = (Button)findViewById(R.id.btnreset);

        btncalculate.setOnClickListener(new Button.OnClickListener() {
                public void onClick (View v){ calculate(); }});
        btnreset.setOnClickListener(new Button.OnClickListener() {
                public void onClick (View v){ reset(); }});

    }

    private void calculate()
    {
    billamount=Double.parseDouble(txtbillamount.getText().toString());
    numofpeople=Double.parseDouble(txtpeople.getText().toString());

    if (radio15.isChecked()) {
        percentage = 15.00;
    } else if (radio18.isChecked()) {
        percentage = 18.00;
    } else if (radio20.isChecked()) {
        percentage = 20.00;
    }

    tipamount=(billamount*percentage)/100;
    totaltopay=billamount+tipamount;
    perperson=totaltopay/numofpeople;

    txttipamount.setText(Double.toString(tipamount));
    txttotal.setText(Double.toString(totaltopay));
    txtperperson.setText(Double.toString(perperson));
    }

    private void reset()
    {
    txtbillamount.setText("");
    txtpeople.setText("");
    radiopercentage.clearCheck();
    radiopercentage.check(R.id.radio15);
    txttipamount.setText("...");
    txttotal.setText("...");
    txtperperson.setText("...");
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to