From 416eaef6e8f4af45602dcf0a10e2c128112e40a0 Mon Sep 17 00:00:00 2001 From: Paul Cruz Date: Thu, 3 Mar 2016 15:44:08 -0500 Subject: [PATCH 1/4] made factory class --- .classpath | 6 ++++++ src/roulette/Factory.java | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 .classpath create mode 100644 src/roulette/Factory.java 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..3e6d576 --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,20 @@ +package roulette; + + +import util.ConsoleReader; + +public class Factory { + private Bet[] myPossibleBets = { + new RedBlack("Red or Black", 1), + new OddEven("Odd or Even", 1), + new ThreeConsecutive("Three in a Row", 11), + }; + public 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])); + } + int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); + return myPossibleBets[response - 1]; + } +} From 455be785db7160dad8b2aa0c42443a1c9732d895 Mon Sep 17 00:00:00 2001 From: Paul Cruz Date: Thu, 3 Mar 2016 15:47:09 -0500 Subject: [PATCH 2/4] made factory class --- src/roulette/Factory.java | 2 +- src/roulette/OddEven.java | 27 --------------------------- src/roulette/RedBlack.java | 26 -------------------------- src/roulette/ThreeConsecutive.java | 27 --------------------------- 4 files changed, 1 insertion(+), 81 deletions(-) delete mode 100644 src/roulette/OddEven.java delete mode 100644 src/roulette/RedBlack.java delete mode 100644 src/roulette/ThreeConsecutive.java diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java index 3e6d576..af7e8fe 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -1,6 +1,6 @@ package roulette; - +import roulette.bets.*; import util.ConsoleReader; public class Factory { 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); - } -} From 4fa483f33c24d26b206a3b8cf100aeb54b8d314b Mon Sep 17 00:00:00 2001 From: Paul Cruz Date: Thu, 3 Mar 2016 16:02:41 -0500 Subject: [PATCH 3/4] added reflection --- src/roulette/Factory.java | 41 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java index af7e8fe..711077d 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -1,20 +1,39 @@ package roulette; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + import roulette.bets.*; import util.ConsoleReader; public class Factory { - private Bet[] myPossibleBets = { - new RedBlack("Red or Black", 1), - new OddEven("Odd or Even", 1), - new ThreeConsecutive("Three in a Row", 11), - }; - public Bet promptForBet(){ + private static final String PREFIX = "roulette.bets."; + private String[] myPossibleBets = { + "RedBlack", + "OddEven", + "ThreeConsecutive", + }; + private String[] descriptions = { + "Red or Black", + "Odd or Even", + "Three in a Row", + + }; + private int[] odds = { + 1, + 1, + 11, + + }; + 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); - return myPossibleBets[response - 1]; + 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); + return (Bet) betCon.newInstance(new Object[]{new String(descriptions[response-1]), new Integer(odds[response-1])}); } } From ef2f3135a6d67aa133601e83e76bf56623fcf1e4 Mon Sep 17 00:00:00 2001 From: Paul Cruz Date: Thu, 3 Mar 2016 16:10:16 -0500 Subject: [PATCH 4/4] added properties file --- src/resources/bets.properties | 3 +++ src/roulette/Factory.java | 18 +++++------------- 2 files changed, 8 insertions(+), 13 deletions(-) create mode 100644 src/resources/bets.properties 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 index 711077d..53a24c1 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -2,6 +2,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.ResourceBundle; import roulette.bets.*; import util.ConsoleReader; @@ -13,18 +14,7 @@ public class Factory { "OddEven", "ThreeConsecutive", }; - private String[] descriptions = { - "Red or Black", - "Odd or Even", - "Three in a Row", - - }; - private int[] odds = { - 1, - 1, - 11, - - }; + 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++) { @@ -34,6 +24,8 @@ public Bet promptForBet() throws NoSuchMethodException, SecurityException, Class String className = PREFIX + myPossibleBets[response-1]; Class[] params = new Class[]{String.class, Integer.class}; Constructor betCon = Class.forName(className).getConstructor(params); - return (Bet) betCon.newInstance(new Object[]{new String(descriptions[response-1]), new Integer(odds[response-1])}); + 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)}); } }