Exactly how you implement this relationship will be different in
C++ or in
Java.
Because this is the design stage, you do not yet need to consider those details.It is important to record and understand the relationships among your classes. Think back to the
Check
class you designed earlier. You had several choices for how to represent the amount of the check: You could use:
- A floating-point number, like 57.35, but then there would be round-off issues to consider
- An integer, like 5735, representing the number of pennies the check was for, but then you'd have to divide by 100 whenever you had calculations to do
- Two integers, like 57 and 35, for the dollars and the cents, but then you'd have to be careful they didn't get "out of sync."
What would it mean if the dollars were 57 and the cents were -35?
Here's a solution: Use a
Money
class that knows all the business rules about representing money and keeps them in one place. Each
Check
object will be associated with a
Money
object.