Homework 3 Reflections

I spent time today implementing the program specified by Homework 3 to be in a better place for answering student questions. It's a fun program to write.

While implementing my solution, I found it helpful to place checks on the number of videos currently rented by a customer. I did the following:

														  public void incrementRented() {
														    numberOfVideosRented++;
														    if (numberOfVideosRented > 3) {
														      System.out.println("ERROR: Customer " + name + " has more than 3 videos.");
														      System.exit(1);
														    }
														  }
														
														  public void decrementRented() {
														    numberOfVideosRented--;
														    if (numberOfVideosRented < 0) {
														      System.out.println("ERROR: Customer " + name + " has less than 0 videos.");
														      System.exit(1);
														    }
														  }
														


This is an example of two things: implementing a class invariant and failing fast.

  • The class invariant is that a customer should always have between 0 and 3 videos rented inclusive.
  • Failing fast is printing out an error message and calling System.exit() to end the program as soon as the invariant is broken.

Applying this strategy allowed me to find bugs in my customer types as soon as they did something weird, such as rent 4 videos at once.

Finally, if you would like to see an example of the type of output your program can produce, take a look at the output generated by my program. Note: your output doesn't have to look exactly like this as long as it contains the items requested by the problem statement; plus, only the final report at the end of my output is required. (I found it helpful to print out the activity of each day and night, but that's not required.)

Hope this helps and have fun implementing your own solution!

© Ken Anderson 2012