|

This sample code provides a real-world example of how to use the state and decorator patterns. The sample code is spread over five (5) files: gamblerNstates.h, gamblers.cpp, states.cpp, decorators.cpp, and main.cpp.
// State and Decorator pattern sample code in C++
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: gamblerNstates.h
Contains: - Gambler, GamblerDecorator, GamblerWithCar class definitions
- State, RichState, NormalState, PoorState class definitions
------------------------------------------------------------ */
#include <iostream>
#define ODD 1
#define EVEN 0
#define RICH 25000 // Amount of money required to be in the rich state
#define POOR 0 // Amount of money required to be in the normal state
#define NORMAL 5000 // Amount of money given at the beginning of the game
using namespace std;
class Gambler
{
class State* wealthState; // defines current states of the gambler
public:
// member functions
Gambler();
// Default constructor
// Pre-conditions: None
// Post-conditions: Gambler constructed
Gambler(Gambler* newGambler);
// Constructor - constucts a new gambler, needed for use of the decorator
// Pre-conditions: newGambler is a valid gambler
// Post-conditions: an identical gambler is constructed
void checkState();
// checks the balance of the gambler and changes the current state accordingly
// Pre-conditions: none
// Post-conditions: current state of the gambler is updated
void placeBet(bool on, long amount);
// stores information of the bet placed by the player
// Pre-conditions: valid input parameters
// Post-conditions: information about next bet is stored
void checkWinning(bool result);
// checks the bet information and confirm a win or lost
// Pre-conditions: bet information is up to date
// Post-conditions: balance and state is updated accordingly
virtual void specialOps();
// special operations the gambler can perform according to his state
void addBalance(long addition);
// changes balance of the gambler according to the input
// Pre-conditions: none
// Post-conditions: Balance is updated to balance + addition
long getBalance();
// Pre-conditions: none
// Post-conditions: balance of the gambler is returned
bool hasCar();
// Pre-conditions: none
// Post-contitions: returns true of gambler has a car
private:
// member functions
State* getState();
// Pre-conditions: none
// Post-conditions: Returns a pointer to the current state of the gambler
// variables
long balance; // current balance of the gambler
long betAmount; // amount of money for next bet
int currentState; // state of gambler (rich, normal, poor)
bool betOn; // odd or even
};
class GamblerDecorator : public Gambler
{
// abstract decorator for gambler - adds functionality to specialOps()
public:
// member functions
GamblerDecorator();
// Default Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed
GamblerDecorator(Gambler* newGambler);
// Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed for newGamblre
virtual void specialOps() = 0;
// virtual function
// must be inplemented by concrete decorators
};
class GamblerWithCar : public GamblerDecorator
{
// concrete decorator - adds responsibility to specialOps()
public:
// member funcitons
GamblerWithCar();
// Default Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed
GamblerWithCar(Gambler* newGambler);
// Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed for newGamblre
void specialOps();
// Gambler::specialOps() is called. New functionality of souping up your car is added
// Pre-conditions: none
// Post-conditions: special operation performed
void newOps();
// newFunctionality only when gambler has a car
// Pre-conditions: none
// Post-conditions: car is souped up according to user input
private:
// variables
double pimpFactor; // how pimping your ride is
};
class State
{
// abstract states used to define a gambler depending on his balance
public:
// member funcitons
State();
// Default consturctor
// Pre-conditions: none
// Post-conditions: a new state is created
State(Gambler* newGambler);
// Consturctor
// Pre-conditions: none
// Post-conditions: a new state is created for newGambler
State(State* oldState);
// Consturctor
// Pre-conditions: none
// Post-conditions: a new state is created for the same gambler as oldState
Gambler* getGambler();
// Pre-conditions: none
// Post-conditions: a pointer to the gambler associate with the state is returned
bool hasCar();
// Pre-oonditions: none
// Post-conditions: returns true of the gambler has a car
virtual void specialOps() = 0;
// virtual function
// must be implemented by concrete states
// variables
Gambler* myGambler; // pointer to associated gambler
bool haveCar; // true of gambler has a car
};
class NormalState : public State
{
public:
// member functions
NormalState() {}
// Default consturctor
// Pre-conditions: none
// Post-conditions: a new normal state is created
NormalState(Gambler* newGambler) : State(newGambler) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new normal state is created for newGambler
NormalState(State* oldState) : State(oldState) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new normal state is created for the same gambler as oldState
void specialOps();
// Pre-conditions: none
// Post-conditions: performed the special operations associated with normal state
};
class PoorState : public State
{
public:
// member functions
PoorState() {}
// Default consturctor
// Pre-conditions: none
// Post-conditions: a new poor state is created
PoorState(Gambler* newGambler) : State(newGambler) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new poor state is created for newGambler
PoorState(State* oldState) : State(oldState) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new poor state is created for the same gambler as oldState
void specialOps();
// Pre-conditions: none
// Post-conditions: performed the special operations associated with poor state
};
class RichState : public State
{
public:
// member functions
RichState() {}
// Default consturctor
// Pre-conditions: none
// Post-conditions: a new rich state is created
RichState(Gambler* newGambler) : State(newGambler) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new rich state is created for newGambler
RichState(State* oldState) : State(oldState) { }
// Consturctor
// Pre-conditions: none
// Post-conditions: a new rich state is created for the same gambler as oldState
virtual void specialOps();
// Pre-conditions: none
// Post-conditions: performed the special operations associated with rich state
};
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: gamblers.cpp
Contains: - Gamble class implementations
------------------------------------------------------------ */
#include "gamblerNstates.h"
Gambler::Gambler()
// Default constructor
// Pre-conditions: None
// Post-conditions: Gambler constructed
{
balance = NORMAL;
currentState = NORMAL;
wealthState = new NormalState(this);
}
Gambler::Gambler(Gambler* newGambler)
// Constructor - constucts a new gambler, needed for use of the decorator
// Pre-conditions: newGambler is a valid gambler
// Post-conditions: an identical gambler is constructed
{
balance = newGambler->getBalance();
wealthState = newGambler->getState();
if (balance > RICH)
currentState = RICH;
else if (balance < POOR)
currentState = POOR;
else
wealthState = new NormalState(this);
}
void Gambler::placeBet(bool on, long amount)
// stores information of the bet placed by the player
// Pre-conditions: valid input parameters
// Post-conditions: information about next bet is stored
{
betAmount = amount;
betOn = on;
}
void Gambler::checkWinning(bool result)
// checks the bet information and confirm a win or lost
// Pre-conditions: bet information is up to date
// Post-conditions: balance and state is updated accordingly
{
if (betOn == result)
{
balance += betAmount;
cout << "\nYou won!!" << endl;
}
else
{
balance -= betAmount;
cout << "\nYou lost!!" << endl;
}
checkState();
}
void Gambler::specialOps()
// special operations the gambler can perform according to his state
{
wealthState->specialOps();
checkState();
}
void Gambler::checkState()
// checks the balance of the gambler and changes the current state accordingly
// Pre-conditions: none
// Post-conditions: current state of the gambler is updated
{
if (balance > RICH)
{
if( currentState != RICH )
{
wealthState = new RichState(wealthState);
currentState = RICH;
}
}
else if (balance < POOR)
{
if( currentState != POOR )
{
wealthState = new PoorState(wealthState);
currentState = POOR;
}
}
else if ( currentState != NORMAL )
{
wealthState = new NormalState(wealthState);
currentState = NORMAL;
}
}
void Gambler::addBalance(long addition)
// changes balance of the gambler according to the input
// Pre-conditions: none
// Post-conditions: Balance is updated to balance + addition
{
balance += addition;
}
long Gambler::getBalance()
// Pre-conditions: none
// Post-conditions: balance of the gambler is returned
{
return balance;
}
bool Gambler::hasCar()
// Pre-conditions: none
// Post-contitions: returns true of gambler has a car
{
return wealthState->hasCar();
}
State* Gambler::getState()
// Pre-conditions: none
// Post-conditions: Returns a pointer to the current state of the gambler
{
return wealthState;
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: decorators.cpp
Contains: - GamblerDecorator, GamblerWithCar class implementations
------------------------------------------------------------ */
#include "gamblerNstates.h"
GamblerDecorator::GamblerDecorator()
// Default Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed
{
}
GamblerDecorator::GamblerDecorator(Gambler* newGambler) : Gambler(newGambler)
// Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed for newGamblre
{
}
GamblerWithCar::GamblerWithCar()
// Default Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed
{
pimpFactor = 0;
}
GamblerWithCar::GamblerWithCar(Gambler* newGambler) : GamblerDecorator(newGambler)
// Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed for newGamblre
{
pimpFactor = 0;
}
void GamblerWithCar::specialOps()
// Gambler::specialOps() is called. New functionality of souping up your car is added
// Pre-conditions: none
// Post-conditions: special operation performed
{
int choice, amount;
Gambler::specialOps();
newOps();
Gambler::checkState();
}
void GamblerWithCar::newOps()
// newFunctionality only when gambler has a car
// Pre-conditions: none
// Post-conditions: car is souped up according to user input
{
int choice, amount;
cout << "\nYour current PIMP factor is " << pimpFactor << endl;
cout << "Want to spend some money souping up your car [1 = Yes / 2 = No]? ->";
cin >> choice;
if (choice == 1)
{
cout << "How much do you want to spend? ->";
cin >> amount;
Gambler::addBalance((-1)*amount);
pimpFactor += amount / (314.159);
cout << "Your new PIMP factor is " << pimpFactor << endl;
}
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: states.cpp
Contains: - State, RichState, NormalState, PoorState class implementations
------------------------------------------------------------ */
#include "gamblerNstates.h"
State::State()
// Default consturctor
// Pre-conditions: none
// Post-conditions: a new state is created
{
}
State::State(Gambler* newGambler)
// Consturctor
// Pre-conditions: none
// Post-conditions: a new state is created for newGambler
{
myGambler = newGambler;
haveCar = false;
}
State::State(State* oldState)
// Consturctor
// Pre-conditions: none
// Post-conditions: a new state is created for the same gambler as oldState
{
myGambler = oldState->getGambler();
haveCar = oldState->haveCar;
}
Gambler* State::getGambler()
// Pre-conditions: none
// Post-conditions: a pointer to the gambler associate with the state is returned
{
return myGambler;
}
bool State::hasCar()
// Pre-oonditions: none
// Post-conditions: returns true of the gambler has a car
{
return haveCar;
}
void NormalState::specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with normal state
{
cout << "There is nothing special to do!!!" << endl;
}
void PoorState::specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with poor state
{
cout << "Made 200 bucks from picking up cands" << endl;
myGambler->addBalance(200);
}
void RichState::specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with rich state
{
int choice;
if (!haveCar)
{
cout << "Do you want to buy a car [1 = Yes / 2 = No]? ->";
cin >> choice;
if (choice == 1)
{
haveCar = true;
myGambler->addBalance(-20000);
}
}
else
{
cout << "Driving around is fun" << endl;
}
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: main.cpp
Contains: - driver program to demostrate decorator and state
design pattern working together
------------------------------------------------------------ */
#include <stdlib.h>
#include <time.h>
#include "gamblerNstates.h"
using namespace std;
int main()
{
Gambler* myGambler = new Gambler();
bool done = false;
char choiceChar;
int choice, temp;
bool ownCar = false;
long amount;
srand(time(NULL));
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$$ Welcome to the Happy Happy Joy Joy Casino $$" << endl;
cout << "$$ - where your money is as good as ours - $$" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
while(!done)
{
if(myGambler->hasCar()&&!ownCar)
{
// if a gambler buys a car for the first time
myGambler = new GamblerWithCar(myGambler);
ownCar = true;
}
cout << "\nWhat would you like to do?" << endl;
cout << "1. Place a bet 2. Something special 3. Quit ->";
cin >> choiceChar;
choice = atoi(&choiceChar);
switch (choice)
{
case 1:
{
cout << "\nYou currently have $" << myGambler->getBalance() << endl;
cout << "How much would you like to bet? ->";
cin >> amount;
cout << "1. Even 2. Odd ->";
cin >> choice;
if ((choice == 1)||(choice == 2))
{
myGambler->placeBet((choice-1),amount);
temp = rand()%2;
myGambler->checkWinning(temp);
}
else
cout << "Invalid selection!" << endl;
break;
}
case 2:
{
myGambler->specialOps();
break;
}
case 3:
{
done = true;
break;
}
default:
{
break;
}
}
}
return 0;
}

This sample code provides a real-world example of how to use the state and decorator patterns. The sample code is spread over five (5) files: Gambler.java, GamblerWithCar.java, State.java, GamblerDecorator.java, and Main.java.
// State and Decorator pattern sample code in Java
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: Gambler.java
Contains: - Gambler class implementation
------------------------------------------------------------ */
package gambleFull;
import java.io.IOException;
class Gambler
{
private final int ODD = 1;
private final int EVEN = 0;
private final long RICH = 25000;
private final long POOR = 0;
private final long NORMAL = 5000;
private State state;
private long balance;
private long betAmount;
private int currentState;
private boolean betOn;
private State wealthState;
public Gambler()
// Default constructor
// Pre-conditions: None
// Post-conditions: Gambler constructed
{
balance = NORMAL;
currentState = (int) NORMAL;
wealthState = new NormalState(this);
}
public Gambler(Gambler newGambler)
// Constructor - constucts a new gambler, needed for use of the decorator
// Pre-conditions: newGambler is a valid gambler
// Post-conditions: An identical gambler is constructed
{
balance = newGambler.getBalance();
wealthState = newGambler.getState();
if (balance > RICH)
currentState = (int) RICH;
else if (balance < POOR)
currentState = (int) POOR;
else
wealthState = new NormalState(this);
}
public void placeBet(boolean on, long amount)
// stores information of the bet placed by the player
// Pre-conditions: Valid input parameters
// Post-conditions: Information about next bet is stored
{
betAmount = amount;
betOn = on;
}
public void checkWinning(boolean result)
// checks the bet information and confirm a win or lost
// Pre-conditions: Bet information is up to date
// Post-conditions: Balance and state is updated accordingly
{
if (betOn == result)
{
balance += betAmount;
System.out.println("\nYou won!!\n");
}
else
{
balance -= betAmount;
System.out.println("\nYou lost!!\n");
}
checkState();
}
public void specialOp()
// Special operations the gambler can perform according to his state
{
try { wealthState.specialOps(); }
catch (IOException e) { e.printStackTrace(); }
checkState();
}
void checkState()
// checks the balance of the gambler and changes the current state accordingly
// Pre-conditions: None
// Post-conditions: Current state of the gambler is updated
{
if (balance > RICH)
{
if( currentState != RICH )
{
wealthState = new RichState(wealthState);
currentState = (int) RICH;
}
}
else if (balance < POOR)
{
if( currentState != POOR )
{
wealthState = new PoorState(wealthState);
currentState = (int) POOR;
}
}
else if ( currentState != NORMAL )
{
wealthState = new NormalState(wealthState);
currentState = (int) NORMAL;
}
}
void addBalance(long addition)
// changes balance of the gambler according to the input
// Pre-conditions: None
// Post-conditions: Balance is updated to balance + addition
{
balance += addition;
}
long getBalance()
// Pre-conditions: None
// Post-conditions: Balance of the gambler is returned
{
return balance;
}
boolean hasCar()
// Pre-conditions: None
// Post-contitions: Returns true of gambler has a car
{
return wealthState.hasCar();
}
State getState()
// Pre-conditions: None
// Post-conditions: Returns a reference to the current state of the gambler
{
return wealthState;
}
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: GamblerWithCar.java
Contains: - GamblerWithCar class implementations
------------------------------------------------------------ */
package gambleFull;
import java.io.DataInputStream;
import java.io.IOException;
class GamblerWithCar extends GamblerDecorator
{
protected int pimpFactor;
public GamblerWithCar()
// Default Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed
{
pimpFactor = 0;
}
public GamblerWithCar(Gambler newGambler)
// Constructor
// Pre-conditions: none
// Post-conditions: an concrete decorator is constructed for newGambler
{
pimpFactor = 0;
}
void specialOps()
// Gambler.specialOps() is called. New functionality of souping up your car is added
// Pre-conditions: none
// Post-conditions: car souped up according to user input
{
int choice = 0;
int amount = 0;
specialOp();
System.out.println("\nYour current PIMP factor is " + pimpFactor + "\n");
System.out.println("Want to spend some money souping up your car [1 = Yes / 2 = No]? ->");
DataInputStream in = new DataInputStream(System.in);
try
{ choice = Integer.parseInt(in.readLine()); }
catch (NumberFormatException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
if (choice == 1)
{
System.out.println("\nHow much do you want to spend? ->");
try
{ choice = Integer.parseInt(in.readLine()); }
catch (NumberFormatException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
addBalance((-1)*amount);
pimpFactor += amount / (314.159);
System.out.println("\nYour new PIMP factor is " + pimpFactor + "\n");
}
}
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: State.java
Contains: - State, RichState, NormalState, PoorState class implementations
------------------------------------------------------------ */
package gambleFull;
import java.io.DataInputStream;
import java.io.IOException;
abstract class State
{
Gambler myGambler;
boolean haveCar;
State()
// Default constructor
// Pre-conditions: none
// Post-conditions: a new state is created
{
}
State(Gambler newGambler)
// constructor
// Pre-conditions: none
// Post-conditions: a new state is created for newGambler
{
myGambler = newGambler;
haveCar = false;
}
State(State oldState)
// constructor
// Pre-conditions: none
// Post-conditions: a new state is created for the same gambler as oldState
{
myGambler = oldState.getGambler();
this.haveCar = oldState.haveCar;
}
Gambler getGambler()
// Pre-conditions: none
// Post-conditions: a reference to the gambler associate with the state is returned
{
return myGambler;
}
boolean hasCar()
// Pre-oonditions: none
// Post-conditions: returns true of the gambler has a car
{
return haveCar;
}
abstract void specialOps() throws IOException;
}
class PoorState extends State
{
public PoorState() {}
// Default constructor
// Pre-conditions: none
// Post-conditions: a new poor state is created
public PoorState(Gambler newGambler)
// constructor
// Pre-conditions: none
// Post-conditions: a new poor state is created for newGambler
{
super(newGambler);
}
public PoorState(State oldState)
// Consturctor
// Pre-conditions: none
// Post-conditions: a new poor state is created for the same gambler as oldState
{
super(oldState);
}
void specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with poor state
{
System.out.println("Made 200 bucks from picking up cans\n");
myGambler.addBalance(200);
}
}
class NormalState extends State
{
public NormalState() {}
// Default constructor
// Pre-conditions: none
// Post-conditions: a new normal state is created
public NormalState(Gambler newGambler)
// constructor
// Pre-conditions: none
// Post-conditions: a new normal state is created for newGambler
{
super(newGambler);
}
public NormalState(State oldState)
// Consturctor
// Pre-conditions: none
// Post-conditions: a new normal state is created for the same gambler as oldState
{
super(oldState);
}
void specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with normal state
{
System.out.println("There is nothing special to do!\n");
}
}
class RichState extends State
{
public RichState() {}
// Default constructor
// Pre-conditions: none
// Post-conditions: a new rich state is created
public RichState(Gambler newGambler)
// constructor
// Pre-conditions: none
// Post-conditions: a new rich state is created for newGambler
{
super(newGambler);
}
public RichState(State oldState)
// constructor
// Pre-conditions: none
// Post-conditions: a new rich state is created for the same gambler as oldState
{
super(oldState);
}
void specialOps()
// Pre-conditions: none
// Post-conditions: performed the special operations associated with rich state
{
int choice;
if (!haveCar)
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("Do you want to buy a car [1 = Yes / 2 = No]? ->\n");
String inputStr = null;
try
{ inputStr = in.readLine(); }
catch (IOException e) { e.printStackTrace(); }
choice = Integer.parseInt(inputStr);
if (choice == 1)
{
haveCar = true;
myGambler.addBalance(-20000);
}
}
else
{
System.out.println("Driving around is fun\n");
}
}
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: GamblerDecorator.java
Contains: - GamblerDecorator class implementations
------------------------------------------------------------ */
package gambleFull;
abstract class GamblerDecorator extends Gambler
{
protected Gambler gambler;
public GamblerDecorator()
// Default Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed
{
}
public GamblerDecorator(Gambler newGambler)
// Constructor
// Pre-conditions: none
// Post-conditions: an abstract decorator is constructed for newGambler
{
}
abstract void specialOps();
}
/* ---------------------------------------------------------
Group: three-ten Software
Members: Rory Hansen
Binh Ho
Ivan Sham
Soo Wei Tan
Date: January 31, 2004
File Name: Main.java
Contains: - driver program to demostrate decorator and state
design pattern working together
------------------------------------------------------------ */
package gambleFull;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;
class Main
{
public static void main(String[] args) throws IOException
{
String owner;
int choice = 0;
int temp;
boolean result = false;
boolean done = false;
boolean ownCar = false;
long amount = 0;
Gambler myGambler = new Gambler();
DataInputStream in = new DataInputStream(System.in);
Random randomNumberGenerator = new Random();
System.out.println(" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" );
System.out.println(" $$ Welcome to the Happy Happy Joy Joy Casino $$\n" );
System.out.println(" $$ - where your money is as good as ours - $$\n" );
System.out.println(" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" );
while (done != true)
{
if (myGambler.hasCar() && !ownCar)
{
myGambler = new GamblerWithCar(myGambler);
ownCar = true;
}
System.out.println("\nWhat would you like to do?\n");
System.out.println("1. Place a bet 2. Something special 3. Quit ->");
try
{ choice = Integer.parseInt(in.readLine()); }
catch (NumberFormatException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
switch (choice)
{
case 1:
{
System.out.println("\nYou currently have $" + myGambler.getBalance() + "\n");
System.out.println("How much would you like to bet? ->");
try
{ amount = Integer.parseInt(in.readLine()); }
catch (NumberFormatException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
System.out.println("1. Even 2. Odd ->");
try
{ choice = Integer.parseInt(in.readLine()); }
catch (NumberFormatException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
if ((choice == 1)||(choice == 2))
{
boolean on;
if (choice == 1)
{ on = false; }
else
{ on = true; }
myGambler.placeBet(on, amount);
temp = (randomNumberGenerator.nextInt())%2;
if (temp == 1)
{ result = false; }
else
{ result = true; }
myGambler.checkWinning(result);
}
else
System.out.println("Invalid selection!\n");
break;
}
case 2:
{
myGambler.specialOp();
break;
}
case 3:
{
done = true;
break;
}
default:
break;
}
}
}
}
|