From 6d242a1bf16d3f05ce36aa7b68027848e84ba553 Mon Sep 17 00:00:00 2001 From: Krista Opsahl-Ong Date: Thu, 3 Mar 2016 15:49:34 -0500 Subject: [PATCH 1/3] Made factory class --- .classpath | 6 ++++++ src/roulette/Bet.java | 2 +- src/roulette/BetFactory.java | 26 ++++++++++++++++++++++++++ src/roulette/Wheel.java | 2 +- 4 files changed, 34 insertions(+), 2 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/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 new file mode 100644 index 0000000..0822855 --- /dev/null +++ b/src/roulette/BetFactory.java @@ -0,0 +1,26 @@ +package roulette; + +import roulette.bets.OddEven; +import roulette.bets.RedBlack; +import roulette.bets.ThreeConsecutive; + +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), + }; + + public Bet getBet(String betType){ + if(betType.equalsIgnoreCase("RedBlack")){ + return myPossibleBets[0]; + } + else if(betType.equalsIgnoreCase("OddEven")){ + return myPossibleBets[1]; + } + else if(betType.equalsIgnoreCase("ThreeConsecutive")){ + return myPossibleBets[2]; + } + return null; + } +} 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; } From ba8df4b8dba3e62e5ea47b6042324514f1197e9f Mon Sep 17 00:00:00 2001 From: Krista Opsahl-Ong Date: Thu, 3 Mar 2016 16:10:06 -0500 Subject: [PATCH 2/3] added reflection --- src/roulette/BetFactory.java | 63 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java index 0822855..66b0e43 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -1,26 +1,57 @@ package roulette; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Map; + import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; 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), - }; - - public Bet getBet(String betType){ - if(betType.equalsIgnoreCase("RedBlack")){ - return myPossibleBets[0]; - } - else if(betType.equalsIgnoreCase("OddEven")){ - return myPossibleBets[1]; - } - else if(betType.equalsIgnoreCase("ThreeConsecutive")){ - return myPossibleBets[2]; + private Map keys; + //private Bet[] myPossibleBets = { new RedBlack("Red or Black", 1), new OddEven("Odd or Even", 1), + //new ThreeConsecutive("Three in a Row", 11), }; + public BetFactory(){ + keys.put("Red or Black",1); + keys.put("Odd or Even",1); + keys.put("Three in a Row",11); + + } + public Bet getBet(String betType) { + Constructor myConstructor = null; + Bet myInstance = null; + try { + try { + myConstructor = (Constructor) Class.forName("roulette.bets"+betType).getConstructor(String.class,Integer.class); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + myConstructor.setAccessible(true); + try { + myInstance = myConstructor.newInstance(betType, keys.get(betType)); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - return null; + return myInstance; } } From 95a2b16c79c2072b0ce4bb43aa490d654ee5914d Mon Sep 17 00:00:00 2001 From: Krista Opsahl-Ong Date: Thu, 3 Mar 2016 16:20:28 -0500 Subject: [PATCH 3/3] Made resources file --- src/roulette/BetFactory.java | 20 +++++++++----------- src/roulette/BetTypes.properties | 3 +++ 2 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 src/roulette/BetTypes.properties diff --git a/src/roulette/BetFactory.java b/src/roulette/BetFactory.java index 66b0e43..8608c8b 100644 --- a/src/roulette/BetFactory.java +++ b/src/roulette/BetFactory.java @@ -4,27 +4,25 @@ import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; +import java.util.ResourceBundle; import roulette.bets.OddEven; import roulette.bets.RedBlack; import roulette.bets.ThreeConsecutive; public class BetFactory { - private Map keys; - //private Bet[] myPossibleBets = { new RedBlack("Red or Black", 1), new OddEven("Odd or Even", 1), - //new ThreeConsecutive("Three in a Row", 11), }; - public BetFactory(){ - keys.put("Red or Black",1); - keys.put("Odd or Even",1); - keys.put("Three in a Row",11); - - } + private ResourceBundle myBundle; public Bet getBet(String betType) { Constructor myConstructor = null; Bet myInstance = null; + + myBundle = ResourceBundle.getBundle("roulette/BetTypes.properties"); + String myClassName = myBundle.getString(betType).split(",")[0]; + int myClassInt = Integer.parseInt(myBundle.getString(betType).split(",")[1]); + try { try { - myConstructor = (Constructor) Class.forName("roulette.bets"+betType).getConstructor(String.class,Integer.class); + myConstructor = (Constructor) Class.forName("roulette.bets"+myClassName).getConstructor(String.class,Integer.class); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -34,7 +32,7 @@ public Bet getBet(String betType) { } myConstructor.setAccessible(true); try { - myInstance = myConstructor.newInstance(betType, keys.get(betType)); + myInstance = myConstructor.newInstance(betType,myClassInt); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/roulette/BetTypes.properties b/src/roulette/BetTypes.properties new file mode 100644 index 0000000..21b1ffa --- /dev/null +++ b/src/roulette/BetTypes.properties @@ -0,0 +1,3 @@ +RedorBlack = RedBlack,1 +OddorEven = OddEven,1 +ThreeinaRow = ThreeConsecutive,11 \ No newline at end of file