diff --git a/src/Main.java b/src/Main.java index 2e5e77c..b0ef9f2 100755 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.lang.reflect.InvocationTargetException; + import roulette.Gambler; import roulette.Game; @@ -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); diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..4e46af7 --- /dev/null +++ b/src/roulette/Factory.java @@ -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 myBets; + + public Factory () { + myBets = new ArrayList(); + + } + + 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; + } + +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 63c6ed6..cf97e0e 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -1,5 +1,7 @@ package roulette; +import java.lang.reflect.InvocationTargetException; + import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; @@ -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(); } @@ -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(); @@ -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]; } } diff --git a/src/roulette/bets/OddEven.java b/src/roulette/bets/OddEven.java index 9241d57..184a3b2 100644 --- a/src/roulette/bets/OddEven.java +++ b/src/roulette/bets/OddEven.java @@ -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 = ""; }