It seems to me that you want to delete item from the cart if it's already there, or add it to the cart if it is not there yet. This logic handles it well, but wouldn't it be nicer to add method hasItem/containsItem/get/etc. to cart and use it explicitly to find out which path to take - add vs. remove?