From b316ea5aa7e80816c83886f74fcc2654c70f692c Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 3 Mar 2016 15:48:07 -0500 Subject: [PATCH 1/2] created initial factory class --- src/roulette/Factory.java | 21 +++++++++++++++++++++ src/roulette/Game.java | 12 +----------- 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/roulette/Factory.java diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..b1a0f3a --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,21 @@ +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].getDescription())); + } + int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); + return myPossibleBets[response - 1]; + } +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 977c80a..0d90875 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -12,11 +12,6 @@ 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 Wheel myWheel; /** @@ -65,11 +60,6 @@ public void play (Gambler player) { * 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]; + return (new Factory()).promptForBet(); } } From aa30784f1b94e7cfce89223abcff0f2b93c6ab96 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 3 Mar 2016 16:13:31 -0500 Subject: [PATCH 2/2] added reflection --- src/roulette/Factory.java | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java index b1a0f3a..91235ac 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -3,19 +3,40 @@ import util.ConsoleReader; public class Factory { + + private String[] betTypes = { + "RedBlack", + "OddEven", + "ThreeConsecutive" + }; - private Bet[] myPossibleBets = { - new RedBlack("Red or Black", 1), - new OddEven("Odd or Even", 1), - new ThreeConsecutive("Three in a Row", 11), - }; + private String[] betDescriptions = { + "Red or Black", + "Odd or Even", + "Three in a Row" + }; + private int[] betOdds = { + 1, + 1, + 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].getDescription())); + for (int k = 0; k < betTypes.length; k++) { + System.out.println(String.format("%d) %s", (k + 1), betDescriptions[k])); + } + int response = ConsoleReader.promptRange("Please make a choice", 1, betTypes.length); + Bet bet = null; + try { + bet = (Bet) Class.forName("roulette." + betTypes[response - 1]) + .getConstructor(String.class, int.class).newInstance(betDescriptions[response - 1], + betOdds[response - 1]); + } + catch (Exception e) { + e.printStackTrace(); } - int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + return bet; } }