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..331d2a7 --- /dev/null +++ b/src/bets.properties @@ -0,0 +1,3 @@ +REDBLACK = 1,"Red or Black" +ODDEVEN = 1,"Odd or Even" +THREECONSECUTIVE = 11,"Three in a Row" \ No newline at end of file diff --git a/src/resources/Snippet.java b/src/resources/Snippet.java new file mode 100644 index 0000000..640e31e --- /dev/null +++ b/src/resources/Snippet.java @@ -0,0 +1,5 @@ +package resources; + +public class Snippet { + //String[] properties = myResources.getStringArray(bets[i]); +} diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..57e290f --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,43 @@ +package roulette; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.*; + +public class Factory { + Class[] bets = { OddEven.class, ThreeConsecutive.class, RedBlack.class }; + String[] betName = { "REDBLACK", "ODDEVEN", "THREECONSECUTIVE" }; + int[] betNumber = {1, 1, 11}; + private ResourceBundle myResources = ResourceBundle.getBundle("bets"); + + public Bet makeBet(int i) { + Bet b = null; + Constructor cons = null; + try { + cons = bets[i].getDeclaredConstructor(new Class[] { String.class, int.class }); + } catch (NoSuchMethodException | SecurityException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + try { + String line = myResources.getString(betName[i]); + String[] properties = line.split(","); + b = (Bet) cons.newInstance(properties[1], Integer.parseInt(properties[0])); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return b; + } + + public List getBetNames() { + List names = new ArrayList(); + + for (Class c : bets) { + names.add(c.getName()); + } + + return names; + } +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 977c80a..3ab4ce7 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -1,5 +1,7 @@ package roulette; +import java.util.List; + import util.ConsoleReader; @@ -11,12 +13,9 @@ public class Game { // name of the game private static final String DEFAULT_NAME = "Roulette"; + private Factory myFactory = new Factory(); // 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 Wheel myWheel; /** @@ -65,11 +64,14 @@ public void play (Gambler player) { * Prompt the user to make a bet from a menu of choices. */ private Bet promptForBet () { + List names = myFactory.getBetNames(); + + 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())); + for (int k = 0; k < names.size(); k++) { + System.out.println(String.format("%d) %s", (k + 1), names.get(k))); } - int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + int response = ConsoleReader.promptRange("Please make a choice", 1, names.size()); + return myFactory.makeBet(response - 1); } }