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/resources/bets.properties b/src/resources/bets.properties new file mode 100644 index 0000000..e6740b6 --- /dev/null +++ b/src/resources/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/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..53a24c1 --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,31 @@ +package roulette; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ResourceBundle; + +import roulette.bets.*; +import util.ConsoleReader; + +public class Factory { + private static final String PREFIX = "roulette.bets."; + private String[] myPossibleBets = { + "RedBlack", + "OddEven", + "ThreeConsecutive", + }; + + public Bet promptForBet() throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, 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); + String className = PREFIX + myPossibleBets[response-1]; + Class[] params = new Class[]{String.class, Integer.class}; + Constructor betCon = Class.forName(className).getConstructor(params); + String description = ResourceBundle.getBundle("bets").getString(myPossibleBets[response-1]).split(",")[0]; + int odds = Integer.parseInt(ResourceBundle.getBundle("bets").getString(myPossibleBets[response-1]).split(",")[1]); + return (Bet) betCon.newInstance(new Object[]{new String(description), new Integer(odds)}); + } +} diff --git a/src/roulette/OddEven.java b/src/roulette/OddEven.java deleted file mode 100644 index f430f37..0000000 --- a/src/roulette/OddEven.java +++ /dev/null @@ -1,27 +0,0 @@ -package roulette; - -import util.ConsoleReader; - - -public class OddEven extends Bet { - public OddEven (String description, int odds) { - super(description, odds); - } - - /** - * @see Bet#place() - */ - @Override - public String place () { - return ConsoleReader.promptOneOf("Please bet", "even", "odd"); - } - - /** - * @see Bet#isMade(String, Wheel) - */ - @Override - public boolean isMade (String betChoice, Wheel wheel) { - return (wheel.getNumber() % 2 == 0 && betChoice.equals("even")) || - (wheel.getNumber() % 2 == 1 && betChoice.equals("odd")); - } -} diff --git a/src/roulette/RedBlack.java b/src/roulette/RedBlack.java deleted file mode 100644 index 8ccd4b8..0000000 --- a/src/roulette/RedBlack.java +++ /dev/null @@ -1,26 +0,0 @@ -package roulette; - -import util.ConsoleReader; - - -public class RedBlack extends Bet { - public RedBlack (String description, int odds) { - super(description, odds); - } - - /** - * @see Bet#place() - */ - @Override - public String place () { - return ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED); - } - - /** - * @see Bet#isMade(String, Wheel) - */ - @Override - public boolean isMade (String betChoice, Wheel wheel) { - return wheel.getColor().equals(betChoice); - } -} diff --git a/src/roulette/ThreeConsecutive.java b/src/roulette/ThreeConsecutive.java deleted file mode 100644 index e3331ed..0000000 --- a/src/roulette/ThreeConsecutive.java +++ /dev/null @@ -1,27 +0,0 @@ -package roulette; - -import util.ConsoleReader; - - -public class ThreeConsecutive extends Bet { - public ThreeConsecutive (String description, int odds) { - super(description, odds); - } - - /** - * @see Bet#place() - */ - @Override - public String place () { - return "" + ConsoleReader.promptRange("Enter first of three consecutive numbers", 1, 34); - } - - /** - * @see Bet#isMade(String, Wheel) - */ - @Override - public boolean isMade (String betChoice, Wheel wheel) { - int start = Integer.parseInt(betChoice); - return (start <= wheel.getNumber() && wheel.getNumber() < start + 3); - } -}