Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When setting new section I need to keep track of the counters. for example if m1 is Brass and I use setSection(Strings); I want it to Brass-- and Strings++ But I'm not sure how to do it with if statments and im not sure if getSection()toString() would get me the original section or not

Java
/**This function sets a musician's Orchestra Section. 
 @param section  is a SymphonySection indicating the musician's Orchestra Section.*/

 public void setSection(SymphonySection section) {
    this.section = section;

    if (getSection().toString().equals( "Strings" )){
        Strings--;
    }
    else if (getSection().toString().equals( "Brass" )){
        Brass--;
    }
    else if (getSection().toString().equals( "Conductor" )){
        Conductor--;
    }
    else if (getSection().toString().equals( "Percussion" )){
        Percussion--;
    }
    else if (getSection().toString().equals( "WoodWinds" )){
        WoodWinds--;
    }

    if (section.toString().equals( "Strings" )){
        Strings++;
    }
    else if (section.toString().equals( "Brass" )){
        Brass ++;
    }
    else if (section.toString().equals( "Conductor" )){
        Conductor ++;
    }
    else if (section.toString().equals( "Percussion" )){
        Percussion ++;
    }
    else if (section.toString().equals( "WoodWinds" )){
        WoodWinds ++;
    }

} 
Posted
Updated 28-Sep-12 2:32am
v2

1 solution

- do not name variables with captial letter first. Those are limited to class names. Instead, place an "i" in front for int values: iString, iBrass.

- If you can (is this homework?) male the Object "section" into an enum.
Much safer to work with Enum than Strings.

Your Problem:

Java
// lower all and raise the selection therefor by 2
iStrings--;
iBrass--;
iConductor--;
iPercussion--;
iWoodWinds--;
if (section.toString().equals( "Strings" )){
  iStrings+=2;
}
else if (section.toString().equals( "Brass" )){
  iBrass+=2;
}
else if (section.toString().equals( "Conductor" )){
  iConductor+=2;
}
else if (section.toString().equals( "Percussion" )){
  iPercussion+=2;
}
else if (section.toString().equals( "WoodWinds" )){
iWoodWinds+=2;
}


but you need to split the code, you will not be able to make both, increase and decrease in the same method.
...Unless you have a value indicating wether it is increase or decrease:

Java
enum DIR{
	INCREASE,
	DECREASE
}

public void setSection(SymphonySection section, DIR direction) {
if(dircetion.equals(DIR.INCREASE){
  // increase
}
else{
  // decrease
}
}


if enum is not allowed you could also use a boolean or int value (-1 and 1) for that.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900