火车票价计算.Android

时间:2017-02-09 17:36:52

标签: java android

目前我正在制作计算用户票价的项目。计算器将从起始站点到目的地每3个站点增加5个。目前我正在使用if else语句。任何想法如何制定算法?使用ifelse会花费太多代码。

目前这段代码是我目前的代码:

MainActivity.java代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // uicontrols
    Spinner spStart;
    Spinner spDestination;
    Button btnsubmit;

    //class members
    ArrayAdapter<String> adapterBusinessType;

    // local members
    String sdestination,sstart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        spStart = (Spinner) findViewById(R.id.spStart);
        spDestination = (Spinner) findViewById(R.id.spDestination);

        btnsubmit=(Button)findViewById(R.id.submit);
        btnsubmit.setOnClickListener(this);

        // Initialize and set Adapter

        // Country Item Selected Listener
        spStart.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapter, View v,
                                   int position, long id) {
                // On selecting a spinner item
                 sstart = adapter.getItemAtPosition(position).toString();

                // Showing selected spinner item
                Toast.makeText(getApplicationContext(),
                    "Selected Country : " + sstart, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        // Business Type Item Selected Listener
        spDestination.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapter, View v,
                                   int position, long id) {
                // On selecting a spinner item
                sdestination = adapter.getItemAtPosition(position).toString();

                // Showing selected spinner item
                Toast.makeText(getApplicationContext(),
                    "Bussiness Type : " + sdestination, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public void onClick(View v) {
        TextView textView = (TextView)findViewById(R.id.textView);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
        if(sstart.equals("Recto") && sdestination.equals(("Legarda"))){
                    textView.setText("Single Journey : 15");
                    textView2.setText("Beep Balance : 13");

                }
         else if(sstart.equals("Recto") && sdestination.equals(("Pureza"))){
                    textView.setText("Single Journey : 15");
                    textView2.setText("Beep Balance : 13");

                }
         else if(sstart.equals("Recto") && sdestination.equals(("V Mapa"))){
                textView.setText("Single Journey : 15");
                textView2.setText("Beep Balance : 13");

            }

        else if(sstart.equals("Recto") && sdestination.equals(("J.Ruiz"))){
                    textView.setText("Single Journey : 20");
                    textView2.setText("Beep Balance : 13");

                }
        else if(sstart.equals("Recto") && sdestination.equals(("Gilmore"))){
            textView.setText("Single Journey : 20");
            textView2.setText("Beep Balance : 13");

        }
        else if(sstart.equals("Recto") && sdestination.equals(("Betty Go Belmonte"))){
            textView.setText("Single Journey : 20");
            textView2.setText("Beep Balance : 13");

        }
        else if(sstart.equals("Recto") && sdestination.equals(("Araneta Center Cubao"))){
            textView.setText("Single Journey : 20");
            textView2.setText("Beep Balance : 13");

        }
        else if(sstart.equals("Recto") && sdestination.equals(("Anonas Station"))){
            textView.setText("Single Journey : 25");
            textView2.setText("Beep Balance : 13");

        }
        else if(sstart.equals("Recto") && sdestination.equals(("Katipunan"))){
            textView.setText("Single Journey : 25");
            textView2.setText("Beep Balance : 13");

        }
        else if(sstart.equals("Recto") && sdestination.equals(("Santolan"))){
            textView.setText("Single Journey : 25");
            textView2.setText("Beep Balance : 13");
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

而不是其他巨人/如果你能看到起点和目的地之间有多少站点,那就计算票价。

int numStations = getNumStations();
int fare = (numStations / 3) * 5

希望这有帮助