Enumeration by integer representation

Figure 315. Weekdays int representation Slide presentation Create comment in forum
public class Day {

  static public final int
    MONDAY    = 1,
    TUESDAY   = 2,
    WEDNESDAY = 3,
    THURSDAY  = 4,
    FRIDAY    = 5,
    SATURDAY  = 6,
    SUNDAY    = 7;
}

Figure 316. Weekly offered lectures Slide presentation Create comment in forum
public class Lecture {

    public final int dayHeld; /* e.g. to be held on Tuesdays */

    public final String title; /* e.g. «PHP introduction» */

    public Lecture(final int dayHeld, final String title) {
        this.dayHeld = dayHeld;
        this.title = title;
    }
}

Figure 317. Weekly offered lectures Slide presentation Create comment in forum
Class Driver:

final Lecture
  phpIntro     = new Lecture(Day.MONDAY, "PHP introduction"),
  advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");

Figure 318. Integer values to day names Slide presentation Create comment in forum
public class Day {
...
  public static String getDaysName(final int day) {
    switch (day) {
      case MONDAY:    return "Monday";
      case TUESDAY:   return "Tuesday";
      case WEDNESDAY: return "Wednesday";
      case THURSDAY:  return "Thursday";
      case FRIDAY:    return "Friday";
      case SATURDAY:  return "Saturday";
      case SUNDAY:    return "Sunday";

      default:        return "Illegal day's code: " + day;
    }
  }
}

Figure 319. Providing lecture info Slide presentation Create comment in forum
public class Lecture {
...
  public String toString() {
    return "Lecture «" + title + "» being held each " +
           Day.getDaysName(dayHeld);
  }
}

Figure 320. Printing lecture info Slide presentation Create comment in forum
public class Lecture {
...
  public String toString() {
    return "Lecture «" + title + "» being held each " + Day.getDaysName(dayHeld);
  }
}

Figure 321. Sample lectures Slide presentation Create comment in forum
// Class Driver
final Lecture 
  phpIntro = new Lecture(
    Day.MONDAY, "PHP introduction"),
  advancedJava = new Lecture(
    Day.FRIDAY, "Advanced Java");

System.out.println(phpIntro);
System.out.println(advancedJava);
Lecture «PHP introduction» 
  being held each Monday
Lecture «Advanced Java» 
  being held each Friday

Figure 322. Bogus enumeration value Slide presentation Create comment in forum
// Class Screwed

final Lecture phpIntro =
    new Lecture(88, "PHP introduction");

System.out.println(phpIntro);
Lecture «PHP introduction» being
held each Illegal day's code: 88

Figure 323. Argument order mismatch Slide presentation Create comment in forum
/**
* Charge double prices on weekends
* @param day Day of week
* @param amount
* @return the effective amount for
*         given day of week.
*/
static public int getPrice(
   final int day, final int amount) {
  switch (day) {
    case Day.SATURDAY:
    case Day.SUNDAY: return 2 * amount;

    default: return amount;
  }
}
// Correct
System.out.println(Screwed2.
   getPrice(Day.SUNDAY,  2));

// Argument mismatch
System.out.println(Screwed2.
   getPrice(2, Day.SUNDAY));
4
7

No warning message whatsoever!