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

Attempt at reflection myk3 at200 #16

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>
27 changes: 27 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package roulette;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import roulette.OddEven;
import roulette.RedBlack;
import roulette.ThreeConsecutive;

public class Factory {
String[] bets = {"RedBlack", "OddEven", "RedBlack"};

public Bet createBet(int input) throws NoSuchMethodException, SecurityException {
Bet bet;
Class betClass;
try {
betClass = Class.forName(bets[input]);
Constructor betConstructor = betClass.getConstructor(String.class, Integer.class);
bet = (Bet) betConstructor.newInstance(bets[input], input);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return null;
}
return bet;
}
}
29 changes: 24 additions & 5 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package roulette;

import util.ConsoleReader;

import roulette.Factory;

/**
* Plays a game of roulette.
Expand All @@ -11,6 +11,8 @@
public class Game {
// name of the game
private static final String DEFAULT_NAME = "Roulette";
private Factory betFactory = new Factory();

// add new bet subclasses here
private Bet[] myPossibleBets = {
new RedBlack("Red or Black", 1),
Expand Down Expand Up @@ -66,10 +68,27 @@ public void play (Gambler player) {
*/
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()));
for (int k = 0; k < 3; k++) {
try {
System.out.println(String.format("%d) %s", (k + 1), betFactory.createBet(k)));
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
int response = ConsoleReader.promptRange("Please make a choice", 1, 3);
try {
return betFactory.createBet(response);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}