A more elegant object-oriented solution

Some hints on how you could write a more elegant implementation, by making it more object-oriented.