HomeChallengesRepsProblemsLeaderboardTake a challenge

The object that protects itself

A basket whose rules live in every caller. Move them inside.

Beginner12 min on the clock4 graded checks · 100 pointsObjects That Behave
Graded by running your code against real cases.

What you build

  • Make the items unreachable from outside the class
  • Add through a method that rejects a quantity of zero or less
  • Adding the same sku twice should increase the quantity, not duplicate the line
  • Expose the lines for reading without handing over the array itself
  • Expose a count derived from the items

Done means

No caller outside the class can put the basket into a state its methods would refuse.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. The items cannot be reached from outside+30Objects that protect themselves
  2. It refuses a quantity that makes no sense+25Objects that protect themselves
  3. The same sku twice is one line+20Objects that protect themselves
  4. The contents are readable and the count is derived+25Objects that protect themselves

What it teaches

The rest of Objects That Behave