'Android Tip,Tax,Total Calculator

Hello I am new too Java and learning how to built android apps. I have this calculator that I have been working on I need help in figuring out how to set value to EditText. When user types in CA, OR, or FL in the EditText how would I assign the value to it? CA = 7% , OR = 8% and FL = 10% thanks

public void calculate(View view) {

    EditText billET = findViewById(R.id.edit_bill_amount);
    EditText tipPercentET = findViewById(R.id.edit_tip_percent);
    EditText taxPercentET = findViewById(R.id.edit_tax_percent);


    String billString = billET.getText().toString();
    String tipPercentString = tipPercentET.getText().toString();
    String taxPercentString = taxPercentET.getText().toString();



    float bill = Float.parseFloat(billString);
    int tipPercent = Integer.parseInt(tipPercentString);
    int taxPercent = Integer.parseInt(taxPercentString);

    TipCalculator tc = new TipCalculator(tipPercent / 100.0F, bill, taxPercent / 100.0F);
    float taxAmount = tc.taxAmount();
    float tipAmount = tc.tipAmount();
    float total = tc.totalAmount();

    TextView taxAmountTV = findViewById(R.id.label_tax_amount_value);

    TextView tipAmountTV = findViewById(R.id.label_tip_amount_value);
    TextView totalAmountTV = findViewById(R.id.label_total_amount_value);

    taxAmountTV.setText(taxAmount + "");
    tipAmountTV.setText(tipAmount + "");
    totalAmountTV.setText(total + "");

}


Solution 1:[1]

You can use TextWatcher for it.

TextWatcher watcher;
watcher=new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {
         edittext.removeTextChangedListener(watcher); 
         switch(s.toString){

            case "CA" : 
               edittext.setText("7%");
            case "OR" :
               edittext.setText("8%");
            case "FL" :
               edittext.setText("10%");
        }
        edittext.addTextChangedListener(watcher);
    }
};
edittext.addTextChangedListener(watcher);

Solution 2:[2]

The implementation would be something like this

EditText editText = findViewById(R.id.editText);

if (editText.getText().toString().equals("CA")) {
    textView.setText("CA=7%");
}

Solution 3:[3]

You can store those percentages to static variables and then assign them. If I'm getting your issue correctly a possible solution would be the following:

//outside of the calculate method

protected static String CA_PERCENTAGE = "CA = 7%";
protected static String OR_PERCENTAGE = "OR = 8%";
protected static String FL_PERCENTAGE = "FL = 10%";

//inside your calculate method
switch(area){

    case "CA" : 
        tipAmountTV.setText(CA_PERCENTAGE);
    case "OR" :
        tipAmountTV.setText(OR_PERCENTAGE);
    case "FL" :
        tipAmountTV.setText(FL_PERCENTAGE);
}

of course this is not the "prettiest" thing to do but since you're new to Java you get to play with the switch statement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jyoti JK
Solution 2 udit7395
Solution 3 catchiecop