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/roulette/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..2ac23a8 --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,53 @@ +package roulette; + +import java.lang.reflect.Constructor; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.ResourceBundle; + +public class Factory { + private static final String PATH = "roulette.bets"; + private Map betTypes; + private ResourceBundle myBundle; + + public Factory () { + myBundle = ResourceBundle.getBundle(PATH); + betTypes = new HashMap<>(); + + int counter = 0; + for(String key: myBundle.keySet()) { + String className = "roulette." + key; + String DescriptionAndInt = myBundle.getString(key); + String [] arguments = DescriptionAndInt.split(","); + betTypes.put(counter, new Parameters(className, arguments[0], Integer.parseInt(arguments[1]))); + counter++; + } + + } + + public Bet getBet (int index){ + try { + Class bet = Class.forName(betTypes.get(index).getGameName()); + Constructor c = bet.getConstructor(String.class, Integer.TYPE); + return (Bet) c.newInstance(betTypes.get(index).getDescription(), betTypes.get(index).getOdds()); + } catch (ReflectiveOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + } + + public void print(){ + for (int i = 0; i < betTypes.size(); i++) { + System.out.println(String.format("%d) %s", (i + 1), getBet(i).getDescription())); + } + } + + public int getLenth(){ + return betTypes.size(); + } + +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 977c80a..2833df3 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -12,11 +12,7 @@ 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), - }; + private Factory betOptions; private Wheel myWheel; /** @@ -24,6 +20,7 @@ public class Game { */ public Game () { myWheel = new Wheel(); + betOptions = new Factory(); } /** @@ -66,10 +63,8 @@ public void play (Gambler player) { */ 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]; + betOptions.print(); + int response = ConsoleReader.promptRange("Please make a choice", 1, betOptions.getLenth()); + return betOptions.getBet(response - 1); } } diff --git a/src/roulette/Parameters.java b/src/roulette/Parameters.java new file mode 100644 index 0000000..c687ce5 --- /dev/null +++ b/src/roulette/Parameters.java @@ -0,0 +1,27 @@ +package roulette; + +public class Parameters { + + private String description; + private String gameName; + private int odds; + + public Parameters (String gameName, String description, int odds) { + this.description = description; + this.gameName = gameName; + this.odds = odds; + } + + public String getDescription() { + return description; + } + + public int getOdds() { + return odds; + } + + public String getGameName() { + return gameName; + } + +} diff --git a/src/roulette/bets.properties b/src/roulette/bets.properties new file mode 100644 index 0000000..fdef635 --- /dev/null +++ b/src/roulette/bets.properties @@ -0,0 +1,3 @@ +RedBlack= Red or Black,1 +ThreeConsecutive = Three in a row,11 +OddEven= Odd or Even,1 \ No newline at end of file