Cruz Loyde wrote:. . . Prof gave sets of instructions on how to do the methods so i really cant do my own thing.
I never like to see assignments specified in such detail. I think that is over‑specification and it prevents you using what may be better solutions like Carey's boolean return type or my suggestion of returning an unmodifiable List.
Carey Brown wrote:A personal preference:
I can see throwing an exception if null is passed in, however, I'd rather have a boolean returned as to whether the add was successful or not.
but I forgot the logic pro/cons to use exception. For the above case using return is better rather tan exception propagation.
What I was taught is that you use an exception only to catch processing that is exceptional. That is, when you are not expecting the condition to happen, but you're protecting against it. Another way to think of it is you throw an exception when there is no good way to recover from the condition.
You don't want to use an exception to signal the success or failure of a process, because these are normal conditions in the method. You might want to throw an exception when null is passed because this is often a sign that something has gone wrong with the calling code, but I could also see returning failure (false) in this case too.
All things are lawful, but not all things are profitable.
I might have missed this in the thread, but if the List<Media> is not supposed to have duplicates, then isn't that a Set?
You can then skip the whole checking thing in the addMedia method, so long as equals has been correctly defined.
Of course it is, but I htink OP has been told not to use Sets (not certain). More precisely it would be a set orderd by insertion order, like a linked hash set.
Judging from OP's replies, he's still a ways off from being able to implement equals() that would work for this problem. This exercise might lead up to that though. He just needs to figure out what "duplicate" means. So far, he hasn't been able to articulate that concept very well so naturally, he's having difficulty coding it.
Edit: I take that back. Missed his last post with the code that actually does what he needed to do.