Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.lang.reflect.InvocationTargetException;

import roulette.Gambler;
import roulette.Game;

Expand All @@ -8,7 +10,7 @@
* @author Robert C. Duvall
*/
public class Main {
public static void main (String[] args) {
public static void main (String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Game game = new Game();
Gambler player = new Gambler("Robert", 1000);

Expand Down
32 changes: 32 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package roulette;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class Factory {

private List<Bet> myBets;

public Factory () {
myBets = new ArrayList<Bet>();

}

public Bet createBet (String classname, String description, int odds)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, NoSuchMethodException,
SecurityException, IllegalArgumentException, InvocationTargetException {
// Bet myInstance = (Bet) Class.forName(classname).newInstance();

Class<?> clazz = Class.forName("roulette.bets." + classname);
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);

Bet instance = (Bet) constructor.newInstance(description, new Integer(odds));

myBets.add(instance);
return instance;
}

}
47 changes: 44 additions & 3 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package roulette;

import java.lang.reflect.InvocationTargetException;

import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
Expand All @@ -15,17 +17,33 @@ public class Game {
// name of the game
private static final String DEFAULT_NAME = "Roulette";
// add new bet subclasses here
private Factory myFactory;

private Bet[] myPossibleBets = {
new RedBlack("Red or Black", 1),
new OddEven("Odd or Even", 1),
new ThreeConsecutive("Three in a Row", 11),
};

private String[] myNames = {
"RedBlack", "OddEven", "ThreeConsecutive"
};

private String[] myDescription = {
"Red or Black", "Odd or Even", "Three in a Row"
};

private int[] myOdds = {
1, 1, 11
};

private Wheel myWheel;

/**
* Construct the game.
*/
public Game () {
myFactory = new Factory();
myWheel = new Wheel();
}

Expand All @@ -43,8 +61,15 @@ public String getName () {
* that the bet is won or lost.
*
* @param player one that wants to play a round of the game
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public void play (Gambler player) {
public void play (Gambler player) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
Bet b = promptForBet();
Expand All @@ -66,13 +91,29 @@ public void play (Gambler player) {

/**
* Prompt the user to make a bet from a menu of choices.
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private Bet promptForBet () {
private Bet promptForBet () throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
System.out.println("You can make one of the following types of bets:");
for (int k = 0; k < myPossibleBets.length; k++) {
System.out.println(String.format("%d) %s", (k + 1), myPossibleBets[k]));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];

/**
* Factory generates a class using reflection
*/

return myFactory.createBet(myNames[response - 1], myDescription[response - 1],
myOdds[response - 1]);


// return myPossibleBets[response - 1];
}
}
2 changes: 1 addition & 1 deletion src/roulette/bets/OddEven.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class OddEven extends Bet {
private String myChoice;

public OddEven (String description, int odds) {
public OddEven (String description, Integer odds) {
super(description, odds);
myChoice = "";
}
Expand Down