Skip to content

Reflection bew21 tlw37 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# example_roulette
Program to refactor that plays a game of roulette

Cameron Givler
Bailey Wall

##Questions

###1. In what ways is the refactored code simpler?
The code is simpler because we now have eliminated many if statements that switched
between different types of bets. Instead of if statements, we subclassed `Bet` with
the three different types (plus the ones we added). We then implemented abstract
methods inside `Bet`, `getPrompt()` and `wonBet()`. These methods are no longer implemented
in game, and simply have functionality defined in their respective subclass. We
also broke up `Play()` into multiple helper methods to improve readability.

###2. In what ways is the refactored code more complex?
There are now more classes that you have to look through to figure out the functionality
of each type of bet.

###3. What trade-offs did you make when refactoring your old code?
Our implementation requires instantiating a new class for each `Bet` subclass, even if that
specific instance isn't currently being used. We had to make this trade-off because we have to
be able to access myDescription from within each class.


###4. Which code do you prefer and why?
I prefer the new code because I think it is easier to read. It is also more easily
extended. The only modifications that need to be made to extend the number of
possible bets is to create a new subclass of bet and add it to the array of possible bets.
9 changes: 5 additions & 4 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public abstract class Bet {
* @param description name of this kind of bet
* @param odds odds given by the house for this kind of bet
*/
public Bet (String description, int odds) {
myDescription = description;
myOdds = odds;

public Bet (String description, Integer odds) {
myDescription = description;
myOdds = odds;
}

/**
Expand Down Expand Up @@ -46,4 +47,4 @@ public String toString () {
* @param wheel information needed to check if bet won or lost
*/
public abstract boolean isMade (Wheel.SpinResult spinResult);
}
}
14 changes: 7 additions & 7 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roulette;

import roulette.bets.BetFactory;
import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
Expand All @@ -15,10 +16,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 String[] myPossibleBets = {
"RedBlack", "OddEven", "ThreeConsecutive"
};
private Wheel myWheel;

Expand Down Expand Up @@ -72,7 +71,8 @@ private Bet promptForBet () {
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];
String response = ConsoleReader.promptOneOf("Please make a choice", myPossibleBets);
BetFactory factory = new BetFactory();
return factory.createBet(response);
}
}
}
3 changes: 3 additions & 0 deletions src/roulette/bets/Bet.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OddEven = 1
RedBlack = 1
ThreeConsecutive = 11
45 changes: 45 additions & 0 deletions src/roulette/bets/BetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package roulette.bets;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import roulette.Bet;

public class BetFactory {

public BetFactory() {
}

public Bet createBet(String s) {
s = "roulette.bets." + s;
Class clazz;
try {
clazz = Class.forName(s);
Constructor con = clazz.getConstructor(String.class);
return (Bet) con.newInstance(s);

} catch (ClassNotFoundException 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 (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
return null;
}
}
5 changes: 2 additions & 3 deletions src/roulette/bets/OddEven.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
public class OddEven extends Bet {
private String myChoice;

public OddEven (String description, int odds) {
super(description, odds);
myChoice = "";
public OddEven (String description) {
super(description, 1);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/roulette/bets/RedBlack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
public class RedBlack extends Bet {
private String myChoice;

public RedBlack (String description, int odds) {
super(description, odds);
// no initialization needed for myChoice
public RedBlack (String description) {
super(description, 1);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/roulette/bets/ThreeConsecutive.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class ThreeConsecutive extends Bet {
public static final int NUM_CONSECUTIVE = 3;
private int myStart;

public ThreeConsecutive (String description, int odds) {
super(description, odds);
public ThreeConsecutive (String description) {
super(description, 11);
}

/**
Expand Down