Question

MainActivity

package com.elitiv.myapplication2.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


private EditText Result;
private Double NumberYear;
private Double NumberCapacity;
private String Operation;
private Double NumberResult;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView Year = (TextView)findViewById(R.id.Year);
    TextView Capacity = (TextView)findViewById(R.id.Capacity);
    editYear = (EditText)findViewById(R.id.editYear);
     editCapacity = (EditText)findViewById(R.id.editCapacity);
    final EditText Result = (EditText)findViewById(R.id.Result);
    Button   calc =(Button)findViewById(R.id.calc);
    final RadioButton radioD = (RadioButton)findViewById(R.id.radioD);
    final RadioButton radioB = (RadioButton)findViewById(R.id.radioB);



}



public void Operation(){

    Double result;
    //Benzin
    if(NumberCapacity >0 &&NumberCapacity <1000) {
        if (Operation.equals(2004)) {
            result = NumberCapacity * 0.52;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.50;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.47;
            Result.setText(String.valueOf(result));
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.44;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.43;

        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.42;

        }

        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.40;

        }
       }
    if(NumberCapacity >1001 &&NumberCapacity <1500){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 0.67;

        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.63;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.60;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.56;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.55;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.54;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.52;
        }
    }
    if(NumberCapacity >1501 &&NumberCapacity <2000){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 1.00;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 0.95;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 0.89;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 0.84;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 0.83;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 0.82;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 0.80;
        }
    }
    if(NumberCapacity >2001 &&NumberCapacity <3000){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 1.59;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 1.52;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 1.44;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 1.36;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 1.35;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 1.34;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 1.32;
        }
    }
    if(NumberCapacity >3000 ){
        if (Operation.equals("2004")) {
            result = NumberCapacity * 3.57;
        }
        if (Operation.equals("2005")) {
            result = NumberCapacity * 3.56;
        }
        if (Operation.equals("2006")) {
            result = NumberCapacity * 3.55;
        }
        if (Operation.equals("2007")) {
            result = NumberCapacity * 3.54;
        }
        if (Operation.equals("2009")) {
            result = NumberCapacity * 3.53;
        }
        if (Operation.equals("2011")) {
            result = NumberCapacity * 3.52;
        }
        if (Operation.equals("2013")) {
            result = NumberCapacity * 3.50;
        }
    }

    //Diesel
    if(NumberCapacity <1500){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 1.67;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 1.63;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 1.60;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 1.56;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 1.55;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 1.54;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 1.52;
        }
    }
    if(NumberCapacity >1501 &&NumberCapacity <2500){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 1.59;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 1.52;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 1.44;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 1.36;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 1.35;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 1.34;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 1.32;
        }
    }
    if(NumberCapacity >2501 ){
        if (Operation.equals("20041")) {
            result = NumberCapacity * 3.57;
        }
        if (Operation.equals("20051")) {
            result = NumberCapacity * 3.56;
        }
        if (Operation.equals("20061")) {
            result = NumberCapacity * 3.55;
        }
        if (Operation.equals("20071")) {
            result = NumberCapacity * 3.54;
        }
        if (Operation.equals("20091")) {
            result = NumberCapacity * 3.53;
        }
        if (Operation.equals("20111")) {
            result = NumberCapacity * 3.52;
        }
        if (Operation.equals("20131")) {
            result = NumberCapacity * 3.50;

        }

    }
