There are two classes to design: the
InvalidBet exception and the
Table class.
InvalidBet is thrown when the
Player attempts to place a bet which exceeds
the table's limit.
InvalidBet extends Exception {
}
This class simply inherits all features of the superclass.
In Python, the declaration must have a body, and the
pass statement is used for this
purpose.
class InvalidBet ( Exception ):
pass
Table contains all the
Bets created by the
Player. A table also has a betting limit, and
the sum of all of a player's bets must be less than or equal to this
limit. We assume a single Player in the
simulation.
int limit ;This is the table limit. The sum of a
Player's bets must be less than or
equal to this limit.
List bets ;This is a LinkedList of the
Bets currently active. These will
result in either wins or losses to the
Player.
boolean isValid(Bet bet);Validates this bet. If the sum of all bets is less than
or equal to the table limit, then the bet is valid, return
true. Otherwise, return
false.
void placeBet(Bet bet)
throws InvalidBet;Adds this bet to the list of working bets. If the sum of
all bets is greater than the table limit, then an exception
should be thrown (Java) or raised (Python). This is a rare
circumstance, and indicates a bug in the
Player more than anything else.
ListIterator iterator();Returns a ListIterator over the
list of bets. This gives us the freedom to change the
representation from LinkedList to any
other Collection with no impact
to other parts of the application.
In Python, we can return an iterator over the available
list of Bet instances. The traditional
Python approach is to return the list object itself, rather
than an iterator over the list. With the introduction of the
generators in Python 2.3, however, it
can be slightly more flexible to return an iterator rather
than the list object.
Note that we need to be able remove Bets from the table.
Consequently, we have to update the list, which requires that
we use a ListIterator, not an
Iterator.
Java: public String toString();
Python: def __str__(self):
reports on all of the currently placed bets.