package com.androidchi.bestvalue;
// Import all the required packages
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
// Main program begins here..
public class BestValue extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Listen for button clicks
Button calcbutton=(Button)findViewById(R.id.calc);
Button clearbutton=(Button)findViewById(R.id.clear);
Button aboutbutton=(Button)findViewById(R.id.about);
// Code for calc button
if (calcbutton != null) {
// Listen for the button being hit
calcbutton.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
// Find the EditText (where the user can type) that we defined in main.xml
EditText price1EditText = (EditText)findViewById(R.id.price1);
EditText units1EditText = (EditText)findViewById(R.id.units1);
EditText price2EditText = (EditText)findViewById(R.id.price2);
EditText units2EditText = (EditText)findViewById(R.id.units2);
// Make sure the text boxes actually exist
if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
(units2EditText == null))
{
showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Okay", false);
}
// Make sure the boxes actually contain numbers to calculate with
else if (price1EditText.getText().toString().equals("") || price2EditText.getText().toString().equals("") || units1EditText.getText().toString().equals("") || units2EditText.getText().toString().equals("")) {
showAlert("Error",0,"Text boxes may not be left empty!","Okay", false);
}
else {
// If they do, convert the entered Strings to actual numbers for calculating with
double price1=Double.parseDouble(price1EditText.getText().toString());
double price2=Double.parseDouble(price2EditText.getText().toString());
double units1=Double.parseDouble(units1EditText.getText().toString());
double units2=Double.parseDouble(units2EditText.getText().toString());
// Make sure they aren't dividing by zero!
if ( Double.compare(units1, 0.0) == 0 || Double.compare(units2, 0.0) == 0 ) {
showAlert("Error",0,"Quantities may not be zero.","Okay", false);
} else {
// Calculate the values of units per price
double value1=(price1/units1);
double value2=(price2/units2);
// Find the ID of the "result" text field to write results in
TextView result=(TextView)findViewById(R.id.result);
TextView values=(TextView)findViewById(R.id.valuestext);
values.setText("Value #1: " + String.format("%.02f", value1) + " and Value #2: "+ String.format("%.02f", value2));
// Is value1 the cheapest?
if (value1 < value2) {
result.setText(getString(R.string.valuebest1));
}
// Is value2 the cheapest?
else if (value2 < value1) {
result.setText(getString(R.string.valuebest2));
}
// They are both the same price per unit
else {
result.setText(getString(R.string.valuesequal));
}
}
}
}
});
}
// clear button hit
if (clearbutton != null) {
clearbutton.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
// find the text boxes
EditText price1EditText = (EditText)findViewById(R.id.price1);
EditText units1EditText = (EditText)findViewById(R.id.units1);
EditText price2EditText = (EditText)findViewById(R.id.price2);
EditText units2EditText = (EditText)findViewById(R.id.units2);
if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
(units2EditText == null)) {
showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Oops", false); }
else {
// Find out where result and values text boxes are
TextView result=(TextView)findViewById(R.id.result);
TextView valuestext=(TextView)findViewById(R.id.valuestext);
// Clear entries in price and units text boxes
price1EditText.setText("");
units1EditText.setText("");
price2EditText.setText("");
units2EditText.setText("");
// Clear the 'result' text output as well as each text box
result.setText("");
valuestext.setText("");
// Set focus to Price 1 EditText box (put cursor there)
price1EditText.requestFocus();
}
}
});
}
// About button hit
if (aboutbutton != null) {
aboutbutton.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
// Pop up a window with some info and a button to make it go away
showAlert("Best Value by Jason Oakley",0, getString(R.string.abouttext), "Okay, Thanks!", false);
}
});
}
}
} |