"Finally, you'll be expected to implement equality appropriately for all the types." From Problem Set 3: The Librarians - Overview - Paragraph 1
Have you ever heard of value objects and entities? The difference is all about what establishes the objects identity. A value objects identity is about it's values. An entities is not. An entity could change every value and still be itself. Understand, this is about modeling. This is a choice we're making about how we treat something.
For example, if you loan me a dollar, and I give you back a dollar, only for you to get upset that I didn't give you back the same dollar, then you're treating your dollar like an entity and not a value object.
Caring more about the dollars serial number than the face value of the dollar is a modeling choice. It really has nothing to do with dollars being immutable. However, when dealing with something like a gift card, whose value can change over time, trying to model that things identity as a value object based on its current value becomes silly.
Immutable things can have their identities modeled either way. There are advantages to being able to model identity on values. But mutable things make basing identity on values a huge pain.
Identity is important to you here because how you implement equals (and you were told to implement equals) shows how you're modeling identity.
Overriding equals is not a no-no for mutable objects if you've given them an identity. Just because part of an object is mutable doesn't mean the whole thing is. Keep wherever you're storing the identity immutable and the rest can mutate as it likes. This will work with HashCodes and Sets just fine if you understand that those will be operating on the immutable identity and not the mutable values.
the assignment does not allow the change of the BookCopy(Book book) constructor - Ray
/**
* Make a new BookCopy, initially in good condition.
* @param book the Book of which this is a copy
*/
public BookCopy(Book book) {
throw new RuntimeException("not implemented yet");
}
Looks to me like you're being required to change it.
Don't change the method signatures and specifications: The public methods provided for you to implement in Book, BookCopy, and Library must use the method signatures and the specifications that we provided.
That means you can't simply pass in the UUID Doc Brown wanted you to use. Your constructor will have to reach out and get it (yuck).
Now this is student code so such shenanigans are forgivable. But, rather than making you import or implement some UUID library, a typical student solution for this problem is to use a static int. You can increment it in the constructor. It will tell you how many instances of BookCopy
you have created (not that you really care about that). It wont tell you which book it's a copy of, or how many copies of that book exist, but it will still be unique and that's enough. Save whatever it was when the BookCopy
was constructed and save whatever book it's a copy of and that's your identity.
BookCopy
?