From 9c4880f2ceeca9a49dcc9d444a138cef8a4871d1 Mon Sep 17 00:00:00 2001 From: austinhua Date: Thu, 3 Mar 2016 18:08:17 -0500 Subject: [PATCH] Implemented reflection for lab --- .classpath | 6 +++++ bet.properties | 3 +++ src/roulette/BetFactory.java | 23 +++++++++++++++++++ src/roulette/Game.java | 36 +++++++++++++++++++++--------- src/roulette/OddEven.java | 27 ---------------------- src/roulette/RedBlack.java | 26 --------------------- src/roulette/ThreeConsecutive.java | 27 ---------------------- 7 files changed, 57 insertions(+), 91 deletions(-) create mode 100644 .classpath create mode 100644 bet.properties create mode 100644 src/roulette/BetFactory.java delete mode 100644 src/roulette/OddEven.java delete mode 100644 src/roulette/RedBlack.java delete mode 100644 src/roulette/ThreeConsecutive.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/bet.properties b/bet.properties new file mode 100644 index 0000000..d25b34e --- /dev/null +++ b/bet.properties @@ -0,0 +1,3 @@ +OddEven = Odd or Even, 1 +RedBlack = Red or Black, 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 new file mode 100644 index 0000000..90514b1 --- /dev/null +++ b/src/roulette/BetFactory.java @@ -0,0 +1,23 @@ +package roulette; + +import java.lang.reflect.Constructor; +import java.util.ResourceBundle; + +public class BetFactory { + private ResourceBundle myResourceBundle; + + public BetFactory(ResourceBundle myResourceBundle) { + this.myResourceBundle = myResourceBundle; + } + + + public Bet makeBet(String betName) throws Exception { + Class betClass = Class.forName(betName); + Constructor betConstructor = betClass.getConstructors()[0]; + String[] args = myResourceBundle.getString(betName).split(","); + Bet newBet = (Bet) betConstructor.newInstance(args[0], Integer.parseInt(args[1])); + + return newBet; + } + +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 63c6ed6..e23e0b1 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -1,5 +1,9 @@ package roulette; +import java.util.Enumeration; +import java.util.List; +import java.util.ResourceBundle; + import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; @@ -14,12 +18,12 @@ public class Game { // name of the game private static final String DEFAULT_NAME = "Roulette"; + + public static final String BET_RESOURCES = "bet.properties"; + private ResourceBundle myResources; + // 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 BetFactory betMaker; private Wheel myWheel; /** @@ -27,6 +31,8 @@ public class Game { */ public Game () { myWheel = new Wheel(); + myResources = ResourceBundle.getBundle(BET_RESOURCES); + betMaker = new BetFactory(myResources); } /** @@ -47,7 +53,8 @@ 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(); + try { + Bet b = promptForBet(); b.place(); System.out.print("Spinning ..."); @@ -62,17 +69,24 @@ public void play (Gambler player) { amount *= -1; } player.updateBankroll(amount); + } + catch (Exception e) { + System.out.println("Please enter a valid bet"); + } } /** * Prompt the user to make a bet from a menu of choices. */ - private Bet promptForBet () { + private Bet promptForBet () throws Exception{ + Enumeration bets = myResources.getKeys(); 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 k = 1; + while (bets.hasMoreElements()) { + System.out.println(String.format("%d) %s", (k), bets.nextElement())); + k++; } - int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + String response = ConsoleReader.promptString("Please enter the bet name"); + return betMaker.makeBet(response); } } 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); - } -}