From 8140141eb6b14b05096aa6c67c85c29e4f505554 Mon Sep 17 00:00:00 2001 From: Cambidge Shae Van Wagoner Date: Thu, 3 Mar 2016 15:42:29 -0500 Subject: [PATCH 1/3] factory first step --- .classpath | 6 ++++++ src/roulette/BetFactory.java | 35 +++++++++++++++++++++++++++++++++++ src/roulette/Game.java | 14 +++----------- 3 files changed, 44 insertions(+), 11 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..9a6f226 --- /dev/null +++ b/src/roulette/BetFactory.java @@ -0,0 +1,35 @@ +package roulette; + +import roulette.bets.OddEven; +import roulette.bets.RedBlack; +import roulette.bets.ThreeConsecutive; +import util.ConsoleReader; + +public class BetFactory { + private Bet[] myPossibleBets = { + new RedBlack("Red or Black", 1), + new OddEven("Odd or Even", 1), + new ThreeConsecutive("Three in a Row", 11), + }; + + private String RedBlack = "roulette.RedBlack"; + private String OddEven = "roulette.OddEven"; + private String ThreeConsecutive = "roulette.ThreeConsecutive"; + + public BetFactory() { + + } + + public Bet getBet() { + + 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]; + + } +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 63c6ed6..6fe5acf 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -15,11 +15,8 @@ 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 BetFactory betFactory; + private Wheel myWheel; /** @@ -68,11 +65,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])); - } - int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + return betFactory.getBet(); } } From f9445790cfc90ac2295d70f54630581cfc0b3720 Mon Sep 17 00:00:00 2001 From: Cambidge Shae Van Wagoner Date: Thu, 3 Mar 2016 15:53:42 -0500 Subject: [PATCH 2/3] step 2 --- src/roulette/BetFactory.java | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java index 9a6f226..9b365b2 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -1,15 +1,18 @@ package roulette; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; import util.ConsoleReader; public class BetFactory { - private Bet[] myPossibleBets = { - new RedBlack("Red or Black", 1), - new OddEven("Odd or Even", 1), - new ThreeConsecutive("Three in a Row", 11), + private String[] myPossibleBets = { + "Red or Black", + "Odd or Even", + "Three in a Row" }; private String RedBlack = "roulette.RedBlack"; @@ -20,7 +23,7 @@ public BetFactory() { } - public Bet getBet() { + public Bet getBet() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { System.out.println("You can make one of the following types of bets:"); for (int k = 0; k < myPossibleBets.length; k++) { @@ -29,7 +32,23 @@ public Bet getBet() { int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - return myPossibleBets[response - 1]; + switch(response) { + case 1: + Class redblack = Class.forName(RedBlack); + Constructor c = redblack.getConstructor(Integer.class, String.class); + return (Bet) c.newInstance("Red or Black", 1); + case 2: + Class oddEven = Class.forName(OddEven); + Constructor b = oddEven.getConstructor(Integer.class, String.class); + return (Bet) b.newInstance("Odd or Even", 1); + case 3: + Class threeConsecutives = Class.forName(ThreeConsecutive); + Constructor a = threeConsecutives.getConstructor(Integer.class, String.class); + return (Bet) a.newInstance("ThreeConsecutive", 11); + + } + + return null; } } From 338d7a8030b88308492304af9e0cc4ad7351b4f2 Mon Sep 17 00:00:00 2001 From: Cambidge Shae Van Wagoner Date: Thu, 3 Mar 2016 16:05:18 -0500 Subject: [PATCH 3/3] step 3 --- src/roulette/Bet.java | 2 +- src/roulette/BetFactory.java | 22 ++++++---------------- src/roulette/Game.java | 28 +++++++++++++++++++++++++++- src/roulette/OddEven.java | 8 +++++++- src/roulette/Wheel.java | 2 +- src/util/factory.properties | 3 +++ 6 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 src/util/factory.properties diff --git a/src/roulette/Bet.java b/src/roulette/Bet.java index 15d54a2..9477887 100755 --- a/src/roulette/Bet.java +++ b/src/roulette/Bet.java @@ -38,7 +38,7 @@ public String toString () { /** * Place bet by prompting user for the specific information need to complete this bet. */ - public abstract void place (); + public abstract String place (); /** * Checks if bet is won or lost given result of spinning the wheel. diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java index 9b365b2..88bbc7f 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -2,6 +2,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.ResourceBundle; import roulette.bets.OddEven; import roulette.bets.RedBlack; @@ -19,6 +20,8 @@ public class BetFactory { private String OddEven = "roulette.OddEven"; private String ThreeConsecutive = "roulette.ThreeConsecutive"; + private ResourceBundle propertiesBundle = ResourceBundle.getBundle("util/factory"); + public BetFactory() { } @@ -32,23 +35,10 @@ public Bet getBet() throws InstantiationException, IllegalAccessException, Illeg int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length); - switch(response) { - case 1: - Class redblack = Class.forName(RedBlack); - Constructor c = redblack.getConstructor(Integer.class, String.class); - return (Bet) c.newInstance("Red or Black", 1); - case 2: - Class oddEven = Class.forName(OddEven); - Constructor b = oddEven.getConstructor(Integer.class, String.class); - return (Bet) b.newInstance("Odd or Even", 1); - case 3: - Class threeConsecutives = Class.forName(ThreeConsecutive); - Constructor a = threeConsecutives.getConstructor(Integer.class, String.class); - return (Bet) a.newInstance("ThreeConsecutive", 11); - - } + Class bet = Class.forName(propertiesBundle.getString(myPossibleBets[response])); + Constructor c = bet.getConstructor(Integer.class, String.class); + return (Bet) c.newInstance(myPossibleBets[response], 1); - return null; } } diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 6fe5acf..e643b75 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -1,5 +1,7 @@ package roulette; +import java.lang.reflect.InvocationTargetException; + import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; @@ -65,6 +67,30 @@ public void play (Gambler player) { * Prompt the user to make a bet from a menu of choices. */ private Bet promptForBet () { - return betFactory.getBet(); + try { + return betFactory.getBet(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; } } diff --git a/src/roulette/OddEven.java b/src/roulette/OddEven.java index f430f37..6023940 100644 --- a/src/roulette/OddEven.java +++ b/src/roulette/OddEven.java @@ -1,5 +1,6 @@ package roulette; +import roulette.Wheel.SpinResult; import util.ConsoleReader; @@ -19,9 +20,14 @@ public String place () { /** * @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")); } + + @Override + public boolean isMade(SpinResult spinResult) { + // TODO Auto-generated method stub + return false; + } } diff --git a/src/roulette/Wheel.java b/src/roulette/Wheel.java index 00b3f68..7dbabcb 100755 --- a/src/roulette/Wheel.java +++ b/src/roulette/Wheel.java @@ -96,7 +96,7 @@ private String getColor () { } // @return number of the current spot on the wheel - private int getNumber () { + int getNumber () { return myValue; } diff --git a/src/util/factory.properties b/src/util/factory.properties new file mode 100644 index 0000000..6d271db --- /dev/null +++ b/src/util/factory.properties @@ -0,0 +1,3 @@ +Red Or Black = roulette.RedBlack +Odd Or Even = roulette.OddEven +Three in a Row = roulette.ThreeConsecutive \ No newline at end of file