0

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

/**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 ++;
    }

}
Mat
  • 202,337
  • 40
  • 393
  • 406
Moe
  • 19
  • 2
  • 4

3 Answers3

2
  1. Does your SymphonySection class have a toString method where it returns the section name? If not you need to implement this so that it returns the section name, e.g. Brass, Strings etc. Check here how to implement in the SymphonySection class
  2. I would suggest using a switch (which as of Java 7 supports Strings) statement to neaten things up a tad, or a map as suggested by Peter Lawrey
  3. Please also take a look at Java naming conventions, here, to make your code more readable. In the above case I am guessing Strings, Brass, etc. are variables of type SymphonySection and hence should have lower case first characters, e.g. strings, brass, etc. WoodWinds, would be woodWinds which follows the conventional camelcase coding style.

Hope this helps.

Hayden

Community
  • 1
  • 1
Hayden
  • 2,818
  • 2
  • 22
  • 30
1

I would use a Map of counts for each section.

private final Map<SymphonySection, Integer> count = new HashMap<>();
public void setSection(SymphonySection section) {
    if(section == this.section) return;

    count.put(this.section, count.get(this.section)-1);
    Integer prevCount = count.get(section)
    count.put(section, prevCount == null ? 1 : (prevCount+1));

    this.section = section;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • thanks.. but is there any way to do it with if statements like the way i wrote it? I know Maps but we did not do them yet in this class so I just prefer not to use them. – Moe Sep 28 '12 at 08:28
  • You can use reflection to get a field by name instead. As your fields match the `section.toString()` you can use this to look up the field. Using lots of if/else will work provided you don't change the old value until you have finished (as I did) – Peter Lawrey Sep 28 '12 at 10:08
0

- If you are having Java 7, and you want to go with the basics, then please use switch.

- Java 7 support String for switch cases.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75