java - Change if-else statement into switch statement -
i having trouble on how can handle condition. have 5 items filtered , planning add more, add items increases possible combination such 1*2*3*4*5 combinations 5 when have 10 items , 3 million combinations. on current thread recommend me use switch no idea how perform reduce possible combination. can change if-statement switch or there other way can handle these combinations. here's program
// filter selected for(int subloop=0; subloop<sspecialty[loop].length; subloop++) { if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // regaffil } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // administration } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // roomambience } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // tuition fee } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // specialty } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // regaffil , admin 0-1 } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // regaffil , type 0-2 } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // regaffil , ambience 0-3 } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // reg afill , tuition 0-4 } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // reg afill , specialty 0-5 } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // admin , type 1-2 } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // admin , ambience 1-3 } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // admin , tuition 1-4 } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // admin , specialty 1-5 } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // type , ambience 2-3 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // type , tuition 2-4 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // type , specialty 2-5 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // ambience , tuition 3-4 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); // ambience , specialty 3-5 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // tuition , specialty 4-5 } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); // without specialty } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && thisfilter[4].equalsignorecase("*") ) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && thisfilter[4].equalsignorecase("*")) { list.add(slist[loop]); } // specialty else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4]) ) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && thisfilter[1].equalsignorecase("*") && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (thisfilter[0].equalsignorecase("*") && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && sambience[loop].equalsignorecase(thisfilter[3]) && amounttf == 0 && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); } else if (sreg[loop].equalsignorecase(thisfilter[0]) && sadmin[loop].equalsignorecase(thisfilter[1]) && thisfilter[3].equalsignorecase("*") && amounttf >= thetuitionfee && sspecialty[loop][subloop].equalsignorecase(thisfilter[4])) { list.add(slist[loop]); }
first of all, see lot of repeated statements in ifs.
i would, in first iteration of 'optimization' put statements in variables this:
boolean filterzero = thisfilter[0].equalsignorecase("*"); boolean adminloop = sadmin[loop].equalsignorecase(thisfilter[1]); boolean filterthree = thisfilter[3].equalsignorecase("*"); boolean filterfour = thisfilter[4].equalsignorecase("*");
then convert more readable like:
else if (filterzero && adminloop && filterthree && amounttf == 0 && filterfour) { list.add(slist[loop]); }
also, try see if there less case don't want list.add(slist[loop]);
in case, simplify conditions.
if it's not case, check conditions comes together. example, if filterzero
, filterthree
in condition when used, can concatenation of them this:
boolean filterzerothree = thisfilter[0].equalsignorecase("*") && thisfilter[3].equalsignorecase("*");
Comments
Post a Comment