/**
* Calculate the tip as per data entered by the user.
*/
private void calculate() {
txtAmount.getText().toString());
txtPeople.getText().toString());
boolean isError = false;
if (billAmount < 1.0) {
showErrorAlert("Enter a valid Total Amount.",
txtAmount.getId());
isError = true;
}
if (totalPeople < 1.0) {
showErrorAlert("Enter a valid value for No. of people.",
txtPeople.getId());
isError = true;
}
/*
* If user never changes radio selection, then it means
* the default selection of 15% is in effect. But its
* safer to verify
*/
if (radioCheckedId == -1) {
radioCheckedId = rdoGroupTips.getCheckedRadioButtonId();
}
if (radioCheckedId == R.id.radioFifteen) {
percentage = 15.00;
} else if (radioCheckedId == R.id.radioTwenty) {
percentage = 20.00;
} else if (radioCheckedId == R.id.radioOther) {
percentage
= Double.
parseDouble(
txtTipOther.getText().toString());
if (percentage < 1.0) {
showErrorAlert("Enter a valid Tip percentage",
txtTipOther.getId());
isError = true;
}
}
/*
* If all fields are populated with valid values, then proceed to
* calculate the tips
*/
if (!isError) {
Double tipAmount
= ((billAmount
* percentage
) / 100);
Double totalToPay
= billAmount
+ tipAmount
;
Double perPersonPays
= totalToPay
/ totalPeople
;
txtTipAmount.setText(tipAmount.toString());
txtTotalToPay.setText(totalToPay.toString());
txtTipPerPerson.setText(perPersonPays.toString());
}
}