From 64f1f3b09c2d23ccb0b6d18649296278702c7c44 Mon Sep 17 00:00:00 2001 From: Jon Im Date: Thu, 3 Mar 2016 15:55:21 -0500 Subject: [PATCH 1/3] Move bet subclasses and print to Factory --- .classpath | 6 ++++++ src/roulette/BetFactory.java | 32 ++++++++++++++++++++++++++++++++ src/roulette/Game.java | 23 ++++++++--------------- 3 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 .classpath create mode 100644 src/roulette/BetFactory.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/BetFactory.java b/src/roulette/BetFactory.java new file mode 100644 index 0000000..07c7ee3 --- /dev/null +++ b/src/roulette/BetFactory.java @@ -0,0 +1,32 @@ +package roulette; + +import java.util.Arrays; +import java.util.List; +import util.ConsoleReader; + +public class BetFactory { + + private List myPossibleBets = Arrays.asList(new Bet[]{ + new RedBlack("Red or Black", 1), + new OddEven("Odd or Even", 1), + new ThreeConsecutive("Three in a Row", 11), + }); + + public Bet getBetInstance(int betNumber){ + return myPossibleBets.get(betNumber); + } + + /** + * 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).getDescription())); + } + } + + public int getNumberOfBets() { + return myPossibleBets.size(); + } +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 977c80a..3ec7638 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,10 @@ 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]; + int response = ConsoleReader.promptRange("Please make a choice", 1, myFactory.getNumberOfBets()); + return myFactory.getBetInstance(response); } + + } From 992a5bc6830a11f1ba224521be26182bed92ec11 Mon Sep 17 00:00:00 2001 From: Jon Im Date: Thu, 3 Mar 2016 16:24:28 -0500 Subject: [PATCH 2/3] Use reflection to instantiate bet --- src/roulette/BetFactory.java | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java index 07c7ee3..2956d8c 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -3,17 +3,33 @@ import java.util.Arrays; import java.util.List; import util.ConsoleReader; +import java.lang.reflect.*; public class BetFactory { - private List myPossibleBets = Arrays.asList(new Bet[]{ - new RedBlack("Red or Black", 1), - new OddEven("Odd or Even", 1), - new ThreeConsecutive("Three in a Row", 11), + private List myPossibleBets = Arrays.asList(new String[]{ + "RedBlack", + "OddEven", + "ThreeConsecutive" +// new RedBlack("Red or Black", 1), +// new OddEven("Odd or Even", 1), +// new ThreeConsecutive("Three in a Row", 11), }); public Bet getBetInstance(int betNumber){ - return myPossibleBets.get(betNumber); + Class c; + try { + c = Class.forName("roulette." + myPossibleBets.get(betNumber-1)); + Constructor construct = c.getConstructor(String.class, Integer.TYPE);//getConstructor(String.class, Integer.TYPE); + return (Bet) construct.newInstance(myPossibleBets.get(betNumber-1), 1); + } + catch (ReflectiveOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + } /** @@ -22,7 +38,7 @@ public Bet getBetInstance(int betNumber){ 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).getDescription())); + System.out.println(String.format("%d) %s", (k + 1), this.getBetInstance(k).getDescription())); } } From de690f18a24d983499ecc4d57ba3d8ba0730f2b4 Mon Sep 17 00:00:00 2001 From: Jon Im Date: Thu, 3 Mar 2016 16:31:59 -0500 Subject: [PATCH 3/3] Get descriptions from properties file --- src/bets.properties | 3 +++ src/roulette/BetFactory.java | 13 +++++++------ src/roulette/Game.java | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 src/bets.properties 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 index 2956d8c..5356a50 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -2,6 +2,7 @@ import java.util.Arrays; import java.util.List; +import java.util.ResourceBundle; import util.ConsoleReader; import java.lang.reflect.*; @@ -11,17 +12,17 @@ public class BetFactory { "RedBlack", "OddEven", "ThreeConsecutive" -// new RedBlack("Red or Black", 1), -// new OddEven("Odd or Even", 1), -// new ThreeConsecutive("Three in a Row", 11), }); + 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);//getConstructor(String.class, Integer.TYPE); - return (Bet) construct.newInstance(myPossibleBets.get(betNumber-1), 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 @@ -38,7 +39,7 @@ public Bet getBetInstance(int betNumber){ 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), this.getBetInstance(k).getDescription())); + System.out.println(String.format("%d) %s", (k + 1), myPossibleBets.get(k))); } } diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 3ec7638..e6f3d78 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -60,6 +60,7 @@ public void play (Gambler player) { } private Bet promptForBet () { + myFactory.printOptions(); int response = ConsoleReader.promptRange("Please make a choice", 1, myFactory.getNumberOfBets()); return myFactory.getBetInstance(response); }