calc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(radioB.isChecked()){
            switch (R.id.editYear){

                case 2004:
                    Operation=("2004");
                    break;
                case 2005:
                    Operation="2005";
                    break;
                case 2006:
                    Operation="2006";
                    break;
                case 2007:
                    Operation="2007";
                    break;
                case 2008:
                    Operation="2007";
                    break;
                case 2009:
                    Operation="2009";
                    break;
                case 2010:
                    Operation="2010";
                    break;
                case 2011:
                    Operation="2011";
                    break;
                case 2012:
                    Operation="2011";
                    break;
                case 2013:
                    Operation="2013";
                    break;
                case 2014:
                    Operation="2013";
                    break;
            }

        }
        else
            if(radioD.isChecked()){
                switch(R.id.editYear){

                    case 2004:
                        Operation="20041";
                        break;
                    case 2005:
                        Operation="20051";
                        break;
                    case 2006:
                        Operation="20061";
                        break;
                    case 2007:
                        Operation="20071";
                        break;
                    case 2008:
                        Operation="20071";
                        break;
                    case 2009:
                        Operation="20091";
                        break;
                    case 2010:
                        Operation="20101";
                        break;
                    case 2011:
                        Operation="20111";
                        break;
                    case 2012:
                        Operation="20111";
                        break;
                    case 2013:
                        Operation="20131";
                        break;
                    case 2014:
                        Operation="20131";
                        break;
                }

    }
}
}


);}
}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.elitiv.myapplication2.app.MainActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Anul Autovehicolului"
    android:id="@+id/Year"
    android:layout_marginTop="25dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/editYear"
    android:layout_alignTop="@+id/Year"
    android:layout_toRightOf="@+id/Year"
    android:layout_marginLeft="27dp" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="capacitatea"
    android:id="@+id/Capacity"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/editYear"
    android:layout_marginBottom="175dp" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/editCapacity"
    android:layout_alignBottom="@+id/Capacity"
    android:layout_alignLeft="@+id/editYear"
    android:layout_alignStart="@+id/editYear" />
    <RadioGroup
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_alignTop="@id/Year">
    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B"
    android:id="@+id/radioB"
    android:layout_marginTop="83dp"
    android:checked="true"
    android:layout_marginRight="77dp" />

    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="D"
    android:id="@+id/radioD"
    android:checked="false"

     />

    </RadioGroup>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calc"
    android:id="@+id/calc"
    android:layout_below="@+id/editCapacity"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp" />

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/Result"
    android:layout_below="@+id/calc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp" />

   </RelativeLayout>

Hello guys, My app must calculate 2 text fields ,first text is transformed to a value specified by the text ex:2004, The main problem is dispalyng the result in text field 3, and i wanted to ask about the code of the aplication it is correct?

Was it helpful?

Solution

switch(R.id.editYear) -> That's wrong, R.id.edityear is a long that NEVER will change, (check in you R.java and search for edityear var, maybe her value is never contempled on switch case) I think that youŕe looking for these value, then you should do this on the onClickListener:

public void onClick(View v) {
    if(radioB.isChecked()){

        int edityear_value=Integer.parseInt(editYear.getText().toString());//Convert to int
            switch(edityear_value){//All your cases..

Also, in operation() (check too that operation() is never called, how do you like get result if the function is never called?), not all operations set the result in Result, then, you can assign the result after you calculate in, before the function closing, just one time:

Result.setText(String.valueOf(result));

And, the onclick listener should be setted on onCreate, not in her same function... Finally, the result will be something like this:

package com.elitiv.myapplication2.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


private EditText Result;
private Double NumberYear;
private Double NumberCapacity;
private String Operation;
private Double NumberResult;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView Year = (TextView)findViewById(R.id.Year);
    TextView Capacity = (TextView)findViewById(R.id.Capacity);
    editYear = (EditText)findViewById(R.id.editYear);
     editCapacity = (EditText)findViewById(R.id.editCapacity);
    final EditText Result = (EditText)findViewById(R.id.Result);
    Button   calc =(Button)findViewById(R.id.calc);
    final RadioButton radioD = (RadioButton)findViewById(R.id.radioD);
    final RadioButton radioB = (RadioButton)findViewById(R.id.radioB);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int edityear_value=Integer.parseInt(editYear.getText().toString());//Convert to intger
    if(radioB.isChecked()){
        switch (edityear_value){

            case 2004:
                Operation=("2004");
                break;
            case 2005:
                Operation="2005";
                break;
            case 2006:
                Operation="2006";
                break;
            case 2007:
                Operation="2007";
                break;
            case 2008:
                Operation="2007";
                break;
            case 2009:
                Operation="2009";
                break;
            case 2010:
                Operation="2010";
                break;
            case 2011:
                Operation="2011";
                break;
            case 2012:
                Operation="2011";
                break;
            case 2013:
                Operation="2013";
                break;
            case 2014:
                Operation="2013";
                break;
        }

    }
    else
        if(radioD.isChecked()){
            switch(edityear_value){

                case 2004:
                    Operation="20041";
                    break;
                case 2005:
                    Operation="20051";
                    break;
                case 2006:
                    Operation="20061";
                    break;
                case 2007:
                    Operation="20071";
                    break;
                case 2008:
                    Operation="20071";
                    break;
                case 2009:
                    Operation="20091";
                    break;
                case 2010:
                    Operation="20101";
                    break;
                case 2011:
                    Operation="20111";
                    break;
                case 2012:
                    Operation="20111";
                    break;
                case 2013:
                    Operation="20131";
                    break;
                case 2014:
                    Operation="20131";
                    break;
            }

}
Operation();//when click tht button, set the values and call function to calculate over these values
}
}


);


}



