This is a natural consequence of session attributes not being type-safe. Suppressing the warning is fine if you know for sure that your application always assigns the same type to that attribute.

What's more problematic is your naming. It looks like your Cart class encapsulates a single entry of a shopping cart. So why is it called Cart and not something like CartEntry?

If you write a separate Cart class that encapsulates a Map<String, CartEntry> (after all, why are IDs represented by int? Are you going to use arithmetic on them?) you can cast the attribute to Cart and you won't get a warning:

Vojislav Kovacevic

Greenhorn

Posts: 12

posted 3 weeks ago

Hi Stephen,

It's just the logic I went with on the fly.

The ids are there as keys to separate each product (which is what the Cart class contains).

I will think about what you said and try to implement it and see if it's a better solution.