Week 8

Cookie Jar

One object that remembers a capacity and a count, and refuses the actions that break its rules.

You type

You build the jar. Tests, and a little program, deposit and withdraw.

The tool
The jar remembers two numbers a jar capacity and count cookies
What comes out

A jar whose count stays inside its rules.

The question, more slowly

The question is a cookie jar object. It remembers a capacity and how many cookies are inside. Deposit adds, withdraw removes. You cannot deposit past the capacity, withdraw more than you have, or create a jar with a negative capacity. Those failures raise the errors the spec names.

The class stores the two numbers. The methods change them or refuse. A property is a natural place to guard the capacity. The tests, which the spec describes, create a jar and call the methods. They do not care how you print.

People track only the count and forget the capacity, so deposit never refuses. Write the refusals as sentences before you write the methods.

Before you code

Write the rules in a sentence each: how many it can hold, what deposit does, what withdraw does, and what is not allowed. The spec is that list.