Reputation: 21
Does anyone know how to convert this if-then-else statement to if-then statement? It is so confusing. Where to put the else part and to ensure that the structure is the same with if-then-else statement? Thank you for your help.
if ((symptom1.equalsIgnoreCase("Yes"))) //fever 3 to 14 days
{
weight = 0.75; //cf
if ((symptom2.equalsIgnoreCase("Yes"))) //rash on any part of the body
{
weight = 0.55; //cf
if ((symptom3.equalsIgnoreCase("Yes"))) //muscle pain
{
weight = 0.45; //cf
if ((symptom4.equalsIgnoreCase("Yes"))) //low blood pressure
{
weight = 0.38; //cf
if ((symptom5.equalsIgnoreCase("Yes"))) //bleeding gum
{
weight = 0.48; //cf
if ((symptom6.equalsIgnoreCase("Yes"))) //bloody feces
{
weight = 0.35; //cf
//severe
newWeight = 0.7 * 0.35; //cf for disease = 0.7 [min=0.35]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultSevereDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
else //consult doctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//uncomplicated
else
{
newWeight = 0.6 * 0.3; //cf for disease = 0.6 [min=0.3]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultUncomplicatedDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//absent dengue
else if ((symptom1.equalsIgnoreCase("No"))) //fever 1 to 3 days
{
weight = 0.7; //cf
if ((symptom2.equalsIgnoreCase("No"))) //rash on any part of the body
{
weight = 0.5; //cf
if ((symptom3.equalsIgnoreCase("No"))) //muscle pain
{
weight = 0.4; //cf
if ((symptom4.equalsIgnoreCase("No"))) //low blood pressure
{
weight = 0.3; //cf
if ((symptom5.equalsIgnoreCase("No"))) //bleeding gum
{
weight = 0.4; //cf
if ((symptom6.equalsIgnoreCase("No"))) //bloody feces
{
weight = 0.35; //cf
//absent
newWeight = 0.8 * 0.3; //cf for disease = 0.8 [min=0.3]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultAbsentDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
Upvotes: 0
Views: 139
Reputation: 3474
You should take any decent IDE and do that work by yourself. Here are the hints:
1) Find repetitive blocks of code, e.g.
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
and move them to a method like startCfDiseaseIntent
.
2) Remove your commentaries, and replace them with self-describing code:
symptom1.equalsIgnoreCase("Yes"))) //fever 3 to 14 days
Should be extracted to local variable
boolean isFeverMoreThanTwoDays = symptom1.equalsIgnoreCase("Yes");
And then reused.
You can still leave a commentary, but near that boolean
.
Moreover you can combine new booleans to create new self-decribing booleans, like:
boolean patientHasExtremelyDangerousInfection = isFever... && isMusclePainPresent && isGumBleeding && ...;
And then use them to make a decision.
3) Find unnecessary code:
In the second half, where symptom1.equalsIgnoreCase("No")
all of your else blocks are the same, so you can collapse all the checks to one with &&
, and remove that whole if-then-else
tree.
In the first half: my IDE shows that almost all calls to weight = ...; // cf
are overwritten further without being read in-between, so there are a lot of redundant lines over there - make them useful, or remove.
4) Create a test if necessary. Your code consumes 7 variables that can be either "yes" or "no". That makes 2^7 input variations, which is a small number. You can easily create a program that executes all of these variations, consumes resulting cf
and puts that data into some file. Afterwards with the help of that file you can assure that your new code actually works according to the old logic.
Upvotes: 1
Reputation:
I assumed that the value of symptomN must be "Yes" or "No".
static boolean check(String[] symptoms, String... values) {
if (values.length != symptoms.length) {
throw new IllegalArgumentException();
}
for (int i = 0; i < symptoms.length; ++i) {
if (!symptoms[i].equalsIgnoreCase(values[i])) {
return false;
}
}
return true;
}
and
String[] symptoms = { symptom1, symptom2, symptom3, symptom4, symptom5, symptom6 };
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")) {
weight = 0.35; //cf
//severe
newWeight = 0.7 * 0.35; //cf for disease = 0.7 [min=0.35]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultSevereDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "Yes", "No")) {
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "No", "Yes")) {
// ...
Upvotes: 1