Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- /**
- * Data model for our StockTicker game.
- *
- * This is a really simple model, with no data validation or business rules.
- *
- * @author Jim Parry
- */
- public class Game {
- /** The allowed stock actions */
- public enum Action {
- UP, DOWN, DIV
- };
- /** Is logging enabled? */
- public static boolean LOGGING = true;
- /** Has the game started? */
- private boolean started;
- /** Is the game over? */
- private boolean over;
- /** First stock on the game board */
- private Stock stock1 = null;
- /** Second stock on the game board */
- private Stock stock2 = null;
- /** Third stock on the game board */
- private Stock stock3 = null;
- /** Our (at this point) only player */
- private Player player = null;
- //-------------------------------------------------------------
- // C o n s t r u c t o r s
- //-------------------------------------------------------------
- /**
- * Default constructor.
- * Initialize all fields.
- */
- public Game() {
- this.started = false;
- this.over = false;
- this.stock1 = null;
- this.stock2 = null;
- this.stock3 = null;
- this.player = null;
- }
- /**
- * Convenience constructor.
- * Establish initial fields as specified.
- *
- * @param stock1 First stock
- * @param stock2 Second stock
- * @param stock3 Third stock
- * @param player Our only player
- */
- public Game(Stock stock1, Stock stock2, Stock stock3, Player player) {
- this();
- setStock1(stock1);
- setStock2(stock2);
- setStock3(stock3);
- setPlayer(player);
- }
- //-------------------------------------------------------------
- // A c c e s s o r s
- //-------------------------------------------------------------
- /**
- * Has the game started yet?
- * @return the started
- */
- public boolean isStarted() {
- return started;
- }
- /**
- * Is the game over yet?
- * @return the over
- */
- public boolean isOver() {
- return over;
- }
- /**
- * What is the first stock?
- * @return the stock1
- */
- public Stock getStock1() {
- return stock1;
- }
- /**
- * WHat is the second stock?
- * @return the stock2
- */
- public Stock getStock2() {
- return stock2;
- }
- /**
- * What is the third stock?
- * @return the stock3
- */
- public Stock getStock3() {
- return stock3;
- }
- /**
- * WHo is playing the game?
- * @return the player
- */
- public Player getPlayer() {
- return player;
- }
- //-------------------------------------------------------------
- // M u t a t o r s
- //-------------------------------------------------------------
- /**
- * Specify whether the game has started.
- *
- * Business rule: cannot be set true if the game is over
- *
- * @param started true if started, false otherwise
- */
- public void setStarted(boolean started) {
- if (started == over) {
- return;
- }
- this.started = started;
- }
- /**
- * Specify whether the game is over
- *
- * Business rule: can only be set to true if the game has started
- *
- * @param true if over, false otherwise
- */
- public void setOver(boolean over) {
- if (over != started) {
- return;
- }
- this.over = over;
- }
- /**
- * Set the first stock on the board
- * @param stock1 the stock1 to set
- */
- public void setStock1(Stock stock1) {
- if (stock1 != null) {
- this.stock1 = stock1;
- }
- }
- /**
- * Set the second stock on the board
- * @param stock2 the stock2 to set
- */
- public void setStock2(Stock stock2) {
- if (stock2 != null) {
- if (stock2 != stock1) {
- this.stock2 = stock2;
- }
- }
- }
- /**
- * Set the third stock on the board
- * @param stock3 the stock3 to set
- */
- public void setStock3(Stock stock3) {
- if (stock3 != null) {
- if (stock3 != stock1) {
- if (stock3 != stock2) {
- this.stock3 = stock3;
- }
- }
- }
- }
- /**
- * Set the player
- * @param player the player to set
- */
- public void setPlayer(Player player) {
- if (player != null) {
- if (player.getName() != "?") {
- this.player = player;
- }
- }
- }
- //-------------------------------------------------------------
- // W o r k m e t h o d s
- //-------------------------------------------------------------
- /**
- * Calculate the net worth of a player.
- *
- * @param playerName The name of the player whose net worth we want.
- * @return His/her net worth
- */
- public int netWorth(String playerName) {
- // make sure it is our player
- if (!playerName.equals(player.getName())) {
- return 0;
- }
- int worth = 0;
- worth = player.getCash();
- worth += player.getHolding1() * stock1.getValue();
- worth += player.getHolding2() * stock2.getValue();
- worth += player.getHolding3() * stock3.getValue();
- return worth;
- }
- /**
- * Handle a stock action.
- *
- * For now, this method acts using stock ordinals, which will indicate whether
- * stock 1, 2, or 3 is the one to act on. Eventually, this will be a lookup code,
- * so we don't have direct access to the Stock objects.
- *
- * Business rules:
- * 1) if a stock splits (its value reaches or exceeds 200), then the stock is
- * deemed to have split; double the holdings in that stock for all players
- * holding some, and reset the stock value to “par”
- * 2) if a stock gets delisted (its value drops to or below 0), then the
- * stock is deemed to have been delisted; zero the holdings in that stock
- * for all players holding some, and reset the stock value to “par”
- * 3) if a dividend request is received, and if the stock's value is above
- * par, then pay dividends for that stock to any player holding some; the
- * amount of the “dividend” is determined by the action amount times
- * the player's holdings of that stock
- * 4) log all actions
- * 5) ignore any actions if the game has not started or if the game is over
- *
- * @param whichStock The ordinal of the stock to act on, in the range 1-3.
- * @param action The action to take with this stock
- * @param amount The amount the stock is go up or down by, or the dividend it is to pay
- */
- public void act(int whichStock, Action action, int amount) {
- Stock stock01 = stock1;
- //test if should be 2 or 3
- Stock stock02 = stock2;
- Stock stock03 = stock3;
- switch (action) {
- case UP:
- //handle a stock split
- if (stock01.getValue() + amount >= 200) {
- player.setHolding1(player.getHolding1() * 2);
- stock01.setValue(100);
- } else if (stock02.getValue() + amount >= 200) {
- player.setHolding2(player.getHolding2() * 2);
- stock02.setValue(100);
- } else if (stock03.getValue() + amount >= 200) {
- player.setHolding3(player.getHolding3() * 2);
- stock03.setValue(100);
- } else {
- switch (whichStock) {
- case 1:
- stock01.setValue(stock01.getValue() + amount);
- break;
- case 2:
- stock02.setValue(stock02.getValue() + amount);
- break;
- case 3:
- stock03.setValue(stock03.getValue() + amount);
- break;
- }
- }
- break;
- case DOWN:
- //statement goes here
- //handle delisting
- if (stock01.getValue() - amount <= 0) {
- player.setHolding1(0);
- stock01.setValue(100);
- } else if (stock02.getValue() - amount <= 0) {
- player.setHolding2(0);
- stock02.setValue(100);
- } else if (stock03.getValue() - amount <= 0) {
- player.setHolding3(0);
- stock03.setValue(100);
- } else {
- switch (whichStock) {
- case 1:
- stock01.setValue(stock01.getValue() - amount);
- break;
- case 2:
- stock02.setValue(stock02.getValue() - amount);
- break;
- case 3:
- stock03.setValue(stock03.getValue() - amount);
- break;
- }
- }
- break;
- case DIV:
- switch (whichStock) {
- case 1:
- if (stock01.getValue() > 100) {
- player.setCash(player.getCash() + (player.getHolding1() * amount));
- break;
- } else {
- break;
- }
- case 2:
- if (stock02.getValue() > 100) {
- player.setCash(player.getCash() + (player.getHolding2() * amount));
- break;
- } else {
- break;
- }
- case 3:
- if (stock03.getValue() > 100) {
- player.setCash(player.getCash() + (player.getHolding3() * amount));
- break;
- } else {
- break;
- }
- }
- break;
- }
- }
- /**
- * Start the game!
- * Just a convenience method.
- */
- public void start() {
- if (started != true) {
- return;
- }
- }
- /**
- * Stop the game!
- * Just a convenience method.
- */
- public void stop() {
- if (over != true) {
- return;
- }
- }
- /**
- * Produce a nice report of the game state.
- */
- public void report() {
- System.out.println("Stock1's value = " + stock1.getValue());
- System.out.println("Stock2's value = " + stock2.getValue());
- System.out.println("Stock3's value = " + stock3.getValue());
- System.out.println("Player: " + player.getName() + "Holdings: "
- + player.getHolding1() + "," + player.getHolding2() + ","
- + player.getHolding3() + "Net Worth: " + netWorth(player.getName()));
- System.out.println("Game status: " + (started));
- }
- /**
- * Log a message to the console, if logging is enabled
- *
- * @param message The message to log
- */
- public void log(String message) {
- if (LOGGING) {
- System.out.println("Game log messages");
- } else {
- return;
- }
- }
- //-------------------------------------------------------------
- // U t i l i t y m e t h o d s
- //-------------------------------------------------------------
- /**
- * Return a text representation for the game.
- * @return Text representation
- */
- public String toString() {
- String result = "StockTicker! ";
- result += "Underway: " + isStarted() + " ";
- result += "Over: " + isOver() + "\n";
- result += "Stocks: \n";
- result += getStock1() + "\n";
- result += getStock2() + "\n";
- result += getStock3() + "\n";
- result += getPlayer();
- return result;
- }
- // No equals method - this is meant to be a singleton (only one of them), which
- // we will tackle later.
- }
Add Comment
Please, Sign In to add comment