diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/bets.properties b/src/bets.properties new file mode 100644 index 0000000..5cc4eb0 --- /dev/null +++ b/src/bets.properties @@ -0,0 +1,3 @@ +RedBlack=Red or Black,1 +OddEven=Odd or Even,1 +ThreeConsecutive=Three in a Row,11 \ No newline at end of file diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java new file mode 100644 index 0000000..5356a50 --- /dev/null +++ b/src/roulette/BetFactory.java @@ -0,0 +1,49 @@ +package roulette; + +import java.util.Arrays; +import java.util.List; +import java.util.ResourceBundle; +import util.ConsoleReader; +import java.lang.reflect.*; + +public class BetFactory { + + private List myPossibleBets = Arrays.asList(new String[]{ + "RedBlack", + "OddEven", + "ThreeConsecutive" + }); + + private ResourceBundle myDescriptionsBundle = ResourceBundle.getBundle("bets"); + + public Bet getBetInstance(int betNumber){ + Class c; + try { + c = Class.forName("roulette." + myPossibleBets.get(betNumber-1)); + Constructor construct = c.getConstructor(String.class, Integer.TYPE); + String[] description = myDescriptionsBundle.getString(myPossibleBets.get(betNumber-1)).split(","); + return (Bet) construct.newInstance(description[0], Integer.parseInt(description[1])); + } + catch (ReflectiveOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + + } + + /** + * Prompt the user to make a bet from a menu of choices. + */ + public void printOptions () { + System.out.println("You can make one of the following types of bets:"); + for (int k = 0; k < myPossibleBets.size(); k++) { + System.out.println(String.format("%d) %s", (k + 1), myPossibleBets.get(k))); + } + } + + public int getNumberOfBets() { + return myPossibleBets.size(); + } +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 977c80a..e6f3d78 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -11,12 +11,9 @@ public class Game { // name of the game private static final String DEFAULT_NAME = "Roulette"; - // add new bet subclasses here - private Bet[] myPossibleBets = { - new RedBlack("Red or Black", 1), - new OddEven("Odd or Even", 1), - new ThreeConsecutive("Three in a Row", 11), - }; + + BetFactory myFactory = new BetFactory(); + private Wheel myWheel; /** @@ -44,6 +41,7 @@ public String getName () { public void play (Gambler player) { int amount = ConsoleReader.promptRange("How much do you want to bet", 0, player.getBankroll()); + Bet b = promptForBet(); String betChoice = b.place(); @@ -61,15 +59,11 @@ public void play (Gambler player) { player.updateBankroll(amount); } - /** - * Prompt the user to make a bet from a menu of choices. - */ private Bet promptForBet () { - 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].getDescription())); - } - int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + myFactory.printOptions(); + int response = ConsoleReader.promptRange("Please make a choice", 1, myFactory.getNumberOfBets()); + return myFactory.getBetInstance(response); } + + }