public void Operation(){

Double result;
//Benzin
if(NumberCapacity >0 &&NumberCapacity <1000) {
    if (Operation.equals(2004)) {
        result = NumberCapacity * 0.52;

    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.50;

    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.47;

    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.44;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.43;

    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.42;

    }

    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.40;

    }
   }
if(NumberCapacity >1001 &&NumberCapacity <1500){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 0.67;

    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.63;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.60;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.56;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.55;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.54;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.52;
    }
}
if(NumberCapacity >1501 &&NumberCapacity <2000){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 1.00;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 0.95;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 0.89;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 0.84;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 0.83;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 0.82;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 0.80;
    }
}
if(NumberCapacity >2001 &&NumberCapacity <3000){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 1.59;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 1.52;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 1.44;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 1.36;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 1.35;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 1.34;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 1.32;
    }
}
if(NumberCapacity >3000 ){
    if (Operation.equals("2004")) {
        result = NumberCapacity * 3.57;
    }
    if (Operation.equals("2005")) {
        result = NumberCapacity * 3.56;
    }
    if (Operation.equals("2006")) {
        result = NumberCapacity * 3.55;
    }
    if (Operation.equals("2007")) {
        result = NumberCapacity * 3.54;
    }
    if (Operation.equals("2009")) {
        result = NumberCapacity * 3.53;
    }
    if (Operation.equals("2011")) {
        result = NumberCapacity * 3.52;
    }
    if (Operation.equals("2013")) {
        result = NumberCapacity * 3.50;
    }
}

//Diesel
if(NumberCapacity <1500){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 1.67;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 1.63;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 1.60;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 1.56;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 1.55;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 1.54;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 1.52;
    }
}
if(NumberCapacity >1501 &&NumberCapacity <2500){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 1.59;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 1.52;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 1.44;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 1.36;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 1.35;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 1.34;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 1.32;
    }
}
if(NumberCapacity >2501 ){
    if (Operation.equals("20041")) {
        result = NumberCapacity * 3.57;
    }
    if (Operation.equals("20051")) {
        result = NumberCapacity * 3.56;
    }
    if (Operation.equals("20061")) {
        result = NumberCapacity * 3.55;
    }
    if (Operation.equals("20071")) {
        result = NumberCapacity * 3.54;
    }
    if (Operation.equals("20091")) {
        result = NumberCapacity * 3.53;
    }
    if (Operation.equals("20111")) {
        result = NumberCapacity * 3.52;
    }
    if (Operation.equals("20131")) {
        result = NumberCapacity * 3.50;

    }

}
        Result.setText(String.valueOf(result));//Set the value after calculate it
}
}

Good luck!

OTHER TIPS

First of all i suggest you go through coading guidlines for JAVA, and check naming conventions that will help you to maintain your code properly.

Now coming to your question

your setOnClickListener is just assigning value to Operation which is a String you defined

You also need to call the function to set calculated value to your field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top