Chapter 8. Bet Class

Table of Contents

Overview
Questions and Answers
Design
Fields
Constructors
Methods
Deliverables

In addition to the design of the Bet class, this chapter also presents some additional questions and answers on the nature of an object, identity and state change. This continues some of the ideas from On Object Identity

Overview

A Bet is an amount that the player has wagered on a specific Outcome. This class has the responsibilty for maintaining the association between an amount, an Outcome, and a specific Player.

This is the classic record declaration: a passive association among data elements. The only methods we can see are three pairs of getters and setters to get and set each of the three attributes.

The general scenario is to have a Player construct a number of Bet instances. The wheel is spun to select a winning Bin. Then each of the Bet objects will be checked to see if they are winners or losers. A winning Bet has an Outcome that matches one in the winning Bin; winners will return money to the Player. All other bets are losers, which remove the money from the Player.

Building Bets. In the long run, we'll need to build a variety of bets. Building a Bet involves two parts: an Outcome and an amount. There are several places to get Outcomes. First, we can create an Outcome object as part of constructing a Bet. Here's what it might look like in Java.

Bet myBet= new Bet( new Outcome("red",2), 25 )

A better choice is to get Outcome obects from the Wheel. To do this, we'd have to make sure that we saved Outcomes from the BinBuilder. We'd also have add specific Outcome getters to the Wheel. We could, for example, include a getBlack method that returns an instance of the black Outcome. In Python, for example, we might have a method function like the following.

class Wheel( object ):
...
    def getBlack( self ):
        return self.black

A third approach is to keep a Map (in Python, a dictionary) that maps an Outcome's string name to the relevant Outcome object. Our BinBuilder can accumulate this Map, and we can get Outcomes out of the Map with a getter method like the following.

Outcome getOutcome( String name ) {
    return outcomeMap.get(name);
}

For now, we'll build Outcomes manually in order to produce a good test of the Bet class.