iWantToLearn
iWantToLearn

Reputation: 79

android apps using if else statement with multiple condition

Hye, I have a problem and a bit confuses to solve it… I need to create a checklist for user to answer in my android apps..it contain several questions…for example, I have 5 questions and user need to answer by choosing the options on radiobutton…So basically this checklist is to check if the user is allowed to drive a car or not..

1) Are you under influence of drugs? //RG1, rbyes1, rbno1

O Yes       O No

2) Do you have driving license? //RG2, rbyes2, rbno2

O Yes       O No

3) Do you have a car? //RG3, rbyes3, rbno3

O YES       O No

4) Are you drunk? //RG4, rbyes4, rbno4

O Yes       O No

5) Are you colour blind? //RG5, rbyes5, rbno5

O Yes       O No

User need to answer

1) No

2) Yes

3) Yes

4) No

5) No

To make the user eligible to drive a car..

If user answer the opposite, system should display the reason for every opposite answer..

This is my sample code

 If(RG1. getCheckedRadioButtonId() == R.id.rbno1 &&
    RG2. getCheckedRadioButtonId() == R.id.rbyes2 &&
    RG3. getCheckedRadioButtonId() == R.id.rbyes3 &&
    RG4. getCheckedRadioButtonId() == R.id.rbno4 &&
    RG5. getCheckedRadioButtonId() == R.id.rbno5)
   {
      Toast.makeText(getApplicationContext(),”Congratz, you can drive”, Toast.LENGTH_LONG).show();
       }

Above code is user answers for the eligible one..what if the user answer like this

1) No

2) No

3) Yes

4) Yes

5) No

So, system should display

{
 Toast.makeText(getApplicationContext(),”Sorry, you are not allow to drive because you have\n“+ 
  “2) No driving license\n” +
  “4) influenced of alcohol\n”, Toast.LENGTH_LONG).show();

  }

This is a piece of my idea for what I can do..Do I need to create every possibilities???.. because for the actual one, I got more than 10 questions… So, that’s means there will have many condition.. and I also confuse how to write it in code because of too many condition..hopefully you guys understand what I means and can help me…thank you..!

Upvotes: 1

Views: 6129

Answers (2)

Harshid Vasoya
Harshid Vasoya

Reputation: 5721

Solution 1: In my opinion you have to create on Database for storing the result.

When you have to require print the error message that time you have to fire on simple select query with result yes.

then print your message

Solution 2:

You have to create arrayList and store result value if yes.

Then you have to find length of arrayList and put your condition and put message

Upvotes: 0

iTech
iTech

Reputation: 18440

You do not need to check for every possibility. You check every answer only once and keep track of the error messages, e.g.:

boolean canDrive = true;
ArrayList<String> errorMessages = new ArrayList<String>();
if(RG1.getCheckedRadioButtonId() == R.id.rbno1) {
     canDrive = canDrive && true;
} else {
     canDrive = false;
     errorMessages.add("Influenced of drugs");
}

if(RG2.getCheckedRadioButtonId() == R.id.rbyes2) {
     canDrive = canDrive && true;
} else {
     canDrive = false;
     errorMessages.add("No driving license");
}

// Check the rest of your answers

At the end of your conditions you will have the variable canDrive reflects the result of the user answers and you will have all the error messages in errorMessages list which you can loop through and display appropriately.

Try to make your code more generic, consider using Array of your CheckedRadioButtons and you can store the error message for each question using checkBox.setTag()

Upvotes: 3

Related Questions