Skip to content
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>
3 changes: 3 additions & 0 deletions src/bets.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RedBlack=Red or Black,1
OddEven=Odd or Even,1
ThreeConsecutive=Three in a Row,11
49 changes: 49 additions & 0 deletions src/roulette/BetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package roulette;

import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import util.ConsoleReader;
import java.lang.reflect.*;

public class BetFactory {

private List<String> myPossibleBets = Arrays.asList(new String[]{
"RedBlack",
"OddEven",
"ThreeConsecutive"
});

private ResourceBundle myDescriptionsBundle = ResourceBundle.getBundle("bets");

public Bet getBetInstance(int betNumber){
Class<?> c;
try {
c = Class.forName("roulette." + myPossibleBets.get(betNumber-1));
Constructor construct = c.getConstructor(String.class, Integer.TYPE);
String[] description = myDescriptionsBundle.getString(myPossibleBets.get(betNumber-1)).split(",");
return (Bet) construct.newInstance(description[0], Integer.parseInt(description[1]));
}
catch (ReflectiveOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;


}

/**
* Prompt the user to make a bet from a menu of choices.
*/
public void printOptions () {
System.out.println("You can make one of the following types of bets:");
for (int k = 0; k < myPossibleBets.size(); k++) {
System.out.println(String.format("%d) %s", (k + 1), myPossibleBets.get(k)));
}
}

public int getNumberOfBets() {
return myPossibleBets.size();
}
}
24 changes: 9 additions & 15 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
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),
};

BetFactory myFactory = new BetFactory();

private Wheel myWheel;

/**
Expand Down Expand Up @@ -44,6 +41,7 @@ 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();
String betChoice = b.place();

Expand All @@ -61,15 +59,11 @@ public void play (Gambler player) {
player.updateBankroll(amount);
}

/**
* 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].getDescription()));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
myFactory.printOptions();
int response = ConsoleReader.promptRange("Please make a choice", 1, myFactory.getNumberOfBets());
return myFactory.getBetInstance(response);
}


}