From 1b234e1ad096a2803d67c74d5ff3d5cd46783012 Mon Sep 17 00:00:00 2001 From: Raghav Date: Thu, 3 Mar 2016 15:44:21 -0500 Subject: [PATCH 1/3] created factory class --- .classpath | 6 ++++++ src/roulette/Factory.java | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 .classpath create mode 100644 src/roulette/Factory.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/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..ca4f58e --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,36 @@ +package roulette; + +import java.util.ArrayList; +import java.util.List; + +import roulette.bets.OddEven; +import roulette.bets.RedBlack; +import roulette.bets.ThreeConsecutive; +import util.ConsoleReader; + +public class Factory { + + List myBets; + + public Factory() { + // TODO Auto-generated constructor stub + myBets = new ArrayList(); + myBets.add( new OddEven("Odd or Even", 1)); + myBets.add( new RedBlack("Red or Black", 1)); + myBets.add( new ThreeConsecutive("Three in a Row", 11)); + } + + public List getBets(){ + return myBets; + } + + private Bet promptForBet () { + System.out.println("You can make one of the following types of bets:"); + for (int k = 0; k < myBets.size(); k++) { + System.out.println(String.format("%d) %s", (k + 1), myBets.get(k))); + } + int response = ConsoleReader.promptRange("Please make a choice", 1, myBets.size()); + return myBets.get(response - 1); + } + +} From 517d5ef89a6aebfaa52103c22d2e101609a4b2aa Mon Sep 17 00:00:00 2001 From: Raghav Date: Thu, 3 Mar 2016 15:58:19 -0500 Subject: [PATCH 2/3] implemented reflection --- src/roulette/Factory.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java index ca4f58e..3f1aa96 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -15,11 +15,34 @@ public class Factory { public Factory() { // TODO Auto-generated constructor stub myBets = new ArrayList(); + myBets.add( new OddEven("Odd or Even", 1)); myBets.add( new RedBlack("Red or Black", 1)); myBets.add( new ThreeConsecutive("Three in a Row", 11)); } + public static Bet createBet(String type) { + + Class c; + Bet bet; + try { + c = Class.forName("roulette."+ type ); + bet = (Bet)c.newInstance(); + return bet; + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch(IllegalAccessException e){ + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + public List getBets(){ return myBets; } From 2b4192d3e5a5fab94371425042e5136c83aaa620 Mon Sep 17 00:00:00 2001 From: Raghav Date: Thu, 3 Mar 2016 16:20:02 -0500 Subject: [PATCH 3/3] created properties file for bets --- src/roulette/Bets.properties | 3 +++ src/roulette/Factory.java | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 src/roulette/Bets.properties diff --git a/src/roulette/Bets.properties b/src/roulette/Bets.properties new file mode 100644 index 0000000..e88ae7d --- /dev/null +++ b/src/roulette/Bets.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/Factory.java b/src/roulette/Factory.java index 3f1aa96..d99cd51 100644 --- a/src/roulette/Factory.java +++ b/src/roulette/Factory.java @@ -1,7 +1,9 @@ package roulette; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; +import java.util.ResourceBundle; import roulette.bets.OddEven; import roulette.bets.RedBlack; @@ -11,14 +13,12 @@ public class Factory { List myBets; + private static ResourceBundle myResourceBundle = ResourceBundle.getBundle("roulette/Bets"); public Factory() { // TODO Auto-generated constructor stub myBets = new ArrayList(); - - myBets.add( new OddEven("Odd or Even", 1)); - myBets.add( new RedBlack("Red or Black", 1)); - myBets.add( new ThreeConsecutive("Three in a Row", 11)); + } public static Bet createBet(String type) { @@ -27,7 +27,8 @@ public static Bet createBet(String type) { Bet bet; try { c = Class.forName("roulette."+ type ); - bet = (Bet)c.newInstance(); + String[] args = myResourceBundle.getString(type).split(","); + bet = (Bet)c.getConstructor(String.class, Integer.class).newInstance(args[0], Integer.parseInt(args[1])); return bet; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block @@ -37,6 +38,10 @@ public static Bet createBet(String type) { } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); + } catch(InvocationTargetException e){ + e.printStackTrace(); + } catch(NoSuchMethodException e){ + e.printStackTrace(); } return null; @@ -50,7 +55,7 @@ public List getBets(){ private Bet promptForBet () { System.out.println("You can make one of the following types of bets:"); for (int k = 0; k < myBets.size(); k++) { - System.out.println(String.format("%d) %s", (k + 1), myBets.get(k))); + System.out.println(myResourceBundle.getKeys()); } int response = ConsoleReader.promptRange("Please make a choice", 1, myBets.size()); return myBets.get(response - 1);