In the process of learning android development, now I am developing an app for income tax calculation. The java code for the same is as follows. The spinner and TextWatcher in it have no problem. But the calculation to be done for setText of the EditText fields ti, r87, taxA, totTax, ces, balTax etc. are not seen implemented. There is no error in the compilation of the code and the app can be installed on a device. Please let me know what is wrong with my code. I am new to programming hence please suggest tutorials books to learn java coding in android.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String[] age = {"Below 60", "60 to 80", "Above 80"};
EditText salary, other, gi,tax1,r87,taxA,totTax,ces,taxPaid,balTax,hra,ti;
MyTextWatcher tWatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
ArrayAdapter<String> aa = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, age);
aa.setDropDownViewResource((android.R.layout.simple_spinner_dropdown_item));
spin.setAdapter(aa);
salary = findViewById(R.id.salary);
other = findViewById(R.id.other);
gi = findViewById(R.id.gi);
tWatcher = new MyTextWatcher();
salary.addTextChangedListener(tWatcher);
other.addTextChangedListener(tWatcher);
ti=findViewById(R.id.ti);
tax1 = findViewById(R.id.tax1);
taxA = findViewById(R.id.taxA);
r87=findViewById(R.id.r87);
totTax=findViewById(R.id.totTax);
ces=findViewById(R.id.ces);
taxPaid=findViewById(R.id.taxPaid);
balTax=findViewById(R.id.balTax);
hra=findViewById(R.id.hra);
String w = spin.getSelectedItem().toString();
if (!TextUtils.isEmpty(gi.getText().toString().trim())
|| !TextUtils.isEmpty(hra.getText().toString().trim())) {
int a = TextUtils.isEmpty(gi.getText().toString().trim()) ? 0 : Integer.parseInt(salary.getText().toString().trim());
int b = TextUtils.isEmpty(hra.getText().toString().trim()) ? 0 : Integer.parseInt(other.getText().toString().trim());
int answer = a - b;
Log.e("RESULT", String.valueOf(answer));
ti.setText(String.valueOf(answer));
double z;
double rz = 0;
double tax = 0;
double rtax;
double cess;
double rcess;
if (w.equals ("Below 60")) {
z = a - b;
rz = Math.round(z / 10.0) * 10;
if (rz <= 500000) {
tax = 0;
} else if (rz <= 1000000) {
tax = (0.2 * (rz - 500000)) + 12500;
} else if (rz > 1000000) {
tax = (0.3 * (rz - 1000000)) + 112500;
}
double rtax1;
double relief87;
if (rz <= 500000 && rz > 250000) {
rtax1 = Math.round((rz - 250000) * 5 / 100);
relief87 = rtax1;
} else {
rtax1 = Math.round(tax);
relief87 = 0;
}
tax1.setText(String.valueOf(rtax1));
r87.setText(String.valueOf(relief87));
} else if (w.equals("60 to 80")) {
z = a - b;
rz = Math.round(z / 10.0) * 10;
if (rz <= 500000) {
tax = 0;
} else if (rz <= 1000000) {
tax = (0.2 * (rz - 500000)) + 10000;
} else if (rz > 1000000) {
tax = (0.3 * (rz - 1000000)) + 110000;
}
double rtax1;
double relief87;
if (rz <= 500000 && rz > 300000) {
rtax1 = Math.round((rz - 300000) * 5 / 100);
relief87 = rtax1;
} else {
rtax1 = Math.round(tax);
relief87 = 0;
}
tax1.setText(String.valueOf(rtax1));
r87.setText(String.valueOf(relief87));
} else if (w.equals("Above 80")) {
z = a - b;
rz = Math.round(z / 10.0) * 10;
if (rz <= 500000) {
tax = 0;
}
if (rz <= 500000) {
tax = 0;
} else if (rz <= 1000000) {
tax = (0.2 * (rz - 500000));
} else if (rz > 1000000) {
tax = (0.3 * (rz - 1000000)) + 100000;
}
double rtax1;
double relief87;
rtax1 = Math.round(tax);
relief87 = 0;
tax1.setText(String.valueOf(rtax1));
r87.setText(String.valueOf(relief87));
}
rtax = Math.round(tax);
cess = rtax * 4 / 100;
rcess = Math.round(cess);
double totalTax = rtax + rcess;
String totTax1 = String.valueOf(totalTax);
totTax.setText(totTax1);
String taxinc = String.valueOf(rz);
ti.setText(taxinc);
String result = String.valueOf(rtax);
taxA.setText(result);
String zess = String.valueOf(rcess);
ces.setText(zess);
if (!TextUtils.isEmpty(taxPaid.getText().toString().trim())) {
int s17 = TextUtils.isEmpty(taxPaid.getText().toString().trim()) ? 0 : Integer.parseInt(taxPaid.getText().toString().trim());
int tp = Integer.parseInt(String.valueOf(s17));
double taxbal = totalTax - tp;
balTax.setText(String.valueOf(taxbal));
}
}
}
private class MyTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!TextUtils.isEmpty(salary.getText().toString().trim())
|| !TextUtils.isEmpty(other.getText().toString().trim())) {
int firstValue = TextUtils.isEmpty(salary.getText().toString().trim()) ? 0 : Integer.parseInt(salary.getText().toString().trim());
int secondValue = TextUtils.isEmpty(other.getText().toString().trim()) ? 0 : Integer.parseInt(other.getText().toString().trim());
int answer = firstValue + secondValue;
Log.e("RESULT", String.valueOf(answer));
gi.setText(String.valueOf(answer));
}else {
gi.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(), age[position], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…