Skip to content
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

plg5 cf6 bcw22 #26

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>
3 changes: 3 additions & 0 deletions src/bets.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REDBLACK = 1,"Red or Black"
ODDEVEN = 1,"Odd or Even"
THREECONSECUTIVE = 11,"Three in a Row"
5 changes: 5 additions & 0 deletions src/resources/Snippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package resources;

public class Snippet {
//String[] properties = myResources.getStringArray(bets[i]);
}
43 changes: 43 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package roulette;

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

public class Factory {
Class[] bets = { OddEven.class, ThreeConsecutive.class, RedBlack.class };
String[] betName = { "REDBLACK", "ODDEVEN", "THREECONSECUTIVE" };
int[] betNumber = {1, 1, 11};
private ResourceBundle myResources = ResourceBundle.getBundle("bets");

public Bet makeBet(int i) {
Bet b = null;
Constructor cons = null;
try {
cons = bets[i].getDeclaredConstructor(new Class[] { String.class, int.class });
} catch (NoSuchMethodException | SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String line = myResources.getString(betName[i]);
String[] properties = line.split(",");
b = (Bet) cons.newInstance(properties[1], Integer.parseInt(properties[0]));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}

public List<String> getBetNames() {
List<String> names = new ArrayList<String>();

for (Class c : bets) {
names.add(c.getName());
}

return names;
}
}
20 changes: 11 additions & 9 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package roulette;

import java.util.List;

import util.ConsoleReader;


Expand All @@ -11,12 +13,9 @@
public class Game {
// name of the game
private static final String DEFAULT_NAME = "Roulette";
private Factory myFactory = new Factory();
// 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 Wheel myWheel;

/**
Expand Down Expand Up @@ -65,11 +64,14 @@ public void play (Gambler player) {
* Prompt the user to make a bet from a menu of choices.
*/
private Bet promptForBet () {
List<String> names = myFactory.getBetNames();


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()));
for (int k = 0; k < names.size(); k++) {
System.out.println(String.format("%d) %s", (k + 1), names.get(k)));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
int response = ConsoleReader.promptRange("Please make a choice", 1, names.size());
return myFactory.makeBet(response - 1);
}
}